- ACM fixed

This commit is contained in:
Wiesner András 2025-07-28 13:58:15 +02:00
parent c0ea9c3ab3
commit 45d1cd86d3

View File

@ -32,9 +32,6 @@ static osEventFlagsId_t flags; ///< Event flags
#define USB_ACM_COMM_INIT (0x02) ///< Communication has been initialized
#define USB_ACM_HOSTBOUND_DATA_AVAIL (0x04) ///< Hostbound data is available
#define USB_ACM_LOOP_TIMEOUT_TICKS (100) ///< Main loop timeout for interrupt status transmission
#define USB_ACM_INITIAL_DELAY_TICKS (100) ///< Delay before sending data
// -------------------------
static void thread_usb_acm(void *arg) {
@ -42,25 +39,26 @@ static void thread_usb_acm(void *arg) {
osEventFlagsSet(flags, USB_ACM_DATA_IN_DONE); // assume we can write to the data endpoint
osDelay(USB_ACM_INITIAL_DELAY_TICKS); // inject some initial delay
osDelay(100); // inject some initial delay
while (true) {
uint32_t signals = osEventFlagsWait(flags, USB_ACM_HOSTBOUND_DATA_AVAIL, osFlagsWaitAny, USB_ACM_LOOP_TIMEOUT_TICKS);
uint32_t signals = osEventFlagsWait(flags, USB_ACM_HOSTBOUND_DATA_AVAIL, osFlagsWaitAny, osWaitForever);
if (signals != osErrorTimeout) { // check timeout
if (signals & USB_ACM_HOSTBOUND_DATA_AVAIL) { // data hostbound available
do {
osEventFlagsWait(flags, USB_ACM_DATA_IN_DONE, osFlagsWaitAny, 1); // wait for the IN DONE flag
uint32_t readSize = bfifo_read(&fifo, tx_buffer, USB_ACM_PCKT_BUFSIZE); // read from the fifo
osEventFlagsWait(flags, USB_ACM_DATA_IN_DONE, osFlagsWaitAny, osWaitForever); // wait for the IN DONE flag
uint32_t readSize = bfifo_read(&fifo, tx_buffer, USB_ACM_PCKT_BUFSIZE); // read from the fifo
if (readSize > 0) {
uint32_t writeSize = usbcore_schedule_transmission(acms.ep_assignments.data_ep, tx_buffer, readSize); // write data acquired from the buffer
bfifo_pop(&fifo, writeSize, 0); // pop with no blocking
}
} while (bfifo_get_used(&fifo) > 0);
osEventFlagsSet(flags, USB_ACM_DATA_IN_DONE);
}
} else { // timeout
// send an all-zero interrupt
usbcore_schedule_transmission(acms.ep_assignments.control_ep, (const uint8_t *)&(acms.interrupt_data), sizeof(uint16_t));
// usbcore_schedule_transmission(acms.ep_assignments.control_ep, (const uint8_t *)&(acms.interrupt_data), sizeof(uint16_t));
}
}
}
@ -97,7 +95,7 @@ void usb_acm_init(const Usb_Acm_EpAssignments *as) {
// create thread
osThreadAttr_t attr;
memset(&attr, 0, sizeof(osThreadAttr_t));
attr.stack_size = 512;
attr.stack_size = 2048;
attr.name = "acm";
th = osThreadNew(thread_usb_acm, NULL, &attr);
}
@ -114,7 +112,9 @@ static void usb_cdc_review_comm_init() {
acms.commInit = lcOk && clsOk;
// signal the processing thread
osEventFlagsSet(flags, USB_ACM_COMM_INIT);
if (acms.commInit) {
osEventFlagsSet(flags, USB_ACM_COMM_INIT);
}
}
int usb_acm_process_and_return(Usb_CallbackEvent *cbevt) {
@ -135,6 +135,7 @@ int usb_acm_process_and_return(Usb_CallbackEvent *cbevt) {
case USB_ACM_GET_LINE_CODING: // get line coding
cbevt->reply_data = (const uint8_t *)&acms.line_coding; // expert move: pass the pointer, no copying
cbevt->reply_size = sizeof(Usb_Acm_LineCodingStruct);
usb_cdc_review_comm_init(); // review the communcation initialization state
ret = 0;
break;
case USB_ACM_SEND_BREAK: // send break
@ -178,8 +179,11 @@ int usb_acm_process_and_return(Usb_CallbackEvent *cbevt) {
void usb_acm_write(const uint8_t *data, uint32_t size) {
if (acms.moduleInit) {
bfifo_push_all(&fifo, data, size);
osEventFlagsSet(flags, USB_ACM_HOSTBOUND_DATA_AVAIL);
uint32_t index = 0;
do {
index += bfifo_push(&fifo, data + index, size - index);
osEventFlagsSet(flags, USB_ACM_HOSTBOUND_DATA_AVAIL);
} while ((size - index) > 0);
}
}