2026-03-27 5:40 AM - last edited on 2026-03-27 5:55 AM by mƎALLEm
Hello everyone,
I am currently working with the STM32G431CBT6 and designing a custom ESC using the DRV8301 gate driver. My application requires a total of 9 ADC channels:
ADC1 → 4 channels
ADC2 → 5 channels
I configured both ADCs in independent mode with DMA (circular) using STM32CubeMX.
When I start ADC with DMA using the following function:
void ADC_Start_System(void)
{
// 1. Calibration
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
HAL_Delay(10);
// 2. Start ADC1
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc1_buffer, 4) != HAL_OK)
Error_Handler();
// 3. Start ADC2
if (HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2_buffer, 5) != HAL_OK)
Error_Handler();
}
ADC1 + DMA works perfectly
MCU gets stuck when calling:
HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2_buffer, 5);
Calibration is performed before start
DMA is configured in circular mode
NVIC interrupts for DMA channels are enabled
Separate DMA channels used for ADC1 and ADC2
Buffers are correctly sized:
uint16_t adc1_buffer[4];
uint16_t adc2_buffer[5];
ADC mode is independent (not dual mode)
MCU: STM32G431CBT6
Toolchain: STM32CubeMX + HAL
Application: ESC (motor control)
No external trigger (software start used)
Continuous conversion mode enabled
Why does HAL_ADC_Start_DMA() work for ADC1 but cause the MCU to hang for ADC2?
Is there any known limitation or special requirement for using ADC2 with DMA on STM32G4 (especially when ADC1 is already running with DMA)?
Thanks in advance!
2026-03-27 5:50 AM - edited 2026-03-27 6:06 AM
The cpu is too busy with the interrupts generated by ADC1 to progress any further in the code.
Reduce the sample rate by increasing the sampling time or having a timer trigger it.
Debug the code and hit pause when it's "stuck" to see where execution is. Likely it's within an interrupt.
2026-03-27 5:56 AM - edited 2026-03-27 5:59 AM
Disable ADC1 and let ADC2 working, what happens?
You didn't show the DMA settings of DMA2 in CubeMx. Did you configure it?
2026-03-27 7:25 AM
I tested the system using ADC1 only, and it worked perfectly. However, when I disabled ADC1 and enabled ADC2, the program gets stuck.
Previously, I implemented a bare-metal solution where both ADC1 and ADC2 were configured (even running together), and that setup worked without any issues.
Now, using the HAL library, ADC2 is not functioning correctly and causes the system to hang. I’m trying to understand why this issue occurs with HAL, while the same configuration works fine in bare-metal implementation.
2026-03-27 7:30 AM
@shohanur00 wrote:
when I disabled ADC1 and enabled ADC2, the program gets stuck.
Where it gets stuck?
2026-03-27 7:33 AM
2026-03-27 7:52 AM
if (HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2_buffer, 5) != HAL_OK)
Error_Handler();In this part it gate stuck
2026-03-27 7:55 AM
Retreive what HAL_ADC_Start_DMA() returns to get the error..
2026-03-27 8:03 AM - last edited on 2026-03-27 8:07 AM by mƎALLEm
HAL_StatusTypeDef status;
status = HAL_ADC_Start_DMA(&hadc2, (uint32_t*)adc2_buffer, 5);
if(status != HAL_OK)
{
char msg[100];
sprintf(msg, "ADC2 DMA Start FAIL! Status: %d\r\n", status);
HAL_UART_Transmit_DMA(&huart3, (uint8_t*)msg, strlen(msg));
}
- I have tried to print the error , but when I call start function It get stucked. Nothing print out
Post edited by a ST moderate to follow the community rules. In next time please use </> button to share your code.
2026-03-27 8:08 AM - edited 2026-03-27 8:09 AM
.No need to use printf. Put a breakpoint at if() line and show the status value in the debugger live expression.