From a49cc13bc872eb58254080a137035c8255b9792c Mon Sep 17 00:00:00 2001 From: peterj Date: Fri, 29 May 2020 14:35:50 +0100 Subject: [PATCH 1/8] [Issue #65] CRC implementation. --- src/SDI12.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/SDI12.h | 5 ++++ 2 files changed, 81 insertions(+) diff --git a/src/SDI12.cpp b/src/SDI12.cpp index 10fcfb2..f212d05 100644 --- a/src/SDI12.cpp +++ b/src/SDI12.cpp @@ -848,6 +848,82 @@ void SDI12::sendResponse(FlashString resp) { } setState(SDI12_LISTENING); // return to listening state } +ifdef USE_CRC +#define POLY 0xa001 +String SDI12::addCRCResponse(String &resp) { + char crcStr[3] = {0}; + uint16_t crc = 0; + + for(int i = 0; i < resp.length(); i++) { + crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + } + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (resp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); +} + +char * SDI12::addCRCResponse(char *resp) { + char *crcStr = "\0"; + uint16_t crc = 0; + + for(int i = 0; i < strlen(resp); i++) { + crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + } + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (strncat(resp, crcStr,3)); +} + +String SDI12::addCRCResponse(FlashString resp) { + char crcStr[3] = {0}; + char respBuffer[SDI12_BUFFER_SIZE - 5]; // don't need space for the CRC or CR/LF + uint16_t crc = 0; + int i = 0; + char responsechar ; + + + for(i = 0; i < strlen_P((PGM_P)resp); i++) { + responsechar = (char)pgm_read_byte((const char *)resp + i); + crc ^= (uint16_t)responsechar; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + respBuffer[i] = responsechar; + } + respBuffer[++i] = '\0'; + String outResp = respBuffer; + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (outResp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); +} +#endif //USE_CRC /* ============== 7. Interrupt Service Routine =================== diff --git a/src/SDI12.h b/src/SDI12.h index 52a3edc..1cfcbc9 100644 --- a/src/SDI12.h +++ b/src/SDI12.h @@ -130,6 +130,11 @@ class SDI12 : public Stream void sendResponse(String &resp); // sends the String resp out on the data line (for slave use) void sendResponse(const char *resp); // sends the String resp out on the data line (for slave use) void sendResponse(FlashString resp); // sends the String resp out on the data line (for slave use) + #ifdef USE_CRC + String addCRCResponse(String &resp); // Add CRC to the resp string (for slave use) + char * addCRCResponse( char *resp); // Add CRC to the resp string (for slave use) + String addCRCResponse(FlashString resp); // Add CRC to the resp string (for slave use) + #endif int available(); // returns the number of bytes available in buffer int peek(); // reveals next byte in buffer without consuming From 96feec02c99bb8e1a56a79392e86f7a6bfd5bbb8 Mon Sep 17 00:00:00 2001 From: DalesLandNet Date: Mon, 12 Jul 2021 16:08:32 +0100 Subject: [PATCH 2/8] Correct missing #, Also add CRC calculate function and correct one of the overloaded methods --- src/SDI12.cpp | 44 ++++++++++++++++++++++++++++++++++---------- src/SDI12.h | 5 +++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/SDI12.cpp b/src/SDI12.cpp index f212d05..58e1d5c 100644 --- a/src/SDI12.cpp +++ b/src/SDI12.cpp @@ -848,7 +848,8 @@ void SDI12::sendResponse(FlashString resp) { } setState(SDI12_LISTENING); // return to listening state } -ifdef USE_CRC + +#ifdef USE_CRC #define POLY 0xa001 String SDI12::addCRCResponse(String &resp) { char crcStr[3] = {0}; @@ -857,12 +858,12 @@ String SDI12::addCRCResponse(String &resp) { for(int i = 0; i < resp.length(); i++) { crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself for (int j = 0; j < 8; j++){ //count = 1 to 8 - if (crc & 0x0001){ //if the least significant bit of the CRC is one - crc >>= 1; //right shift the CRC one bit - crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself } else { - crc >>= 1; //right shift the CRC one bit + crc >>= 1; //right shift the CRC one bit } } } @@ -873,15 +874,15 @@ String SDI12::addCRCResponse(String &resp) { } char * SDI12::addCRCResponse(char *resp) { - char *crcStr = "\0"; - uint16_t crc = 0; + char *crcStr[3] = {0}; + uint16_t crc = 0; for(int i = 0; i < strlen(resp); i++) { crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself for (int j = 0; j < 8; j++){ //count = 1 to 8 - if (crc & 0x0001){ //if the least significant bit of the CRC is one - crc >>= 1; //right shift the CRC one bit - crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself } else { crc >>= 1; //right shift the CRC one bit @@ -923,6 +924,29 @@ String SDI12::addCRCResponse(FlashString resp) { crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); return (outResp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); } + +String SDI12::calculateCRC(String &resp){ + char crcStr[3] = {0}; + uint16_t crc = 0; + + for(int i = 0; i < resp.length(); i++) { + crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + } + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); +} + #endif //USE_CRC diff --git a/src/SDI12.h b/src/SDI12.h index 1cfcbc9..d6291f4 100644 --- a/src/SDI12.h +++ b/src/SDI12.h @@ -51,6 +51,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA typedef const __FlashStringHelper *FlashString; + +//#define USE_CRC + #define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field #define SDI12_BUFFER_SIZE 81 //
is a single character // + has a maximum value of 75 characters. @@ -65,6 +68,7 @@ enum LookaheadMode SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. }; + #define READTIME sdi12timer.SDI12TimerRead() #else #define READTIME TCNTX @@ -134,6 +138,7 @@ class SDI12 : public Stream String addCRCResponse(String &resp); // Add CRC to the resp string (for slave use) char * addCRCResponse( char *resp); // Add CRC to the resp string (for slave use) String addCRCResponse(FlashString resp); // Add CRC to the resp string (for slave use) + String calculateCRC(String &resp); // Calculate the CRC for a response #endif int available(); // returns the number of bytes available in buffer From f543a69022de16664e8a429b6ee73d0163da0f6e Mon Sep 17 00:00:00 2001 From: DalesLandNet Date: Tue, 13 Jul 2021 11:34:45 +0100 Subject: [PATCH 3/8] [Issue #79] ESP32- Core panic when using in conjunction with the "preferences" library. --- src/SDI12.cpp | 8 ++++---- src/SDI12_boards.cpp | 2 +- src/SDI12_boards.h | 8 ++++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/SDI12.cpp b/src/SDI12.cpp index 58e1d5c..31d1105 100644 --- a/src/SDI12.cpp +++ b/src/SDI12.cpp @@ -874,7 +874,7 @@ String SDI12::addCRCResponse(String &resp) { } char * SDI12::addCRCResponse(char *resp) { - char *crcStr[3] = {0}; + char crcStr[3] = {0}; uint16_t crc = 0; for(int i = 0; i < strlen(resp); i++) { @@ -979,12 +979,12 @@ takes to either a HIGH vs a LOW, and helps maintain a constant timing. the ISR is instructed to call handleInterrupt() when they trigger. */ // 7.1 - Passes off responsibility for the interrupt to the active object. -void SDI12::handleInterrupt(){ +void USE_INSTRUCTION_RAM SDI12::handleInterrupt(){ if (_activeObject) _activeObject->receiveISR(); } // 7.2 - Creates a blank slate of bits for an incoming character -void SDI12::startChar() +void USE_INSTRUCTION_RAM SDI12::startChar() { rxState = 0; // got a start bit rxMask = 0x01; // 0b00000001, bit mask, lsb first @@ -992,7 +992,7 @@ void SDI12::startChar() } // startChar // 7.3 - The actual interrupt service routine -void SDI12::receiveISR() +void USE_INSTRUCTION_RAM SDI12::receiveISR() { sdi12timer_t thisBitTCNT = READTIME; // time of this data transition (plus ISR latency) diff --git a/src/SDI12_boards.cpp b/src/SDI12_boards.cpp index d3eacae..1298c7f 100644 --- a/src/SDI12_boards.cpp +++ b/src/SDI12_boards.cpp @@ -228,7 +228,7 @@ SDI12Timer::SDI12Timer(){} void SDI12Timer::configSDI12TimerPrescale(void) { } void SDI12Timer::resetSDI12TimerPrescale(void) { } - sdi12timer_t SDI12Timer::SDI12TimerRead(void) + sdi12timer_t USE_INSTRUCTION_RAM SDI12Timer::SDI12TimerRead(void) { // Its a one microsecond clock but we want 64uS ticks so divide by 64 i.e. right shift 6 return((sdi12timer_t) (micros() >> 6)); diff --git a/src/SDI12_boards.h b/src/SDI12_boards.h index 542a005..7dcd054 100644 --- a/src/SDI12_boards.h +++ b/src/SDI12_boards.h @@ -14,6 +14,14 @@ sensors. This library provides a general software solution, without requiring typedef uint8_t sdi12timer_t; #endif +#if defined(ESP32) +#define USE_INSTRUCTION_RAM IRAM_ATTR +#elif defined(ESP8266) +#define USE_INSTRUCTION_RAM ICACHE_RAM_ATTR +#else +#define USE_INSTRUCTION_RAM +#endif + class SDI12Timer { public: From 02d84cd78c471ba8077792d9d1f425effd03b593 Mon Sep 17 00:00:00 2001 From: DalesLandNet Date: Wed, 14 Jul 2021 15:19:49 +0100 Subject: [PATCH 4/8] Update with suggestions from the pull request --- src/SDI12.cpp | 12 ++++++------ src/SDI12.h | 4 ++-- src/SDI12_boards.cpp | 2 +- src/SDI12_boards.h | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/SDI12.cpp b/src/SDI12.cpp index 31d1105..2832ca6 100644 --- a/src/SDI12.cpp +++ b/src/SDI12.cpp @@ -849,7 +849,7 @@ void SDI12::sendResponse(FlashString resp) { setState(SDI12_LISTENING); // return to listening state } -#ifdef USE_CRC +#ifdef ENVIRODIY_SDI12_USE_CRC #define POLY 0xa001 String SDI12::addCRCResponse(String &resp) { char crcStr[3] = {0}; @@ -889,7 +889,7 @@ char * SDI12::addCRCResponse(char *resp) { } } } - crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); return (strncat(resp, crcStr,3)); @@ -947,7 +947,7 @@ String SDI12::calculateCRC(String &resp){ return (String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); } -#endif //USE_CRC +#endif //ENVIRODIY_SDI12_USE_CRC /* ============== 7. Interrupt Service Routine =================== @@ -979,12 +979,12 @@ takes to either a HIGH vs a LOW, and helps maintain a constant timing. the ISR is instructed to call handleInterrupt() when they trigger. */ // 7.1 - Passes off responsibility for the interrupt to the active object. -void USE_INSTRUCTION_RAM SDI12::handleInterrupt(){ +void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::handleInterrupt(){ if (_activeObject) _activeObject->receiveISR(); } // 7.2 - Creates a blank slate of bits for an incoming character -void USE_INSTRUCTION_RAM SDI12::startChar() +void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::startChar() { rxState = 0; // got a start bit rxMask = 0x01; // 0b00000001, bit mask, lsb first @@ -992,7 +992,7 @@ void USE_INSTRUCTION_RAM SDI12::startChar() } // startChar // 7.3 - The actual interrupt service routine -void USE_INSTRUCTION_RAM SDI12::receiveISR() +void ESPFAMILY_USE_INSTRUCTION_RAM SDI12::receiveISR() { sdi12timer_t thisBitTCNT = READTIME; // time of this data transition (plus ISR latency) diff --git a/src/SDI12.h b/src/SDI12.h index d6291f4..9495bb1 100644 --- a/src/SDI12.h +++ b/src/SDI12.h @@ -52,7 +52,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA typedef const __FlashStringHelper *FlashString; -//#define USE_CRC +//#define ENVIRODIY_SDI12_USE_CRC #define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field #define SDI12_BUFFER_SIZE 81 //
is a single character @@ -134,7 +134,7 @@ class SDI12 : public Stream void sendResponse(String &resp); // sends the String resp out on the data line (for slave use) void sendResponse(const char *resp); // sends the String resp out on the data line (for slave use) void sendResponse(FlashString resp); // sends the String resp out on the data line (for slave use) - #ifdef USE_CRC + #ifdef ENVIRODIY_SDI12_USE_CRC String addCRCResponse(String &resp); // Add CRC to the resp string (for slave use) char * addCRCResponse( char *resp); // Add CRC to the resp string (for slave use) String addCRCResponse(FlashString resp); // Add CRC to the resp string (for slave use) diff --git a/src/SDI12_boards.cpp b/src/SDI12_boards.cpp index 1298c7f..e18dd62 100644 --- a/src/SDI12_boards.cpp +++ b/src/SDI12_boards.cpp @@ -228,7 +228,7 @@ SDI12Timer::SDI12Timer(){} void SDI12Timer::configSDI12TimerPrescale(void) { } void SDI12Timer::resetSDI12TimerPrescale(void) { } - sdi12timer_t USE_INSTRUCTION_RAM SDI12Timer::SDI12TimerRead(void) + sdi12timer_t ESPFAMILY_USE_INSTRUCTION_RAM SDI12Timer::SDI12TimerRead(void) { // Its a one microsecond clock but we want 64uS ticks so divide by 64 i.e. right shift 6 return((sdi12timer_t) (micros() >> 6)); diff --git a/src/SDI12_boards.h b/src/SDI12_boards.h index 7dcd054..bd3f2b7 100644 --- a/src/SDI12_boards.h +++ b/src/SDI12_boards.h @@ -15,11 +15,11 @@ sensors. This library provides a general software solution, without requiring #endif #if defined(ESP32) -#define USE_INSTRUCTION_RAM IRAM_ATTR +#define ESPFAMILY_USE_INSTRUCTION_RAM IRAM_ATTR #elif defined(ESP8266) -#define USE_INSTRUCTION_RAM ICACHE_RAM_ATTR +#define ESPFAMILY_USE_INSTRUCTION_RAM ICACHE_RAM_ATTR #else -#define USE_INSTRUCTION_RAM +#define ESPFAMILY_USE_INSTRUCTION_RAM #endif class SDI12Timer From 19449f106fb046b491f78e27a39b65f35258dc1a Mon Sep 17 00:00:00 2001 From: DalesLandNet Date: Mon, 12 Jul 2021 16:08:32 +0100 Subject: [PATCH 5/8] Correct missing #, Also add CRC calculate function and correct one of the overloaded methods --- src/SDI12.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/SDI12.h | 6 +++ 2 files changed, 106 insertions(+) diff --git a/src/SDI12.cpp b/src/SDI12.cpp index c71e998..8b50599 100644 --- a/src/SDI12.cpp +++ b/src/SDI12.cpp @@ -547,6 +547,106 @@ void SDI12::sendResponse(FlashString resp) { setState(SDI12_LISTENING); // return to listening state } +#ifdef USE_CRC +#define POLY 0xa001 +String SDI12::addCRCResponse(String &resp) { + char crcStr[3] = {0}; + uint16_t crc = 0; + + for(int i = 0; i < resp.length(); i++) { + crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + } + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (resp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); +} + +char * SDI12::addCRCResponse(char *resp) { + char *crcStr[3] = {0}; + uint16_t crc = 0; + + for(int i = 0; i < strlen(resp); i++) { + crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + } + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (strncat(resp, crcStr,3)); +} + +String SDI12::addCRCResponse(FlashString resp) { + char crcStr[3] = {0}; + char respBuffer[SDI12_BUFFER_SIZE - 5]; // don't need space for the CRC or CR/LF + uint16_t crc = 0; + int i = 0; + char responsechar ; + + + for(i = 0; i < strlen_P((PGM_P)resp); i++) { + responsechar = (char)pgm_read_byte((const char *)resp + i); + crc ^= (uint16_t)responsechar; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + respBuffer[i] = responsechar; + } + respBuffer[++i] = '\0'; + String outResp = respBuffer; + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (outResp + String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); +} + +String SDI12::calculateCRC(String &resp){ + char crcStr[3] = {0}; + uint16_t crc = 0; + + for(int i = 0; i < resp.length(); i++) { + crc ^= (uint16_t)resp[i]; //Set the CRC equal to the exclusive OR of the character and itself + for (int j = 0; j < 8; j++){ //count = 1 to 8 + if (crc & 0x0001){ //if the least significant bit of the CRC is one + crc >>= 1; //right shift the CRC one bit + crc ^= POLY; //set CRC equal to the exclusive OR of POLY and itself + } + else { + crc >>= 1; //right shift the CRC one bit + } + } + } + crcStr[0] = (char)( 0x0040 | (crc >> 12)); + crcStr[1] = (char)( 0x0040 | ((crc >> 6) & 0x003F)); + crcStr[2] = (char)( 0x0040 | (crc & 0x003F)); + return (String(crcStr[0]) + String(crcStr[1]) + String(crcStr[2])); +} + +#endif //USE_CRC + /* ================ Interrupt Service Routine =======================================*/ diff --git a/src/SDI12.h b/src/SDI12.h index ec949e3..c749e5b 100644 --- a/src/SDI12.h +++ b/src/SDI12.h @@ -909,6 +909,12 @@ class SDI12 : public Stream { void sendCommand(const char* cmd, int8_t extraWakeTime = SDI12_WAKE_DELAY); /// @copydoc SDI12::sendCommand(String&, int8_t) void sendCommand(FlashString cmd, int8_t extraWakeTime = SDI12_WAKE_DELAY); + #ifdef USE_CRC + String addCRCResponse(String &resp); // Add CRC to the resp string (for slave use) + char * addCRCResponse( char *resp); // Add CRC to the resp string (for slave use) + String addCRCResponse(FlashString resp); // Add CRC to the resp string (for slave use) + String calculateCRC(String &resp); // Calculate the CRC for a response + #endif /** * @brief Send a response out on the data line (for slave use) From 793ed697b8effe0defe32ff355ad796757c6f477 Mon Sep 17 00:00:00 2001 From: DalesLandNet Date: Tue, 13 Jul 2021 11:34:45 +0100 Subject: [PATCH 6/8] [Issue #79] ESP32- Core panic when using in conjunction with the "preferences" library. --- src/SDI12.cpp | 12 +++--------- src/SDI12_boards.cpp | 10 ++++++++++ src/SDI12_boards.h | 8 ++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/SDI12.cpp b/src/SDI12.cpp index 8b50599..aff0995 100644 --- a/src/SDI12.cpp +++ b/src/SDI12.cpp @@ -652,25 +652,19 @@ String SDI12::calculateCRC(String &resp){ // Passes off responsibility for the interrupt to the active object. // On espressif boards (ESP8266 and ESP32), the ISR must be stored in IRAM -#if defined(ESP32) || defined(ESP8266) -void ICACHE_RAM_ATTR SDI12::handleInterrupt() { +void USE_INSTRUCTION_RAM SDI12::handleInterrupt(){ if (_activeObject) _activeObject->receiveISR(); } -#else -void SDI12::handleInterrupt() { - if (_activeObject) _activeObject->receiveISR(); -} -#endif // Creates a blank slate of bits for an incoming character -void SDI12::startChar() { +void USE_INSTRUCTION_RAM SDI12::startChar() { rxState = 0x00; // 0b00000000, got a start bit rxMask = 0x01; // 0b00000001, bit mask, lsb first rxValue = 0x00; // 0b00000000, RX character to be, a blank slate } // startChar // The actual interrupt service routine -void SDI12::receiveISR() { +void USE_INSTRUCTION_RAM SDI12::receiveISR() { // time of this data transition (plus ISR latency) sdi12timer_t thisBitTCNT = READTIME; diff --git a/src/SDI12_boards.cpp b/src/SDI12_boards.cpp index beb28e8..91c0cf5 100644 --- a/src/SDI12_boards.cpp +++ b/src/SDI12_boards.cpp @@ -262,6 +262,7 @@ void SDI12Timer::resetSDI12TimerPrescale(void) { while (GCLK->STATUS.bit.SYNCBUSY) {} // Wait for synchronization } +<<<<<<< Upstream, based on fd9699b390edeac3a8681e2a6d4fe2ba8b1f9a51 // Espressif ESP32/ESP8266 boards // #elif defined(ESP32) || defined(ESP8266) @@ -273,6 +274,15 @@ sdi12timer_t SDI12Timer::SDI12TimerRead(void) { // 6 return ((sdi12timer_t)(micros() >> 6)); } +======= + void SDI12Timer::configSDI12TimerPrescale(void) { } + void SDI12Timer::resetSDI12TimerPrescale(void) { } + sdi12timer_t USE_INSTRUCTION_RAM SDI12Timer::SDI12TimerRead(void) + { + // Its a one microsecond clock but we want 64uS ticks so divide by 64 i.e. right shift 6 + return((sdi12timer_t) (micros() >> 6)); + } +>>>>>>> f543a69 [Issue #79] ESP32- Core panic when using in conjunction with the "preferences" library. // Unknown board #else #error "Please define your board timer and pins" diff --git a/src/SDI12_boards.h b/src/SDI12_boards.h index a2a533e..a0d8ff4 100644 --- a/src/SDI12_boards.h +++ b/src/SDI12_boards.h @@ -26,6 +26,14 @@ typedef uint32_t sdi12timer_t; typedef uint8_t sdi12timer_t; #endif +#if defined(ESP32) +#define USE_INSTRUCTION_RAM IRAM_ATTR +#elif defined(ESP8266) +#define USE_INSTRUCTION_RAM ICACHE_RAM_ATTR +#else +#define USE_INSTRUCTION_RAM +#endif + /** * @brief The class used to define the processor timer for the SDI-12 serial emulation. */ From 14c5182e9e4e750d2869057dae54c1c56779d0c6 Mon Sep 17 00:00:00 2001 From: DalesLandNet Date: Wed, 14 Jul 2021 15:19:49 +0100 Subject: [PATCH 7/8] Update with suggestions from the pull request --- src/SDI12_boards.cpp | 11 ++--------- src/SDI12_boards.h | 1 + 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/SDI12_boards.cpp b/src/SDI12_boards.cpp index 91c0cf5..260247d 100644 --- a/src/SDI12_boards.cpp +++ b/src/SDI12_boards.cpp @@ -262,7 +262,6 @@ void SDI12Timer::resetSDI12TimerPrescale(void) { while (GCLK->STATUS.bit.SYNCBUSY) {} // Wait for synchronization } -<<<<<<< Upstream, based on fd9699b390edeac3a8681e2a6d4fe2ba8b1f9a51 // Espressif ESP32/ESP8266 boards // #elif defined(ESP32) || defined(ESP8266) @@ -272,17 +271,11 @@ void SDI12Timer::resetSDI12TimerPrescale(void) {} sdi12timer_t SDI12Timer::SDI12TimerRead(void) { // Its a one microsecond clock but we want 64uS ticks so divide by 64 i.e. right shift // 6 - return ((sdi12timer_t)(micros() >> 6)); -} -======= - void SDI12Timer::configSDI12TimerPrescale(void) { } - void SDI12Timer::resetSDI12TimerPrescale(void) { } sdi12timer_t USE_INSTRUCTION_RAM SDI12Timer::SDI12TimerRead(void) { // Its a one microsecond clock but we want 64uS ticks so divide by 64 i.e. right shift 6 - return((sdi12timer_t) (micros() >> 6)); - } ->>>>>>> f543a69 [Issue #79] ESP32- Core panic when using in conjunction with the "preferences" library. + return ((sdi12timer_t)(micros() >> 6)); +} // Unknown board #else #error "Please define your board timer and pins" diff --git a/src/SDI12_boards.h b/src/SDI12_boards.h index a0d8ff4..0ce053f 100644 --- a/src/SDI12_boards.h +++ b/src/SDI12_boards.h @@ -26,6 +26,7 @@ typedef uint32_t sdi12timer_t; typedef uint8_t sdi12timer_t; #endif +// On espressif boards (ESP8266 and ESP32), the ISR must be stored in IRAM #if defined(ESP32) #define USE_INSTRUCTION_RAM IRAM_ATTR #elif defined(ESP8266) From 014d58fb8bbd5fccd0c12cae906c0f945a1e7d3f Mon Sep 17 00:00:00 2001 From: DalesLandNet Date: Mon, 19 Jul 2021 10:05:28 +0100 Subject: [PATCH 8/8] Change depricated symbol ICACHE_RAM_ATTR that had been used for the ESP8266 this is now the same as the ESP32 --- src/SDI12_boards.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/SDI12_boards.h b/src/SDI12_boards.h index f918959..f5e044a 100644 --- a/src/SDI12_boards.h +++ b/src/SDI12_boards.h @@ -19,23 +19,17 @@ sensors. This library provides a general software solution, without requiring #include #if defined(ESP32) || defined(ESP8266) +// On espressif boards (ESP8266 and ESP32), the ISR must be stored in IRAM +#define ESPFAMILY_USE_INSTRUCTION_RAM IRAM_ATTR /** The interger type (size) of the timer return value */ typedef uint32_t sdi12timer_t; -#else -/** The interger type (size) of the timer return value */ -typedef uint8_t sdi12timer_t; -#endif -// On espressif boards (ESP8266 and ESP32), the ISR must be stored in IRAM -#if defined(ESP32) -#define ESPFAMILY_USE_INSTRUCTION_RAM IRAM_ATTR -#elif defined(ESP8266) -#define ESPFAMILY_USE_INSTRUCTION_RAM ICACHE_RAM_ATTR #else #define ESPFAMILY_USE_INSTRUCTION_RAM +/** The interger type (size) of the timer return value */ +typedef uint8_t sdi12timer_t; #endif - /** * @brief The class used to define the processor timer for the SDI-12 serial emulation. */