From 6d305e85d9ceaa4ba7bd00234695ca72e5eff4e4 Mon Sep 17 00:00:00 2001 From: Perotto Date: Thu, 28 Jun 2018 10:27:08 -0300 Subject: [PATCH 1/2] Utilizar memoria flash para tabela de CRC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Como a tabela de CRC consome muita memoria SRAM, sendo apenas leitura eu fiz modificações para que ela salve esta tabela na flash, poupando 20% de memória SRAM de um ATmega328P . Interessantemente também poupa memória FLASH. Para mais informações https://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html https://www.arduino.cc/reference/pt/language/variables/utilities/progmem/ --- libraries/ModbusSerial/ModbusSerial.h | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/libraries/ModbusSerial/ModbusSerial.h b/libraries/ModbusSerial/ModbusSerial.h index 5843817..d14ecd8 100644 --- a/libraries/ModbusSerial/ModbusSerial.h +++ b/libraries/ModbusSerial/ModbusSerial.h @@ -1,19 +1,23 @@ /* ModbusSerial.h - Header for ModbusSerial Library - Copyright (C) 2014 André Sarmento Barbosa + Copyright (C) 2014 André Sarmento Barbosa */ #include #include -#ifndef MODBUSSERIAL_H -#define MODBUSSERIAL_H //#define USE_SOFTWARE_SERIAL +#define USE_FLASH_PROGMEM + #ifdef USE_SOFTWARE_SERIAL #include #endif +#ifdef USE_FLASH_PROGMEM +#include +#endif + class ModbusSerial : public Modbus { private: Stream* _port; @@ -41,8 +45,14 @@ class ModbusSerial : public Modbus { bool send(byte* frame); }; -/* Table of CRC values for high–order byte */ -const byte _auchCRCHi[] = { +/* Table of CRC values for high–order byte */ +#ifndef USE_FLASH_PROGMEM + const byte _auchCRCHi[] = { +#endif + +#ifdef USE_FLASH_PROGMEM + const byte _auchCRCHi[] PROGMEM = { +#endif 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, @@ -62,8 +72,14 @@ const byte _auchCRCHi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40}; -/* Table of CRC values for low–order byte */ -const byte _auchCRCLo[] = { +/* Table of CRC values for low–order byte */ +#ifndef USE_FLASH_PROGMEM + const byte _auchCRCLo[] = { +#endif + +#ifdef USE_FLASH_PROGMEM + const byte _auchCRCLo[] PROGMEM = { +#endif 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, @@ -83,4 +99,3 @@ const byte _auchCRCLo[] = { 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40}; -#endif //MODBUSSERIAL_H From 3c3680fa0bdab6ceb6e0529bf81639d6d88723de Mon Sep 17 00:00:00 2001 From: Perotto Date: Thu, 28 Jun 2018 11:05:28 -0300 Subject: [PATCH 2/2] Update ModbusSerial.h --- libraries/ModbusSerial/ModbusSerial.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/ModbusSerial/ModbusSerial.h b/libraries/ModbusSerial/ModbusSerial.h index d14ecd8..e63fa95 100644 --- a/libraries/ModbusSerial/ModbusSerial.h +++ b/libraries/ModbusSerial/ModbusSerial.h @@ -5,6 +5,8 @@ #include #include +#ifndef MODBUSSERIAL_H +#define MODBUSSERIAL_H //#define USE_SOFTWARE_SERIAL #define USE_FLASH_PROGMEM