0
votes


I am trying to implement the following scenario on STM32F103C8 Microcontroller.
On PB11 and PB10 I've LED and Button connected respectively. LED is blinking continuously 500ms, but when button is pressed it blinks with 100ms delay 20 times.
I have also connected UART (PA3-PA2) and Potentiometer on ADC (PA0). My task is to transfer ADC reading to UART in DMA mode.
LED and Button interrupt worked well, but as soon as i have added the code for ADC and USART handling it stopped working.
Could you please advice, where is my mistake in ADC-DMA-UART processing and how can i fix it?

Snippets from Main.c

//Buffer for ADC.
uint16_t buffer[5];

huart2.Instance->CR3 |= USART_CR3_DMAT;

//Transfer ADC reading to Buffer in DMA.
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)buffer, 5); 

while (1)
  {
        //LED blinking
    HAL_GPIO_TogglePin(GPIOB, LED_Pin);
    HAL_Delay(500);
  }

//ADC callback function - When buffer is full transfer to UART.
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
    HAL_DMA_Start_IT(&hdma_usart2_tx, (uint32_t)buffer, (uint32_t)&huart2.Instance->DR, sizeof(buffer));
}


//Interrupt handler for Button.
void EXTI15_10_IRQHandler(void) {
    HAL_GPIO_EXTI_IRQHandler(BT_Pin);
}

//Callback function for Button.
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
    if(GPIO_Pin == BT_Pin){
        for(volatile int i=20; i>0; i--){
            HAL_GPIO_TogglePin(GPIOB, LED_Pin);
            HAL_Delay(100);
    }
}
1
so after you added //Transfer ADC reading to Buffer in DMA. HAL_ADC_Start_DMA(&hadc1, (uint32_t*)buffer, 5); you couldnt interrupt the LED anymore ?Ali Ben Zarrouk
Yes. After I've added DMA and ADC.ussrback

1 Answers

0
votes

The most likely reason to me is that the ADC interrupt handler (including ST library functions and the callback you presented) is triggered too frequently, so that the ISR of the EXTI triggered by the push button is suppressed (permanently or nearly permanently).

This can happen even more easily if you selected a minimal sample time and continuous conversion mode (because sampling and conversion then happen as often as it goes, and the IRQ that triggers your conversion-complete callback (HAL_ADC_ConvCmpltCallback()) might run all the time.

In order to verify/falsify my assumption, please inspect

  • your interrupt priorities for ADC and EXTI (and others you may have on the system)
  • what happens if you select a longer ADC sampling period, or if you slow down the clock source of the ADC (without slowing down the CPU clock, of course).

If this didn't fix your problem, you may be able post another, refined question.