Skip to content

Commit 6e9249b

Browse files
committed
Device core affinity using current core
Mode CRSF to a device on Core 0
1 parent af5cf89 commit 6e9249b

6 files changed

Lines changed: 115 additions & 61 deletions

File tree

src/lib/CRSF/CRSF.cpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
HardwareSerial CRSF::Port = HardwareSerial(1);
1111
portMUX_TYPE FIFOmux = portMUX_INITIALIZER_UNLOCKED;
12-
TaskHandle_t xESP32uartTask = NULL;
1312
#elif defined(PLATFORM_ESP8266)
1413
HardwareSerial CRSF::Port = Serial;
1514
#elif CRSF_TX_MODULE_STM32
@@ -111,8 +110,14 @@ void CRSF::Begin()
111110
UARTwdtLastChecked = millis() + UARTwdtInterval; // allows a delay before the first time the UARTwdt() function is called
112111

113112
#if defined(PLATFORM_ESP32)
114-
disableCore0WDT();
115-
xTaskCreatePinnedToCore(ESP32uartTask, "ESP32uartTask", 3000, NULL, 0, &xESP32uartTask, 0);
113+
// disableCore0WDT(); PAK
114+
portDISABLE_INTERRUPTS();
115+
CRSF::Port.begin(TxToHandsetBauds[UARTcurrentBaudIdx], SERIAL_8N1,
116+
GPIO_PIN_RCSIGNAL_RX, GPIO_PIN_RCSIGNAL_TX,
117+
false, 500);
118+
CRSF::duplex_set_RX();
119+
portENABLE_INTERRUPTS();
120+
flush_port_input();
116121

117122
#elif defined(PLATFORM_ESP8266)
118123
CRSF::Port.flush();
@@ -162,12 +167,6 @@ void CRSF::Begin()
162167
void CRSF::End()
163168
{
164169
#if CRSF_TX_MODULE
165-
#ifdef PLATFORM_ESP32
166-
if (xESP32uartTask != NULL)
167-
{
168-
vTaskDelete(xESP32uartTask);
169-
}
170-
#endif
171170
uint32_t startTime = millis();
172171
while (SerialOutFIFO.peek() > 0)
173172
{
@@ -814,29 +813,6 @@ bool CRSF::UARTwdt()
814813
return retval;
815814
}
816815

817-
#ifdef PLATFORM_ESP32
818-
//RTOS task to read and write CRSF packets to the serial port
819-
void ICACHE_RAM_ATTR CRSF::ESP32uartTask(void *pvParameters)
820-
{
821-
DBGLN("ESP32 CRSF UART LISTEN TASK STARTED");
822-
devicesInit();
823-
portDISABLE_INTERRUPTS();
824-
CRSF::Port.begin(TxToHandsetBauds[UARTcurrentBaudIdx], SERIAL_8N1,
825-
GPIO_PIN_RCSIGNAL_RX, GPIO_PIN_RCSIGNAL_TX,
826-
false, 500);
827-
CRSF::duplex_set_RX();
828-
portENABLE_INTERRUPTS();
829-
flush_port_input();
830-
devicesStart();
831-
(void)pvParameters;
832-
for (;;)
833-
{
834-
handleUARTin();
835-
devicesUpdate(millis());
836-
}
837-
}
838-
#endif // PLATFORM_ESP32
839-
840816
#elif CRSF_RX_MODULE // !CRSF_TX_MODULE
841817
bool CRSF::RXhandleUARTout()
842818
{

src/lib/CRSF/CRSF.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,6 @@ class CRSF
149149
static uint8_t MspData[ELRS_MSP_BUFFER];
150150
static uint8_t MspDataLength;
151151

152-
#ifdef PLATFORM_ESP32
153-
static void ESP32uartTask(void *pvParameters);
154-
static void ESP32syncPacketTask(void *pvParameters);
155-
#endif
156-
157152
static void ICACHE_RAM_ATTR adjustMaxPacketSize();
158153
static void duplex_set_RX();
159154
static void duplex_set_TX();

src/lib/CRSF/devCRSF.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "targets.h"
2+
#include "common.h"
3+
#include "device.h"
4+
5+
#include "CRSF.h"
6+
7+
static int start()
8+
{
9+
CRSF::Begin();
10+
return DURATION_IMMEDIATELY;
11+
}
12+
13+
static int timeout()
14+
{
15+
CRSF::handleUARTin();
16+
return DURATION_IMMEDIATELY;
17+
}
18+
19+
device_t CRSF_device = {
20+
.initialize = nullptr,
21+
.start = start,
22+
.event = nullptr,
23+
.timeout = timeout
24+
};

src/lib/CRSF/devCRSF.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include "device.h"
4+
5+
extern device_t CRSF_device;

src/lib/DEVICE/device.cpp

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,115 @@
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+
1220
static device_affinity_t *uiDevices;
1321
static 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

2438
void 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

3051
void 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

4271
void 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

5996
void devicesTriggerEvent()
6097
{
61-
eventFired = true;
98+
eventFired[0] = true;
99+
eventFired[1] = true;
62100
}
63101

64102
void 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

src/src/tx_main.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SX1280Driver Radio;
2525
#include "stubborn_sender.h"
2626

2727
#include "helpers.h"
28+
#include "devCRSF.h"
2829
#include "devLED.h"
2930
#include "devOLED.h"
3031
#include "devBuzzer.h"
@@ -92,6 +93,7 @@ StubbornSender MspSender(ELRS_MSP_MAX_PACKAGES);
9293
uint8_t CRSFinBuffer[CRSF_MAX_PACKET_LEN+1];
9394

9495
device_affinity_t ui_devices[] = {
96+
{&CRSF_device, 0},
9597
#ifdef HAS_LED
9698
{&LED_device, 1},
9799
#endif
@@ -951,7 +953,6 @@ void setup()
951953
hwTimer.init();
952954
//hwTimer.resume(); //uncomment to automatically start the RX timer and leave it running
953955
connectionState = noCrossfire;
954-
crsf.Begin();
955956
}
956957

957958
devicesStart();
@@ -983,10 +984,6 @@ void loop()
983984
}
984985
#endif
985986

986-
#if defined(PLATFORM_STM32) || defined(PLATFORM_ESP8266)
987-
crsf.handleUARTin();
988-
#endif
989-
990987
if (connectionState > MODE_STATES)
991988
{
992989
return;

0 commit comments

Comments
 (0)