From 82e105407f1530b8b5aa503a3d892cd58d887ce4 Mon Sep 17 00:00:00 2001 From: Bola Malek Date: Fri, 20 Jul 2018 18:20:36 -0700 Subject: [PATCH] Fix TimerOc issue and add debug support. --- .gdbinit | 16 ++++++++++++ Makefile | 2 ++ TimerOc.h | 3 +-- main.cpp | 70 ++------------------------------------------------ timer_oc_api.c | 67 ++++++++++++++++++++++++++++++++++++++++++----- timer_oc_api.h | 20 --------------- 6 files changed, 81 insertions(+), 97 deletions(-) create mode 100644 .gdbinit diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..32f20df --- /dev/null +++ b/.gdbinit @@ -0,0 +1,16 @@ + +set print pretty on +set mem inaccessible-by-default off + +define reload + load + run +end + +define reset + monitor reset halt +end + +file BUILD/DeBroglie.elf +target remote localhost:3333 +reset diff --git a/Makefile b/Makefile index af1d075..32ef505 100644 --- a/Makefile +++ b/Makefile @@ -275,6 +275,7 @@ ELF2BIN = 'arm-none-eabi-objcopy' PREPROC = 'arm-none-eabi-cpp' '-E' '-P' '-Wl,--gc-sections' '-Wl,--wrap,main' '-Wl,--wrap,_malloc_r' '-Wl,--wrap,_free_r' '-Wl,--wrap,_realloc_r' '-Wl,--wrap,_memalign_r' '-Wl,--wrap,_calloc_r' '-Wl,--wrap,exit' '-Wl,--wrap,atexit' '-Wl,-n' '-mcpu=cortex-m7' '-mthumb' '-mfpu=fpv5-sp-d16' '-mfloat-abi=softfp' +C_FLAGS += -ggdb3 C_FLAGS += -std=gnu99 C_FLAGS += -DDEVICE_SPI=1 C_FLAGS += -DTARGET_STM32F746xG @@ -331,6 +332,7 @@ C_FLAGS += -include C_FLAGS += mbed_config.h C_FLAGS += -DDEBUG +CXX_FLAGS += -ggdb3 CXX_FLAGS += -std=gnu++11 CXX_FLAGS += -fno-rtti CXX_FLAGS += -Wvla diff --git a/TimerOc.h b/TimerOc.h index 76c2b09..a9706a2 100644 --- a/TimerOc.h +++ b/TimerOc.h @@ -41,10 +41,9 @@ class TimerOc { } void stop() { - printf("before cs\n"); core_util_critical_section_enter(); - lock_deep_sleep(); timer_oc_stop(&_timer_oc); + unlock_deep_sleep(); core_util_critical_section_exit(); } diff --git a/main.cpp b/main.cpp index d74ac07..62de63e 100644 --- a/main.cpp +++ b/main.cpp @@ -2,79 +2,16 @@ #include "mbed.h" #include "minig.h" -DigitalOut red{LED1}; DigitalOut blue{LED2}; -DigitalOut green{LED3}; Ticker led_ticker; void toggle_led() { static int i = 0; - - red = i & 1; - blue = i & 2; - green = i & 4; + blue = i & 1; i++; } -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Definition of TIM instance */ -#define TIMx TIM3 - -/* Definition for TIMx clock resources */ -#define TIMx_CLK_ENABLE __HAL_RCC_TIM3_CLK_ENABLE -#define DMAx_CLK_ENABLE __HAL_RCC_DMA1_CLK_ENABLE - -/* Definition for TIMx Pins */ -#define TIMx_CHANNEL3_GPIO_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE - -#define TIMx_GPIO_CHANNEL3_PORT GPIOB -#define GPIO_PIN_CHANNEL3 GPIO_PIN_0 - -#define GPIO_AF_TIMx GPIO_AF2_TIM3 - -/* Definition for TIMx's DMA */ -#define TIMx_CC3_DMA_INST DMA1_Stream7 - -/* Definition for ADCx's NVIC */ -#define TIMx_DMA_IRQn DMA1_Stream7_IRQn -#define TIMx_DMA_IRQHandler DMA1_Stream7_IRQHandler - -void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) { - static DMA_HandleTypeDef hdma_tim; - - /* Enable DMA clock */ - DMAx_CLK_ENABLE(); - - /* Set the parameters to be configured */ - hdma_tim.Init.Channel = DMA_CHANNEL_5; - hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH; - hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE; - hdma_tim.Init.MemInc = DMA_MINC_ENABLE; - hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; - hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; - hdma_tim.Init.Mode = DMA_CIRCULAR; - hdma_tim.Init.Priority = DMA_PRIORITY_HIGH; - hdma_tim.Init.FIFOMode = DMA_FIFOMODE_DISABLE; - hdma_tim.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; - hdma_tim.Init.MemBurst = DMA_MBURST_SINGLE; - hdma_tim.Init.PeriphBurst = DMA_PBURST_SINGLE; - - /* Set hdma_tim instance */ - hdma_tim.Instance = TIMx_CC3_DMA_INST; - - /* Link hdma_tim to hdma[TIM_DMA_ID_CC3] (channel3) */ - __HAL_LINKDMA(htim, hdma[TIM_DMA_ID_CC3], hdma_tim); - - /* Initialize TIMx DMA handle */ - HAL_DMA_Init(htim->hdma[TIM_DMA_ID_CC3]); - - /*##-2- Configure the NVIC for DMA #########################################*/ - /* NVIC configuration for DMA transfer complete interrupt */ - HAL_NVIC_SetPriority(TIMx_DMA_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(TIMx_DMA_IRQn); -} -uint32_t ticks[3] = {4, 8, 4}; +uint32_t ticks[4] = {4, 8, 4, 4}; // MiniG minig; int main() { @@ -84,11 +21,8 @@ int main() { // minig.init(); TimerOc timer_oc{PB_0_ALT0}; - printf("before start\n"); timer_oc.start(100000 /* us*/, 3, ticks); - printf("before wait\n"); wait_ms(210); - printf("after wait\n"); timer_oc.stop(); while (1) { diff --git a/timer_oc_api.c b/timer_oc_api.c index dfc69ef..e5a510b 100644 --- a/timer_oc_api.c +++ b/timer_oc_api.c @@ -7,6 +7,62 @@ #include "pwmout_device.h" static TIM_HandleTypeDef TimHandle; +/* Definition for TIMx clock resources */ +#define DMAx_CLK_ENABLE __HAL_RCC_DMA1_CLK_ENABLE + +/* Definition for TIMx's DMA */ +#define TIMx_CC3_DMA_INST DMA1_Stream7 + +/* Definition for ADCx's NVIC */ +#define TIMx_DMA_IRQn DMA1_Stream7_IRQn +#define TIMx_DMA_IRQHandler DMA1_Stream7_IRQHandler + +void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim) { + static DMA_HandleTypeDef hdma_tim; + + /* Enable DMA clock */ + DMAx_CLK_ENABLE(); + + /* Set the parameters to be configured */ + hdma_tim.Init.Channel = DMA_CHANNEL_5; + hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_tim.Init.MemInc = DMA_MINC_ENABLE; + hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; + hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; + hdma_tim.Init.Mode = DMA_CIRCULAR; + hdma_tim.Init.Priority = DMA_PRIORITY_HIGH; + hdma_tim.Init.FIFOMode = DMA_FIFOMODE_DISABLE; + hdma_tim.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; + hdma_tim.Init.MemBurst = DMA_MBURST_SINGLE; + hdma_tim.Init.PeriphBurst = DMA_PBURST_SINGLE; + + /* Set hdma_tim instance */ + hdma_tim.Instance = TIMx_CC3_DMA_INST; + + /* Link hdma_tim to hdma[TIM_DMA_ID_CC3] (channel3) */ + __HAL_LINKDMA(htim, hdma[TIM_DMA_ID_CC3], hdma_tim); + + /* Initialize TIMx DMA handle */ + HAL_DMA_Init(htim->hdma[TIM_DMA_ID_CC3]); + + /*##-2- Configure the NVIC for DMA #########################################*/ + /* NVIC configuration for DMA transfer complete interrupt */ + HAL_NVIC_SetPriority(TIMx_DMA_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(TIMx_DMA_IRQn); +} + + +/** +* @brief This function handles DMA interrupt request. +* @param None +* @retval None +*/ +void TIMx_DMA_IRQHandler(void) +{ + HAL_DMA_IRQHandler(TimHandle.hdma[TIM_DMA_ID_CC3]); +} + void timer_oc_init(timer_oc_t *obj, PinName pin) { // Get the peripheral name from the pin and assign it to the object @@ -211,7 +267,7 @@ void timer_oc_start(timer_oc_t *obj, int us_period, int num_repetitions, ticks[i] = ticks[i] / obj->prescaler; } - if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { + if (HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { error("Cannot initialize OC\n"); } @@ -252,7 +308,7 @@ void timer_oc_start(timer_oc_t *obj, int us_period, int num_repetitions, return; } - if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { + if (HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, channel) != HAL_OK) { error("Cannot initialize OC\n"); } @@ -262,16 +318,13 @@ void timer_oc_start(timer_oc_t *obj, int us_period, int num_repetitions, } else #endif { - if (HAL_TIM_PWM_Start_DMA(&TimHandle, channel, &ticks[1], num_repetitions) != HAL_OK) { + if (HAL_TIM_OC_Start_DMA(&TimHandle, channel, &ticks[1], num_repetitions) != HAL_OK) { error("Cannot start DMA\n"); } } - wait_ms(210); - timer_oc_stop(obj); } void timer_oc_stop(timer_oc_t *obj) { - printf("stopping 1\n"); TimHandle.Instance = (TIM_TypeDef *)(obj->pwm); int channel = 0; switch (obj->channel) { @@ -291,7 +344,7 @@ void timer_oc_stop(timer_oc_t *obj) { return; } - if (HAL_TIM_PWM_Stop_DMA(&TimHandle, channel) != HAL_OK) { + if (HAL_TIM_OC_Stop_DMA(&TimHandle, channel) != HAL_OK) { error("Cannot start DMA\n"); } } diff --git a/timer_oc_api.h b/timer_oc_api.h index 18881d4..f7177b8 100644 --- a/timer_oc_api.h +++ b/timer_oc_api.h @@ -17,30 +17,10 @@ struct timer_oc_s { uint8_t inverted; }; - -/** TimerOc hal structure. timer_oc_s is declared in the target's hal - */ typedef struct timer_oc_s timer_oc_t; -/** Initialize the pwm out peripheral and configure the pin - * - * @param obj The timer_oc object to initialize - * @param pin The timer_oc pin to initialize - */ void timer_oc_init(timer_oc_t *obj, PinName pin); - -/** Deinitialize the timer_oc object - * - * @param obj The timer_oc object - */ void timer_oc_free(timer_oc_t *obj); - -/** Set the output duty-cycle in range <0.0f, 1.0f> - * - * Value 0.0f represents 0 percentage, 1.0f represents 100 percent. - * @param obj The timer_oc object - * @param percent The floating-point percentage number - */ void timer_oc_start(timer_oc_t *obj, int us_period, int num_repetitions, uint32_t * ticks); void timer_oc_stop(timer_oc_t *obj);