1- #include < stdint.h>
2-
3- #include " device.h"
1+ #include " targets.h"
42#include " common.h"
53#include " logging.h"
64#include " helpers.h"
5+ #include " device.h"
76
7+ // /////////////////////////////////////
88// Even though we aren't using anything this keeps the PIO dependency analyzer happy!
9- #include " POWERMGNT.h"
109
11- static volatile bool eventFired = false ;
10+ #if defined(Regulatory_Domain_AU_915) || defined(Regulatory_Domain_EU_868) || defined(Regulatory_Domain_IN_866) || defined(Regulatory_Domain_FCC_915) || defined(Regulatory_Domain_AU_433) || defined(Regulatory_Domain_EU_433)
11+ #include " SX127xDriver.h"
12+ #endif
13+
14+ #if defined(Regulatory_Domain_ISM_2400)
15+ #include " SX1280Driver.h"
16+ #endif
17+
18+ // /////////////////////////////////////
19+
1220static device_affinity_t *uiDevices;
1321static uint8_t deviceCount;
14- static unsigned long deviceTimeout[16 ] = {0 };
1522
16- static connectionState_e lastConnectionState = disconnected;
23+ static bool eventFired[2 ] = {false , false };
24+ static connectionState_e lastConnectionState[2 ] = {disconnected, disconnected};
25+
26+ static unsigned long deviceTimeout[16 ] = {0 };
1727
1828#if defined(PLATFORM_ESP32)
19- #define LOOP_CORE xPortGetCoreID ()
29+ static TaskHandle_t xDeviceTask = NULL ;
30+ static SemaphoreHandle_t taskSemaphore;
31+ static SemaphoreHandle_t completeSemaphore;
32+ static void deviceTask (void *pvArgs);
33+ #define CURRENT_CORE xPortGetCoreID ()
2034#else
21- #define LOOP_CORE -1
35+ #define CURRENT_CORE -1
2236#endif
2337
2438void devicesRegister (device_affinity_t *devices, uint8_t count)
2539{
2640 uiDevices = devices;
2741 deviceCount = count;
42+
43+ #if defined(PLATFORM_ESP32)
44+ taskSemaphore = xSemaphoreCreateBinary ();
45+ completeSemaphore = xSemaphoreCreateBinary ();
46+ disableCore0WDT ();
47+ xTaskCreatePinnedToCore (deviceTask, " deviceTask" , 3000 , NULL , 0 , &xDeviceTask, 0 );
48+ #endif
2849}
2950
3051void devicesInit ()
3152{
32- uint32_t core = LOOP_CORE;
53+ int32_t core = CURRENT_CORE;
54+
3355 for (size_t i=0 ; i<deviceCount ; i++) {
3456 if (uiDevices[i].core == core || core == -1 ) {
3557 if (uiDevices[i].device ->initialize ) {
3658 (uiDevices[i].device ->initialize )();
3759 }
3860 }
3961 }
62+ #if defined(PLATFORM_ESP32)
63+ if (core == 1 )
64+ {
65+ xSemaphoreGive (taskSemaphore);
66+ xSemaphoreTake (completeSemaphore, portMAX_DELAY);
67+ }
68+ #endif
4069}
4170
4271void devicesStart ()
4372{
44- uint32_t core = LOOP_CORE ;
73+ int32_t core = CURRENT_CORE ;
4574 unsigned long now = millis ();
75+
4676 for (size_t i=0 ; i<deviceCount ; i++)
4777 {
48- deviceTimeout[i] = 0xFFFFFFFF ;
4978 if (uiDevices[i].core == core || core == -1 ) {
79+ deviceTimeout[i] = 0xFFFFFFFF ;
5080 if (uiDevices[i].device ->start )
5181 {
5282 int delay = (uiDevices[i].device ->start )();
5383 deviceTimeout[i] = delay == DURATION_NEVER ? 0xFFFFFFFF : now + delay;
5484 }
5585 }
5686 }
87+ #if defined(PLATFORM_ESP32)
88+ if (core == 1 )
89+ {
90+ xSemaphoreGive (taskSemaphore);
91+ xSemaphoreTake (completeSemaphore, portMAX_DELAY);
92+ }
93+ #endif
5794}
5895
5996void devicesTriggerEvent ()
6097{
61- eventFired = true ;
98+ eventFired[0 ] = true ;
99+ eventFired[1 ] = true ;
62100}
63101
64102void devicesUpdate (unsigned long now)
65103{
66- uint32_t core = LOOP_CORE;
67- bool handleEvents = eventFired;
68- eventFired = false ;
104+ int32_t core = CURRENT_CORE;
105+
106+ bool handleEvents = eventFired[core==-1 ?0 :core];
107+ eventFired[core==-1 ?0 :core] = false ;
108+
69109 for (size_t i=0 ; i<deviceCount ; i++)
70110 {
71111 if (uiDevices[i].core == core || core == -1 ) {
72- if ((handleEvents || lastConnectionState != connectionState) && uiDevices[i].device ->event )
112+ if ((handleEvents || lastConnectionState[core==- 1 ? 0 :core] != connectionState) && uiDevices[i].device ->event )
73113 {
74114 int delay = (uiDevices[i].device ->event )();
75115 if (delay != DURATION_IGNORE)
@@ -79,15 +119,32 @@ void devicesUpdate(unsigned long now)
79119 }
80120 }
81121 }
82- lastConnectionState = connectionState;
122+ lastConnectionState[core==-1 ?0 :core] = connectionState;
123+
83124 for (size_t i=0 ; i<deviceCount ; i++)
84125 {
85126 if (uiDevices[i].core == core || core == -1 ) {
86- if (now > deviceTimeout [i] && uiDevices [i]. device -> timeout )
127+ if (uiDevices [i]. device -> timeout && now >= deviceTimeout [i])
87128 {
88129 int delay = (uiDevices[i].device ->timeout )();
89130 deviceTimeout[i] = delay == DURATION_NEVER ? 0xFFFFFFFF : now + delay;
90131 }
91132 }
92133 }
93134}
135+
136+ #if defined(PLATFORM_ESP32)
137+ static void deviceTask (void *pvArgs)
138+ {
139+ xSemaphoreTake (taskSemaphore, portMAX_DELAY);
140+ devicesInit ();
141+ xSemaphoreGive (completeSemaphore);
142+ xSemaphoreTake (taskSemaphore, portMAX_DELAY);
143+ devicesStart ();
144+ xSemaphoreGive (completeSemaphore);
145+ for (;;)
146+ {
147+ devicesUpdate (millis ());
148+ }
149+ }
150+ #endif
0 commit comments