diff --git a/ESP32_AP-Flasher/include/leds.h b/ESP32_AP-Flasher/include/leds.h index 79153be8..5f6e8762 100644 --- a/ESP32_AP-Flasher/include/leds.h +++ b/ESP32_AP-Flasher/include/leds.h @@ -1,5 +1,11 @@ #include + +#ifdef OPENEPAPERLINK_PCB #include +#endif void ledTask(void* parameter); -void shortBlink(CRGB cname); \ No newline at end of file + +#ifdef OPENEPAPERLINK_PCB +void shortBlink(CRGB cname); +#endif \ No newline at end of file diff --git a/ESP32_AP-Flasher/include/settings.h b/ESP32_AP-Flasher/include/settings.h index df957b57..4137cde9 100644 --- a/ESP32_AP-Flasher/include/settings.h +++ b/ESP32_AP-Flasher/include/settings.h @@ -46,19 +46,19 @@ #if (PINOUT == SIMPLE_AP) /* Lolin32 lite connections to AP tag*/ -#define RXD1 16 -#define TXD1 17 - #define FLASHER_AP_SS 5 #define FLASHER_AP_CLK 18 #define FLASHER_AP_MOSI 23 #define FLASHER_AP_MISO 19 #define FLASHER_AP_RESET 2 -#define FLASHER_AP_POWER 13 +//#define FLASHER_AP_POWER 13 #define FLASHER_AP_POWER2 15 -#define FLASHER_AP_TXD 17 -#define FLASHER_AP_RXD 16 +//#define FLASHER_AP_TXD 17 +//#define FLASHER_AP_RXD 16 #define FLASHER_AP_TEST -1 +#define FLASHER_AP_TXD 12 +#define FLASHER_AP_RXD 13 +#define FLASHER_AP_POWER 25 #define FLASHER_LED 22 #endif diff --git a/ESP32_AP-Flasher/src/flasher.cpp b/ESP32_AP-Flasher/src/flasher.cpp index 7343ea28..edbd6ded 100644 --- a/ESP32_AP-Flasher/src/flasher.cpp +++ b/ESP32_AP-Flasher/src/flasher.cpp @@ -172,7 +172,7 @@ void performDeviceFlash() { vTaskDelay(500 / portTICK_PERIOD_MS); zbs->set_power(1); vTaskDelay(500 / portTICK_PERIOD_MS); - //interfaceWorking = zbs.begin(); + interfaceWorking = zbs->begin(FLASHER_AP_SS, FLASHER_AP_CLK, FLASHER_AP_MOSI, FLASHER_AP_MISO, FLASHER_AP_RESET, FLASHER_AP_POWER, 8000000); if (!interfaceWorking) { Serial.print("I wasn't able to connect to a ZBS tag, please check wiring and definitions in the settings.h file.\n"); return; diff --git a/ESP32_AP-Flasher/src/leds.cpp b/ESP32_AP-Flasher/src/leds.cpp index 60aea77c..af182aec 100644 --- a/ESP32_AP-Flasher/src/leds.cpp +++ b/ESP32_AP-Flasher/src/leds.cpp @@ -1,17 +1,26 @@ #include + +#ifdef OPENEPAPERLINK_PCB #include +#endif #include "settings.h" -QueueHandle_t rgbLedQueue; + QueueHandle_t ledQueue; +#ifdef OPENEPAPERLINK_PCB +QueueHandle_t rgbLedQueue; + struct ledInstructionRGB { CRGB ledColor; uint16_t fadeTime; uint16_t length; }; +CRGB leds[1]; +#endif + struct ledInstruction { uint16_t value; uint16_t fadeTime; @@ -64,7 +73,9 @@ const uint16_t gamma12[256] = { 3502, 3540, 3578, 3616, 3654, 3693, 3732, 3771, 3810, 3850, 3890, 3930, 3971, 4013, 4054, 4095}; -CRGB leds[1]; + + +#ifdef OPENEPAPERLINK_PCB void addToRGBQueue(struct ledInstructionRGB* rgb) { BaseType_t queuestatus = xQueueSend(rgbLedQueue, &rgb, 0); @@ -73,13 +84,6 @@ void addToRGBQueue(struct ledInstructionRGB* rgb) { } } -void addToMonoQueue(struct ledInstruction* mono) { - BaseType_t queuestatus = xQueueSend(ledQueue, &mono, 0); - if (queuestatus == pdFALSE) { - delete mono; - } -} - void addFadeColor(CRGB cname) { struct ledInstructionRGB* rgb = new struct ledInstructionRGB; rgb->ledColor = cname; @@ -88,13 +92,6 @@ void addFadeColor(CRGB cname) { addToRGBQueue(rgb); } -void addFadeMono(uint8_t value) { - struct ledInstruction* mono = new struct ledInstruction; - mono->value = value; - mono->fadeTime = 750; - mono->length = 0; - addToMonoQueue(mono); -} void shortBlink(CRGB cname) { struct ledInstructionRGB* rgb = new struct ledInstructionRGB; @@ -118,14 +115,9 @@ void shortBlink(CRGB cname) { void showRGB() { FastLED.show(); } -void showMono(uint8_t brightness) { - ledcWrite(7, gamma12[brightness]); -} -volatile uint16_t rgbIdlePeriod = 800; -volatile uint16_t monoIdlePeriod = 900; volatile CRGB rgbIdleColor = CRGB::Green; -uint8_t monoValue = 0; +volatile uint16_t rgbIdlePeriod = 800; void rgbIdleStep() { static bool dirUp = true; @@ -150,6 +142,33 @@ void rgbIdleStep() { showRGB(); } } +#endif + + +void addToMonoQueue(struct ledInstruction* mono) { + BaseType_t queuestatus = xQueueSend(ledQueue, &mono, 0); + if (queuestatus == pdFALSE) { + delete mono; + } +} + +void addFadeMono(uint8_t value) { + struct ledInstruction* mono = new struct ledInstruction; + mono->value = value; + mono->fadeTime = 750; + mono->length = 0; + addToMonoQueue(mono); +} + + + +void showMono(uint8_t brightness) { + ledcWrite(7, gamma12[brightness]); +} +volatile uint16_t monoIdlePeriod = 900; + +uint8_t monoValue = 0; + void monoIdleStep() { static bool dirUp = true; @@ -175,11 +194,24 @@ void monoIdleStep() { } void ledTask(void* parameter) { +#ifdef OPENEPAPERLINK_PCB FastLED.addLeds(leds, 1); // GRB ordering is typical leds[0] = CRGB::Blue; showRGB(); - rgbLedQueue = xQueueCreate(30, sizeof(struct ledInstructionRGB*)); + + struct ledInstructionRGB* rgb = nullptr; + // open with a nice RGB crossfade + addFadeColor(CRGB::Red); + addFadeColor(CRGB::Green); + addFadeColor(CRGB::Blue); + addFadeColor(CRGB::Red); + addFadeColor(CRGB::Green); + addFadeColor(CRGB::Blue); + CRGB oldColor = CRGB::Black; + uint16_t rgbInstructionFadeTime = 0; +#endif + ledQueue = xQueueCreate(30, sizeof(struct ledInstruction*)); digitalWrite(FLASHER_LED, HIGH); @@ -187,31 +219,20 @@ void ledTask(void* parameter) { ledcSetup(7, 9500, 12); // 141251 okay // 101251 okay ledcAttachPin(FLASHER_LED, 7); - struct ledInstructionRGB* rgb = nullptr; struct ledInstruction* monoled = nullptr; - // open with a nice RGB crossfade - addFadeColor(CRGB::Red); - addFadeColor(CRGB::Green); - addFadeColor(CRGB::Blue); - addFadeColor(CRGB::Red); - addFadeColor(CRGB::Green); - addFadeColor(CRGB::Blue); - addFadeMono(255); addFadeMono(127); addFadeMono(255); addFadeMono(0); - CRGB oldColor = CRGB::Black; - uint8_t oldBrightness = 0; - uint16_t rgbInstructionFadeTime = 0; + uint16_t monoInstructionFadeTime = 0; while (1) { - +#ifdef OPENEPAPERLINK_PCB // handle RGB led instructions if (rgb == nullptr) { // fetch a led instruction @@ -240,7 +261,7 @@ void ledTask(void* parameter) { rgb = nullptr; } } - +#endif // handle flasher LED (single color) if (monoled == nullptr) { BaseType_t q = xQueueReceive(ledQueue, &monoled, 1); diff --git a/ESP32_AP-Flasher/src/main.cpp b/ESP32_AP-Flasher/src/main.cpp index fc17bf10..75fa3a4b 100644 --- a/ESP32_AP-Flasher/src/main.cpp +++ b/ESP32_AP-Flasher/src/main.cpp @@ -58,7 +58,7 @@ void setup() { Serial.printf("Total PSRAM: %d", ESP.getPsramSize()); Serial.printf("Free PSRAM: %d", ESP.getFreePsram()); - #if (HAS_USB == 1) + #ifdef OPENEPAPERLINK_PCB xTaskCreate(usbFlasherTask, "flasher", 10000, NULL, configMAX_PRIORITIES - 10, NULL); #endif diff --git a/ESP32_AP-Flasher/src/powermgt.cpp b/ESP32_AP-Flasher/src/powermgt.cpp index a07abefb..eff48309 100644 --- a/ESP32_AP-Flasher/src/powermgt.cpp +++ b/ESP32_AP-Flasher/src/powermgt.cpp @@ -1,11 +1,31 @@ -#include #include "powermgt.h" + +#include + #include "settings.h" +#ifdef SIMPLE_AP +void simpleAPPower(bool state) { + pinMode(FLASHER_AP_POWER, INPUT); + pinMode(FLASHER_AP_POWER2, INPUT); + digitalWrite(FLASHER_AP_POWER, state); + digitalWrite(FLASHER_AP_POWER2, state); + pinMode(FLASHER_AP_POWER, OUTPUT); + pinMode(FLASHER_AP_POWER2, OUTPUT); +} +#endif + // On the OpenEPaperLink board, there is no in-rush current limiting. The tags that can be connected to the board can have significant capacity, which, // when drained, will cause the 3v3 rail to sag enough to reset the ESP32. This is obviously not great. To prevent this from happening, we ramp up/down the // voltage with PWM. Ramping down really is unnecessary, as the board has a resistor to dump the charge into. void rampTagPower(uint8_t pin, bool up) { + pinMode(FLASHER_AP_SS, INPUT); + pinMode(FLASHER_AP_CLK, INPUT); + pinMode(FLASHER_AP_MOSI, INPUT); + pinMode(FLASHER_AP_MISO, INPUT); + pinMode(FLASHER_AP_RESET, OUTPUT); + digitalWrite(FLASHER_AP_RESET, HIGH); +#ifdef OPENEPAPERLINK_PCB if (up) { ledcSetup(0, 152000, 8); // 141251 okay // 101251 okay ledcWrite(0, 254); @@ -23,4 +43,10 @@ void rampTagPower(uint8_t pin, bool up) { pinMode(pin, OUTPUT); digitalWrite(pin, HIGH); } +#endif +#ifdef SIMPLE_AP + simpleAPPower(false); + delay(500); + simpleAPPower(up); +#endif } \ No newline at end of file diff --git a/ESP32_AP-Flasher/src/serial.cpp b/ESP32_AP-Flasher/src/serial.cpp index 9698faf3..864ab87e 100644 --- a/ESP32_AP-Flasher/src/serial.cpp +++ b/ESP32_AP-Flasher/src/serial.cpp @@ -8,6 +8,7 @@ #include "settings.h" #include "web.h" #include "zbs_interface.h" +#include "powermgt.h" #define ZBS_RX_WAIT_HEADER 0 #define ZBS_RX_WAIT_PKT_LEN 1 @@ -90,6 +91,7 @@ uint8_t pktindex = 0; char lastchar = 0; uint8_t charindex = 0; uint64_t waitingForVersion = 0; +uint8_t crashcounter = 0; uint16_t version; void ShortRXWaitLoop() { @@ -200,25 +202,12 @@ void SerialRXLoop() { break; case ZBS_RX_WAIT_VER: waitingForVersion = 0; + crashcounter = 0; cmdbuffer[charindex] = lastchar; charindex++; if (charindex == 4) { charindex = 0; version = (uint16_t)strtoul(cmdbuffer, NULL, 16); - /* - uint16_t fsversion; - lookupFirmwareFile(fsversion); - if ((fsversion) && (version != fsversion)) { - Serial.printf("ZBS/Zigbee FW version: %04X, version on SPIFFS: %04X\n", version, fsversion); - Serial.printf("Performing flash update in about 30 seconds"); - vTaskDelay(30000 / portTICK_PERIOD_MS); - performDeviceFlash(); - } else if (!fsversion) { - Serial.println("No ZBS/Zigbee FW binary found on SPIFFS, please upload a zigbeebase000X.bin - format binary to enable flashing"); - } else { - Serial.printf("ZBS/Zigbee FW version: %04X\n", version); - } - */ RXState = ZBS_RX_WAIT_HEADER; } break; @@ -229,13 +218,14 @@ void SerialRXLoop() { extern uint8_t* getDataForFile(File* file); void zbsRxTask(void* parameter) { - Serial1.begin(230400, SERIAL_8N1, RXD1, TXD1); + Serial1.begin(228571, SERIAL_8N1, FLASHER_AP_RXD, FLASHER_AP_TXD); - simplePowerOn(); + rampTagPower(FLASHER_AP_POWER, true); bool firstrun = true; Serial1.print("VER?"); waitingForVersion = esp_timer_get_time(); + while (1) { SerialRXLoop(); @@ -245,14 +235,21 @@ void zbsRxTask(void* parameter) { vTaskDelay(1 / portTICK_PERIOD_MS); if (waitingForVersion) { - if (esp_timer_get_time() - waitingForVersion > 10000*1000ULL) { + if (esp_timer_get_time() - waitingForVersion > 5000*1000ULL) { waitingForVersion = 0; - //performDeviceFlash(); - Serial.println("I wasn't able to connect to a ZBS tag, trying to reboot the tag."); - Serial.println("If this problem persists, please check wiring and definitions in the settings.h file, and presence of the right firmware"); - simplePowerOn(); - wsErr("The AP tag crashed. Restarting tag, regenerating all pending info."); - refreshAllPending(); + wsLog("AP doesn't respond... "+String(crashcounter + 1)); + if (++crashcounter >= 4) { + crashcounter = 0; + Serial.println("I wasn't able to connect to a ZBS tag, trying to reboot the tag."); + Serial.println("If this problem persists, please check wiring and definitions in the settings.h file, and presence of the right firmware"); + rampTagPower(FLASHER_AP_POWER, false); + vTaskDelay(2/portTICK_PERIOD_MS); + rampTagPower(FLASHER_AP_POWER, true); + wsErr("The AP tag crashed. Restarting tag, regenerating all pending info."); + refreshAllPending(); + } else { + Ping(); + } } } @@ -271,4 +268,4 @@ void zbsRxTask(void* parameter) { firstrun = false; } } -} +} \ No newline at end of file diff --git a/ESP32_AP-Flasher/src/web.cpp b/ESP32_AP-Flasher/src/web.cpp index b3fc953a..ef1e21bf 100644 --- a/ESP32_AP-Flasher/src/web.cpp +++ b/ESP32_AP-Flasher/src/web.cpp @@ -2,7 +2,6 @@ #include #include - #include #include #include @@ -13,10 +12,10 @@ #include // https://github.com/tzapu/WiFiManager/tree/feature_asyncwebserver #include "commstructs.h" +#include "leds.h" #include "newproto.h" #include "settings.h" #include "tag_db.h" -#include "leds.h" extern uint8_t data_to_send[]; @@ -52,7 +51,9 @@ void webSocketSendProcess(void *parameter) { } void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { +#ifdef OPENEPAPERLINK_PCB shortBlink(CRGB::BlueViolet); +#endif switch (type) { case WS_EVT_CONNECT: // client connected @@ -159,14 +160,12 @@ void wsSendSysteminfo() { } void wsSendTaginfo(uint8_t mac[6]) { - String json = ""; json = tagDBtoJson(mac); xSemaphoreTake(wsMutex, portMAX_DELAY); ws.textAll(json); xSemaphoreGive(wsMutex); - } void init_web() { @@ -208,7 +207,7 @@ void init_web() { server.serveStatic("/current", LittleFS, "/current/"); server.serveStatic("/", LittleFS, "/www/").setDefaultFile("index.html"); - + server.on( "/imgupload", HTTP_POST, [](AsyncWebServerRequest *request) { request->send(200); @@ -220,15 +219,15 @@ void init_web() { if (request->hasParam("mac")) { String dst = request->getParam("mac")->value(); uint8_t mac[6]; - if (sscanf(dst.c_str(), "%02X%02X%02X%02X%02X%02X", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5])==6) { + if (sscanf(dst.c_str(), "%02X%02X%02X%02X%02X%02X", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) == 6) { json = tagDBtoJson(mac); } } else { - uint8_t startPos=0; + uint8_t startPos = 0; if (request->hasParam("pos")) { startPos = atoi(request->getParam("pos")->value().c_str()); } - json = tagDBtoJson(nullptr,startPos); + json = tagDBtoJson(nullptr, startPos); } request->send(200, "application/json", json); }); @@ -245,8 +244,8 @@ void init_web() { taginfo->modeConfigJson = request->getParam("modecfgjson", true)->value(); taginfo->contentMode = atoi(request->getParam("contentmode", true)->value().c_str()); taginfo->nextupdate = 0; - //memset(taginfo->md5, 0, 16 * sizeof(uint8_t)); - //memset(taginfo->md5pending, 0, 16 * sizeof(uint8_t)); + // memset(taginfo->md5, 0, 16 * sizeof(uint8_t)); + // memset(taginfo->md5pending, 0, 16 * sizeof(uint8_t)); wsSendTaginfo(mac); saveDB("/current/tagDB.json"); request->send(200, "text/plain", "Ok, saved"); diff --git a/esp32_fw/.gitignore b/esp32_fw/.gitignore new file mode 100644 index 00000000..89cc49cb --- /dev/null +++ b/esp32_fw/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch