For that, I have 2 threads - for reading and writing. Writing thread is supposed to be spamming USB with a status messages. Reading thread is supposed to read occasional commands from main PC. Now the problem I have is that reading is sometimes not working and I don't have enough knowledge to understand the reason. I suspect that this is due to the spamming of the write-thread. Essentially it is fine to lose some status-spam messages but it is crucial to process all the incoming messages. I am not sure if I did everything correctly on my side, or is that the application on the main PC side to blame. Should I use interrupts for when there is reading available on the bus? What should I look for to make this fail-safe?
As a project startup, I used demo provided in chibios_trunk\testhal\STM32\multi\USB_CDC with ChibiOS 18.2.
Here is a narrowed-down code that should give a good understanding of how I do this.
Code: Select all
THD_FUNCTION(readThrFunction, arg)
{
(void) arg;
chRegSetThreadName("Read thread");
char readBuffer[128];
event_listener_t usbData;
eventflags_t flags;
int bytesRead = 0;
chEvtRegisterMask((event_source_t *)chnGetEventSource(&PORTAB_SDU1), &usbData, EVENT_MASK(1));
while (TRUE)
{
chEvtWaitAll(EVENT_MASK(1));
flags = chEvtGetAndClearFlags(&usbData);
if (flags & CHN_INPUT_AVAILABLE)
{
bytesRead = chnReadTimeout(&PORTAB_SDU1, readBuffer, 64, 1000);
doStuff();
}
}
}
THD_FUNCTION(writeThrFunction, arg)
{
(void) arg;
chRegSetThreadName("write thread");
char payloadBuffer[64];
int payloadLength = 0;
while (TRUE)
{
getSomeUsefullStuff(...);
chnWriteTimeout(&PORTAB_SDU1, payloadBuffer, payloadLength, 10);
chThdSleepMilliseconds(10);
}
}
static THD_WORKING_AREA(readThrWA, 1024);
static THD_WORKING_AREA(writeThrWA, 1024);
main()
{
... // initialization stuff
chThdCreateStatic(readThrWA, sizeof(readThrWA), HIGHPRIO, readThrFunction, NULL);
chThdCreateStatic(writeThrWA, sizeof(writeThrWA), NORMALPRIO, writeThrFunction, NULL);
while (true) {
chThdSleepMilliseconds(10);
}
}