#include <STM32FreeRTOS.h>
SemaphoreHandle_t mutex;
const bool USE_MUTEX = true; // <-- flip to true to fix it, then re-run
// Print a sequence one character at a time, yielding between each char
// so the scheduler can switch mid-sequence.
void printSeq(const char *seq) {
if (USE_MUTEX) xSemaphoreTake(mutex, portMAX_DELAY);
for (int i = 0; seq[i] != '\0'; i++) {
Serial.print(seq[i]);
vTaskDelay(pdMS_TO_TICKS(2)); // hand off mid-sequence
}
Serial.println();
if (USE_MUTEX) xSemaphoreGive(mutex);
}
void taskA(void *pv) {
for (;;) { printSeq("abcdefg"); vTaskDelay(pdMS_TO_TICKS(20)); }
}
void taskB(void *pv) {
for (;;) { printSeq("1234567"); vTaskDelay(pdMS_TO_TICKS(20)); }
}
void setup() {
Serial.begin(115200);
mutex = xSemaphoreCreateMutex();
xTaskCreate(taskA, "A", 256, NULL, 1, NULL);
xTaskCreate(taskB, "B", 256, NULL, 1, NULL);
vTaskStartScheduler();
}
void loop() {}