| 
 | 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  | 
0 commit comments