SPI_Receive to USB_Transmit via DMA on STM32H7
Dear all,
I try to read out ADC-Data, that is streamed from an external adc (AD7768) out to the uC. What I want to do is to more or less directly stream out the data via USB CDC.
Therefore, I planned to use the DMA between the SPI and USB. However, this doesn't really work consistently. It seems that there may be a timing problem and after some seconds to minutes the transmission via USB stops.
At the moment I only start the HAL_SPI_Receive_DMA before while(1) and again in the receive complete callback before the USB_CDC_TX is called. Another strange thing is, that as soon as I do something inside the USB_Transmit_complete function, for example a HAL_Delay, the code stops here again.
Is there anything I missed? Is it even possible to use USB with DMA and SPI this way?
I also tried it with Interrupts, in this case the Code doesn't stop. However, only as long as I don't put anything in the CDC_Transmit_Complete function.
I am looking forward to your responses.
Thank you a lot in advance
Best regards
Ben
#define usb_packet_size 100
#define nbr_of_bytes 16
uint8_t adc_buffer[4][nbr_of_bytes*usb_packet_size+2];
uint8_t current_buffer_pos = 0;
uint16_t current_data_pos = 0;
int main(void){
//inits etc...
adc_buffer[0][nbr_of_bytes*usb_packet_size+0] = 0x0d;
adc_buffer[0][nbr_of_bytes*usb_packet_size+1] = 0x0a;
adc_buffer[1][nbr_of_bytes*usb_packet_size+0] = 0x0d;
adc_buffer[1][nbr_of_bytes*usb_packet_size+1] = 0x0a;
adc_buffer[2][nbr_of_bytes*usb_packet_size+0] = 0x0d;
adc_buffer[2][nbr_of_bytes*usb_packet_size+1] = 0x0a;
adc_buffer[3][nbr_of_bytes*usb_packet_size+0] = 0x0d;
adc_buffer[3][nbr_of_bytes*usb_packet_size+1] = 0x0a;
HAL_SPI_Receive_DMA(&hspi1, &adc_buffer[current_buffer_pos][current_data_pos], nbr_of_bytes);
while (1)
{
}
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
if (current_data_pos < 16 * (usb_packet_size - 1)) {
current_data_pos += 16;
HAL_SPI_Receive_DMA(&hspi1,
&adc_buffer[current_buffer_pos][current_data_pos], 16);
}
else {
current_data_pos = 0;
uint8_t current_buffer_cpy = current_buffer_pos;
if (current_buffer_pos == 3) {
current_buffer_pos = 0;
} else {
current_buffer_pos++;
}
HAL_SPI_Receive_DMA(&hspi1,
&adc_buffer[current_buffer_pos][current_data_pos], 16);
CDC_Transmit_HS(&adc_buffer[current_buffer_cpy][0],
16 * usb_packet_size + 2);
}
}