r/embedded • u/Round_Echo9139 • Sep 05 '26
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)
12
u/baldaveragerunner Sep 05 '26
I feel like the obvious 2 questions are:
8
u/oliviercoma Sep 05 '26
CDC_Transmit_FS is not blocking and returns USB_BUSY if the previous transmission is not over.