0
\$\begingroup\$

I'm trying to send some data through UART using DMA. I'm also using DMA to control ADC and DAC. When ADC is enabled and I try to send data I get a HardFault error. The HardFault occurs exactly during execution of HAL_UART_IRQHandler(&huart2) at the line: uint32_t isrflags = READ_REG(huart->Instance->ISR); . When I stop the ADC the problem disappears but I can't understand why?

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
    if (i<2048)
    {
        FFT_buffer_in[i] = (3.3*analogInput)/4095;
        i++;
    }
    else
    {
        i = 0;
        doFFT();
    }

}
int main(void)
{
      uint8_t MSG[] = "yo waddup ";


  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_DAC1_Init();
  MX_TIM2_Init();
  MX_TIM1_Init();
  MX_USART2_UART_Init();

   arm_rfft_fast_init_f32(&fft_handler, 2048);

   HAL_ADC_Start_DMA(&hadc1, &analogInput, 1);
   HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (uint32_t*)Wave_LUT, 128, DAC_ALIGN_12B_R);
   HAL_TIM_Base_Start(&htim2);
   HAL_TIM_Base_Start(&htim1);

  while (1)
  {
  //HAL_ADC_Stop(&hadc1); -- only if I uncomment this, the UART_Transmit works

  HAL_UART_Transmit_DMA(&huart2, MSG, sizeof(MSG));
  HAL_UART_IRQHandler(&huart2);

    HAL_Delay(500);

  }
 ```
\$\endgroup\$
5
  • 3
    \$\begingroup\$ You are not supposed to call HAL_UART_IRQHandler. It will be called by the interrupt handler. \$\endgroup\$
    – Codo
    Feb 27, 2021 at 21:06
  • \$\begingroup\$ If I don't call it manually, the HAL_UART_TxCpltCallback never executes - don't know why. Also when I don't call this IRQHandler the UART_Transmit sends data only once instead of sending data every 500ms. \$\endgroup\$
    – blankMCU
    Feb 27, 2021 at 21:29
  • \$\begingroup\$ You do call it directly. It's right there in the code of your question. And if HAL_UART_TxCpltCallback is not called, temporarily remove the DAC and ADC stuff and work on this issue until it's solved. \$\endgroup\$
    – Codo
    Feb 27, 2021 at 22:36
  • \$\begingroup\$ You're supposed to create a UART2_IRQHandler() function which calls HAL_UART_IRQHandler() (or it might be USART2_IRQHandler(), depending on your specific micro which you haven't shared with us). Check your project's assembler startup file (startup_stm_something-or-the-other.s) for the correct function name - it should have all the xxx_IRQHandler function names listed there. \$\endgroup\$
    – brhans
    Feb 28, 2021 at 2:50
  • 1
    \$\begingroup\$ By far the most likely cause of a hardfault on that like of code is that your huart2 uart 'handle' structure has been accidentally overwritten - in particular the Instance member which is supposed to hold a pointer to the UART's register set. My best guess is that you have not correctly configured the ADC DMA correctly, and the DMA is doing a kind of 'buffer overflow' and writing to that region of memory. Show us the code where you configure the ADC DMA & the buffer (array) you're DMAing into. \$\endgroup\$
    – brhans
    Feb 28, 2021 at 2:54

1 Answer 1

0
\$\begingroup\$

I've fixed it. It didn't call the USART2_IRQHanlder() because I didn't enable USART global interrupts. That didn't fix the whole problem though. UART Transmit still didn't work properly - it was in BUSY state after sending 1 message. I followed the code and found why - turns out the HAL_Delay(500) affected the memory where the state was stored and that's why the state value was incorrect. Thanks for all your input, it helped me follow the problem to its root.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.