Skip to content

Commit

Permalink
Fix TimerOc issue and add debug support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bola Malek committed Jul 21, 2018
1 parent 41dc879 commit 82e1054
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 97 deletions.
16 changes: 16 additions & 0 deletions .gdbinit
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions TimerOc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
70 changes: 2 additions & 68 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down
67 changes: 60 additions & 7 deletions timer_oc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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");
}

Expand All @@ -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) {
Expand All @@ -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");
}
}
20 changes: 0 additions & 20 deletions timer_oc_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 82e1054

Please sign in to comment.