diff --git a/drivers/RF24/RF24.cpp b/drivers/RF24/RF24.cpp index 025ad60..fa048e3 100644 --- a/drivers/RF24/RF24.cpp +++ b/drivers/RF24/RF24.cpp @@ -6,7 +6,7 @@ * network topology allowing messages to be routed to nodes. * * Created by Henrik Ekblad -* Copyright (C) 2013-2017 Sensnology AB +* Copyright (C) 2013-2018 Sensnology AB * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors * * Documentation: http://www.mysensors.org @@ -36,9 +36,10 @@ LOCAL uint8_t RF24_NODE_ADDRESS = RF24_BROADCAST_ADDRESS; LOCAL RF24_receiveCallbackType RF24_receiveCallback = NULL; #endif -#ifdef LINUX_SPI_BCM -uint8_t spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes) -uint8_t spi_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command) +#if defined(LINUX_SPI_BCM) +uint8_t RF24_spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes) +uint8_t RF24_spi_txbuff[32+1] +; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command) #endif LOCAL void RF24_csn(const bool level) @@ -51,21 +52,22 @@ LOCAL void RF24_ce(const bool level) hwDigitalWrite(MY_RF24_CE_PIN, level); } -LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t len, +LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, uint8_t len, const bool readMode) { uint8_t status; - uint8_t* current = buf; + uint8_t *current = buf; #if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION) RF24_SPI.beginTransaction(SPISettings(MY_RF24_SPI_SPEED, RF24_SPI_DATA_ORDER, RF24_SPI_DATA_MODE)); #endif + RF24_csn(LOW); // timing delayMicroseconds(10); #ifdef LINUX_SPI_BCM - uint8_t * prx = spi_rxbuff; - uint8_t * ptx = spi_txbuff; + uint8_t *prx = RF24_spi_rxbuff; + uint8_t *ptx = RF24_spi_txbuff; uint8_t size = len + 1; // Add register value to transmit buffer *ptx++ = cmd; @@ -76,7 +78,7 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t *ptx++ = *current++; } } - RF24_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); + RF24_SPI.transfernb( (char *) RF24_spi_txbuff, (char *) RF24_spi_rxbuff, size); if (readMode) { if (size == 2) { status = *++prx; // result is 2nd byte of receive buffer @@ -103,6 +105,7 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t } } #endif + RF24_csn(HIGH); #if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION) RF24_SPI.endTransaction(); @@ -198,7 +201,7 @@ LOCAL void RF24_setRFConfiguration(const uint8_t configuration) RF24_writeByteRegister(RF24_REG_NRF_CONFIG, configuration); } -LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t* address, const uint8_t addressWidth) +LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t *address, const uint8_t addressWidth) { RF24_writeMultiByteRegister(pipe, address, addressWidth); } @@ -283,7 +286,7 @@ LOCAL void RF24_standBy(void) } -LOCAL bool RF24_sendMessage(const uint8_t recipient, const void* buf, const uint8_t len, +LOCAL bool RF24_sendMessage(const uint8_t recipient, const void *buf, const uint8_t len, const bool noACK) { uint8_t RF24_status; @@ -296,7 +299,7 @@ LOCAL bool RF24_sendMessage(const uint8_t recipient, const void* buf, const uint // AutoACK is disabled on the broadcasting pipe - NO_ACK prevents resending RF24_spiMultiByteTransfer((recipient == RF24_BROADCAST_ADDRESS || noACK) ? RF24_CMD_WRITE_TX_PAYLOAD_NO_ACK : - RF24_CMD_WRITE_TX_PAYLOAD, (uint8_t*)buf, len, false ); + RF24_CMD_WRITE_TX_PAYLOAD, (uint8_t *)buf, len, false ); // go, TX starts after ~10us, CE high also enables PA+LNA on supported HW RF24_ce(HIGH); // timeout counter to detect HW issues @@ -337,11 +340,11 @@ LOCAL bool RF24_isDataAvailable(void) } -LOCAL uint8_t RF24_readMessage(void* buf) +LOCAL uint8_t RF24_readMessage(void *buf) { const uint8_t len = RF24_getDynamicPayloadSize(); RF24_DEBUG(PSTR("RF24:RXM:LEN=%" PRIu8 "\n"), len); // read message - RF24_spiMultiByteTransfer(RF24_CMD_READ_RX_PAYLOAD,(uint8_t*)buf,len,true); + RF24_spiMultiByteTransfer(RF24_CMD_READ_RX_PAYLOAD,(uint8_t *)buf,len,true); // clear RX interrupt RF24_setStatus(_BV(RF24_RX_DR)); return len; @@ -500,11 +503,11 @@ LOCAL bool RF24_initialize(void) RF24_setDynamicPayload(_BV(RF24_DPL_P0 + RF24_BROADCAST_PIPE) | _BV(RF24_DPL_P0)); // listen to broadcast pipe RF24_BASE_ID[0] = RF24_BROADCAST_ADDRESS; - RF24_setPipeAddress(RF24_REG_RX_ADDR_P0 + RF24_BROADCAST_PIPE, (uint8_t*)&RF24_BASE_ID, + RF24_setPipeAddress(RF24_REG_RX_ADDR_P0 + RF24_BROADCAST_PIPE, (uint8_t *)&RF24_BASE_ID, RF24_BROADCAST_PIPE > 1 ? 1 : MY_RF24_ADDR_WIDTH); // pipe 0, set full address, later only LSB is updated - RF24_setPipeAddress(RF24_REG_RX_ADDR_P0, (uint8_t*)&RF24_BASE_ID, MY_RF24_ADDR_WIDTH); - RF24_setPipeAddress(RF24_REG_TX_ADDR, (uint8_t*)&RF24_BASE_ID, MY_RF24_ADDR_WIDTH); + RF24_setPipeAddress(RF24_REG_RX_ADDR_P0, (uint8_t *)&RF24_BASE_ID, MY_RF24_ADDR_WIDTH); + RF24_setPipeAddress(RF24_REG_TX_ADDR, (uint8_t *)&RF24_BASE_ID, MY_RF24_ADDR_WIDTH); // reset FIFO RF24_flushRX(); RF24_flushTX(); diff --git a/drivers/RF24/RF24.h b/drivers/RF24/RF24.h index d44e9f3..cecaf7c 100644 --- a/drivers/RF24/RF24.h +++ b/drivers/RF24/RF24.h @@ -6,7 +6,7 @@ * network topology allowing messages to be routed to nodes. * * Created by Henrik Ekblad -* Copyright (C) 2013-2017 Sensnology AB +* Copyright (C) 2013-2018 Sensnology AB * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors * * Documentation: http://www.mysensors.org @@ -34,26 +34,26 @@ * RF24 driver-related log messages, format: [!]SYSTEM:[SUB SYSTEM:]MESSAGE * - [!] Exclamation mark is prepended in case of error * -* |E| SYS | SUB | Message | Comment -* |-|------|------|--------------------|--------------------------------------------------------------------- -* | | RF24 | INIT | | Initialise RF24 radio -* | | RF24 | INIT | PIN,CE=%d,CS=%d | Pin configuration: chip enable (CE), chip select (CS) -* |!| RF24 | INIT | SANCHK FAIL | Sanity check failed, check wiring or replace module -* | | RF24 | SPP | PCT=%d,TX LEVEL=%d | Set TX level, input TX percent (PCT) -* | | RF24 | RBR | REG=%d,VAL=%d | Read register (REG), value=(VAL) -* | | RF24 | WBR | REG=%d,VAL=%d | Write register (REG), value=(VAL) -* | | RF24 | FRX | | Flush RX buffer -* | | RF24 | FTX | | Flush TX buffer -* | | RF24 | OWP | RCPT=%d | Open writing pipe, recipient=(RCPT) -* | | RF24 | STL | | Start listening -* | | RF24 | SPL | | Stop listening -* | | RF24 | SLP | | Set radio to sleep -* | | RF24 | SBY | | Set radio to standby -* | | RF24 | TXM | TO=%d,LEN=%d | Transmit message to=(TO), length=(LEN) -* |!| RF24 | TXM | MAX_RT | Max TX retries, no ACK received -* |!| RF24 | GDP | PYL INV | Invalid payload size -* | | RF24 | RXM | LEN=%d | Read message, length=(LEN) -* | | RF24 | STX | LEVEL=%d | Set TX level, level=(LEVEL) +* |E| SYS | SUB | Message | Comment +* |-|------|------|----------------------|--------------------------------------------------------------------- +* | | RF24 | INIT | | Initialise RF24 radio +* | | RF24 | INIT | PIN,CE=%%d,CS=%%d | Pin configuration: chip enable (CE), chip select (CS) +* |!| RF24 | INIT | SANCHK FAIL | Sanity check failed, check wiring or replace module +* | | RF24 | SPP | PCT=%%d,TX LEVEL=%%d | Set TX level, input TX percent (PCT) +* | | RF24 | RBR | REG=%%d,VAL=%%d | Read register (REG), value=(VAL) +* | | RF24 | WBR | REG=%%d,VAL=%%d | Write register (REG), value=(VAL) +* | | RF24 | FRX | | Flush RX buffer +* | | RF24 | FTX | | Flush TX buffer +* | | RF24 | OWP | RCPT=%%d | Open writing pipe, recipient=(RCPT) +* | | RF24 | STL | | Start listening +* | | RF24 | SPL | | Stop listening +* | | RF24 | SLP | | Set radio to sleep +* | | RF24 | SBY | | Set radio to standby +* | | RF24 | TXM | TO=%%d,LEN=%%d | Transmit message to=(TO), length=(LEN) +* |!| RF24 | TXM | MAX_RT | Max TX retries, no ACK received +* |!| RF24 | GDP | PYL INV | Invalid payload size +* | | RF24 | RXM | LEN=%%d | Read message, length=(LEN) +* | | RF24 | STX | LEVEL=%%d | Set TX level, level=(LEVEL) * */ @@ -174,7 +174,7 @@ LOCAL void RF24_ce(const bool level); * @param readMode * @return */ -LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, const uint8_t len, +LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, const uint8_t len, const bool readMode); /** * @brief RF24_spiByteTransfer @@ -199,7 +199,7 @@ LOCAL uint8_t RF24_RAW_writeByteRegister(const uint8_t cmd, const uint8_t value) // helper macros #define RF24_readByteRegister(__reg) RF24_RAW_readByteRegister(RF24_CMD_READ_REGISTER | (RF24_REGISTER_MASK & (__reg))) //!< RF24_readByteRegister #define RF24_writeByteRegister(__reg,__value) RF24_RAW_writeByteRegister(RF24_CMD_WRITE_REGISTER | (RF24_REGISTER_MASK & (__reg)), __value) //!< RF24_writeByteRegister -#define RF24_writeMultiByteRegister(__reg,__buf,__len) RF24_spiMultiByteTransfer(RF24_CMD_WRITE_REGISTER | (RF24_REGISTER_MASK & (__reg)),(uint8_t*)__buf, __len,false) //!< RF24_writeMultiByteRegister +#define RF24_writeMultiByteRegister(__reg,__buf,__len) RF24_spiMultiByteTransfer(RF24_CMD_WRITE_REGISTER | (RF24_REGISTER_MASK & (__reg)),(uint8_t *)__buf, __len,false) //!< RF24_writeMultiByteRegister /** * @brief RF24_flushRX @@ -256,7 +256,7 @@ LOCAL void RF24_powerUp(void); * @param noACK set True if no ACK is required * @return */ -LOCAL bool RF24_sendMessage(const uint8_t recipient, const void* buf, const uint8_t len, +LOCAL bool RF24_sendMessage(const uint8_t recipient, const void *buf, const uint8_t len, const bool noACK = false); /** * @brief RF24_getDynamicPayloadSize @@ -272,7 +272,7 @@ LOCAL bool RF24_isDataAvailable(void); * @brief RF24_readMessage * @return */ -LOCAL uint8_t RF24_readMessage(void* buf); +LOCAL uint8_t RF24_readMessage(void *buf); /** * @brief RF24_setNodeAddress * @param address @@ -345,7 +345,7 @@ LOCAL void RF24_setRFConfiguration(const uint8_t configuration); * @param address * @param addressWidth */ -LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t* address, const uint8_t addressWidth); +LOCAL void RF24_setPipeAddress(const uint8_t pipe, uint8_t *address, const uint8_t addressWidth); /** * @brief RF24_setPipeLSB * @param pipe diff --git a/drivers/RF24/RF24registers.h b/drivers/RF24/RF24registers.h index 195056e..83b2298 100644 --- a/drivers/RF24/RF24registers.h +++ b/drivers/RF24/RF24registers.h @@ -6,7 +6,7 @@ * network topology allowing messages to be routed to nodes. * * Created by Henrik Ekblad -* Copyright (C) 2013-2017 Sensnology AB +* Copyright (C) 2013-2018 Sensnology AB * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors * * Documentation: http://www.mysensors.org diff --git a/drivers/RFM69/new/RFM69_new.cpp b/drivers/RFM69/new/RFM69_new.cpp index 111d83d..a0a8877 100644 --- a/drivers/RFM69/new/RFM69_new.cpp +++ b/drivers/RFM69/new/RFM69_new.cpp @@ -42,12 +42,12 @@ #endif rfm69_internal_t RFM69; //!< internal variables -volatile uint8_t rfm69_irq; //!< rfm69 irq flag +volatile uint8_t RFM69_irq; //!< rfm69 irq flag #if defined(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]; +uint8_t RFM69_spi_rxbuff[RFM69_MAX_PACKET_LEN + 1]; +uint8_t RFM69_spi_txbuff[RFM69_MAX_PACKET_LEN + 1]; #endif LOCAL void RFM69_csn(const bool level) @@ -60,24 +60,6 @@ LOCAL void RFM69_prepareSPITransaction(void) #if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION) RFM69_SPI.beginTransaction(SPISettings(MY_RFM69_SPI_SPEED, RFM69_SPI_DATA_ORDER, RFM69_SPI_DATA_MODE)); -#else -#if defined(SREG) - _SREG = SREG; -#endif - noInterrupts(); -#if defined(SPCR) && defined(SPSR) - // save current SPI settings - _SPCR = SPCR; - _SPSR = SPSR; -#endif - - // set RFM69 SPI settings -#if !defined(MY_SOFTSPI) - RFM69_SPI.setDataMode(RFM69_SPI_DATA_MODE); - RFM69_SPI.setBitOrder(RFM69_SPI_DATA_ORDER); - RFM69_SPI.setClockDivider(RFM69_CLOCK_DIV); -#endif - #endif } @@ -85,17 +67,6 @@ LOCAL void RFM69_concludeSPITransaction(void) { #if !defined(MY_SOFTSPI) && defined(SPI_HAS_TRANSACTION) RFM69_SPI.endTransaction(); -#else - // restore SPI settings to what they were before talking to RFM69 -#if defined(SPCR) && defined(SPSR) - SPCR = _SPCR; - SPSR = _SPSR; -#endif - // restore the prior interrupt state -#if defined(SREG) - SREG = _SREG; -#endif - interrupts(); #endif } @@ -109,8 +80,8 @@ LOCAL uint8_t RFM69_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_ RFM69_csn(LOW); #if defined(LINUX_SPI_BCM) - uint8_t *prx = spi_rxbuff; - uint8_t *ptx = spi_txbuff; + uint8_t *prx = RFM69_spi_rxbuff; + uint8_t *ptx = RFM69_spi_txbuff; uint8_t size = len + 1; // Add register value to transmit buffer *ptx++ = cmd; @@ -121,7 +92,7 @@ LOCAL uint8_t RFM69_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_ *ptx++ = *current++; } } - RFM69_SPI.transfernb((char *)spi_txbuff, (char *)spi_rxbuff, size); + RFM69_SPI.transfernb((char *)RFM69_spi_txbuff, (char *)RFM69_spi_rxbuff, size); if (aReadMode) { if (size == 2) { status = *++prx; // result is 2nd byte of receive buffer @@ -257,7 +228,7 @@ LOCAL bool RFM69_initialise(const uint32_t frequencyHz) // clear FIFO and flags RFM69_clearFIFO(); // IRQ - rfm69_irq = false; + RFM69_irq = false; hwPinMode(MY_RFM69_IRQ_PIN, INPUT); attachInterrupt(MY_RFM69_IRQ_NUM, RFM69_interruptHandler, RISING); return true; @@ -271,7 +242,7 @@ LOCAL void RFM69_clearFIFO(void) LOCAL void RFM69_interruptHandler(void) { // set flag - rfm69_irq = true; + RFM69_irq = true; } LOCAL void RFM69_interruptHandling(void) @@ -346,10 +317,10 @@ LOCAL void RFM69_interruptHandling(void) LOCAL void RFM69_handler(void) { - if (rfm69_irq) { + if (RFM69_irq) { // radio is in STDBY // clear flag, 8bit - no need for critical section - rfm69_irq = false; + RFM69_irq = false; RFM69_interruptHandling(); } } @@ -429,10 +400,10 @@ LOCAL bool RFM69_sendFrame(rfm69_packet_t *packet, const bool increaseSequenceCo // send message (void)RFM69_setRadioMode(RFM69_RADIO_MODE_TX); // irq upon txsent const uint32_t txStartMS = hwMillis(); - while (!rfm69_irq && (hwMillis() - txStartMS < MY_RFM69_TX_TIMEOUT_MS)) { + while (!RFM69_irq && (hwMillis() - txStartMS < MY_RFM69_TX_TIMEOUT_MS)) { doYield(); }; - return rfm69_irq; + return RFM69_irq; } LOCAL bool RFM69_send(const uint8_t recipient, uint8_t *data, const uint8_t len, diff --git a/drivers/RFM69/new/RFM69_new.h b/drivers/RFM69/new/RFM69_new.h index 204ad24..3c1d820 100644 --- a/drivers/RFM69/new/RFM69_new.h +++ b/drivers/RFM69/new/RFM69_new.h @@ -99,30 +99,10 @@ #define RFM69_SPI_DATA_ORDER MSBFIRST //!< SPI data order #define RFM69_SPI_DATA_MODE SPI_MODE0 //!< SPI mode -// SPI clock divier for non-transaction implementations -#if (MY_RFM69_SPI_SPEED >= F_CPU / 2) -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV2 //!< SPI clock divider 2 -#elif (MY_RFM69_SPI_SPEED >= F_CPU / 4) -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV4 //!< SPI clock divider 4 -#elif (MY_RFM69_SPI_SPEED >= F_CPU / 8) -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV8 //!< SPI clock divider 8 -#elif (MY_RFM69_SPI_SPEED >= F_CPU / 16) -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV16 //!< SPI clock divider 16 -#elif (MY_RFM69_SPI_SPEED >= F_CPU / 32) -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV32 //!< SPI clock divider 32 -#elif (MY_RFM69_SPI_SPEED >= F_CPU / 64) -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV64 //!< SPI clock divider 64 -#elif (MY_RFM69_SPI_SPEED >= F_CPU / 128) -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV128 //!< SPI clock divider 128 -#else -#define RFM69_CLOCK_DIV SPI_CLOCK_DIV256 //!< SPI clock divider 256 -#endif - #if defined(ARDUINO) && !defined(__arm__) && !defined(RFM69_SPI) #include #if defined(MY_SOFTSPI) -SoftSPI -RFM69_SPI; +SoftSPIRFM69_SPI; #else #define RFM69_SPI SPI #endif diff --git a/drivers/RFM95/RFM95.cpp b/drivers/RFM95/RFM95.cpp index 2ca352e..03313ed 100644 --- a/drivers/RFM95/RFM95.cpp +++ b/drivers/RFM95/RFM95.cpp @@ -35,15 +35,15 @@ #define RFM95_DEBUG(x,...) //!< DEBUG null #endif +rfm95_internal_t RFM95; //!< internal variables +volatile uint8_t RFM95_irq; // #if defined(MY_SOFTSPI) -SoftSPI -RFM95_SPI; +SoftSPIRFM95_SPI; #else #define RFM95_SPI SPI //!< SPI #endif