Skip to content

Commit 61e9868

Browse files
Reorganize/cleanup varianthooks
1 parent 2dd0130 commit 61e9868

File tree

8 files changed

+303
-270
lines changed

8 files changed

+303
-270
lines changed

cores/rp2040/_freertos.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern volatile bool __freeRTOSinitted;
3737
extern void __freertos_idle_other_core() __attribute__((weak));
3838
extern void __freertos_resume_other_core() __attribute__((weak));
3939

40+
extern void __initFreeRTOSMutexes();
41+
4042
extern SemaphoreHandle_t __get_freertos_mutex_for_ptr(mutex_t *m, bool recursive = false);
4143

4244
#ifdef __cplusplus

cores/rp2040/freertos/freertos-lwip.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
LWIP-on-FreeRTOs Plumbing
2+
LWIP-on-FreeRTOS Plumbing
33
44
Copyright (c) 2025 Earle F. Philhower, III <[email protected]>
55
@@ -42,7 +42,6 @@ static void lwipThread(void *params);
4242
static TaskHandle_t __lwipTask;
4343
static QueueHandle_t __lwipQueue;
4444

45-
4645
void __startLWIPThread() {
4746
__lwipQueue = xQueueCreate(LWIP_WORK_ENTRIES, sizeof(LWIPWork));
4847
if (!__lwipQueue) {

cores/rp2040/freertos/freertos-lwip.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1919
*/
2020

21+
#pragma once
2122

2223
#ifdef __FREERTOS
2324

24-
#pragma once
25-
2625
// Create the thread and work queue
2726
void __startLWIPThread();
2827

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
Main app loop and infrastructure for FreeRTOS mode
3+
4+
Copyright (c) 2025 Earle F. Philhower, III <[email protected]>
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifdef __FREERTOS
22+
23+
#include <stdlib.h>
24+
25+
/* FreeRTOS includes. */
26+
#include "FreeRTOS.h"
27+
#include "task.h"
28+
#include "timers.h"
29+
#include "semphr.h"
30+
31+
/* Arduino Core includes */
32+
#include <Arduino.h>
33+
34+
#include <lwip_wrap.h>
35+
#include <_freertos.h>
36+
#include "freertos-lwip.h"
37+
#include "freertos-usb.h"
38+
39+
/*-----------------------------------------------------------*/
40+
41+
extern void setup() __attribute__((weak));
42+
extern void loop() __attribute__((weak));
43+
extern void setup1() __attribute__((weak));
44+
extern void loop1() __attribute__((weak));
45+
extern void initVariant();
46+
extern void __loop();
47+
static void __core1(void *params);
48+
extern volatile bool __freeRTOSinitted;
49+
50+
static TaskHandle_t __idleCoreTask[2];
51+
52+
void initFreeRTOS(void) {
53+
__initFreeRTOSMutexes();
54+
}
55+
56+
static void __core0(void *params) {
57+
(void) params;
58+
initVariant();
59+
60+
if (setup1 || loop1) {
61+
TaskHandle_t c1;
62+
xTaskCreate(__core1, "CORE1", 1024, 0, configMAX_PRIORITIES / 2, &c1);
63+
vTaskCoreAffinitySet(c1, 1 << 1);
64+
}
65+
66+
#if !defined(NO_USB) && !defined(USE_TINYUSB)
67+
while (!__usbInitted) {
68+
delay(1);
69+
}
70+
#endif
71+
if (setup) {
72+
setup();
73+
}
74+
if (loop) {
75+
while (1) {
76+
loop();
77+
__loop();
78+
}
79+
} else {
80+
while (1) {
81+
__loop();
82+
}
83+
}
84+
}
85+
86+
static void __core1(void *params) {
87+
(void) params;
88+
#if !defined(NO_USB) && !defined(USE_TINYUSB)
89+
while (!__usbInitted) {
90+
delay(1);
91+
}
92+
#endif
93+
if (setup1) {
94+
setup1();
95+
}
96+
if (loop1) {
97+
while (1) {
98+
loop1();
99+
}
100+
} else {
101+
while (1) {
102+
vTaskDelay(1000);
103+
}
104+
}
105+
}
106+
107+
extern "C" void delay(unsigned long ms) {
108+
vTaskDelay(ms / portTICK_PERIOD_MS);
109+
}
110+
111+
extern "C" void yield() {
112+
taskYIELD();
113+
}
114+
115+
static void __no_inline_not_in_flash_func(IdleThisCore)(void *param) {
116+
(void) param;
117+
while (true) {
118+
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
119+
vTaskPreemptionDisable(nullptr);
120+
portDISABLE_INTERRUPTS();
121+
__otherCoreIdled = true;
122+
while (__otherCoreIdled) {
123+
/* noop */
124+
}
125+
portENABLE_INTERRUPTS();
126+
vTaskPreemptionEnable(nullptr);
127+
}
128+
}
129+
130+
extern "C" void __no_inline_not_in_flash_func(__freertos_idle_other_core)() {
131+
vTaskPreemptionDisable(nullptr);
132+
xTaskNotifyGive(__idleCoreTask[ 1 ^ sio_hw->cpuid ]);
133+
while (!__otherCoreIdled) {
134+
/* noop */
135+
}
136+
portDISABLE_INTERRUPTS();
137+
vTaskSuspendAll();
138+
}
139+
140+
extern "C" void __no_inline_not_in_flash_func(__freertos_resume_other_core)() {
141+
__otherCoreIdled = false;
142+
portENABLE_INTERRUPTS();
143+
xTaskResumeAll();
144+
vTaskPreemptionEnable(nullptr);
145+
}
146+
147+
void startFreeRTOS(void) {
148+
TaskHandle_t c0;
149+
xTaskCreate(__core0, "CORE0", 1024, 0, configMAX_PRIORITIES / 2, &c0);
150+
vTaskCoreAffinitySet(c0, 1 << 0);
151+
152+
// Create the idle-other-core tasks (for when flash is being written)
153+
xTaskCreate(IdleThisCore, "IdleCore0", 128, 0, configMAX_PRIORITIES - 1, __idleCoreTask + 0);
154+
vTaskCoreAffinitySet(__idleCoreTask[0], 1 << 0);
155+
xTaskCreate(IdleThisCore, "IdleCore1", 128, 0, configMAX_PRIORITIES - 1, __idleCoreTask + 1);
156+
vTaskCoreAffinitySet(__idleCoreTask[1], 1 << 1);
157+
158+
__startLWIPThread();
159+
160+
// Initialise and run the freeRTOS scheduler. Execution should never return here.
161+
__freeRTOSinitted = true;
162+
vTaskStartScheduler();
163+
164+
while (true) {
165+
/* noop */
166+
}
167+
}
168+
169+
#endif
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
FreeRTOS USB task
3+
4+
Copyright (c) 2025 Earle F. Philhower, III <[email protected]>
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifdef __FREERTOS
22+
23+
#include <stdlib.h>
24+
25+
/* FreeRTOS includes. */
26+
#include "FreeRTOS.h"
27+
#include "task.h"
28+
#include "timers.h"
29+
#include "semphr.h"
30+
31+
/* Arduino Core includes */
32+
#include <Arduino.h>
33+
#include <RP2040USB.h>
34+
#include "tusb.h"
35+
36+
/* Raspberry PI Pico includes */
37+
#include <pico.h>
38+
#include <pico/time.h>
39+
40+
#include <_freertos.h>
41+
42+
#include "freertos-usb.h"
43+
44+
45+
46+
extern mutex_t __usb_mutex;
47+
static TaskHandle_t __usbTask;
48+
static void __usb(void *param);
49+
50+
volatile bool __usbInitted = false;
51+
52+
static void __usb(void *param) {
53+
(void) param;
54+
55+
tusb_init();
56+
57+
Serial.begin(115200);
58+
59+
__usbInitted = true;
60+
61+
while (true) {
62+
BaseType_t ss = xTaskGetSchedulerState();
63+
if (ss != taskSCHEDULER_SUSPENDED) {
64+
auto m = __get_freertos_mutex_for_ptr(&__usb_mutex);
65+
if (xSemaphoreTake(m, 0)) {
66+
tud_task();
67+
xSemaphoreGive(m);
68+
}
69+
}
70+
vTaskDelay(1 / portTICK_PERIOD_MS);
71+
}
72+
}
73+
74+
extern void __SetupDescHIDReport();
75+
extern void __SetupUSBDescriptor();
76+
77+
void __USBStart() {
78+
mutex_init(&__usb_mutex);
79+
80+
__SetupDescHIDReport();
81+
__SetupUSBDescriptor();
82+
83+
// Make high prio and locked to core 0
84+
xTaskCreate(__usb, "USB", 256, 0, configMAX_PRIORITIES - 2, &__usbTask);
85+
vTaskCoreAffinitySet(__usbTask, 1 << 0);
86+
}
87+
88+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
FreeRTOS USB task
3+
4+
Copyright (c) 2025 Earle F. Philhower, III <[email protected]>
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#pragma once
22+
23+
#ifdef __FREERTOS
24+
extern void __USBStart();
25+
extern volatile bool __usbInitted;
26+
27+
#endif

0 commit comments

Comments
 (0)