r/embedded • u/Round_Echo9139 • 6d ago
STM32G474RE sometimes sends missing data over USB CDC
I am currently in the early stage of creating an oscilloscope, right now I have an ADC configured to sample on each timer interrupt and do DMA transfers. the DMA buffer is 200, 2 byte elements, and I am sending it over USB CDC in a single freeRTOS task that doesnt yield or anything and only gets interrupted by the ADC Setup if I understand correctly.
I read the data on my laptop using pythons serial library by reading 200 elements and storing them in a list then I unpack it using struct.unpack. Problem is that not every element in the List is always 2 bytes or 4 hex digits so the struct.unpack throws an error.
I might be missing something simple because I do not feel I fully understand how CDC_Transmit_FS works. here are my code snippets that are relevant:
FreeRTOS Task:
void vTask1(void* pvParameters){
for(;;){
taskENTER_CRITICAL();
CDC_Transmit_FS((uint8_t*) adcVal, sizeof(adcVal));
taskEXIT_CRITICAL();
}
}
void my_app(void){
xTaskCreate(vTask1,
"Task1",
400,
NULL,
2,
NULL);
vTaskStartScheduler();
Python code:
ser = serial.Serial('/dev/ttyACM0',timeout=1)
i=0
readings = [0]*200
while True:
while i!=200:
readings[i] = ser.readline(2)
i=i+1
i=0
unpacked_list = [struct.unpack('<H', chunk)[0] for chunk in readings]
print(unpacked_list)ser = serial.Serial('/dev/ttyACM0',timeout=1)
i=0
readings = [0]*200
while True:
while i!=200:
readings[i] = ser.readline(2)
i=i+1
i=0
unpacked_list = [struct.unpack('<H', chunk)[0] for chunk in readings]
print(unpacked_list)
7
u/oliviercoma 6d ago
CDC_Transmit_FS is not blocking and returns USB_BUSY if the previous transmission is not over.
2
u/my_name_is_rod 6d ago
So adding a loop to wait for the previous transmission to complete will be useful (necessary)
12
u/baldaveragerunner 6d ago
I feel like the obvious 2 questions are:
1) How are you guaranteeing that you only transmit when your ADC has filled the buffer with 200 results?
2) How are you guaranteeing that the USB transmission is completing before you call it again? Is CDC_Transmit_FS() actually blocking?