cancel
Showing results for 
Search instead for 
Did you mean: 

How to transfer usb cdc data using DMA?

ildong
Visitor

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.

1 REPLY 1
Saket_Om
ST Employee

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

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om