Introduce SPI transactions, harmonize variables
This commit is contained in:
@@ -334,6 +334,12 @@
|
||||
*/
|
||||
//#define MY_DEBUG_VERBOSE_RF24
|
||||
|
||||
/**
|
||||
* @def MY_RF24_SPI_MAX_SPEED
|
||||
* @brief MY_RF24_SPI_MAX_SPEED to overrule default nRF24L01+ SPI speed.
|
||||
*/
|
||||
//#define MY_RF24_SPI_MAX_SPEED 4000000
|
||||
|
||||
/**
|
||||
* @def MY_RF24_CE_PIN
|
||||
* @brief Default RF24 chip enable pin setting. Override in sketch if needed.
|
||||
|
||||
@@ -22,11 +22,9 @@
|
||||
#include "drivers/RF24/RF24.h"
|
||||
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
#include "drivers/AES/AES.h"
|
||||
#include "drivers/AES/AES.h"
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if defined(MY_RF24_ENABLE_ENCRYPTION)
|
||||
AES _aes;
|
||||
uint8_t _dataenc[32] = {0};
|
||||
|
||||
@@ -1,33 +1,29 @@
|
||||
/**
|
||||
* 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 <henrik.ekblad@mysensors.org>
|
||||
* 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.
|
||||
*
|
||||
* Based on maniacbug's RF24 library, copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
|
||||
* Driver refactored and optimized for speed and size by Olivier Mauti <olivier@mysensors.org>
|
||||
*/
|
||||
/*
|
||||
* 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 <henrik.ekblad@mysensors.org>
|
||||
* 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.
|
||||
*
|
||||
* Based on maniacbug's RF24 library, copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
|
||||
* RF24 driver refactored and optimized for speed and size, copyright (C) 2016 Olivier Mauti <olivier@mysensors.org>
|
||||
*/
|
||||
|
||||
|
||||
#include "RF24.h"
|
||||
|
||||
// static variables
|
||||
uint8_t MYS_RF24_base_addr[MY_RF24_ADDR_WIDTH] = { MY_RF24_BASE_RADIO_ID };
|
||||
uint8_t MYS_RF24_rxaddr_register;
|
||||
uint8_t MYS_RF24_nodeAddress = AUTO;
|
||||
|
||||
uint8_t MY_RF24_BASE_ADDR[MY_RF24_ADDR_WIDTH] = { MY_RF24_BASE_RADIO_ID };
|
||||
uint8_t MY_RF24_NODE_ADDRESS = AUTO;
|
||||
|
||||
void csn(bool level) {
|
||||
digitalWrite(MY_RF24_CS_PIN, level);
|
||||
@@ -37,39 +33,48 @@ void ce(bool level) {
|
||||
digitalWrite(MY_RF24_CE_PIN, level);
|
||||
}
|
||||
|
||||
uint8_t spiMultiByteTransfer(uint8_t cmd, uint8_t* buf, uint8_t len, boolean aReadMode) {
|
||||
uint8_t spiMultiByteTransfer(uint8_t cmd, uint8_t* buf, uint8_t len, bool aReadMode) {
|
||||
uint8_t* current = buf;
|
||||
#if !defined(MY_SOFTSPI)
|
||||
_SPI.beginTransaction(SPISettings(MY_RF24_SPI_MAX_SPEED, MY_RF24_SPI_DATA_ORDER, MY_RF24_SPI_DATA_MODE));
|
||||
#endif
|
||||
csn(LOW);
|
||||
// timing
|
||||
delayMicroseconds(10);
|
||||
uint8_t status = _SPI.transfer( cmd );
|
||||
while ( len-- ) {
|
||||
if (aReadMode) {
|
||||
status = _SPI.transfer( NOP );
|
||||
if(buf!=NULL) *current++ = status;
|
||||
if(buf != NULL) *current++ = status;
|
||||
} else status = _SPI.transfer(*current++);
|
||||
}
|
||||
csn(HIGH);
|
||||
#if !defined(MY_SOFTSPI)
|
||||
_SPI.endTransaction();
|
||||
#endif
|
||||
// timing
|
||||
delayMicroseconds(10);
|
||||
return status;
|
||||
}
|
||||
|
||||
#define readRegister(reg) _readRegister( R_REGISTER | ( REGISTER_MASK & (reg) ) )
|
||||
uint8_t _readRegister(uint8_t cmd) {
|
||||
uint8_t value = spiMultiByteTransfer(cmd, NULL, 1, true);
|
||||
uint8_t spiByteTransfer(uint8_t cmd) {
|
||||
return spiMultiByteTransfer( cmd, NULL, 0, false);
|
||||
}
|
||||
|
||||
uint8_t _readByteRegister(uint8_t cmd) {
|
||||
uint8_t value = spiMultiByteTransfer( cmd, NULL, 1, true);
|
||||
RF24_DEBUG(PSTR("read register, reg=%d, value=%d\n"), cmd & (R_REGISTER ^ 0xFF), value);
|
||||
return value;
|
||||
}
|
||||
|
||||
#define writeByteRegister(reg, value) _writeByteRegister( W_REGISTER | ( REGISTER_MASK & (reg) ), value )
|
||||
uint8_t _writeByteRegister(uint8_t cmd, uint8_t value) {
|
||||
RF24_DEBUG(PSTR("write register, reg=%d, value=%d\n"), cmd & (W_REGISTER ^ 0xFF), value);
|
||||
return spiMultiByteTransfer( cmd , &value, 1, false);
|
||||
}
|
||||
|
||||
#define readByteRegister(reg) _readByteRegister( R_REGISTER | ( REGISTER_MASK & (reg) ) )
|
||||
#define writeByteRegister(reg, value) _writeByteRegister( W_REGISTER | ( REGISTER_MASK & (reg) ), value )
|
||||
#define writeMultiByteRegister(reg, buf, len) spiMultiByteTransfer( W_REGISTER | ( REGISTER_MASK & (reg) ), (uint8_t*)buf, len, false )
|
||||
uint8_t spiByteTransfer(uint8_t cmd) {
|
||||
return spiMultiByteTransfer( cmd , NULL, 0, false);
|
||||
}
|
||||
|
||||
void flushRX(void) {
|
||||
RF24_DEBUG(PSTR("flushRX\n"));
|
||||
@@ -93,12 +98,11 @@ void openWritingPipe(uint8_t recipient) {
|
||||
}
|
||||
|
||||
void startListening(void) {
|
||||
|
||||
RF24_DEBUG(PSTR("start listening\n"));
|
||||
// toggle PRX
|
||||
writeByteRegister(NRF_CONFIG, _CONFIGURATION | _BV(PWR_UP) | _BV(PRIM_RX) );
|
||||
writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) | _BV(PRIM_RX) );
|
||||
// all RX pipe addresses must be unique, therefore skip if node ID is 0xFF
|
||||
if(MYS_RF24_nodeAddress!=AUTO) writeByteRegister(RX_ADDR_P0, MYS_RF24_nodeAddress);
|
||||
if(MY_RF24_NODE_ADDRESS!=AUTO) writeByteRegister(RX_ADDR_P0, MY_RF24_NODE_ADDRESS);
|
||||
// start listening
|
||||
ce(HIGH);
|
||||
}
|
||||
@@ -106,8 +110,10 @@ void startListening(void) {
|
||||
void stopListening(void) {
|
||||
RF24_DEBUG(PSTR("stop listening\n"));
|
||||
ce(LOW);
|
||||
// timing
|
||||
delayMicroseconds(130);
|
||||
writeByteRegister(NRF_CONFIG, _CONFIGURATION | _BV(PWR_UP) );
|
||||
writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) );
|
||||
// timing
|
||||
delayMicroseconds(100);
|
||||
}
|
||||
|
||||
@@ -152,7 +158,8 @@ bool sendMessage( uint8_t recipient, const void* buf, uint8_t len ) {
|
||||
}
|
||||
|
||||
uint8_t getDynamicPayloadSize(void) {
|
||||
uint8_t result = spiMultiByteTransfer( R_RX_PL_WID ,NULL,1,true);
|
||||
uint8_t result = spiMultiByteTransfer(R_RX_PL_WID,NULL,1,true);
|
||||
// check if payload size invalid
|
||||
if(result > 32) {
|
||||
RF24_DEBUG(PSTR("invalid payload length = %d\n"),result);
|
||||
flushRX();
|
||||
@@ -168,12 +175,10 @@ bool IsDataAvailable(uint8_t* to) {
|
||||
if(pipe_num <= 5)
|
||||
RF24_DEBUG(PSTR("Data available on pipe %d\n"),pipe_num);
|
||||
#endif
|
||||
|
||||
if (pipe_num == NODE_PIPE)
|
||||
*to = MYS_RF24_nodeAddress;
|
||||
*to = MY_RF24_NODE_ADDRESS;
|
||||
else if (pipe_num == BROADCAST_PIPE)
|
||||
*to = BROADCAST_ADDRESS;
|
||||
|
||||
return (pipe_num <= 5);
|
||||
}
|
||||
|
||||
@@ -188,86 +193,65 @@ uint8_t readMessage( void* buf) {
|
||||
}
|
||||
|
||||
void setNodeAddress(uint8_t address) {
|
||||
MYS_RF24_nodeAddress = address;
|
||||
// activate node pipe
|
||||
MY_RF24_NODE_ADDRESS = address;
|
||||
// enable node pipe
|
||||
writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) | _BV(ERX_P0) );
|
||||
// enable autoACK on pipe 0
|
||||
writeByteRegister(EN_AA, _BV(ENAA_P0) );
|
||||
|
||||
}
|
||||
|
||||
uint8_t getNodeID(void) {
|
||||
return MYS_RF24_nodeAddress;
|
||||
return MY_RF24_NODE_ADDRESS;
|
||||
}
|
||||
|
||||
boolean initializeRF24(void) {
|
||||
bool initializeRF24(void) {
|
||||
// Initialize pins
|
||||
pinMode(MY_RF24_CE_PIN,OUTPUT);
|
||||
pinMode(MY_RF24_CS_PIN,OUTPUT);
|
||||
// Initialize SPI
|
||||
_SPI.begin();
|
||||
// SoftSPI runs at ~2Mhz, no adjustments needed
|
||||
#if !defined(MY_SOFTSPI)
|
||||
// SPI speed max ~2Mhz
|
||||
#if (F_CPU > 128000000)
|
||||
_SPI.setClockDivider(SPI_CLOCK_DIV128);
|
||||
#elif (F_CPU > 64000000)
|
||||
_SPI.setClockDivider(SPI_CLOCK_DIV64);
|
||||
#elif (F_CPU > 32000000)
|
||||
_SPI.setClockDivider(SPI_CLOCK_DIV32);
|
||||
#elif (F_CPU > 16000000)
|
||||
_SPI.setClockDivider(SPI_CLOCK_DIV16);
|
||||
#elif (F_CPU > 8000000)
|
||||
_SPI.setClockDivider(SPI_CLOCK_DIV8);
|
||||
#elif (F_CPU > 4000000)
|
||||
_SPI.setClockDivider(SPI_CLOCK_DIV4);
|
||||
#else
|
||||
_SPI.setClockDivider(SPI_CLOCK_DIV2);
|
||||
#endif
|
||||
#endif
|
||||
ce(LOW);
|
||||
csn(HIGH);
|
||||
// CRC and power up
|
||||
writeByteRegister(NRF_CONFIG, _CONFIGURATION | _BV(PWR_UP) ) ;
|
||||
writeByteRegister(NRF_CONFIG, MY_RF24_CONFIGURATION | _BV(PWR_UP) ) ;
|
||||
// settle >2ms
|
||||
delay(5);
|
||||
// set address width
|
||||
writeByteRegister(SETUP_AW, MY_RF24_ADDR_WIDTH - 2 );
|
||||
// auto retransmit delay 1500us, auto retransmit count 15
|
||||
writeByteRegister(SETUP_RETR, 5 << ARD | 15 << ARC);
|
||||
writeByteRegister(SETUP_RETR, RF24_ARD << ARD | RF24_ARC << ARC);
|
||||
// set channel
|
||||
writeByteRegister(RF_CH, MY_RF24_CHANNEL);
|
||||
// set data rate and pa level
|
||||
writeByteRegister(RF_SETUP, _RF_SETUP);
|
||||
writeByteRegister(RF_SETUP, MY_RF24_RF_SETUP);
|
||||
// sanity check
|
||||
#if defined(MY_RF24_SANITY_CHECK)
|
||||
if(readRegister(RF_SETUP)!=_RF_SETUP) {
|
||||
RF24_DEBUG(PSTR("Sanity check failed: RF_SETUP register=%d instead of %d, check wiring, replace module or non-P version\n"),readRegister(RF_SETUP), _RF_SETUP);
|
||||
return false;
|
||||
}
|
||||
if(readByteRegister(RF_SETUP)!=MY_RF24_RF_SETUP) {
|
||||
RF24_DEBUG(PSTR("Sanity check failed: RF_SETUP register=%d instead of %d, check wiring, replace module or non-P version\n"),readByteRegister(RF_SETUP), MY_RF24_RF_SETUP);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
// toggle features, not necessary on all chips/clones
|
||||
// toggle features (necessary on some clones)
|
||||
writeByteRegister(ACTIVATE,0x73);
|
||||
// enable ACK payload and dynamic payload
|
||||
writeByteRegister(FEATURE, _FEATURE );
|
||||
writeByteRegister(FEATURE, MY_RF24_FEATURE );
|
||||
// enable broadcasting pipe
|
||||
writeByteRegister(EN_RXADDR, _BV(ERX_P0 + BROADCAST_PIPE) );
|
||||
// disable AA on all pipes, activcate when node pipe set
|
||||
// disable AA on all pipes, activate when node pipe set
|
||||
writeByteRegister(EN_AA, 0x00 );
|
||||
// enable dynamic payloads on used pipes
|
||||
writeByteRegister(DYNPD, _BV(DPL_P0 + BROADCAST_PIPE) | _BV(DPL_P0));
|
||||
// listen to broadcast pipe
|
||||
MYS_RF24_base_addr[0] = BROADCAST_ADDRESS;
|
||||
writeMultiByteRegister(RX_ADDR_P0 + BROADCAST_PIPE, (uint8_t*)&MYS_RF24_base_addr, BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH);
|
||||
// listen to broadcast pipe
|
||||
MY_RF24_BASE_ADDR[0] = BROADCAST_ADDRESS;
|
||||
writeMultiByteRegister(RX_ADDR_P0 + BROADCAST_PIPE, (uint8_t*)&MY_RF24_BASE_ADDR, BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH);
|
||||
// pipe 0, set full address, later only LSB is updated
|
||||
writeMultiByteRegister(RX_ADDR_P0, (uint8_t*)&MYS_RF24_base_addr, MY_RF24_ADDR_WIDTH);
|
||||
writeMultiByteRegister(TX_ADDR, (uint8_t*)&MYS_RF24_base_addr, MY_RF24_ADDR_WIDTH);
|
||||
writeMultiByteRegister(RX_ADDR_P0, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
|
||||
writeMultiByteRegister(TX_ADDR, (uint8_t*)&MY_RF24_BASE_ADDR, MY_RF24_ADDR_WIDTH);
|
||||
// reset FIFO
|
||||
flushRX();
|
||||
flushTX();
|
||||
// reset interrupts
|
||||
writeByteRegister(RF24_STATUS, _BV(TX_DS) | _BV(MAX_RT) | _BV(RX_DR));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/*
|
||||
* 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
|
||||
@@ -17,7 +17,7 @@
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* Based on maniacbug's RF24 library, copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
|
||||
* Driver refactored and optimized for speed and size by Olivier Mauti <olivier@mysensors.org>
|
||||
* RF24 driver refactored and optimized for speed and size, copyright (C) 2016 Olivier Mauti <olivier@mysensors.org>
|
||||
*/
|
||||
|
||||
#ifndef __RF24_H__
|
||||
@@ -29,10 +29,18 @@
|
||||
#include <DigitalIO.h>
|
||||
#endif
|
||||
|
||||
// SPI settings
|
||||
#if !defined(MY_RF24_SPI_MAX_SPEED)
|
||||
// default 2Mhz - safe for nRF24L01+ clones
|
||||
#define MY_RF24_SPI_MAX_SPEED 2000000
|
||||
#endif
|
||||
#define MY_RF24_SPI_DATA_ORDER MSBFIRST
|
||||
#define MY_RF24_SPI_DATA_MODE SPI_MODE0
|
||||
|
||||
// settings
|
||||
#define _CONFIGURATION (uint8_t) (RF24_CRC_16 << 1)
|
||||
#define _FEATURE (uint8_t)( _BV(EN_DPL) | _BV(EN_ACK_PAY) )
|
||||
#define _RF_SETUP (uint8_t)( ((MY_RF24_DATARATE & 0b10 ) << 4) | ((MY_RF24_DATARATE & 0b01 ) << 3) | (MY_RF24_PA_LEVEL << 1) ) + 1 // +1 for Si24R1
|
||||
#define MY_RF24_CONFIGURATION (uint8_t) (RF24_CRC_16 << 1)
|
||||
#define MY_RF24_FEATURE (uint8_t)( _BV(EN_DPL) | _BV(EN_ACK_PAY) )
|
||||
#define MY_RF24_RF_SETUP (uint8_t)( ((MY_RF24_DATARATE & 0b10 ) << 4) | ((MY_RF24_DATARATE & 0b01 ) << 3) | (MY_RF24_PA_LEVEL << 1) ) + 1 // +1 for Si24R1
|
||||
|
||||
// pipes
|
||||
#define BROADCAST_PIPE 1
|
||||
@@ -61,6 +69,12 @@
|
||||
#define RF24_CRC_8 2
|
||||
#define RF24_CRC_16 3
|
||||
|
||||
// ARD, auto retry delay
|
||||
#define RF24_ARD 5 //=1500us
|
||||
|
||||
// ARD, auto retry count
|
||||
#define RF24_ARC 15
|
||||
|
||||
#if defined (ARDUINO) && !defined (__arm__) && !defined (_SPI)
|
||||
#if defined(SPI_UART)
|
||||
#include <SPI_UART.h>
|
||||
@@ -107,8 +121,6 @@
|
||||
SPIUARTClass uspi;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// nRF24L01(+) register definitions
|
||||
#define NRF_CONFIG 0x00
|
||||
#define EN_AA 0x01
|
||||
@@ -216,8 +228,6 @@
|
||||
#define RF_PWR_LOW 1
|
||||
#define RF_PWR_HIGH 2
|
||||
|
||||
|
||||
|
||||
#endif // __RF24_H__
|
||||
|
||||
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
|
||||
/*
|
||||
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
|
||||
|
||||
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.
|
||||
|
||||
Added Arduino Due support from https://github.com/mcrosson/
|
||||
*/
|
||||
|
||||
#ifndef __RF24_CONFIG_H__
|
||||
#define __RF24_CONFIG_H__
|
||||
#include "MyConfig.h"
|
||||
|
||||
#if ARDUINO < 100
|
||||
#include <WProgram.h>
|
||||
#else
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/*** USER DEFINES: ***/
|
||||
//#define FAILURE_HANDLING
|
||||
//#define SPI_UART // Requires library from https://github.com/TMRh20/Sketches/tree/master/SPI_UART
|
||||
//#define MY_SOFTSPI // Requires library from https://github.com/greiman/DigitalIO
|
||||
/**********************/
|
||||
|
||||
// Define _BV for non-Arduino platforms and for Arduino DUE
|
||||
#if defined (ARDUINO) && !defined (__arm__) && !defined(_SPI)
|
||||
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||||
#define RF24_TINY
|
||||
#define _SPI SPI
|
||||
#elif defined SPI_UART
|
||||
#include <SPI_UART.h>
|
||||
#define _SPI uspi
|
||||
#elif defined MY_SOFTSPI
|
||||
#include "drivers/AVR/DigitalIO/DigitalIO.h"
|
||||
|
||||
// change these pins to your liking
|
||||
const uint8_t SOFT_SPI_MISO_PIN = MY_SOFT_SPI_MOSI_PIN;
|
||||
const uint8_t SOFT_SPI_MOSI_PIN = MY_SOFT_SPI_MISO_PIN;
|
||||
const uint8_t SOFT_SPI_SCK_PIN = MY_SOFT_SPI_SCK_PIN;
|
||||
|
||||
const uint8_t SPI_MODE = 0;
|
||||
#define _SPI spi
|
||||
#elif defined(ARDUINO_ARCH_SAMD)
|
||||
#include <SPI.h>
|
||||
#define _SPI SPI1
|
||||
#else
|
||||
#include <SPI.h>
|
||||
#define _SPI SPI
|
||||
#endif
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#if defined(__arm__) || defined (CORE_TEENSY)
|
||||
#include <SPI.h>
|
||||
#endif
|
||||
|
||||
#if !defined(CORE_TEENSY)
|
||||
#define _BV(x) (1<<(x))
|
||||
#if !defined(__arm__)
|
||||
extern HardwareSPI SPI;
|
||||
#endif
|
||||
#else
|
||||
#define printf Serial.printf
|
||||
#endif
|
||||
#if ! defined(_SPI)
|
||||
#define _SPI SPI
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Avoid spurious warnings
|
||||
// Arduino DUE is arm and uses traditional PROGMEM constructs
|
||||
#if 1
|
||||
#if ! defined( NATIVE ) && defined( ARDUINO ) && ! defined(__arm__) && ! defined( CORE_TEENSY3 ) && ! defined(ESP8266)
|
||||
#undef PROGMEM
|
||||
#define PROGMEM __attribute__(( section(".progmem.data") ))
|
||||
#undef PSTR
|
||||
#define PSTR(s) (__extension__({static const char __c[] PROGMEM = (s); &__c[0];}))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Progmem is Arduino-specific
|
||||
// Arduino DUE is arm and does not include avr/pgmspace
|
||||
#if defined(__AVR__)
|
||||
#include <avr/pgmspace.h>
|
||||
#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 ) && ! defined(ESP8266) && ! defined(ARDUINO_ARCH_SAMD)
|
||||
typedef uint16_t prog_uint16_t;
|
||||
#define PSTR(x) (x)
|
||||
#define printf_P printf
|
||||
#define strlen_P strlen
|
||||
#define PROGMEM
|
||||
#define pgm_read_word(p) (*(p))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// ATTiny support code is from https://github.com/jscrane/RF24
|
||||
|
||||
#if defined(RF24_TINY)
|
||||
#include <stdio.h>
|
||||
#include <Arduino.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
#define SPI_CLOCK_DIV4 0x00
|
||||
#define SPI_CLOCK_DIV16 0x01
|
||||
#define SPI_CLOCK_DIV64 0x02
|
||||
#define SPI_CLOCK_DIV128 0x03
|
||||
#define SPI_CLOCK_DIV2 0x04
|
||||
#define SPI_CLOCK_DIV8 0x05
|
||||
#define SPI_CLOCK_DIV32 0x06
|
||||
//#define SPI_CLOCK_DIV64 0x07
|
||||
|
||||
#define SPI_MODE0 0x00
|
||||
#define SPI_MODE1 0x04
|
||||
#define SPI_MODE2 0x08
|
||||
#define SPI_MODE3 0x0C
|
||||
|
||||
#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
|
||||
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
|
||||
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
|
||||
|
||||
|
||||
|
||||
class SPIClass {
|
||||
public:
|
||||
static byte transfer(byte _data);
|
||||
|
||||
// SPI Configuration methods
|
||||
|
||||
inline static void attachInterrupt();
|
||||
inline static void detachInterrupt(); // Default
|
||||
|
||||
static void begin(); // Default
|
||||
static void end();
|
||||
|
||||
static void setBitOrder(uint8_t);
|
||||
static void setDataMode(uint8_t);
|
||||
static void setClockDivider(uint8_t);
|
||||
};
|
||||
extern SPIClass SPI;
|
||||
|
||||
#endif //ATTiny
|
||||
#endif // __RF24_CONFIG_H__
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* 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 <henrik.ekblad@mysensors.org>
|
||||
* 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 tekka,
|
||||
* Contribution by a-lurker and Anticimex,
|
||||
* Contribution by Norbert Truchsess <norbert.truchsess@t-online.de>
|
||||
* 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/esp8266_gateway for wiring instructions.
|
||||
* nRF24L01+ ESP8266
|
||||
* VCC VCC
|
||||
* CE GPIO4
|
||||
* CSN/CS GPIO15
|
||||
* SCK GPIO14
|
||||
* MISO GPIO12
|
||||
* MOSI GPIO13
|
||||
* GND GND
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <EEPROM.h>
|
||||
#include <SPI.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
// Enable debug prints to serial monitor
|
||||
#define MY_DEBUG
|
||||
|
||||
// Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
|
||||
#define MY_BAUD_RATE 9600
|
||||
|
||||
// Enables and select radio type (if attached)
|
||||
#define MY_RADIO_NRF24
|
||||
//#define MY_RADIO_RFM69
|
||||
|
||||
#define MY_GATEWAY_ESP8266
|
||||
|
||||
#define MY_ESP8266_SSID "MySSID"
|
||||
#define MY_ESP8266_PASSWORD "MyVerySecretPassword"
|
||||
|
||||
// Enable UDP communication
|
||||
//#define MY_USE_UDP
|
||||
|
||||
// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
|
||||
//#define MY_IP_ADDRESS 192,168,178,87
|
||||
|
||||
// If using static ip you need to define Gateway and Subnet address as well
|
||||
#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
|
||||
#define MY_IP_SUBNET_ADDRESS 255,255,255,0
|
||||
|
||||
// The port to keep open on node server mode
|
||||
#define MY_PORT 5003
|
||||
|
||||
// How many clients should be able to connect to this gateway (default 1)
|
||||
#define MY_GATEWAY_MAX_CLIENTS 2
|
||||
|
||||
// Controller ip address. Enables client mode (default is "server" mode).
|
||||
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
|
||||
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
|
||||
|
||||
// Enable inclusion mode
|
||||
#define MY_INCLUSION_MODE_FEATURE
|
||||
|
||||
// Enable Inclusion mode button on gateway
|
||||
// #define MY_INCLUSION_BUTTON_FEATURE
|
||||
// Set inclusion mode duration (in seconds)
|
||||
#define MY_INCLUSION_MODE_DURATION 60
|
||||
// Digital pin used for inclusion mode button
|
||||
#define MY_INCLUSION_MODE_BUTTON_PIN 3
|
||||
|
||||
|
||||
// Flash leds on rx/tx/err
|
||||
// #define MY_LEDS_BLINKING_FEATURE
|
||||
// Set blinking period
|
||||
// #define MY_DEFAULT_LED_BLINK_PERIOD 300
|
||||
|
||||
// Led pins used if blinking feature is enabled above
|
||||
#define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin
|
||||
#define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
|
||||
#define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED
|
||||
|
||||
#if defined(MY_USE_UDP)
|
||||
#include <WiFiUDP.h>
|
||||
#else
|
||||
#include <ESP8266WiFi.h>
|
||||
#endif
|
||||
|
||||
#include <MySensor.h>
|
||||
|
||||
void setup() {
|
||||
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("ArduinoOTA start");
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("\nArduinoOTA end");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
||||
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
||||
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
Serial.println("Ready");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
}
|
||||
|
||||
void presentation() {
|
||||
// Present locally attached sensors here
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// Send locally attech sensors data here
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@ MY_SIGNING_NODE_WHITELISTING LITERAL1
|
||||
MY_SIGNING_ATSHA204_PIN LITERAL1
|
||||
MY_SIGNING_SOFT_RANDOMSEED_PIN LITERAL1
|
||||
MY_RF24_ENABLE_ENCRYPTION LITERAL1
|
||||
MY_RF24_SPI_MAX_SPEED LITERAL1
|
||||
MY_RF24_CE_PIN LITERAL1
|
||||
MY_RF24_CS_PIN LITERAL1
|
||||
MY_RF24_PA_LEVEL LITERAL1
|
||||
|
||||
Reference in New Issue
Block a user