Skip to content

Commit 9c0b84b

Browse files
committed
Fix semaphore communication conflict
1 parent 02c737a commit 9c0b84b

5 files changed

Lines changed: 32 additions & 13 deletions

File tree

include/base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Base
4747
TaskHandle_t processSerialHandle = nullptr;
4848
// semaphore to synchronize them
4949
semaphore_t serialSemaphore;
50-
semaphore_t receiverSemaphore;
50+
SemaphoreHandle_t receiverSemaphore;
5151
// current queue position
5252
volatile int queueCurrent = 0;
5353
// queue end position

include/calibration.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ class CalibrationConfig
125125
* @brief print RGBW calibration parameters when no data is received
126126
*
127127
*/
128-
void printCalibration(char* output, size_t outputSize)
128+
template <size_t N>
129+
void printCalibration(char (&output)[N])
129130
{
130-
snprintf(output, outputSize,"RGBW => Gain: %i/255, red: %i, green: %i, blue: %i\r\n", gain, red, green, blue);
131-
printf(output);
131+
snprintf(output, N, "RGBW => Gain: %i/255, red: %i, green: %i, blue: %i\r\n", gain, red, green, blue);
132+
fputs(output, stdout);
132133
}
133134
} calibrationConfig;
134135

include/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void processData()
151151
}
152152
else if (frameState.getCount() == 0x2aa2 && (input == 0x15 || input == 0x35))
153153
{
154-
statistics.print(currentTime, &base.receiverSemaphore, input == 0x15);
154+
statistics.print(currentTime, input == 0x15);
155155

156156
frameState.setRegroup(true);
157157

include/statistics.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class
118118
* @param curTime
119119
* @param taskHandle
120120
*/
121-
void print(unsigned long curTime, semaphore_t* receiverSemaphore, bool isWelcome)
121+
void print(unsigned long curTime, bool isWelcome)
122122
{
123123
startTime = curTime;
124124
goodFrames = 0;
@@ -127,7 +127,7 @@ class
127127

128128
welcomeMessage.store(isWelcome);
129129
printLogs.store(true);
130-
sem_release(receiverSemaphore);
130+
multicore_fifo_push_blocking(0xFF);
131131
}
132132

133133
void printToSerial(TaskHandle_t taskHandle1, TaskHandle_t taskHandle2)
@@ -139,15 +139,15 @@ class
139139
(taskHandle1 != nullptr) ? uxTaskGetStackHighWaterMark(taskHandle1) : 0,
140140
(taskHandle2 != nullptr) ? uxTaskGetStackHighWaterMark(taskHandle2) : 0,
141141
xPortGetFreeHeapSize());
142-
printf(output);
142+
fputs(output, stdout);
143143

144144
#if defined(NEOPIXEL_RGBW)
145-
calibrationConfig.printCalibration(output, sizeof(output));
145+
calibrationConfig.printCalibration(output);
146146
#endif
147147

148148
if (welcomeMessage.exchange(false))
149149
{
150-
printf(HELLO_MESSAGE);
150+
fputs(HELLO_MESSAGE, stdout);
151151
}
152152
}
153153

source/main.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
#include "FreeRTOS.h"
3232
#include "task.h"
33+
#include "pico/multicore.h"
34+
#include "hardware/irq.h"
35+
#include "semphr.h"
3336
#include <stdio.h>
3437
#include <algorithm>
3538
#include "pico/stdio/driver.h"
@@ -140,7 +143,7 @@ static void core0( void *pvParameters )
140143
{
141144
for( ;; )
142145
{
143-
if (sem_acquire_timeout_us(&base.receiverSemaphore, portMAX_DELAY))
146+
if (xSemaphoreTake(base.receiverSemaphore, portMAX_DELAY) == pdTRUE)
144147
{
145148
int wanted, received;
146149
do
@@ -156,6 +159,8 @@ static void core0( void *pvParameters )
156159
if (statistics.printLogs.exchange(false))
157160
{
158161
statistics.printToSerial(base.processDataHandle, base.processSerialHandle);
162+
stdio_flush();
163+
vTaskDelay(pdMS_TO_TICKS(2));
159164
}
160165

161166
sem_release(&base.serialSemaphore);
@@ -165,7 +170,16 @@ static void core0( void *pvParameters )
165170

166171
static void serialEvent(void *)
167172
{
168-
sem_release(&base.receiverSemaphore);
173+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
174+
xSemaphoreGiveFromISR(base.receiverSemaphore, &xHigherPriorityTaskWoken);
175+
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
176+
}
177+
178+
void on_fifo_irq()
179+
{
180+
multicore_fifo_drain();
181+
182+
serialEvent(nullptr);
169183
}
170184

171185
int main(void)
@@ -174,7 +188,11 @@ int main(void)
174188

175189
sem_init(&base.serialSemaphore, 0, 1);
176190

177-
sem_init(&base.receiverSemaphore, 0, 1);
191+
base.receiverSemaphore = xSemaphoreCreateBinary();
192+
193+
multicore_fifo_clear_irq();
194+
irq_set_exclusive_handler(SIO_IRQ_FIFO, on_fifo_irq);
195+
irq_set_enabled(SIO_IRQ_FIFO, true);
178196

179197
multicore_launch_core1(core1);
180198

0 commit comments

Comments
 (0)