diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-05-28 09:05:53 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-05-28 09:05:53 +0200 |
commit | edaad76b66e47d945d7aa352d13de5ab8251317a (patch) | |
tree | e1a90d4cd86e095cbea4a04832a49fe8b35853cc | |
parent | a819d3cbddbb5294b98b2fd6590f9fff13491d85 (diff) | |
download | radio-controller-edaad76b66e47d945d7aa352d13de5ab8251317a.tar.gz radio-controller-edaad76b66e47d945d7aa352d13de5ab8251317a.tar.bz2 radio-controller-edaad76b66e47d945d7aa352d13de5ab8251317a.tar.xz radio-controller-edaad76b66e47d945d7aa352d13de5ab8251317a.zip |
o Naming pins.
o Correct prescaler for 1us resolution.
o Trying out a lock structure for locking between an ISR and a main loop.
-rw-r--r-- | CMakeLists.txt | 13 | ||||
-rw-r--r-- | bsp/radio-controller-1/Inc/main.h | 5 | ||||
-rw-r--r-- | bsp/radio-controller-1/Src/main.c | 8 | ||||
-rw-r--r-- | bsp/radio-controller-1/Src/stm32f1xx_hal_msp.c | 6 | ||||
-rw-r--r-- | bsp/radio-controller-1/Src/stm32f1xx_it.c | 2 | ||||
-rw-r--r-- | bsp/radio-controller-1/radio-controller-1.gpdsc | 4 | ||||
-rw-r--r-- | bsp/radio-controller-1/radio-controller-1.ioc | 6 | ||||
-rw-r--r-- | include/radio-controller.h | 2 | ||||
-rw-r--r-- | src/radio-controller.cpp | 134 |
9 files changed, 144 insertions, 36 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 75d1f7b..de75f5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,16 +34,17 @@ target_sources(firmware PUBLIC mcucpp_process(TARGET firmware) -# stm32cube sources +# BSP sources mcu_include_directories_from_sources( SOURCE_DIR bsp/radio-controller-1 EXCLUDE Templates EXCLUDE _template.c EXCLUDE Examples EXCLUDE DSP_Lib - SOURCES_VAR STM32_CUBEMX_SOURCES - HEADERS_VAR STM32_CUBEMX_HEADERS - INCLUDES_VAR STM32_CUBEMX_INCLUDES + SOURCES_VAR BSP_SOURCES + HEADERS_VAR BSP_HEADERS + INCLUDES_VAR BSP_INCLUDES ) -target_include_directories(firmware PUBLIC ${STM32_CUBEMX_INCLUDES}) -target_sources(firmware PUBLIC ${STM32_CUBEMX_SOURCES} ${STM32_CUBEMX_HEADERS}) +list(FILTER BSP_INCLUDES INCLUDE REGEX Inc) +target_include_directories(firmware PUBLIC ${BSP_INCLUDES}) +target_sources(firmware PUBLIC ${BSP_SOURCES} ${BSP_HEADERS}) diff --git a/bsp/radio-controller-1/Inc/main.h b/bsp/radio-controller-1/Inc/main.h index 6795087..be96275 100644 --- a/bsp/radio-controller-1/Inc/main.h +++ b/bsp/radio-controller-1/Inc/main.h @@ -49,7 +49,10 @@ /* USER CODE END Includes */ /* Private define ------------------------------------------------------------*/ -#define RX_TIMER_PRESCALER (72000000 / 72000) +#define RX_TIMER_PRESCALER (7200 - 1) + +#define RADIO_RX_Pin GPIO_PIN_8 +#define RADIO_RX_GPIO_Port GPIOA /* USER CODE BEGIN Private defines */ #ifdef __cplusplus diff --git a/bsp/radio-controller-1/Src/main.c b/bsp/radio-controller-1/Src/main.c index f57b555..4b49e82 100644 --- a/bsp/radio-controller-1/Src/main.c +++ b/bsp/radio-controller-1/Src/main.c @@ -47,6 +47,7 @@ /* USER CODE BEGIN Includes */ #include "radio-controller.h" +#include <mcu/init.h> /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ @@ -182,7 +183,7 @@ static void MX_TIM1_Init(void) htim1.Instance = TIM1; htim1.Init.Prescaler = RX_TIMER_PRESCALER; htim1.Init.CounterMode = TIM_COUNTERMODE_UP; - htim1.Init.Period = 65535; + htim1.Init.Period = 0xffff; htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim1.Init.RepetitionCounter = 0; if (HAL_TIM_IC_Init(&htim1) != HAL_OK) @@ -271,10 +272,7 @@ static void MX_GPIO_Init(void) void Error_Handler(void) { /* USER CODE BEGIN Error_Handler */ - /* User can add his own implementation to report the HAL error return state */ - while(1) - { - } + halt(); /* USER CODE END Error_Handler */ } diff --git a/bsp/radio-controller-1/Src/stm32f1xx_hal_msp.c b/bsp/radio-controller-1/Src/stm32f1xx_hal_msp.c index b90d153..0b02774 100644 --- a/bsp/radio-controller-1/Src/stm32f1xx_hal_msp.c +++ b/bsp/radio-controller-1/Src/stm32f1xx_hal_msp.c @@ -103,10 +103,10 @@ void HAL_TIM_IC_MspInit(TIM_HandleTypeDef* htim_ic) /**TIM1 GPIO Configuration PA8 ------> TIM1_CH1 */ - GPIO_InitStruct.Pin = GPIO_PIN_8; + GPIO_InitStruct.Pin = RADIO_RX_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + HAL_GPIO_Init(RADIO_RX_GPIO_Port, &GPIO_InitStruct); /* Peripheral DMA init*/ @@ -151,7 +151,7 @@ void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef* htim_ic) /**TIM1 GPIO Configuration PA8 ------> TIM1_CH1 */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8); + HAL_GPIO_DeInit(RADIO_RX_GPIO_Port, RADIO_RX_Pin); /* Peripheral DMA DeInit*/ HAL_DMA_DeInit(htim_ic->hdma[TIM_DMA_ID_CC1]); diff --git a/bsp/radio-controller-1/Src/stm32f1xx_it.c b/bsp/radio-controller-1/Src/stm32f1xx_it.c index ac5d604..8064fb8 100644 --- a/bsp/radio-controller-1/Src/stm32f1xx_it.c +++ b/bsp/radio-controller-1/Src/stm32f1xx_it.c @@ -234,7 +234,6 @@ void USB_LP_CAN1_RX0_IRQHandler(void) void TIM1_TRG_COM_IRQHandler(void) { /* USER CODE BEGIN TIM1_TRG_COM_IRQn 0 */ - it_tim1(); /* USER CODE END TIM1_TRG_COM_IRQn 0 */ HAL_TIM_IRQHandler(&htim1); /* USER CODE BEGIN TIM1_TRG_COM_IRQn 1 */ @@ -248,7 +247,6 @@ void TIM1_TRG_COM_IRQHandler(void) void TIM1_CC_IRQHandler(void) { /* USER CODE BEGIN TIM1_CC_IRQn 0 */ - it_tim1(); /* USER CODE END TIM1_CC_IRQn 0 */ HAL_TIM_IRQHandler(&htim1); /* USER CODE BEGIN TIM1_CC_IRQn 1 */ diff --git a/bsp/radio-controller-1/radio-controller-1.gpdsc b/bsp/radio-controller-1/radio-controller-1.gpdsc index 7835cd1..dd069ed 100644 --- a/bsp/radio-controller-1/radio-controller-1.gpdsc +++ b/bsp/radio-controller-1/radio-controller-1.gpdsc @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- ****************************************************************************** * File Name : radio-controller-1.gpdsc - * Date : 27/05/2017 10:57:24 + * Date : 27/05/2017 23:23:20 * Description : Generator PDSC File generated by STM32CubeMX (DO NOT EDIT!) ****************************************************************************** --> @@ -11,7 +11,7 @@ <description>STM32CubeMX generated pack description</description> <releases> <release version="1.0.0"> - - Generated: 27/05/2017 10:57:24 + - Generated: 27/05/2017 23:23:20 </release> </releases> <requirements> diff --git a/bsp/radio-controller-1/radio-controller-1.ioc b/bsp/radio-controller-1/radio-controller-1.ioc index 1948f13..c7f9ca7 100644 --- a/bsp/radio-controller-1/radio-controller-1.ioc +++ b/bsp/radio-controller-1/radio-controller-1.ioc @@ -38,7 +38,7 @@ Mcu.Pin7=PA11 Mcu.Pin8=PA12 Mcu.Pin9=PA13 Mcu.PinsNb=13 -Mcu.UserConstants=RX_TIMER_PRESCALER,(72000000 / 72000) +Mcu.UserConstants=RX_TIMER_PRESCALER,(7200 - 1) Mcu.UserName=STM32F103C6Tx MxCube.Version=4.20.1 MxDb.Version=DB.4.0.200 @@ -69,6 +69,8 @@ PA2.Mode=Asynchronous PA2.Signal=USART2_TX PA3.Mode=Asynchronous PA3.Signal=USART2_RX +PA8.GPIOParameters=GPIO_Label +PA8.GPIO_Label=RADIO_RX PA8.Signal=S_TIM1_CH1 PC14-OSC32_IN.Mode=LSE-External-Oscillator PC14-OSC32_IN.Signal=RCC_OSC32_IN @@ -137,7 +139,7 @@ SH.S_TIM1_CH1.0=TIM1_CH1,Input_Capture1_from_TI1 SH.S_TIM1_CH1.ConfNb=1 TIM1.Channel-Input_Capture1_from_TI1=TIM_CHANNEL_1 TIM1.IPParameters=Channel-Input_Capture1_from_TI1,Prescaler,Period -TIM1.Period=65535 +TIM1.Period=0xffff TIM1.Prescaler=RX_TIMER_PRESCALER USART2.IPParameters=VirtualMode USART2.VirtualMode=VM_ASYNC diff --git a/include/radio-controller.h b/include/radio-controller.h index 58f2594..cad5f88 100644 --- a/include/radio-controller.h +++ b/include/radio-controller.h @@ -8,8 +8,6 @@ void main_pre_init(); void main_post_init(); void main_loop(); -void it_tim1(); - #ifdef __cplusplus } #endif diff --git a/src/radio-controller.cpp b/src/radio-controller.cpp index 461a910..61cacfd 100644 --- a/src/radio-controller.cpp +++ b/src/radio-controller.cpp @@ -1,5 +1,6 @@ +#include <usbd_cdc_if.h> +#include <cinttypes> #include "radio-controller.h" -#include "stm32f1xx_hal.h" #include "mcu/arm/semihosting.h" #include "mcu/stm32cube/uart.h" #include "mcu/stm32cube/debug.h" @@ -14,10 +15,89 @@ extern UART_HandleTypeDef huart2; mcu::stm32cube::uart::uart_port uart2(&huart2); mcu::stm32cube::debug::dbg<100> dbg(uart2); -void main_pre_init() { +using interval_us = uint16_t; + +class mutex { + volatile uint8_t locked; + +public: + mutex() : locked(0) + {} + + __always_inline + bool try_lock() + { + auto l = __LDREXB(&locked); + + if (l == 1) { + return false; + } + + return __STREXB(1, &locked) == 0; + } + + __always_inline + bool lock() + { + do { + while(__LDREXB(&locked) == 1); + } while(__STREXB(1, &locked) != 0); + } + + __always_inline + bool unlock() + { + locked = 0; + } +}; + +template<unsigned int BufferSize> +class sample_buffer { + interval_us samples[BufferSize]; + + volatile unsigned int _size; + +public: + void reset() + { + _size = 0; + } + + unsigned int size() + { + return _size; + } + + bool is_full() + { + return _size == BufferSize; + } + + __noinline + void append(interval_us sample) + { + if (_size == BufferSize) { + return; + } + + samples[_size++] = sample; + } + + interval_us at(int i) + { + return samples[i]; + } +}; + +sample_buffer<10> buffer; +mutex buffer_lock; + +void main_pre_init() +{ } -void main_post_init() { +void main_post_init() +{ bool debugger_connected = (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) != 0; if (debugger_connected) { semihosting::enable(); @@ -25,36 +105,64 @@ void main_post_init() { printf("Radio Controller\n"); - HAL_TIM_Base_Start_IT(&htim1); -// HAL_TIM_IC_Start_IT(&htim1, 1); +// HAL_TIM_Base_Start_IT(&htim1); + if (HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_1) != HAL_OK) { + halt(); + } uart2.enable(); + + buffer.reset(); } static uint32_t tick_next = 0; +static uint32_t cnt_prev = 0; static bool seen_high = false; -void main_loop() { +void main_loop() +{ auto now = HAL_GetTick(); if (now >= tick_next) { -// puts(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8) ? "1" : "0"); - uint32_t cnt = __HAL_TIM_GET_COUNTER(&htim1); - dbg.println("cnt=%lu, now=%lu, seen_high=%d", cnt, now, seen_high); + auto diff = static_cast<int>(cnt - cnt_prev); + dbg.println("cnt=%lu, diff=%d, now=%" PRIu32 ", seen_high=%d, buffer_size=%d", cnt, diff, now, seen_high, + buffer.size()); + cnt_prev = cnt; + + auto *str = "1234567890\n"; + CDC_Transmit_FS(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(str)), 5); + tick_next += 1000; seen_high = false; - dbg.println("wat"); } - seen_high |= HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8); + if (buffer.is_full()) { + buffer_lock.lock(); + + for (unsigned int i = 0; i < buffer.size(); i++) { + dbg.println("%d", static_cast<int>(buffer.at(i))); + } + dbg.println("\n"); + + buffer.reset(); +// buffer_lock.unlock(); + } + + seen_high |= HAL_GPIO_ReadPin(RADIO_RX_GPIO_Port, RADIO_RX_Pin); #ifdef HAL_IWDG_MODULE_ENABLED HAL_IWDG_Refresh(&hiwdg); #endif } -void it_tim1() { - ; +void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) +{ + auto input_capture = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1); + + if (buffer_lock.try_lock()) { + buffer.append(static_cast<uint16_t>(input_capture)); + buffer_lock.unlock(); + } } |