1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 11:36:13 +01:00

move WiFiESP::loop() to core 1

This commit is contained in:
Harald Barth 2022-08-16 12:25:49 +02:00
parent 162e1f9d3e
commit b9c1e779ae
7 changed files with 37 additions and 33 deletions

View File

@ -47,7 +47,6 @@
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
// wifi or ethernet ring streams with multiple client types // wifi or ethernet ring streams with multiple client types
RingStream * CommandDistributor::ring=0; RingStream * CommandDistributor::ring=0;
// byte CommandDistributor::ringClient=NO_CLIENT;
CommandDistributor::clientType CommandDistributor::clients[8]={ CommandDistributor::clientType CommandDistributor::clients[8]={
NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE}; NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE,NONE_TYPE};
@ -57,7 +56,6 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream
if (Diag::WIFI && Diag::CMD) if (Diag::WIFI && Diag::CMD)
DIAG(F("Parse C=%d T=%d B=%s"),clientId, clients[clientId], buffer); DIAG(F("Parse C=%d T=%d B=%s"),clientId, clients[clientId], buffer);
ring=stream; ring=stream;
// ringClient=stream->peekTargetMark();
// First check if the client is not known // First check if the client is not known
// yet and in that case determinine type // yet and in that case determinine type
@ -78,7 +76,6 @@ void CommandDistributor::parse(byte clientId,byte * buffer, RingStream * stream
else if (clients[clientId] == WITHROTTLE_TYPE) else if (clients[clientId] == WITHROTTLE_TYPE)
WiThrottle::getThrottle(clientId)->parse(ring, buffer); WiThrottle::getThrottle(clientId)->parse(ring, buffer);
// ringClient=NO_CLIENT;
} }
void CommandDistributor::forget(byte clientId) { void CommandDistributor::forget(byte clientId) {
@ -100,22 +97,22 @@ void CommandDistributor::broadcastToClients(clientType type) {
// before merging broadcasts in the ring, then reinstate it in case // before merging broadcasts in the ring, then reinstate it in case
// the process continues to output to its client. // the process continues to output to its client.
if (ring) { if (ring) {
if ((rememberClient = ring->peekTargetMark()) != NO_CLIENT) { if ((rememberClient = ring->peekTargetMark()) != RingStream::NO_CLIENT) {
DIAG(F("CD precommit client %d"), rememberClient); //DIAG(F("CD precommit client %d"), rememberClient);
ring->commit(); ring->commit();
} }
/* loop through ring clients */ /* loop through ring clients */
for (byte clientId=0; clientId<sizeof(clients); clientId++) { for (byte clientId=0; clientId<sizeof(clients); clientId++) {
if (clients[clientId]==type) { if (clients[clientId]==type) {
DIAG(F("CD mark client %d"), clientId); //DIAG(F("CD mark client %d"), clientId);
ring->mark(clientId); ring->mark(clientId);
ring->print(broadcastBufferWriter->getString()); ring->print(broadcastBufferWriter->getString());
DIAG(F("CD commit client %d"), clientId); //DIAG(F("CD commit client %d"), clientId);
ring->commit(); ring->commit();
} }
} }
if (ring->peekTargetMark()!=NO_CLIENT) { if (ring->peekTargetMark()!=RingStream::NO_CLIENT) {
DIAG(F("CD postmark client %d"), rememberClient); //DIAG(F("CD postmark client %d"), rememberClient);
ring->mark(rememberClient); ring->mark(rememberClient);
} }
} }

View File

@ -47,8 +47,6 @@ private:
static StringBuffer * broadcastBufferWriter; static StringBuffer * broadcastBufferWriter;
#ifdef CD_HANDLE_RING #ifdef CD_HANDLE_RING
static RingStream * ring; static RingStream * ring;
static const byte NO_CLIENT=255; //XXX remove later
// static byte ringClient;
static clientType clients[8]; static clientType clients[8];
#endif #endif
}; };

View File

@ -143,6 +143,10 @@ void loop()
#if WIFI_ON #if WIFI_ON
WifiInterface::loop(); WifiInterface::loop();
#endif //WIFI_ON #endif //WIFI_ON
#else //ARDUINO_ARCH_ESP32
#ifndef WIFI_TASK_ON_CORE0
WifiESP::loop();
#endif
#endif //ARDUINO_ARCH_ESP32 #endif //ARDUINO_ARCH_ESP32
#if ETHERNET_ON #if ETHERNET_ON
EthernetInterface::loop(); EthernetInterface::loop();

View File

@ -143,7 +143,7 @@ int RingStream::freeSpace() {
// mark start of message with client id (0...9) // mark start of message with client id (0...9)
void RingStream::mark(uint8_t b) { void RingStream::mark(uint8_t b) {
DIAG(F("RS mark client %d at %d core %d"), b, _pos_write, xPortGetCoreID()); //DIAG(F("RS mark client %d at %d core %d"), b, _pos_write, xPortGetCoreID());
_ringClient = b; _ringClient = b;
_mark=_pos_write; _mark=_pos_write;
write(b); // client id write(b); // client id
@ -165,14 +165,14 @@ void RingStream::info() {
bool RingStream::commit() { bool RingStream::commit() {
_flashInsert=NULL; // prepared for first read _flashInsert=NULL; // prepared for first read
if (_overflow) { if (_overflow) {
DIAG(F("RingStream(%d) commit(%d) OVERFLOW"),_len, _count); //DIAG(F("RingStream(%d) commit(%d) OVERFLOW"),_len, _count);
// just throw it away // just throw it away
_pos_write=_mark; _pos_write=_mark;
_overflow=false; _overflow=false;
return false; // commit failed return false; // commit failed
} }
if (_count==0) { if (_count==0) {
DIAG(F("RS commit count=0 rewind back to %d core %d"), _mark, xPortGetCoreID()); //DIAG(F("RS commit count=0 rewind back to %d core %d"), _mark, xPortGetCoreID());
// ignore empty response // ignore empty response
_pos_write=_mark; _pos_write=_mark;
_ringClient = NO_CLIENT; //XXX make else clause later _ringClient = NO_CLIENT; //XXX make else clause later
@ -188,7 +188,7 @@ bool RingStream::commit() {
{ char s[_count+2]; { char s[_count+2];
strncpy(s, (const char*)&(_buffer[_mark+1]), _count); strncpy(s, (const char*)&(_buffer[_mark+1]), _count);
s[_count]=0; s[_count]=0;
DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s); //DIAG(F("RS commit count=%d core %d \"%s\""), _count, xPortGetCoreID(), s);
} }
_ringClient = NO_CLIENT; _ringClient = NO_CLIENT;
return true; // commit worked return true; // commit worked

View File

@ -52,6 +52,7 @@ class RingStream : public Print {
if ((_pos_read==_pos_write) && !_overflow) return -1; // empty if ((_pos_read==_pos_write) && !_overflow) return -1; // empty
return _buffer[_pos_read]; return _buffer[_pos_read];
}; };
static const byte NO_CLIENT=255;
private: private:
int _len; int _len;
int _pos_write; int _pos_write;
@ -61,7 +62,6 @@ class RingStream : public Print {
int _count; int _count;
byte * _buffer; byte * _buffer;
char * _flashInsert; char * _flashInsert;
static const byte NO_CLIENT=255; // must be same as in CommandDistributor
byte _ringClient = NO_CLIENT; byte _ringClient = NO_CLIENT;
}; };

View File

@ -611,12 +611,12 @@ byte WiThrottle::stashClient;
char WiThrottle::stashThrottleChar; char WiThrottle::stashThrottleChar;
void WiThrottle::getLocoCallback(int16_t locoid) { void WiThrottle::getLocoCallback(int16_t locoid) {
DIAG(F("LocoCallback mark client %d"), stashClient); //DIAG(F("LocoCallback mark client %d"), stashClient);
stashStream->mark(stashClient); stashStream->mark(stashClient);
if (locoid<=0) { if (locoid<=0) {
StringFormatter::send(stashStream,F("HMNo loco found on prog track\n")); StringFormatter::send(stashStream,F("HMNo loco found on prog track\n"));
DIAG(F("LocoCallback commit (noloco)")); //DIAG(F("LocoCallback commit (noloco)"));
stashStream->commit(); // done here, commit and return stashStream->commit(); // done here, commit and return
return; return;
} }
@ -627,7 +627,7 @@ void WiThrottle::getLocoCallback(int16_t locoid) {
locoid = locoid ^ LONG_ADDR_MARKER; // remove marker bit to get real long addr locoid = locoid ^ LONG_ADDR_MARKER; // remove marker bit to get real long addr
if (locoid <= HIGHEST_SHORT_ADDR ) { // out of range for long addr if (locoid <= HIGHEST_SHORT_ADDR ) { // out of range for long addr
StringFormatter::send(stashStream,F("HMLong addr %d <= %d unsupported\n"), locoid, HIGHEST_SHORT_ADDR); StringFormatter::send(stashStream,F("HMLong addr %d <= %d unsupported\n"), locoid, HIGHEST_SHORT_ADDR);
DIAG(F("LocoCallback commit (error)")); //DIAG(F("LocoCallback commit (error)"));
stashStream->commit(); // done here, commit and return stashStream->commit(); // done here, commit and return
return; return;
} }
@ -641,7 +641,7 @@ void WiThrottle::getLocoCallback(int16_t locoid) {
stashInstance->multithrottle(stashStream, (byte *)addcmd); stashInstance->multithrottle(stashStream, (byte *)addcmd);
TrackManager::setMainPower(POWERMODE::ON); TrackManager::setMainPower(POWERMODE::ON);
TrackManager::setJoin(true); // <1 JOIN> so we can drive loco away TrackManager::setJoin(true); // <1 JOIN> so we can drive loco away
DIAG(F("LocoCallback commit success")); //DIAG(F("LocoCallback commit success"));
stashStream->commit(); stashStream->commit();
CommandDistributor::broadcastPower(); CommandDistributor::broadcastPower();

View File

@ -95,14 +95,15 @@ public:
static std::vector<NetworkClient> clients; // a list to hold all clients static std::vector<NetworkClient> clients; // a list to hold all clients
static WiFiServer *server = NULL; static WiFiServer *server = NULL;
static RingStream *outboundRing = new RingStream(2048); static RingStream *outboundRing = new RingStream(2048);
//static RingStream *eventRing = new RingStream(2048);
static bool APmode = false; static bool APmode = false;
#ifdef WIFI_TASK_ON_CORE0
void wifiLoop(void *){ void wifiLoop(void *){
for(;;){ for(;;){
WifiESP::loop(); WifiESP::loop();
} }
} }
#endif
bool WifiESP::setup(const char *SSid, bool WifiESP::setup(const char *SSid,
const char *password, const char *password,
@ -196,6 +197,7 @@ bool WifiESP::setup(const char *SSid,
server->begin(); server->begin();
// server started here // server started here
#ifdef WIFI_TASK_ON_CORE0
//start loop task //start loop task
if (pdPASS != xTaskCreatePinnedToCore( if (pdPASS != xTaskCreatePinnedToCore(
wifiLoop, /* Task function. */ wifiLoop, /* Task function. */
@ -211,7 +213,10 @@ bool WifiESP::setup(const char *SSid,
// report server started after wifiLoop creation // report server started after wifiLoop creation
// when everything looks good // when everything looks good
DIAG(F("Server up port %d"),port); DIAG(F("Server starting (core 0) port %d"),port);
#else
DIAG(F("Server will be started on port %d"),port);
#endif
return true; return true;
} }
@ -261,7 +266,7 @@ void WifiESP::loop() {
cmd[len]=0; cmd[len]=0;
outboundRing->mark(clientId); outboundRing->mark(clientId);
CommandDistributor::parse(clientId,cmd,outboundRing); CommandDistributor::parse(clientId,cmd,outboundRing);
if (outboundRing->peekTargetMark()!=255) //XXX fix 255 later if (outboundRing->peekTargetMark()!=RingStream::NO_CLIENT)
outboundRing->commit(); outboundRing->commit();
} }
} }