Skip to main content
Visitor II
July 14, 2025
Question

How to transfer usb cdc data using DMA?

  • July 14, 2025
  • 1 reply
  • 753 views

Hello everyone.
I am using st32f407 chip to drive ad7606c ADC chip, and I want to output real time graph from PC to matlab via usb.
ad7606c outputs busy signal at the end of conversion, and GPIO callback function is executed like below according to busy signal.

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
 if(GPIO_Pin == BUSY_Pin && dma_busy == 0) {
 Start_ADC_DMA_Receive();
 }
}

Then the ‘Start_ADC_DMA_Receive()’ function is executed, which is shown below.

// Start receiving DMA (call only once, repeat in subsequent callbacks)
void Start_ADC_DMA_Receive(void) {
dma_busy = 1;
//HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_3); // Toggle LEDs for status checking

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET); // CS Low

However, if the DMA reception completion callback function is not executed afterward, the PA3(CS) pin output does not go high. The code is shown below.

// DMA Receive Complete Callback
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
if (hspi->Instance == SPI1) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET); // CS High


Why is the DMA callback function not executing?

I'll provide the full code as an attachment.


Edited to apply source code formatting - please see How to insert source code for future reference.

    This topic has been closed for replies.

    1 reply

    Technical Moderator
    July 14, 2025

    Hello @ildong 

    You only initialize the RX DMA handle, but you use HAL_SPI_TransmitReceive_DMA() which requires both TX and RX DMA handles to be properly initialized and linked.

    You need to initialize and link the TX DMA handle (hdma_spi1_tx) as well, because HAL_SPI_TransmitReceive_DMA() uses both TX and RX DMA streams.

    Please refer to the example below: 

    STM32CubeF4/Projects/STM32F4-Discovery/Examples/SPI/SPI_FullDuplex_ComDMA at master · STMicroelectronics/STM32CubeF4 · GitHub

    ildongAuthor
    Visitor II
    July 15, 2025

    I have added the code for tx as you told me.
    I'm using the toggle signal output to see which functions are working, and the 'Start_ADC_DMA_Receive' function is working, but the 'HAL_SPI_RxCpltCallback' function, which should be the next one to work, is not working.
    I'd like to know if this is a code issue or another issue.

    I'll attach the full code.

    Technical Moderator
    July 15, 2025

    Hello @ildong 

    Did you check the trafic on bus with logic analyser ? 

    Did the SPI clock generated on the bus? 

    You are sending a dummy data. If you want to juste receive why you don't use HAL_SPI_Receive_DMA ().

    Update the call to HAL_SPI_TransmitReceive_DMA as below to check the return value:

    if (HAL_SPI_TransmitReceive_DMA(&hspi1,(uint8_t*)dummy_tx,(uint8_t*)adc_buf[active_buf], ADC_BUF_SIZE) != HAL_OK)
    {
     Error_handler()
    };