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,26 @@ 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 );
166169 }
167170
168- sem_release (&base. serialSemaphore );
171+ multicore_fifo_push_blocking ( 0xAA );
169172 }
170173 }
171174}
172175
173176static void serialEvent (void *)
174177{
175- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
176- xSemaphoreGiveFromISR (base.receiverSemaphore , &xHigherPriorityTaskWoken);
177- portYIELD_FROM_ISR (xHigherPriorityTaskWoken);
178+ if (xPortIsInsideInterrupt ())
179+ {
180+ BaseType_t xHigherPriorityTaskWoken = pdFALSE;
181+ xSemaphoreGiveFromISR (base.receiverSemaphore , &xHigherPriorityTaskWoken);
182+ portYIELD_FROM_ISR (xHigherPriorityTaskWoken);
183+ }
184+ else
185+ {
186+ xSemaphoreGive (base.receiverSemaphore );
187+ }
178188}
179189
180190void on_fifo_irq ()
@@ -190,8 +200,6 @@ int main(void)
190200{
191201 stdio_init_all ();
192202
193- sem_init (&base.serialSemaphore , 0 , 1 );
194-
195203 base.receiverSemaphore = xSemaphoreCreateBinary ();
196204
197205 multicore_fifo_clear_irq ();
0 commit comments