Potential DAC and FreeRTOS conflict?
Hello,
I am using STM32F417 to control a sounder among many other functions. In the firmware, the DAC is controlling the sounder which works fine when tested alone. However, the moment the FreeRTOS is enabled, the DAC stops working. What I am suspecting is that the Timer I am using to trigger the DAC (TIM6) is somehow deprioritized or disabled as I can hear the sounder clicking and thats because only the first sample of the waveform is actually sent to the sounder.
These are the functions I use to control the sounder
void set_sounder_freq_2khz(uint8_t volume)
{
// configure DAC to generate amplitude controlled square wave for the buzzer
HAL_TIM_Base_Stop(&DAC_HTIM);
HAL_DAC_Stop_DMA(&DAC_HDAC, DAC_CHANNEL);
// DAC sampling frequency is set to 8kHz
memset(dac_buff, 0, sizeof(dac_buff));
for(uint32_t i=0; i < (sizeof(dac_buff)/sizeof(uint16_t)); i+=4) {
dac_buff[i] = (uint16_t)volume;
dac_buff[i+1] = (uint16_t)volume;
}
}
void sounder_on(bool on)
{
// DAC_ChannelConfTypeDef sConfig;
if (on == true) {
//HAL_TIM_PWM_Start(&SOUNDER_HTIM, SOUNDER_CHANNEL);
HAL_DAC_Start_DMA( &DAC_HDAC,
DAC_CHANNEL,
(uint32_t *)dac_buff,
sizeof(dac_buff)/sizeof(uint16_t),
DAC_ALIGN_8B_R);
HAL_TIM_Base_Start(&DAC_HTIM);
//SOUNDER_EN_HIGH;
}
else {
//SOUNDER_EN_LOW;
HAL_TIM_Base_Stop(&DAC_HTIM);
HAL_DAC_Stop_DMA(&DAC_HDAC, DAC_CHANNEL);
}
}This is how they are called in my test.
int main(void)
{
/* USER CODE BEGIN 1 */
// Set up the IRQ vectors
extern uint32_t Image$$ER_APPLICATION$$Base;
SCB->VTOR = (uint32_t) &Image$$ER_APPLICATION$$Base;
__DSB();
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
MX_CRC_Init();
MX_CRYP_Init();
MX_HASH_Init();
MX_RNG_Init();
MX_ADC1_Init();
MX_I2C1_Init();
MX_SPI2_Init();
MX_SPI3_Init();
MX_TIM2_Init();
MX_TIM3_Init();
MX_TIM4_Init();
MX_TIM8_Init();
MX_USART2_UART_Init();
MX_TIM6_Init();
MX_RTC_Init();
MX_DAC_Init();
//MX_ADC3_Init();
#ifdef HAL_CAN_MODULE_ENABLED
MX_CAN1_Init();
#endif
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* definition and creation of defaultTask */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* USER CODE BEGIN RTOS_THREADS */
set_sounder_freq_2khz(20);
sounder_on(true);
HAL_Delay(1000);
sounder_on(false);
/* USER CODE END RTOS_THREADS */
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}Note that if I remove the following initiliaization of the FreeRTOS default task, the sounder works fine
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);I even tried to have an empty defaultTask and the sounder still doesn't work.
Any suggestions would be greatly appreciated!
Many thanks
Samer
