How to get ADC-DMA working on a STM32G431KB board with CubeIDE
Hi,
I am using a STM32G431KB board with STMCubeIDE. I am trying to read out the one ADC using DMA but have run into problems. As I am new to the STM32G4 and STM STMCubeIDE. I decided to follow the example of Shawn Hymel https://www.youtube.com/watch?v=EsZLgqhqfO0. A number of other examples are available which are essentially the same.
I got the single conversion example working correctly. This tells me the ADC is working.
The ADC-DMA example did not work and despite several weeks of investigation I am unable to find the reason for this. I have attached the key parts of the main code. This sets up the ADC and immediately before calling HAL_ADC_Start_DMA writes a constant value of 2047 to the ADC buffer. Once started the buffer is read out to the UART where the conversion values (albeit without sync) are written.
What I can deduce from this code is:
1. Zeros are being written to the ADC buffer by the DMA instead of the ADC conversion value. (The debugger shows that the “2047�?s are overwritten with zeros).
2. The buffer half-complete and buffer complete interrupts are being called.
3. The timing of the digital toggle on GPIO pin 10 is consistent with a ADC clock of 170/4 MHz, suggesting the clocks are set correctly.
Other tests revealed:
4. HAL_ADC_Start_DMA does not return an error code.
5. Reading the forum it seems the capacitor switching can lead to the ADC conversion becoming frail at too high a clock frequency. So I tried changing the clock pre-scaler from divide by 4 to divide by 6. This gave no improvement.
Incidentally, I did try to import the code to the Arduino IDE. That did not work either.
If anybody has any idea what is wrong, or where to look. Please let me know.
Many thanks in advance.
volatile uint16_t adc_buf[ADC_BUF_LEN];
char msg [20];
int16_t ix;
int main(void)
{
HAL_Init();
for (int i = 0;i < ADC_BUF_LEN; i++){
adc_buf[i] = 2047;
}
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buf, ADC_BUF_LEN);
sprintf(msg, "Started ADC \r\n");
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
HAL_Delay(1);
while (1)
{
for (int i = 0; i < ADC_BUF_LEN; i++ ){
sprintf(msg, "%i : %hu\r\n",i, adc_buf[i]);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
HAL_Delay(1);
}
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc1){
// Test_set GPIO pin High
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc2){
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
}
