Added initial support for ESP8266 as WiFi gateway for MySensors

This commit is contained in:
Yveaux
2015-08-26 12:00:29 +02:00
parent 01c86cf53e
commit 64e28005b4
17 changed files with 694 additions and 26 deletions
+4
View File
@@ -1,3 +1,5 @@
#if defined(__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(__AVR__)
+28 -19
View File
@@ -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[0];
#else
return pgm_read_byte(&child_pipe_enable[0]);
#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));
}
/****************************************************************************/
+7 -4
View File
@@ -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 <avr/pgmspace.h>
#define PRIPSTR "%S"
#else
#if defined(ESP8266)
#include <pgmspace.h>
#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
+4
View File
@@ -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();
}
+4
View File
@@ -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(ESP8266)
// TODO !!
#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
+6
View File
@@ -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);
+1 -1
View File
@@ -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
+6 -2
View File
@@ -1,6 +1,10 @@
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#if defined(__AVR__)
#include <avr/pgmspace.h>
#define PRIPSTR "%S"
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include "sha256.h"