From 08a8edb68ab6afbc6449dff9b4875c5607142816 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Tue, 13 Nov 2018 08:07:54 +0100 Subject: [PATCH] unfinished changes, just to be able to transfer files --- mos.yml | 15 +++- src/RF24/RF24.cpp | 156 +++++++++++++++++++------------------- src/RF24/RF24_config.h | 45 +++++------ src/RadioHead/RadioHead.h | 4 +- src/main.cpp | 76 +++++++++++++------ 5 files changed, 167 insertions(+), 129 deletions(-) diff --git a/mos.yml b/mos.yml index 4ad4820..0b15fc2 100644 --- a/mos.yml +++ b/mos.yml @@ -11,19 +11,25 @@ platform: esp8266 sources: - src - - src/RadioHead + #- src/RadioHead + - src/RF24 includes: - - src/RadioHead + #- src/RadioHead + - src/RF24 filesystem: - fs cdefs: - MPIDE: 0 - TEENSYDUINO: 0 + #RF_RadioHead + # MPIDE: 0 RH_PLATFORM: RH_PLATFORM_ESP8266 MONGOOSE_OS: 1 + ESP_PLATFORM: + RF_RF24: + RF24_SPI_TRANSACTIONS: + FAILURE_HANDLING: libs: # common mgos libs @@ -55,6 +61,7 @@ config_schema: - ["i2c.scl_gpio", 5] - ["i2c.sda_gpio", 4] - ["i2c.freq", 1000000] + - ["spi.enable", true] - ["wifi.ap.ssid", "Fastclock-Master-??????"] - ["wifi.ap.pass", "Fastclock-Master"] - ["wifi.sta.ssid", "Pinguin"] diff --git a/src/RF24/RF24.cpp b/src/RF24/RF24.cpp index 7d7ad0d..38e5546 100644 --- a/src/RF24/RF24.cpp +++ b/src/RF24/RF24.cpp @@ -18,12 +18,12 @@ void RF24::csn(bool mode) #if defined (RF24_TINY) if (ce_pin != csn_pin) { digitalWrite(csn_pin,mode); - } + } else { if (mode == HIGH) { PORTB |= (1<CSN HIGH delayMicroseconds(100); // allow csn to settle. - } + } else { PORTB &= ~(1<CSN LOW delayMicroseconds(11); // allow csn to settle @@ -31,17 +31,17 @@ void RF24::csn(bool mode) } // Return, CSN toggle complete return; - + #elif defined(ARDUINO) && !defined (RF24_SPI_TRANSACTIONS) // Minimum ideal SPI bus speed is 2x data rate // If we assume 2Mbs data rate and 16Mhz clock, a // divider of 4 is the minimum we want. // CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz - - #if !defined (SOFTSPI) + + #if !defined (SOFTSPI) _SPI.setBitOrder(MSBFIRST); _SPI.setDataMode(SPI_MODE0); - _SPI.setClockDivider(SPI_CLOCK_DIV2); + //_SPI.setClockDivider(SPI_CLOCK_DIV2); #endif #elif defined (RF24_RPi) if(!mode) @@ -96,13 +96,13 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len) *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) ); while (len--){ *ptx++ = RF24_NOP; } // Dummy operation, just for reading - + _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); - + status = *prx++; // status is 1st byte of receive buffer // decrement before to skip status byte - while ( --size ){ *buf++ = *prx++; } + while ( --size ){ *buf++ = *prx++; } endTransaction(); //unlocks mutex and setting csn high #else @@ -124,19 +124,19 @@ uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len) uint8_t RF24::read_register(uint8_t reg) { uint8_t result; - + #if defined (RF24_LINUX) - + beginTransaction(); - + uint8_t * prx = spi_rxbuff; - uint8_t * ptx = spi_txbuff; + uint8_t * ptx = spi_txbuff; *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) ); *ptx++ = RF24_NOP ; // Dummy operation, just for reading - + _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); result = *++prx; // result is 2nd byte of receive buffer - + endTransaction(); #else @@ -156,7 +156,7 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len) { uint8_t status; - #if defined (RF24_LINUX) + #if defined (RF24_LINUX) beginTransaction(); uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; @@ -165,7 +165,7 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len) *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); while ( len-- ) *ptx++ = *buf++; - + _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); status = *prx; // status is 1st byte of receive buffer endTransaction(); @@ -195,8 +195,8 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value) uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); - *ptx = value ; - + *ptx = value ; + _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); status = *prx++; // status is 1st byte of receive buffer endTransaction(); @@ -221,10 +221,10 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri data_len = rf24_min(data_len, payload_size); uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; - + //printf("[Writing %u bytes %u blanks]",data_len,blank_len); IF_SERIAL_DEBUG( printf("[Writing %u bytes %u blanks]\n",data_len,blank_len); ); - + #if defined (RF24_LINUX) beginTransaction(); uint8_t * prx = spi_rxbuff; @@ -237,7 +237,7 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri *ptx++ = *current++; while ( blank_len-- ) *ptx++ = 0; - + _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); status = *prx; // status is 1st byte of receive buffer endTransaction(); @@ -251,7 +251,7 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri } while ( blank_len-- ) { _SPI.transfer(0); - } + } endTransaction(); #endif @@ -268,11 +268,11 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len) if(data_len > payload_size) data_len = payload_size; uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; - + //printf("[Reading %u bytes %u blanks]",data_len,blank_len); IF_SERIAL_DEBUG( printf("[Reading %u bytes %u blanks]\n",data_len,blank_len); ); - + #if defined (RF24_LINUX) beginTransaction(); uint8_t * prx = spi_rxbuff; @@ -281,19 +281,19 @@ uint8_t RF24::read_payload(void* buf, uint8_t data_len) size = data_len + blank_len + 1; // Add register value to transmit buffer *ptx++ = R_RX_PAYLOAD; - while(--size) + while(--size) *ptx++ = RF24_NOP; - + size = data_len + blank_len + 1; // Size has been lost during while, re affect - + _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); - - status = *prx++; // 1st byte is status - + + status = *prx++; // 1st byte is status + if (data_len > 0) { while ( --data_len ) // Decrement before to skip 1st status byte *current++ = *prx++; - + *current = *prx; } endTransaction(); @@ -333,11 +333,11 @@ uint8_t RF24::flush_tx(void) uint8_t RF24::spiTrans(uint8_t cmd){ uint8_t status; - + beginTransaction(); status = _SPI.transfer( cmd ); endTransaction(); - + return status; } @@ -427,7 +427,7 @@ RF24::RF24(uint16_t _cepin, uint16_t _cspin): #if defined (RF24_LINUX) && !defined (MRAA)//RPi constructor RF24::RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed): - ce_pin(_cepin),csn_pin(_cspin),spi_speed(_spi_speed),p_variant(false), payload_size(32), dynamic_payloads_enabled(false),addr_width(5)//,pipe0_reading_address(0) + ce_pin(_cepin),csn_pin(_cspin),spi_speed(_spi_speed),p_variant(false), payload_size(32), dynamic_payloads_enabled(false),addr_width(5)//,pipe0_reading_address(0) { pipe0_reading_address[0]=0; } @@ -443,7 +443,7 @@ void RF24::setChannel(uint8_t channel) uint8_t RF24::getChannel() { - + return read_register(RF_CH); } /****************************************************************************/ @@ -542,7 +542,7 @@ void RF24::printDetails(void) default : printf("8 Mhz"); break ; } printf("\n================ NRF Configuration ================\n"); - + #endif //Linux print_status(get_status()); @@ -577,10 +577,10 @@ bool RF24::begin(void) #if defined (RF24_LINUX) #if defined (MRAA) - GPIO(); - gpio.begin(ce_pin,csn_pin); + GPIO(); + gpio.begin(ce_pin,csn_pin); #endif - + #ifdef RF24_RPi switch(csn_pin){ //Ensure valid hardware CS pin case 0: break; @@ -591,14 +591,14 @@ bool RF24::begin(void) default: csn_pin = 0; break; } #endif - + _SPI.begin(csn_pin); pinMode(ce_pin,OUTPUT); - ce(LOW); + ce(LOW); delay(100); - + #elif defined(LITTLEWIRE) pinMode(csn_pin,OUTPUT); _SPI.begin(); @@ -611,13 +611,13 @@ bool RF24::begin(void) delay(200); #else // Initialize pins - if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT); - + if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT); + #if ! defined(LITTLEWIRE) if (ce_pin != csn_pin) #endif pinMode(csn_pin,OUTPUT); - + _SPI.begin(); ce(LOW); csn(HIGH); @@ -656,7 +656,7 @@ bool RF24::begin(void) { p_variant = true ; }*/ - + // Then set the data rate to the slowest (and most reliable) speed supported by all // hardware. setDataRate( RF24_1MBPS ) ; @@ -718,7 +718,7 @@ void RF24::startListening(void) ce(HIGH); // Restore the pipe0 adddress, if exists if (pipe0_reading_address[0] > 0){ - write_register(RX_ADDR_P0, pipe0_reading_address, addr_width); + write_register(RX_ADDR_P0, pipe0_reading_address, addr_width); }else{ closeReadingPipe(0); } @@ -740,18 +740,18 @@ static const uint8_t child_pipe_enable[] PROGMEM = }; void RF24::stopListening(void) -{ +{ ce(LOW); delayMicroseconds(txDelay); - + if(read_register(FEATURE) & _BV(EN_ACK_PAY)){ delayMicroseconds(txDelay); //200 flush_tx(); } //flush_rx(); write_register(NRF_CONFIG, ( read_register(NRF_CONFIG) ) & ~_BV(PRIM_RX) ); - + #if defined (RF24_TINY) || defined (LITTLEWIRE) // for 3 pins solution TX mode is only left with additonal powerDown/powerUp cycle if (ce_pin == csn_pin) { @@ -760,7 +760,7 @@ void RF24::stopListening(void) } #endif write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[0]))); // Enable RX on pipe0 - + //delayMicroseconds(100); } @@ -815,21 +815,21 @@ bool RF24::write( const void* buf, uint8_t len, const bool multicast ) //Wait until complete or failed #if defined (FAILURE_HANDLING) || defined (RF24_LINUX) uint32_t timer = millis(); - #endif - - while( ! ( get_status() & ( _BV(TX_DS) | _BV(MAX_RT) ))) { + #endif + + while( ! ( get_status() & ( _BV(TX_DS) | _BV(MAX_RT) ))) { #if defined (FAILURE_HANDLING) || defined (RF24_LINUX) - if(millis() - timer > 95){ + if(millis() - timer > 95){ errNotify(); #if defined (FAILURE_HANDLING) - return 0; + return 0; #else delay(100); #endif } #endif } - + ce(LOW); uint8_t status = write_register(NRF_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); @@ -865,11 +865,11 @@ bool RF24::writeBlocking( const void* buf, uint8_t len, uint32_t timeout ) if(millis() - timer > timeout){ return 0; } //If this payload has exceeded the user-defined timeout, exit and return 0 } #if defined (FAILURE_HANDLING) || defined (RF24_LINUX) - if(millis() - timer > (timeout+95) ){ + if(millis() - timer > (timeout+95) ){ errNotify(); #if defined (FAILURE_HANDLING) - return 0; - #endif + return 0; + #endif } #endif @@ -902,7 +902,7 @@ bool RF24::writeFast( const void* buf, uint8_t len, const bool multicast ) #if defined (FAILURE_HANDLING) || defined (RF24_LINUX) uint32_t timer = millis(); #endif - + while( ( get_status() & ( _BV(TX_FULL) ))) { //Blocking only if FIFO is full. This will loop and block until TX is successful or fail if( get_status() & _BV(MAX_RT)){ @@ -912,10 +912,10 @@ bool RF24::writeFast( const void* buf, uint8_t len, const bool multicast ) //From the user perspective, if you get a 0, just keep trying to send the same payload } #if defined (FAILURE_HANDLING) || defined (RF24_LINUX) - if(millis() - timer > 95 ){ + if(millis() - timer > 95 ){ errNotify(); #if defined (FAILURE_HANDLING) - return 0; + return 0; #endif } #endif @@ -989,7 +989,7 @@ bool RF24::txStandBy(){ if( millis() - timeout > 95){ errNotify(); #if defined (FAILURE_HANDLING) - return 0; + return 0; #endif } #endif @@ -1022,13 +1022,13 @@ bool RF24::txStandBy(uint32_t timeout, bool startTx){ if( millis() - start > (timeout+95)){ errNotify(); #if defined (FAILURE_HANDLING) - return 0; + return 0; #endif } #endif } - + ce(LOW); //Set STANDBY-I mode return 1; @@ -1052,12 +1052,12 @@ uint8_t RF24::getDynamicPayloadSize(void) { uint8_t result = 0; - #if defined (RF24_LINUX) + #if defined (RF24_LINUX) spi_txbuff[0] = R_RX_PL_WID; spi_rxbuff[1] = 0xff; beginTransaction(); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); - result = spi_rxbuff[1]; + result = spi_rxbuff[1]; endTransaction(); #else beginTransaction(); @@ -1132,8 +1132,8 @@ void RF24::openWritingPipe(uint64_t value) write_register(RX_ADDR_P0, reinterpret_cast(&value), addr_width); write_register(TX_ADDR, reinterpret_cast(&value), addr_width); - - + + //const uint8_t max_payload_size = 32; //write_register(RX_PW_P0,rf24_min(payload_size,max_payload_size)); write_register(RX_PW_P0,payload_size); @@ -1340,7 +1340,7 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len) while ( data_len-- ){ *ptx++ = *current++; } - + _SPI.transfern( (char *) spi_txbuff, size); endTransaction(); #else @@ -1350,8 +1350,8 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len) while ( data_len-- ) _SPI.transfer(*current++); endTransaction(); - - #endif + + #endif } @@ -1446,7 +1446,7 @@ bool RF24::setDataRate(rf24_datarate_e speed) // HIGH and LOW '00' is 1Mbs - our default setup &= ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)) ; - + #if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__) txDelay=250; #else //16Mhz Arduino @@ -1472,7 +1472,7 @@ bool RF24::setDataRate(rf24_datarate_e speed) setup |= _BV(RF_DR_HIGH); #if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__) txDelay=190; - #else //16Mhz Arduino + #else //16Mhz Arduino txDelay=65; #endif } @@ -1542,10 +1542,10 @@ void RF24::setCRCLength(rf24_crclength_e length) rf24_crclength_e RF24::getCRCLength(void) { rf24_crclength_e result = RF24_CRC_DISABLED; - + uint8_t config = read_register(NRF_CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ; uint8_t AA = read_register(EN_AA); - + if ( config & _BV(EN_CRC ) || AA) { if ( config & _BV(CRCO) ) diff --git a/src/RF24/RF24_config.h b/src/RF24/RF24_config.h index 0ffd051..caf71fb 100644 --- a/src/RF24/RF24_config.h +++ b/src/RF24/RF24_config.h @@ -6,7 +6,7 @@ modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. */ - + /* spaniakos Added __ARDUINO_X86__ support */ @@ -14,13 +14,13 @@ #ifndef __RF24_CONFIG_H__ #define __RF24_CONFIG_H__ - /*** USER DEFINES: ***/ + /*** USER DEFINES: ***/ //#define FAILURE_HANDLING //#define SERIAL_DEBUG //#define MINIMAL //#define SPI_UART // Requires library from https://github.com/TMRh20/Sketches/tree/master/SPI_UART //#define SOFTSPI // Requires library from https://github.com/greiman/DigitalIO - + /**********************/ #define rf24_max(a,b) (a>b?a:b) #define rf24_min(a,b) (a - + // RF modules support 10 Mhz SPI bus speed - const uint32_t RF24_SPI_SPEED = 10000000; + const uint32_t RF24_SPI_SPEED = 10000000; #if defined (ARDUINO) && !defined (__arm__) && !defined (__ARDUINO_X86__) #if defined SPI_UART @@ -82,11 +82,11 @@ #ifndef SOFT_SPI_SCK_PIN #define SOFT_SPI_SCK_PIN 7 - #endif + #endif const uint8_t SPI_MODE = 0; #define _SPI spi - - #else + + #else #include #define _SPI SPI #endif @@ -108,9 +108,9 @@ #elif !defined(__arm__) && !defined (__ARDUINO_X86__) extern HardwareSPI SPI; #endif - + #define _BV(x) (1<<(x)) - + #endif #ifdef SERIAL_DEBUG @@ -121,12 +121,12 @@ #define printf_P(...) #endif #endif - + #if defined (__ARDUINO_X86__) #define printf_P printf #define _BV(bit) (1<<(bit)) #endif - + // Progmem is Arduino-specific // Arduino DUE is arm and does not include avr/pgmspace #if defined (ARDUINO_ARCH_ESP8266) @@ -146,11 +146,13 @@ typedef uint16_t prog_uint16_t; + #if !defined(PSTR) #define PSTR(x) (x) #define printf_P printf #define strlen_P strlen #define PROGMEM #define pgm_read_word(p) (*(p)) + #endif #define PRIPSTR "%s" @@ -161,4 +163,3 @@ #endif // __RF24_CONFIG_H__ - diff --git a/src/RadioHead/RadioHead.h b/src/RadioHead/RadioHead.h index 7c8aac2..6089d7d 100644 --- a/src/RadioHead/RadioHead.h +++ b/src/RadioHead/RadioHead.h @@ -886,13 +886,13 @@ // Try to be compatible with systems that support yield() and multitasking // instead of spin-loops // Recent Arduino IDE or Teensy 3 has yield() -#if (RH_PLATFORM == RH_PLATFORM_ARDUINO && ARDUINO >= 155 && !defined(RH_PLATFORM_ATTINY)) || (TEENSYDUINO && defined(__MK20DX128__)) +#if (RH_PLATFORM == RH_PLATFORM_ARDUINO && ARDUINO >= 155 && !defined(RH_PLATFORM_ATTINY)) || (defined(TEENSYDUINO) && TEENSYDUINO && defined(__MK20DX128__)) #define YIELD yield(); #elif (RH_PLATFORM == RH_PLATFORM_ESP8266) // ESP8266 also hash it // freertos/include/freertos/task.h // #include "freertos/task.h" - #define YIELD + #define YIELD #else #define YIELD #endif diff --git a/src/main.cpp b/src/main.cpp index c8c7e25..3d34752 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,13 +2,14 @@ * Copyright (c) 2014-2017 Cesanta Software Limited * All rights reserved */ -#define RF_RadioHead +//#define RF_RadioHead //#define RF_RF24 #include #include #include #include +//#include #ifdef RF_RadioHead #include "RadioHead/RH_NRF24.h" #include "RadioHead/RHDatagram.h" @@ -67,17 +68,18 @@ RHDatagram Datagram(nrf24, THIS_ADRESS); #endif #ifdef RF_RF24 -RF24 radio(PIN_NRF24_CSN,PIN_NRF24_CE); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8 +RF24 radio(PIN_NRF24_CE, PIN_NRF24_CSN); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8 const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate. byte data[32]; //Data buffer for testing data transfer speeds unsigned long sendFailedCounter=0, rxTimer; //Counter and timer for keeping track transfer info unsigned long receivedCounter=0; -unsigned long startTime, stopTime; +unsigned long startTime, stopTime, pauseTime; bool TX=1,RX=0,role=0; #endif +//static SPIImpl SPI; static void setSmallTextSize(void) { display->setFont(&TomThumb); } @@ -91,6 +93,8 @@ static uint8_t getNormalTextHeight() { return Org_01.yAdvance; } static void setLargeTextSize(void) { display->setFont(&FreeMonoBold9pt7b); } //static uint8_t getLargeTextHeight() { return FreeMonoBold9pt7b.yAdvance; } //static uint8_t getLargeTextCharsPerLine() { return 12; } + +// Forward declarations: static void updateDisplay_cb(void *arg); static void fastclockRF_receive_cb(void *arg); static void fastclockRF_send_cb(void *arg); @@ -140,19 +144,25 @@ static void initDisplay_cb(void *arg) { break; } ++step; - if (step <= 3) mgos_set_timer(600 /* ms */, false /* repeat */, initDisplay_cb, NULL); + if (step <= 3) mgos_set_timer(400 /* ms */, false /* repeat */, initDisplay_cb, NULL); } void setup(void) { LOG(LL_INFO, ("*** Setup started")); -#ifdef RF_RadioHead - if (!Datagram.init()) - LOG(LL_ERROR, ("*** Datagram init failed")); + +#if 0 + struct mgos_spi *spi; + + spi = mgos_spi_get_global(); + if (spi == NULL) { + LOG(LL_ERROR, ("SPI is not configured, make sure spi.enable is true")); + return; } #endif + LOG(LL_INFO, ("*** Setting timer")); - mgos_set_timer(2000 /* ms */, false /* repeat */, initDisplay_cb, NULL); - mgos_set_timer(7000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL); + mgos_set_timer(1000 /* ms */, false /* repeat */, initDisplay_cb, NULL); + mgos_set_timer(5000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL); LOG(LL_INFO, ("*** Setup done")); } @@ -267,6 +277,7 @@ static void switchToReceiverRole() static void fastclockRF_receive_cb(void *arg) { (void) arg; + LOG(LL_INFO, ("*** Rcv RF")); #ifdef RF_RadioHead // check for incoming messages if (Datagram.available()) @@ -287,22 +298,24 @@ static void fastclockRF_receive_cb(void *arg) { #endif #ifdef RF_RF24 - uint8_t buf[RH_MAX_MESSAGE_LEN]; +#define RF24_MAX_MESSAGE_LEN 32 + uint8_t buf[RF24_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); - unsigned int counger=0; + unsigned int counter=0; switchToReceiverRole(); - while (radio.available()) { + if (radio.available()) { radio.read(buf, len); counter++; + LOG(LL_INFO, ("%04d: %02x %02x %02x %02x %02x %02x", counter, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])); } if (millis() - rxTimer > 1000) { - rxTimer = millis(); - receivedCounter += counter; - unsigned long numBytes = counter*len; - LOG(LL_INFO, ("Bytes: %d, Msg count: %d", numBytes, counter)); - counter = 0; -} + rxTimer = millis(); + receivedCounter += counter; + unsigned long numBytes = counter*len; + LOG(LL_INFO, ("Bytes: %ld, Msg count: %d", numBytes, counter)); + counter = 0; + } #endif } @@ -327,8 +340,8 @@ static void fastclockRF_send_cb(void *arg) { #ifdef RF_RF24 switchToSenderRole(); - if (!radio.writeFast(&clockMsg,sizeof(clockMsg))) { //Write to the FIFO buffers - sendFailedCounter++; //Keep count of failed payloads + if (!radio.write(&clockMsg, sizeof(clockMsg), true /*multicast*/)) { + sendFailedCounter++; } //This is only required when NO ACK ( enableAutoAck(0) ) payloads are used @@ -342,6 +355,7 @@ static void fastclockRF_send_cb(void *arg) { if (!radio.txStandBy()) { sendFailedCounter += 3; } //Standby, block only until FIFO empty or auto-retry timeout. Flush TX FIFO if failed //radio.txStandBy(1000); //Standby, using extended timeout period of 1 second #endif + LOG(LL_INFO, ("*** send finished")); } static void timeTick_cb(void *arg) { @@ -352,23 +366,32 @@ static void timeTick_cb(void *arg) { incrementClockByMilliseconds(fastclockTimeAdvance); //lastSentTimeTick += fastclockTimeAdvance * msPerModelSecond/1000; lastSentTimeTick = newTimeTick; + LOG(LL_INFO, ("*** tick (adv=%d)", fastclockTimeAdvance)); } static void initFastclockRF_cb(void *arg) { (void) arg; - LOG(LL_INFO, ("*** Setting up RF")); lastSentTimeTick = millis(); + LOG(LL_INFO, ("*** Setting up RF, init lastSentTimeTick=%ld", lastSentTimeTick)); fastclock.day = 0; fastclock.hour = 0; fastclock.minute = 0; fastclock.second = 0; fastclock.millisecond = 0; +#ifdef RF_RadioHead + if (!Datagram.init()) { + LOG(LL_ERROR, ("*** Datagram init failed")); + } +#endif + #ifdef RF_RF24 radio.begin(); + if (radio.isChipConnected()) { LOG(LL_INFO, ("*** RF chip found")); } + else { LOG(LL_ERROR, ("*** ERROR: RF chip not found!")); } radio.setChannel(1); radio.setPALevel(RF24_PA_MAX); - radio.setDataRate(RF24_1MBPS); + radio.setDataRate(RF24_2MBPS); radio.setAutoAck(0); //radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries radio.setCRCLength(RF24_CRC_8); @@ -376,14 +399,21 @@ static void initFastclockRF_cb(void *arg) { radio.openReadingPipe(1,pipes[1]); radio.startListening(); radio.printDetails(); - randomSeed(analogRead(0)); + // @TODO: real random seed! + //randomSeed(analogRead(0)); + //randomSeed(22); radio.powerUp(); + LOG(LL_INFO, ("*** RF payload size=%d bytes", radio.getPayloadSize())); + if (radio.testCarrier() || radio.testRPD()) { LOG(LL_INFO, ("*** Carrier/RPD seen on radio")); } + if (radio.failureDetected) { LOG(LL_ERROR, ("*** Radio error detected!")); } #endif + LOG(LL_INFO, ("*** Setting up timer tasks")); mgos_set_timer(100 /* ms */, true /* repeat */, fastclockRF_receive_cb, NULL); mgos_set_timer(500 /* ms */, true /* repeat */, timeTick_cb, NULL); mgos_set_timer(3000 /* ms */, true /* repeat */, fastclockRF_send_cb, NULL); mgos_set_timer(1000 /* ms */, true /* repeat */, updateDisplay_cb, NULL); + LOG(LL_INFO, ("*** Setting up RF done")); } static void updateDisplay_cb(void *arg) {