diff --git a/libraries/MySensors/MyHwATMega328.cpp b/libraries/MySensors/MyHwATMega328.cpp index 42517db..7d67563 100644 --- a/libraries/MySensors/MyHwATMega328.cpp +++ b/libraries/MySensors/MyHwATMega328.cpp @@ -17,6 +17,8 @@ * version 2 as published by the Free Software Foundation. */ +#ifdef ARDUINO_ARCH_AVR + #include "MyHw.h" #include "MyHwATMega328.h" @@ -215,3 +217,4 @@ void MyHwATMega328::debugPrint(bool isGW, const char *fmt, ... ) { } #endif +#endif // #ifdef ARDUINO_ARCH_AVR diff --git a/libraries/MySensors/MyHwATMega328.h b/libraries/MySensors/MyHwATMega328.h index 9e42b55..e18b72e 100644 --- a/libraries/MySensors/MyHwATMega328.h +++ b/libraries/MySensors/MyHwATMega328.h @@ -20,6 +20,8 @@ #ifndef MyHwATMega328_h #define MyHwATMega328_h +#ifdef ARDUINO_ARCH_AVR + #include "MyHw.h" #include "MyConfig.h" #include "MyMessage.h" @@ -116,3 +118,4 @@ private: void internalSleep(unsigned long ms); }; #endif +#endif // #ifdef ARDUINO_ARCH_AVR diff --git a/libraries/MySensors/MyHwESP8266.cpp b/libraries/MySensors/MyHwESP8266.cpp new file mode 100644 index 0000000..9c85f62 --- /dev/null +++ b/libraries/MySensors/MyHwESP8266.cpp @@ -0,0 +1,184 @@ +/** + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2015 Sensnology AB + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + */ + +#ifdef ARDUINO_ARCH_ESP8266 + +#include "MyHw.h" +#include "MyHwESP8266.h" +#include + +/* +int8_t pinIntTrigger = 0; +void wakeUp() //place to send the interrupts +{ + pinIntTrigger = 1; +} +void wakeUp2() //place to send the second interrupts +{ + pinIntTrigger = 2; +} + +// Watchdog Timer interrupt service routine. This routine is required +// to allow automatic WDIF and WDIE bit clearance in hardware. +ISR (WDT_vect) +{ + // WDIE & WDIF is cleared in hardware upon entering this ISR + wdt_disable(); +} +*/ + +static void hw_initConfigBlock( size_t length = 1024 /*ATMega328 has 1024 bytes*/ ) +{ + static bool initDone = false; + if (!initDone) + { + EEPROM.begin(length); + initDone = true; + } +} + +void hw_readConfigBlock(void* buf, void* adr, size_t length) +{ + hw_initConfigBlock(); + uint8_t* dst = static_cast(buf); + int offs = reinterpret_cast(adr); + while (length-- > 0) + { + *dst++ = EEPROM.read(offs++); + } +} + +void hw_writeConfigBlock(void* buf, void* adr, size_t length) +{ + hw_initConfigBlock(); + uint8_t* src = static_cast(buf); + int offs = reinterpret_cast(adr); + while (length-- > 0) + { + EEPROM.write(offs++, *src++); + } + EEPROM.commit(); +} + +uint8_t hw_readConfig(int adr) +{ + uint8_t value; + hw_readConfigBlock(&value, reinterpret_cast(adr), 1); + return value; +} + +void hw_writeConfig(int adr, uint8_t value) +{ + uint8_t curr = hw_readConfig(adr); + if (curr != value) + { + hw_writeConfigBlock(&value, reinterpret_cast(adr), 1); + } +} + + + +MyHwESP8266::MyHwESP8266() : MyHw() +{ +} + + +/* + +// The following was redifined as macros to save space + +inline uint8_t MyHwATMega328::readConfig(uint8_t pos) { + return eeprom_read_byte((uint8_t*)pos); +} + +inline void MyHwATMega328::writeConfig(uint8_t pos, uint8_t value) { + eeprom_update_byte((uint8_t*)pos, value); +} + +inline void MyHwATMega328::readConfigBlock(void* buf, void * pos, size_t length) { + eeprom_read_block(buf, (void*)pos, length); +} + +inline void MyHwATMega328::writeConfigBlock(void* pos, void* buf, size_t length) { + eeprom_write_block((void*)pos, (void*)buf, length); +} + + +inline void MyHwATMega328::init() { + Serial.begin(BAUD_RATE); +} + +inline void MyHwATMega328::watchdogReset() { + wdt_reset(); +} + +inline void MyHwATMega328::reboot() { + wdt_enable(WDTO_15MS); for (;;); +} + +inline unsigned long MyHwATMega328::millis() { + return ::millis(); +} +*/ + + +void MyHwESP8266::sleep(unsigned long ms) { + // TODO: Not supported! +} + +bool MyHwESP8266::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) { + // TODO: Not supported! + return false; +} + +inline uint8_t MyHwESP8266::sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) { + // TODO: Not supported! + return 0; +} + + + +#ifdef DEBUG +void MyHwESP8266::debugPrint(bool isGW, const char *fmt, ... ) { + char fmtBuffer[300]; + if (isGW) { + // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE) + snprintf_P(fmtBuffer, 299, PSTR("0;0;%d;0;%d;"), C_INTERNAL, I_LOG_MESSAGE); + Serial.print(fmtBuffer); + } + va_list args; + va_start (args, fmt ); + va_end (args); + if (isGW) { + // Truncate message if this is gateway node + vsnprintf_P(fmtBuffer, 60, fmt, args); + fmtBuffer[59] = '\n'; + fmtBuffer[60] = '\0'; + } else { + vsnprintf_P(fmtBuffer, 299, fmt, args); + } + va_end (args); + Serial.print(fmtBuffer); + Serial.flush(); + + //Serial.write(freeRam()); +} +#endif + +#endif // #ifdef ARDUINO_ARCH_ESP8266 diff --git a/libraries/MySensors/MyHwESP8266.h b/libraries/MySensors/MyHwESP8266.h new file mode 100644 index 0000000..e281421 --- /dev/null +++ b/libraries/MySensors/MyHwESP8266.h @@ -0,0 +1,86 @@ +/** + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2015 Sensnology AB + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + */ + +#ifdef ARDUINO_ARCH_ESP8266 + +#ifndef MyHwESP8266_h +#define MyHwESP8266_h + +#include "MyHw.h" +#include "MyConfig.h" +#include "MyMessage.h" + + +#ifdef __cplusplus +#include +#include +#endif + +// Define these as macros to save valuable space + +#define hw_digitalWrite(__pin, __value) (digitalWrite(__pin, __value)) +#define hw_init() Serial.begin(BAUD_RATE) +#define hw_watchdogReset() wdt_reset() +#define hw_reboot() wdt_enable(WDTO_15MS); while (1) +#define hw_millis() millis() + +void hw_readConfigBlock(void* buf, void* adr, size_t length); +void hw_writeConfigBlock(void* buf, void* adr, size_t length); +void hw_writeConfig(int adr, uint8_t value); +uint8_t hw_readConfig(int adr); + +enum period_t +{ + SLEEP_15Ms, + SLEEP_30MS, + SLEEP_60MS, + SLEEP_120MS, + SLEEP_250MS, + SLEEP_500MS, + SLEEP_1S, + SLEEP_2S, + SLEEP_4S, + SLEEP_8S, + SLEEP_FOREVER +}; + +class MyHwESP8266 : public MyHw +{ +public: + MyHwESP8266(); + +/* void init(); + void watchdogReset(); + void reboot(); + unsigned long millis(); + uint8_t readConfig(uint8_t pos); + void writeConfig(uint8_t pos, uint8_t value); + void readConfigBlock(void* buf, void * pos, size_t length); + void writeConfigBlock(void* pos, void* buf, size_t length); */ + + void sleep(unsigned long ms); + bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms); + uint8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms); +#ifdef DEBUG + void debugPrint(bool isGW, const char *fmt, ... ); +#endif +}; +#endif + +#endif // #ifdef ARDUINO_ARCH_ESP8266 diff --git a/libraries/MySensors/MyMessage.h b/libraries/MySensors/MyMessage.h index 6c7c471..d3be005 100644 --- a/libraries/MySensors/MyMessage.h +++ b/libraries/MySensors/MyMessage.h @@ -163,7 +163,9 @@ typedef enum { +#ifndef BIT #define BIT(n) ( 1<<(n) ) +#endif // Create a bitmask of length len. #define BIT_MASK(len) ( BIT(len)-1 ) // Create a bitfield mask of length starting at bit 'start'. diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index dc784c8..6c3a348 100644 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -95,14 +95,12 @@ bool MySensor::isValidFirmware() { #ifdef WITH_LEDS_BLINKING void MySensor::handleLedsBlinking() { - static unsigned long next_time = hw_millis() + ledBlinkPeriod; - // Just return if it is not the time... // http://playground.arduino.cc/Code/TimingRollover - if ((long)(hw_millis() - next_time) < 0) + if ((long)(hw_millis() - blink_next_time) < 0) return; else - next_time = hw_millis() + ledBlinkPeriod; + blink_next_time = hw_millis() + ledBlinkPeriod; // do the actual blinking if(countRx && countRx != 255) { diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index 3577693..18f4cb7 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -41,8 +41,13 @@ // Set the hardware driver to use (initialized by MySensor-class) //#if defined __AVR_ATmega328P__ +#if defined(ARDUINO_ARCH_ESP8266) +#include "MyHwESP8266.h" +typedef MyHwESP8266 MyHwDriver; +#elif defined(ARDUINO_ARCH_AVR) #include "MyHwATMega328.h" typedef MyHwATMega328 MyHwDriver; +#endif //#endif @@ -356,6 +361,7 @@ class MySensor unsigned long ledBlinkPeriod; void handleLedsBlinking(); // do the actual blinking + unsigned long blink_next_time; #endif MyTransport& radio; diff --git a/libraries/MySensors/MySigningAtsha204.cpp b/libraries/MySensors/MySigningAtsha204.cpp index 29c4d59..06d1b45 100644 --- a/libraries/MySensors/MySigningAtsha204.cpp +++ b/libraries/MySensors/MySigningAtsha204.cpp @@ -24,6 +24,7 @@ * HMAC-SHA256 authentication with a readout-protected key. * */ +#if defined(ARDUINO_ARCH_AVR) #include "MySigning.h" #include "MySigningAtsha204.h" @@ -266,3 +267,4 @@ uint8_t* MySigningAtsha204::sha256(const uint8_t* data, size_t sz) { DEBUG_SIGNING_PRINTBUF(F("SHA:"), &rx_buffer[SHA204_BUFFER_POS_DATA], 32); return &rx_buffer[SHA204_BUFFER_POS_DATA]; } +#endif // #if defined(ARDUINO_ARCH_AVR) diff --git a/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino b/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino new file mode 100644 index 0000000..c5be624 --- /dev/null +++ b/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino @@ -0,0 +1,226 @@ +/** + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2015 Sensnology AB + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik EKblad + * Contribution by a-lurker and Anticimex, + * Contribution by Norbert Truchsess + * Contribution by Ivo Pullens (ESP8266 support) + * + * DESCRIPTION + * The EthernetGateway sends data received from sensors to the WiFi link. + * The gateway also accepts input on ethernet interface, which is then sent out to the radio network. + * + * VERA CONFIGURATION: + * Enter "ip-number:port" in the ip-field of the Arduino GW device. This will temporarily override any serial configuration for the Vera plugin. + * E.g. If you want to use the defualt values in this sketch enter: 192.168.178.66:5003 + * + * LED purposes: + * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h + * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved + * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly + * - ERR (red) - fast blink on error during transmission error or recieve crc error + * + * See http://www.mysensors.org/build/ethernet_gateway for wiring instructions. + * The ESP8266 however requires different wiring: + * nRF24L01+ ESP8266 + * VCC VCC + * CE GPIO4 + * CSN/CS GPIO15 + * SCK GPIO14 + * MISO GPIO12 + * MOSI GPIO13 + * + * Not all ESP8266 modules have all pins available on their external interface. + * This code has been tested on an ESP-12 module. + * The ESP8266 requires a certain pin configuration to download code, and another one to run code: + * - Connect REST (reset) via 10K pullup resistor to VCC, and via switch to GND ('reset switch') + * - Connect GPIO15 via 10K pulldown resistor to GND + * - Connect CH_PD via 10K resistor to VCC + * - Connect GPIO2 via 10K resistor to VCC + * - Connect GPIO0 via 10K resistor to VCC, and via switch to GND ('bootload switch') + * + * Inclusion mode button: + * - Connect GPIO5 via switch to GND ('inclusion switch') + * + * Hardware SHA204 signing is currently not supported! + * + * Make sure to fill in your ssid and WiFi password below for ssid & pass. + */ +#define NO_PORTB_PINCHANGES + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include "GatewayUtil.h" + +const char *ssid = "MySSID"; // cannot be longer than 32 characters! +const char *pass = "MyVerySecretPassword"; // + +#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled +#define INCLUSION_MODE_PIN 5 // Digital pin used for inclusion mode button + +#define RADIO_CE_PIN 4 // radio chip enable +#define RADIO_SPI_SS_PIN 15 // radio SPI serial select + +#ifdef WITH_LEDS_BLINKING +#define RADIO_ERROR_LED_PIN 7 // Error led pin +#define RADIO_RX_LED_PIN 8 // Receive led pin +#define RADIO_TX_LED_PIN 9 // the PCB, on board LED +#endif + + +// NRFRF24L01 radio driver (set low transmit power by default) +MyTransportNRF24 transport(RADIO_CE_PIN, RADIO_SPI_SS_PIN, RF24_PA_LEVEL_GW); +//MyTransportRFM69 transport; + + +// Message signing driver (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h) +#ifdef MY_SIGNING_FEATURE +MySigningNone signer; +//MySigningAtsha204Soft signer; +#endif + +// Hardware profile +MyHwESP8266 hw; + +// Construct MySensors library (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h) +// To use LEDs blinking, uncomment WITH_LEDS_BLINKING in MyConfig.h +MySensor gw(transport, hw +#ifdef MY_SIGNING_FEATURE + , signer +#endif +#ifdef WITH_LEDS_BLINKING + , RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN +#endif + ); + + +#define IP_PORT 5003 // The port you want to open + +// a R/W server on the port +WiFiServer server(IP_PORT); +// handle to open connection +WiFiClient client; + +char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface +int inputPos = 0; +bool sentReady = false; + +void output(const char *fmt, ... ) { + va_list args; + va_start (args, fmt ); + vsnprintf_P(serialBuffer, MAX_SEND_LENGTH, fmt, args); + va_end (args); + Serial.print(serialBuffer); + server.write(serialBuffer); +} + +void setup() +{ + // Setup console + hw_init(); + + Serial.println(); Serial.println(); + Serial.println("ESP8266 MySensors Gateway"); + Serial.print("Connecting to "); Serial.println(ssid); + + (void)WiFi.begin(ssid, pass); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("Connected!"); + Serial.print("IP: "); Serial.println(WiFi.localIP()); + Serial.flush(); + + setupGateway(INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output); + + // Initialize gateway at maximum PA level, channel 70 and callback for write operations + gw.begin(incomingMessage, 0, true, 0); + + // start listening for clients + server.begin(); +} + + +void loop() { + gw.process(); + + checkButtonTriggeredInclusion(); + checkInclusionFinished(); + + //check if there are any new clients + if (server.hasClient()) + { + if (client) + { + client.stop(); + } + client = server.available(); + output(PSTR("0;0;%d;0;%d;Gateway startup complete.\n"), C_INTERNAL, I_GATEWAY_READY); + } + + if (client) { + if (!client.connected()) { + client.stop(); + } else if (client.available()) { + // read the bytes incoming from the client + char inChar = client.read(); + if (inputPos60000UL*inclusionTime) { + // inclusionTimeInMinutes minute(s) has passed.. stop inclusion mode + setInclusionMode(false); + } +} + +void parseAndSend(MySensor &gw, char *commandBuffer) { + boolean ok; + MyMessage &msg = gw.getLastMessage(); + + if (parser.parse(msg, commandBuffer)) { + uint8_t command = mGetCommand(msg); + + if (msg.destination==GATEWAY_ADDRESS && command==C_INTERNAL) { + // Handle messages directed to gateway + if (msg.type == I_VERSION) { + // Request for version + serial(PSTR("0;0;%d;0;%d;%s\n"), C_INTERNAL, I_VERSION, LIBRARY_VERSION); + } else if (msg.type == I_INCLUSION_MODE) { + // Request to change inclusion mode + setInclusionMode(atoi(msg.data) == 1); + } + } else { + #ifdef WITH_LEDS_BLINKING + gw.txBlink(1); + #endif + ok = gw.sendRoute(msg); + if (!ok) { + #ifdef WITH_LEDS_BLINKING + gw.errBlink(1); + #endif + } + } + } +} + +void setInclusionMode(boolean newMode) { + if (newMode != inclusionMode) { + inclusionMode = newMode; + // Send back mode change on serial line to ack command + serial(PSTR("0;0;%d;0;%d;%d\n"), C_INTERNAL, I_INCLUSION_MODE, inclusionMode?1:0); + + if (inclusionMode) { + inclusionStartTime = millis(); + } + } +} + + +#else +#error This example is only for use on Arduino. +#endif // ARDUINO + +#endif diff --git a/libraries/MySensors/utility/ATSHA204.cpp b/libraries/MySensors/utility/ATSHA204.cpp index f639bab..113038f 100644 --- a/libraries/MySensors/utility/ATSHA204.cpp +++ b/libraries/MySensors/utility/ATSHA204.cpp @@ -1,3 +1,5 @@ +#if defined(ARDUINO_ARCH_AVR) + #include "Arduino.h" #include "ATSHA204.h" @@ -572,3 +574,5 @@ uint8_t ATSHA204Class::sha204c_check_crc(uint8_t *response) return (crc[0] == response[count] && crc[1] == response[count + 1]) ? SHA204_SUCCESS : SHA204_BAD_CRC; } + +#endif // defined(ARDUINO_ARCH_AVR) \ No newline at end of file diff --git a/libraries/MySensors/utility/RF24.cpp b/libraries/MySensors/utility/RF24.cpp index a0d281f..7be6b98 100644 --- a/libraries/MySensors/utility/RF24.cpp +++ b/libraries/MySensors/utility/RF24.cpp @@ -498,7 +498,7 @@ void RF24::begin(void) // Reset current status // Notice reset and flush is the last thing we do - write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); + write_register(RF24_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); // Set up default configuration. Callers can always change it later. // This channel should be universally safe and not bleed over into adjacent @@ -525,7 +525,7 @@ void RF24::startListening(void) powerUp(); #endif write_register(CONFIG, read_register(CONFIG) | _BV(PRIM_RX)); - write_register(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); + write_register(RF24_STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); // Restore the pipe0 adddress, if exists if (pipe0_reading_address[0] > 0){ @@ -546,22 +546,31 @@ void RF24::startListening(void) } /****************************************************************************/ -static const uint8_t child_pipe_enable[] PROGMEM = +static uint8_t get_child_pipe_mask(const uint8_t idx) { - ERX_P0, ERX_P1, ERX_P2, ERX_P3, ERX_P4, ERX_P5 -}; + static const uint8_t child_pipe_enable[] PROGMEM = + { + _BV(ERX_P0), _BV(ERX_P1), _BV(ERX_P2), _BV(ERX_P3), _BV(ERX_P4), _BV(ERX_P5) + }; +#ifdef ESP8266 + return child_pipe_enable[idx]; +#else + return pgm_read_byte(&child_pipe_enable[idx]); +#endif +} void RF24::stopListening(void) { - ce(LOW); #if defined(__arm__) delayMicroseconds(300); #endif delayMicroseconds(130); + if(read_register(FEATURE) & _BV(EN_ACK_PAY)){ flush_tx(); } + //flush_rx(); write_register(CONFIG, ( read_register(CONFIG) ) & ~_BV(PRIM_RX) ); @@ -569,13 +578,13 @@ void RF24::stopListening(void) // for 3 pins solution TX mode is only left with additonal powerDown/powerUp cycle if (ce_pin == csn_pin) { powerDown(); - powerUp(); + powerUp(); } #endif - write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[0]))); // Enable RX on pipe0 + + write_register(EN_RXADDR,read_register(EN_RXADDR) | get_child_pipe_mask(0)); // Enable RX on pipe0 delayMicroseconds(100); - } /****************************************************************************/ @@ -636,7 +645,7 @@ bool RF24::write( const void* buf, uint8_t len, const bool multicast ) ce(LOW); - uint8_t status = write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); + uint8_t status = write_register(RF24_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); //Max retries exceeded if( status & _BV(MAX_RT)){ @@ -686,7 +695,7 @@ bool RF24::writeBlocking( const void* buf, uint8_t len, uint32_t timeout ) /****************************************************************************/ void RF24::reUseTX(){ - write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag + write_register(RF24_STATUS,_BV(MAX_RT) ); //Clear max retry flag spiTrans( REUSE_TX_PL ); ce(LOW); //Re-Transfer packet ce(HIGH); @@ -709,7 +718,7 @@ bool RF24::writeFast( const void* buf, uint8_t len, const bool multicast ) if( get_status() & _BV(MAX_RT)){ //reUseTX(); //Set re-transmit - write_register(STATUS,_BV(MAX_RT) ); //Clear max retry flag + write_register(RF24_STATUS,_BV(MAX_RT) ); //Clear max retry flag return 0; //Return 0. The previous payload has been retransmitted //From the user perspective, if you get a 0, just keep trying to send the same payload } @@ -777,7 +786,7 @@ bool RF24::txStandBy(){ #endif while( ! (read_register(FIFO_STATUS) & _BV(TX_EMPTY)) ){ if( get_status() & _BV(MAX_RT)){ - write_register(STATUS,_BV(MAX_RT) ); + write_register(RF24_STATUS,_BV(MAX_RT) ); ce(LOW); flush_tx(); //Non blocking, flush the data return 0; @@ -802,7 +811,7 @@ bool RF24::txStandBy(uint32_t timeout){ while( ! (read_register(FIFO_STATUS) & _BV(TX_EMPTY)) ){ if( get_status() & _BV(MAX_RT)){ - write_register(STATUS,_BV(MAX_RT) ); + write_register(RF24_STATUS,_BV(MAX_RT) ); ce(LOW); //Set re-transmit ce(HIGH); if(millis() - start >= timeout){ @@ -891,7 +900,7 @@ void RF24::read( void* buf, uint8_t len ){ read_payload( buf, len ); //Clear the two possible interrupt flags with one command - write_register(STATUS,_BV(RX_DR) | _BV(MAX_RT) | _BV(TX_DS) ); + write_register(RF24_STATUS,_BV(RX_DR) | _BV(MAX_RT) | _BV(TX_DS) ); } @@ -901,7 +910,7 @@ void RF24::whatHappened(bool& tx_ok,bool& tx_fail,bool& rx_ready) { // Read the status & reset the status in one easy call // Or is that such a good idea? - uint8_t status = write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); + uint8_t status = write_register(RF24_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); // Report to the user what happened tx_ok = status & _BV(TX_DS); @@ -972,7 +981,7 @@ void RF24::openReadingPipe(uint8_t child, uint64_t address) // Note it would be more efficient to set all of the bits for all open // pipes at once. However, I thought it would make the calling code // more simple to do it this way. - write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[child]))); + write_register(EN_RXADDR,read_register(EN_RXADDR) | get_child_pipe_mask(child)); } } @@ -1009,7 +1018,7 @@ void RF24::openReadingPipe(uint8_t child, const uint8_t *address) // Note it would be more efficient to set all of the bits for all open // pipes at once. However, I thought it would make the calling code // more simple to do it this way. - write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[child]))); + write_register(EN_RXADDR,read_register(EN_RXADDR) | get_child_pipe_mask(child)); } } @@ -1018,7 +1027,7 @@ void RF24::openReadingPipe(uint8_t child, const uint8_t *address) void RF24::closeReadingPipe( uint8_t pipe ) { - write_register(EN_RXADDR,read_register(EN_RXADDR) & ~_BV(pgm_read_byte(&child_pipe_enable[pipe]))); + write_register(EN_RXADDR,read_register(EN_RXADDR) & ~get_child_pipe_mask(pipe)); } /****************************************************************************/ diff --git a/libraries/MySensors/utility/RF24_config.h b/libraries/MySensors/utility/RF24_config.h index 2addc27..d38ac88 100644 --- a/libraries/MySensors/utility/RF24_config.h +++ b/libraries/MySensors/utility/RF24_config.h @@ -87,7 +87,7 @@ // Avoid spurious warnings // Arduino DUE is arm and uses traditional PROGMEM constructs #if 1 -#if ! defined( NATIVE ) && defined( ARDUINO ) && ! defined(__arm__) && ! defined( CORE_TEENSY3 ) +#if ! defined( NATIVE ) && defined( ARDUINO ) && ! defined(__arm__) && ! defined( CORE_TEENSY3 ) && ! defined(ESP8266) #undef PROGMEM #define PROGMEM __attribute__(( section(".progmem.data") )) #undef PSTR @@ -97,18 +97,21 @@ // Progmem is Arduino-specific // Arduino DUE is arm and does not include avr/pgmspace -#if defined(ARDUINO) && ! defined(__arm__) +#if defined(__AVR__) #include #define PRIPSTR "%S" #else +#if defined(ESP8266) +#include +#else #if ! defined(ARDUINO) // This doesn't work on Arduino DUE typedef char const char; #else // Fill in pgm_read_byte that is used, but missing from DUE #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) #endif +#endif - -#if !defined ( CORE_TEENSY ) +#if !defined ( CORE_TEENSY ) && ! defined(ESP8266) typedef uint16_t prog_uint16_t; #define PSTR(x) (x) #define printf_P printf diff --git a/libraries/MySensors/utility/RFM69.cpp b/libraries/MySensors/utility/RFM69.cpp index 4663480..56e47e5 100644 --- a/libraries/MySensors/utility/RFM69.cpp +++ b/libraries/MySensors/utility/RFM69.cpp @@ -384,8 +384,10 @@ void RFM69::writeReg(byte addr, byte value) void RFM69::select() { noInterrupts(); //save current SPI settings +#ifndef ESP8266 _SPCR = SPCR; _SPSR = SPSR; +#endif //set RFM69 SPI settings SPI.setDataMode(SPI_MODE0); SPI.setBitOrder(MSBFIRST); @@ -397,8 +399,10 @@ void RFM69::select() { void RFM69::unselect() { digitalWrite(_slaveSelectPin, HIGH); //restore SPI settings to what they were before talking to RFM69 +#ifndef ESP8266 SPCR = _SPCR; SPSR = _SPSR; +#endif interrupts(); } diff --git a/libraries/MySensors/utility/RFM69.h b/libraries/MySensors/utility/RFM69.h index c3e8d05..fe7abd1 100644 --- a/libraries/MySensors/utility/RFM69.h +++ b/libraries/MySensors/utility/RFM69.h @@ -43,6 +43,10 @@ #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega2560__) #define RF69_IRQ_PIN 2 #define RF69_IRQ_NUM 2 +#elif defined(ARDUINO_ARCH_ESP8266) + // TODO !! Untested code! Entering unknown territory... + #define RF69_IRQ_PIN 2 + #define RF69_IRQ_NUM 0 #endif #define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access diff --git a/libraries/MySensors/utility/SPIFlash.cpp b/libraries/MySensors/utility/SPIFlash.cpp index e250c17..00e1601 100644 --- a/libraries/MySensors/utility/SPIFlash.cpp +++ b/libraries/MySensors/utility/SPIFlash.cpp @@ -56,8 +56,10 @@ void SPIFlash::select() { #ifndef SPI_HAS_TRANSACTION noInterrupts(); #endif +#ifndef ESP8266 _SPCR = SPCR; _SPSR = SPSR; +#endif #ifdef SPI_HAS_TRANSACTION SPI.beginTransaction(_settings); @@ -80,15 +82,19 @@ void SPIFlash::unselect() { #else interrupts(); #endif +#ifndef ESP8266 SPCR = _SPCR; SPSR = _SPSR; +#endif } /// setup SPI, read device ID etc... boolean SPIFlash::initialize() { +#ifndef ESP8266 _SPCR = SPCR; _SPSR = SPSR; +#endif pinMode(_slaveSelectPin, OUTPUT); #ifdef SPI_HAS_TRANSACTION _settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); diff --git a/libraries/MySensors/utility/nRF24L01.h b/libraries/MySensors/utility/nRF24L01.h index 132de94..02e1d4f 100644 --- a/libraries/MySensors/utility/nRF24L01.h +++ b/libraries/MySensors/utility/nRF24L01.h @@ -31,7 +31,7 @@ #define SETUP_RETR 0x04 #define RF_CH 0x05 #define RF_SETUP 0x06 -#define STATUS 0x07 +#define RF24_STATUS 0x07 #define OBSERVE_TX 0x08 #define CD 0x09 #define RX_ADDR_P0 0x0A diff --git a/libraries/MySensors/utility/sha256.cpp b/libraries/MySensors/utility/sha256.cpp index f0376ab..511d78a 100644 --- a/libraries/MySensors/utility/sha256.cpp +++ b/libraries/MySensors/utility/sha256.cpp @@ -1,6 +1,10 @@ #include -#include -#include +#if defined(__AVR__) + #include + #define PRIPSTR "%S" +#elif defined(ESP8266) +#include +#endif #include "sha256.h"