3030
3131#include " FreeRTOS.h"
3232#include " task.h"
33+ #include " tusb.h"
3334#include " pico/multicore.h"
3435#include " hardware/irq.h"
3536#include " semphr.h"
@@ -136,10 +137,8 @@ static void core1()
136137{
137138 for ( ;; )
138139 {
139- if (sem_acquire_timeout_us (&base.serialSemaphore , portMAX_DELAY))
140- {
141- processData ();
142- }
140+ [[maybe_unused]] auto val = multicore_fifo_pop_blocking ();
141+ processData ();
143142 }
144143}
145144
@@ -153,7 +152,11 @@ static void core0( void *pvParameters )
153152 do
154153 {
155154 wanted = std::min (MAX_BUFFER - base.queueEnd , MAX_BUFFER - 1 );
156- received = stdio_usb.in_chars ((char *)(&(base.buffer [base.queueEnd ])), wanted);
155+
156+ if (stdio_usb_connected () && tud_cdc_available ()) {
157+ received = (int )tud_cdc_read (const_cast <uint8_t *>(&(base.buffer [base.queueEnd ])), (uint32_t )wanted);
158+ }
159+
157160 if (received > 0 )
158161 {
159162 base.queueEnd = (base.queueEnd + received) % (MAX_BUFFER);
@@ -162,19 +165,28 @@ static void core0( void *pvParameters )
162165
163166 if (statistics.printLogs .exchange (false ))
164167 {
165- statistics.printToSerial (base.processDataHandle , base.processSerialHandle );
168+ statistics.printToSerial (base.processSerialHandle );
169+ tud_cdc_write_flush ();
170+ vTaskDelay (pdMS_TO_TICKS (5 ));
166171 }
167172
168- sem_release (&base. serialSemaphore );
173+ multicore_fifo_push_blocking ( 0xAA );
169174 }
170175 }
171176}
172177
173178static void serialEvent (void *)
174179{
175- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
176- xSemaphoreGiveFromISR (base.receiverSemaphore , &xHigherPriorityTaskWoken);
177- portYIELD_FROM_ISR (xHigherPriorityTaskWoken);
180+ if (xPortIsInsideInterrupt ())
181+ {
182+ BaseType_t xHigherPriorityTaskWoken = pdFALSE;
183+ xSemaphoreGiveFromISR (base.receiverSemaphore , &xHigherPriorityTaskWoken);
184+ portYIELD_FROM_ISR (xHigherPriorityTaskWoken);
185+ }
186+ else
187+ {
188+ xSemaphoreGive (base.receiverSemaphore );
189+ }
178190}
179191
180192void on_fifo_irq ()
@@ -190,8 +202,6 @@ int main(void)
190202{
191203 stdio_init_all ();
192204
193- sem_init (&base.serialSemaphore , 0 , 1 );
194-
195205 base.receiverSemaphore = xSemaphoreCreateBinary ();
196206
197207 multicore_fifo_clear_irq ();
0 commit comments