From 66ff5b2880ee72c86b47379e1fb2601138cee800 Mon Sep 17 00:00:00 2001 From: Marcelo Aquino Date: Wed, 19 Apr 2017 17:40:21 -0300 Subject: [PATCH] Linux: Add support for RFM69 (#823) --- MySensors.h | 2 +- configure | 41 +++++++++++++++++++++++------- drivers/Linux/SPIDEV.h | 2 ++ drivers/RFM69/new/RFM69_new.cpp | 11 ++++---- hal/transport/MyTransportRFM69.cpp | 4 ++- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/MySensors.h b/MySensors.h index 41c1439..f3ddfea 100644 --- a/MySensors.h +++ b/MySensors.h @@ -292,7 +292,7 @@ MY_DEFAULT_RX_LED_PIN in your sketch instead to enable LEDs // RAM ROUTING TABLE #if defined(MY_RAM_ROUTING_TABLE_FEATURE) && defined(MY_REPEATER_FEATURE) // activate feature based on architecture -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_SAMD) || defined(LINUX_ARCH_RASPBERRYPI) || defined(__linux__) +#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_SAMD) || defined(__linux__) #define MY_RAM_ROUTING_TABLE_ENABLED #elif defined(ARDUINO_ARCH_AVR) #if defined(__avr_atmega1280__) || defined(__avr_atmega1284__) || defined(__avr_atmega2560__) diff --git a/configure b/configure index f4a15eb..58b7e83 100755 --- a/configure +++ b/configure @@ -24,7 +24,7 @@ Building options: --extra-cxxflags= Extra C++ flags passed to C++ compilation. [] --extra-ldflags= Extra C flags passed to linking. [] --c_compiler= C compiler. [arm-linux-gnueabihf-gcc][gcc] - --cxx_compiler= C++ compiler [arm-linux-gnueabihf-g++][g++] + --cxx_compiler= C++ compiler. [arm-linux-gnueabihf-g++][g++] --build-dir= Compiler directory to store object files. [build] --bin-dir= Compiler directory to store binary files. [bin] --arduino-lib-dir= Arduino library directory. @@ -61,10 +61,10 @@ MySensors options: MQTT publish topic prefix. --my-mqtt-subscribe-topic-prefix= MQTT subscribe topic prefix. - --my-transport=[none|nrf24|rs485|rfm95] + --my-transport=[none|nrf24|rs485|rfm95|rfm69] Set the transport to be used to communicate with other nodes. [nrf24] - --my-rf24-channel=<0-125> RF channel for the sensor net, 0-125. [76] + --my-rf24-channel=<0-125> RF channel for the sensor net. [76] --my-rf24-pa-level=[RF24_PA_MAX|RF24_PA_LOW] RF24 PA level. [RF24_PA_MAX] --my-rf24-ce-pin= Pin number to use for rf24 Chip-Enable. @@ -76,8 +76,12 @@ MySensors options: personalized with the same AES key --my-rx-message-buffer-size= Buffer size for incoming messages when using rf24 interrupts. [20] - --my-rf69-irq-pin= Pin number connected to RFM69 IRQ pin. - --my-rs485-serial-port= + --my-rfm69-frequency=[315|433|868|915] + RFM69 Module Frequency. [868] + --my-is-rfm69hw Enable high-powered rfm69hw. + --my-rfm69-irq-pin= Pin number connected to RFM69 IRQ pin. + --my-rfm69-cs-pin= Pin number to use for RFM69 Chip-Select. + --my-rs485-serial-port= RS485 serial port. You must provide a port. --my-rs485-baudrate= RS485 baudrate. [9600] --my-rs485-de-pin= Pin number connected to RS485 driver enable pin. @@ -400,9 +404,28 @@ for opt do --my-rx-message-buffer-size=*) CPPFLAGS="-DMY_RX_MESSAGE_BUFFER_SIZE=${optarg} $CPPFLAGS" ;; - --my-rf69-irq-pin=*) - CPPFLAGS="-DMY_RF69_IRQ_PIN=${optarg} $CPPFLAGS" - ;; + --my-rfm69-frequency=*) + if [[ ${optarg} == "315" ]]; then + CPPFLAGS="-DMY_RFM69_FREQUENCY=RFM69_315MHZ $CPPFLAGS" + elif [[ ${optarg} == "433" ]]; then + CPPFLAGS="-DMY_RFM69_FREQUENCY=RFM69_433MHZ $CPPFLAGS" + elif [[ ${optarg} == "868" ]]; then + CPPFLAGS="-DMY_RFM69_FREQUENCY=RFM69_868MHZ $CPPFLAGS" + elif [[ ${optarg} == "915" ]]; then + CPPFLAGS="-DMY_RFM69_FREQUENCY=RFM69_915MHZ $CPPFLAGS" + else + echo "[WARNING] Illegal value for --my-rfm69-frequency=${optarg}, ignored" + fi + ;; + --my-is-rfm69hw*) + CPPFLAGS="-DMY_IS_RFM69HW $CPPFLAGS" + ;; + --my-rfm69-irq-pin=*) + CPPFLAGS="-DMY_RFM69_IRQ_PIN=${optarg} $CPPFLAGS" + ;; + --my-rfm69-cs-pin=*) + CPPFLAGS="-DMY_RFM69_CS_PIN=${optarg} $CPPFLAGS" + ;; --my-rs485-serial-port=*) CPPFLAGS="-DMY_RS485_HWSERIAL=\\\"${optarg}\\\" $CPPFLAGS" ;; @@ -527,7 +550,7 @@ if [[ ${transport_type} == "none" ]]; then elif [[ ${transport_type} == "nrf24" ]]; then CPPFLAGS="-DMY_RADIO_NRF24 $CPPFLAGS" elif [[ ${transport_type} == "rfm69" ]]; then - CPPFLAGS="-DMY_RADIO_RFM69 $CPPFLAGS" + CPPFLAGS="-DMY_RADIO_RFM69 -DMY_RFM69_NEW_DRIVER $CPPFLAGS" elif [[ ${transport_type} == "rs485" ]]; then CPPFLAGS="-DMY_RS485 $CPPFLAGS" elif [[ ${transport_type} == "rfm95" ]]; then diff --git a/drivers/Linux/SPIDEV.h b/drivers/Linux/SPIDEV.h index fa89525..ba45dea 100644 --- a/drivers/Linux/SPIDEV.h +++ b/drivers/Linux/SPIDEV.h @@ -33,6 +33,7 @@ #define SPI_CLOCK_BASE 16000000 // 16Mhz +#define SPI_CLOCK_DIV1 1 #define SPI_CLOCK_DIV2 2 #define SPI_CLOCK_DIV4 4 #define SPI_CLOCK_DIV8 8 @@ -40,6 +41,7 @@ #define SPI_CLOCK_DIV32 32 #define SPI_CLOCK_DIV64 64 #define SPI_CLOCK_DIV128 128 +#define SPI_CLOCK_DIV256 256 // SPI Data mode #define SPI_MODE0 SPI_MODE_0 diff --git a/drivers/RFM69/new/RFM69_new.cpp b/drivers/RFM69/new/RFM69_new.cpp index 22cc838..033e24f 100644 --- a/drivers/RFM69/new/RFM69_new.cpp +++ b/drivers/RFM69/new/RFM69_new.cpp @@ -52,7 +52,7 @@ uint8_t _SPCR; //!< _SPCR uint8_t _SPSR; //!< _SPSR #endif -#ifdef LINUX_ARCH_RASPBERRYPI +#ifdef LINUX_SPI_BCM // SPI RX and TX buffers (max packet len + 1 byte for the command) uint8_t spi_rxbuff[RFM69_MAX_PACKET_LEN + 1]; uint8_t spi_txbuff[RFM69_MAX_PACKET_LEN + 1]; @@ -115,7 +115,7 @@ LOCAL uint8_t RFM69_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_ RFM69_prepareSPITransaction(); RFM69_csn(LOW); -#ifdef LINUX_ARCH_RASPBERRYPI +#ifdef LINUX_SPI_BCM uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; uint8_t size = len + 1; // Add register value to transmit buffer @@ -274,7 +274,7 @@ LOCAL void RFM69_interruptHandler(void) RFM69_prepareSPITransaction(); RFM69_csn(LOW); -#ifdef LINUX_ARCH_RASPBERRYPI +#ifdef LINUX_SPI_BCM char data[RFM69_MAX_PACKET_LEN + 1]; // max packet len + 1 byte for the command data[0] = RFM69_REG_FIFO & RFM69_READ_REGISTER; @@ -357,7 +357,8 @@ LOCAL uint8_t RFM69_recv(uint8_t* buf, const uint8_t maxBufSize) // atomic noInterrupts(); - const uint8_t payloadLen = min(RFM69.currentPacket.payloadLen, maxBufSize); + const uint8_t payloadLen = RFM69.currentPacket.payloadLen < maxBufSize? + RFM69.currentPacket.payloadLen : maxBufSize; const uint8_t sender = RFM69.currentPacket.header.sender; const rfm69_sequenceNumber_t sequenceNumber = RFM69.currentPacket.header.sequenceNumber; const uint8_t controlFlags = RFM69.currentPacket.header.controlFlags; @@ -734,7 +735,7 @@ LOCAL int16_t RFM69_getReceivingRSSI(void) LOCAL bool RFM69_setTxPowerPercent(uint8_t newPowerPercent) { - newPowerPercent = min(newPowerPercent, 100); // limit + newPowerPercent = min(newPowerPercent, (uint8_t)100); // limit const rfm69_powerlevel_t newPowerLevel = static_cast (RFM69_MIN_POWER_LEVEL_DBM + (RFM69_MAX_POWER_LEVEL_DBM - RFM69_MIN_POWER_LEVEL_DBM) * (newPowerPercent / 100.0f)); diff --git a/hal/transport/MyTransportRFM69.cpp b/hal/transport/MyTransportRFM69.cpp index 7002e0e..d29aecb 100644 --- a/hal/transport/MyTransportRFM69.cpp +++ b/hal/transport/MyTransportRFM69.cpp @@ -105,6 +105,8 @@ void transportSetTargetRSSI(int16_t targetSignalStrength) { #if !defined(MY_GATEWAY_FEATURE) && !defined(MY_RFM69_ATC_MODE_DISABLED) RFM69_ATCmode(true, targetSignalStrength); +#else + (void)targetSignalStrength; #endif } @@ -203,7 +205,7 @@ bool transportSanityCheck(void) uint8_t transportReceive(void* data) { // save payload length - const uint8_t dataLen = min(MAX_MESSAGE_LENGTH,_radio.DATALEN); + const uint8_t dataLen = _radio.DATALEN < MAX_MESSAGE_LENGTH? _radio.DATALEN : MAX_MESSAGE_LENGTH; (void)memcpy((void*)data, (void*)_radio.DATA, dataLen); // Send ack back if this message wasn't a broadcast if (_radio.ACKRequested()) {