‎2019-08-08 12:22 AM
I'm relatively new to STM32. Before I was a Freescale programmer.
To practice I am writing small programs in order to better understand the structure of STM32Fxxx.
I created a small program that generates two waveforms, staircase and sinusoidal, taking inspiration from the examples of the Stm.
The program works well if I choose to generate one or the other type of waveform.
Then I decided to alternate the two waveforms, making them appear for 10 seconds each.
Unexpectedly, the call to HAL_Delay (10000) causes the blocking of the program, causing it to jump into an infinite loop as can be seen from the screenshot below:
The code is relatively simple, compile without errors, flashes correctly and I can trace it in debug mode in the usual manner.
I'm experimenting with and STM32F0Discovery board. and I'm using Eclipse with arm-none-eabi-gcc toolchain under Debian linux, and GNU MCU Eclipse plugin.
Here is my code:
#include "main.h"
static DAC_HandleTypeDef DacHandle;
static DAC_ChannelConfTypeDef sConfig;
const uint16_t Sine12bit[32] = {
2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,
3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909,
599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};
const uint16_t Escalator8bit[6] = {0x0, 0x3300, 0x6600, 0x9900, 0xCC00, 0xFF00};
uint8_t Idx = 0;
uint8_t selectedWaveform = 1;
uint8_t keyPressed = 0;
/* Private function prototypes -----------------------------------------------*/
static void DAC_Ch1_SinewaveConfig(void);
static void DAC_Ch1_EscalatorConfig(void);
static void TIM6_Config(void);
static void Error_Handler(void);
static void EXTI0_1_IRQHandler_Config(void);
static void SystemClock_Config(void);
int main(void)
{
HAL_Init();
/* Configure the system clock to have a system clock = 48 Mhz */
SystemClock_Config();
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
// Initialize User_Button
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO);
DacHandle.Instance = DAC;
TIM6_Config();
for(;;)
{
HAL_DAC_DeInit(&DacHandle);
if (selectedWaveform == 1)
{
// Sine Wave generator
DAC_Ch1_SinewaveConfig();
selectedWaveform = 0;
}
else
{
// Escalator generator
DAC_Ch1_EscalatorConfig();
selectedWaveform = 1;
}
//selectedWaveform == 0 ? 1 : 0;
HAL_Delay(5000);
}
} // end main()
void TIM6_Config(void)
{
static TIM_HandleTypeDef htim;
TIM_MasterConfigTypeDef sMasterConfig;
/*##-1- Configure the TIM peripheral #######################################*/
/* Time base configuration */
htim.Instance = TIM6;
htim.Init.Period = 0x7FF;
htim.Init.Prescaler = 0;
htim.Init.ClockDivision = 0;
htim.Init.CounterMode = TIM_COUNTERMODE_UP;
htim.Init.RepetitionCounter = 0;
htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_Base_Init(&htim);
/* TIM6 TRGO selection */
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig);
/*##-2- Enable TIM peripheral counter ######################################*/
HAL_TIM_Base_Start(&htim);
}
static void DAC_Ch1_EscalatorConfig(void)
{
/*##-1- Initialize the DAC peripheral ######################################*/
if (HAL_DAC_Init(&DacHandle) != HAL_OK)
{
/* Initiliazation Error */
Error_Handler();
}
/*##-1- DAC channel1 Configuration #########################################*/
sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1) != HAL_OK)
{
/* Channel configuration Error */
Error_Handler();
}
/*##-2- Enable DAC Channel1 and associeted DMA #############################*/
if (HAL_DAC_Start_DMA(&DacHandle, DAC_CHANNEL_1, (uint32_t *)Escalator8bit, 6, DAC_ALIGN_12B_R) != HAL_OK)
{
/* Start DMA Error */
Error_Handler();
}
}
static void DAC_Ch1_SinewaveConfig(void)
{
/*##-1- Initialize the DAC peripheral ######################################*/
if (HAL_DAC_Init(&DacHandle) != HAL_OK)
{
/* Initiliazation Error */
Error_Handler();
}
/*##-1- DAC channel1 Configuration #########################################*/
sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1) != HAL_OK)
{
/* Channel configuration Error */
Error_Handler();
}
/*##-2- Enable DAC Channel1 and associeted DMA #############################*/
if (HAL_DAC_Start_DMA(&DacHandle, DAC_CHANNEL_1, (uint32_t *)Sine12bit, 32, DAC_ALIGN_12B_R) != HAL_OK)
{
/* Start DMA Error */
Error_Handler();
}
}
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable HSE Oscillator and Activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK)
{
Error_Handler();
}
/* Select PLL as system clock source and configure the HCLK and PCLK1 clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1)!= HAL_OK)
{
Error_Handler();
}
}
static void Error_Handler(void)
{
/* Turn LED3 on */
BSP_LED_On(LED3);
while (1)
{
}
}
I 'm really don't know why the program hangs in an infinite loop when calling HAL_Delay().
The WWDG_IRQHandler() is called, but I never activated the watchdog module in no way...
Am I doing something wrong?
Thanks for help,
Max
‎2019-08-08 12:40 AM
I forgot to insert coe snipper of peripheral initialization (DMA and DAC). Here is:
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
{
GPIO_InitTypeDef GPIO_InitStruct;
static DMA_HandleTypeDef hdma_dac1;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO clock ****************************************/
DACx_CHANNEL_GPIO_CLK_ENABLE();
/* DAC Periph clock enable */
DACx_CLK_ENABLE();
/* DMA1 clock enable */
DMAx_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* DAC Channel1 GPIO pin configuration */
GPIO_InitStruct.Pin = DACx_CHANNEL_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(DACx_CHANNEL_GPIO_PORT, &GPIO_InitStruct);
/*##-3- Configure the DMA ##########################################*/
/* Set the parameters to be configured for DACx_DMA1_CHANNEL3 */
hdma_dac1.Instance = DACx_DMA_INSTANCE;
hdma_dac1.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_dac1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_dac1.Init.MemInc = DMA_MINC_ENABLE;
hdma_dac1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; //DMA_PDATAALIGN_BYTE;
hdma_dac1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; //DMA_MDATAALIGN_BYTE;
hdma_dac1.Init.Mode = DMA_CIRCULAR;
hdma_dac1.Init.Priority = DMA_PRIORITY_HIGH;
HAL_DMA_Init(&hdma_dac1);
/* Associate the initialized DMA handle to the the DAC handle */
__HAL_LINKDMA(hdac, DMA_Handle1, hdma_dac1);
/*##-4- Configure the NVIC for DMA #########################################*/
/* Enable the DMA1_Channel3 IRQ Channel */
HAL_NVIC_SetPriority(DACx_DMA_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(DACx_DMA_IRQn);
}
/**
* @brief DeInitializes the DAC MSP.
* @param hdac: pointer to a DAC_HandleTypeDef structure that contains
* the configuration information for the specified DAC.
* @retval None
*/
void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
{
/*##-1- Reset peripherals ##################################################*/
DACx_FORCE_RESET();
DACx_RELEASE_RESET();
/*##-2- Disable peripherals and GPIO Clocks ################################*/
/* De-initialize the DAC Channel1 GPIO pin */
HAL_GPIO_DeInit(DACx_CHANNEL_GPIO_PORT, DACx_CHANNEL_PIN);
/*##-3- Disable the DMA Channel ############################################*/
/* De-Initialize the DMA Channel associate to DAC_Channel1 */
HAL_DMA_DeInit(hdac->DMA_Handle1);
/*##-4- Disable the NVIC for DMA ###########################################*/
HAL_NVIC_DisableIRQ(DACx_DMA_IRQn);
}
/**
* @brief TIM MSP Initialization
* This function configures the hardware resources used in this example:
* - Peripheral's clock enable
* - Peripheral's GPIO Configuration
* @param htim: TIM handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
{
/* TIM6 Periph clock enable */
__HAL_RCC_TIM6_CLK_ENABLE();
}
/**
* @brief TIM MSP De-Initialization
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO to their default state
* @param htim: TIM handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
{
/*##-1- Reset peripherals ##################################################*/
__HAL_RCC_TIM6_FORCE_RESET();
__HAL_RCC_TIM6_RELEASE_RESET();
}
And the peripheral modules I enabled:
/* ########################## Module Selection ############################## */
/**
* @brief This is the list of modules to be used in the HAL driver
*/
#define HAL_MODULE_ENABLED
/* #define HAL_ADC_MODULE_ENABLED */
/* #define HAL_CAN_MODULE_ENABLED */
/* #define HAL_CEC_MODULE_ENABLED */
/* #define HAL_COMP_MODULE_ENABLED */
#define HAL_CORTEX_MODULE_ENABLED
/* #define HAL_CRC_MODULE_ENABLED */
#define HAL_DAC_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
#define HAL_FLASH_MODULE_ENABLED
#define HAL_GPIO_MODULE_ENABLED
/* #define HAL_I2C_MODULE_ENABLED */
/* #define HAL_I2S_MODULE_ENABLED */
/* #define HAL_IRDA_MODULE_ENABLED */
/* #define HAL_IWDG_MODULE_ENABLED */
/* #define HAL_PCD_MODULE_ENABLED */
/* #define HAL_PWR_MODULE_ENABLED */
#define HAL_RCC_MODULE_ENABLED
///#define HAL_RTC_MODULE_ENABLED
/* #define HAL_SMARTCARD_MODULE_ENABLED */
/* #define HAL_SMBUS_MODULE_ENABLED */
/* #define HAL_SPI_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
/* #define HAL_TSC_MODULE_ENABLED */
/* #define HAL_UART_MODULE_ENABLED */
/* #define HAL_USART_MODULE_ENABLED */
/* #define HAL_WWDG_MODULE_ENABLED */
Many thanks in advance for help me.
Max
‎2019-08-08 01:00 AM
> The WWDG_IRQHandler() is called,
No, you have a fault, probably hardfault; it's just that you don't have a separate handler for hardfault nor for the WWDF interrupt handler so both use the same default handler.
JW
‎2019-08-08 12:45 PM
HardFault_Handler() is implemented in stm32f0xx.c . I've tried to implement WWDG_IRQHandler() also amd put breakpoint in both.
Execution do not halt when an exception is encountered after the HAL_Delay() call.
The program stucks in default handler, and the stack this shows "<signal handler called>() at 0xfffffff9.
I've tried to raise the default values of __Main_Stack_Size and _Minimum_Stack_Size (in file sections.ld) with no results.
I'm really puzzled.
Max
‎2019-08-08 03:12 PM
Is it using a TIM or SysTick for the clock?
Do you have an IRQHandler for either?​
‎2019-08-08 04:21 PM
Clive,
You pointed me in the right direction.
A careful examination of the file stm32f0xx_it.c shows that the SysTick_Handler was correct:
void SysTick_Handler(void)
{
HAL_IncTick();
}
that is used by HAL_Delay() call in my main.c .
But I forgot completely to implement the following: :expressionless_face:
void DMA1_Channel2_3_IRQHandler(void)
{
HAL_DMA_IRQHandler(DacHandle.DMA_Handle1);
}
So, after the first transfer, the DMA never was serviced and because that, an hard fault was generated, I presume....
Now the program works perfectly. My brain a little less. I got a headache !!!!
Thank you Clive!
Max