unfinished changes, just to be able to transfer files

This commit is contained in:
Dirk Jahnke 2018-11-13 08:07:54 +01:00
parent 7ad2d6e404
commit 08a8edb68a
5 changed files with 167 additions and 129 deletions

15
mos.yml
View File

@ -11,19 +11,25 @@ platform: esp8266
sources: sources:
- src - src
- src/RadioHead #- src/RadioHead
- src/RF24
includes: includes:
- src/RadioHead #- src/RadioHead
- src/RF24
filesystem: filesystem:
- fs - fs
cdefs: cdefs:
MPIDE: 0 #RF_RadioHead
TEENSYDUINO: 0 # MPIDE: 0
RH_PLATFORM: RH_PLATFORM_ESP8266 RH_PLATFORM: RH_PLATFORM_ESP8266
MONGOOSE_OS: 1 MONGOOSE_OS: 1
ESP_PLATFORM:
RF_RF24:
RF24_SPI_TRANSACTIONS:
FAILURE_HANDLING:
libs: libs:
# common mgos libs # common mgos libs
@ -55,6 +61,7 @@ config_schema:
- ["i2c.scl_gpio", 5] - ["i2c.scl_gpio", 5]
- ["i2c.sda_gpio", 4] - ["i2c.sda_gpio", 4]
- ["i2c.freq", 1000000] - ["i2c.freq", 1000000]
- ["spi.enable", true]
- ["wifi.ap.ssid", "Fastclock-Master-??????"] - ["wifi.ap.ssid", "Fastclock-Master-??????"]
- ["wifi.ap.pass", "Fastclock-Master"] - ["wifi.ap.pass", "Fastclock-Master"]
- ["wifi.sta.ssid", "Pinguin"] - ["wifi.sta.ssid", "Pinguin"]

View File

@ -18,12 +18,12 @@ void RF24::csn(bool mode)
#if defined (RF24_TINY) #if defined (RF24_TINY)
if (ce_pin != csn_pin) { if (ce_pin != csn_pin) {
digitalWrite(csn_pin,mode); digitalWrite(csn_pin,mode);
} }
else { else {
if (mode == HIGH) { if (mode == HIGH) {
PORTB |= (1<<PINB2); // SCK->CSN HIGH PORTB |= (1<<PINB2); // SCK->CSN HIGH
delayMicroseconds(100); // allow csn to settle. delayMicroseconds(100); // allow csn to settle.
} }
else { else {
PORTB &= ~(1<<PINB2); // SCK->CSN LOW PORTB &= ~(1<<PINB2); // SCK->CSN LOW
delayMicroseconds(11); // allow csn to settle delayMicroseconds(11); // allow csn to settle
@ -31,17 +31,17 @@ void RF24::csn(bool mode)
} }
// Return, CSN toggle complete // Return, CSN toggle complete
return; return;
#elif defined(ARDUINO) && !defined (RF24_SPI_TRANSACTIONS) #elif defined(ARDUINO) && !defined (RF24_SPI_TRANSACTIONS)
// Minimum ideal SPI bus speed is 2x data rate // Minimum ideal SPI bus speed is 2x data rate
// If we assume 2Mbs data rate and 16Mhz clock, a // If we assume 2Mbs data rate and 16Mhz clock, a
// divider of 4 is the minimum we want. // divider of 4 is the minimum we want.
// CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz // CLK:BUS 8Mhz:2Mhz, 16Mhz:4Mhz, or 20Mhz:5Mhz
#if !defined (SOFTSPI) #if !defined (SOFTSPI)
_SPI.setBitOrder(MSBFIRST); _SPI.setBitOrder(MSBFIRST);
_SPI.setDataMode(SPI_MODE0); _SPI.setDataMode(SPI_MODE0);
_SPI.setClockDivider(SPI_CLOCK_DIV2); //_SPI.setClockDivider(SPI_CLOCK_DIV2);
#endif #endif
#elif defined (RF24_RPi) #elif defined (RF24_RPi)
if(!mode) 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 ) ); *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) );
while (len--){ *ptx++ = RF24_NOP; } // Dummy operation, just for reading while (len--){ *ptx++ = RF24_NOP; } // Dummy operation, just for reading
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
status = *prx++; // status is 1st byte of receive buffer status = *prx++; // status is 1st byte of receive buffer
// decrement before to skip status byte // decrement before to skip status byte
while ( --size ){ *buf++ = *prx++; } while ( --size ){ *buf++ = *prx++; }
endTransaction(); //unlocks mutex and setting csn high endTransaction(); //unlocks mutex and setting csn high
#else #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 RF24::read_register(uint8_t reg)
{ {
uint8_t result; uint8_t result;
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
beginTransaction(); beginTransaction();
uint8_t * prx = spi_rxbuff; uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff; uint8_t * ptx = spi_txbuff;
*ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) ); *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) );
*ptx++ = RF24_NOP ; // Dummy operation, just for reading *ptx++ = RF24_NOP ; // Dummy operation, just for reading
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
result = *++prx; // result is 2nd byte of receive buffer result = *++prx; // result is 2nd byte of receive buffer
endTransaction(); endTransaction();
#else #else
@ -156,7 +156,7 @@ uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len)
{ {
uint8_t status; uint8_t status;
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
beginTransaction(); beginTransaction();
uint8_t * prx = spi_rxbuff; uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff; 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 ) ); *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) );
while ( len-- ) while ( len-- )
*ptx++ = *buf++; *ptx++ = *buf++;
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer status = *prx; // status is 1st byte of receive buffer
endTransaction(); endTransaction();
@ -195,8 +195,8 @@ uint8_t RF24::write_register(uint8_t reg, uint8_t value)
uint8_t * prx = spi_rxbuff; uint8_t * prx = spi_rxbuff;
uint8_t * ptx = spi_txbuff; uint8_t * ptx = spi_txbuff;
*ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) );
*ptx = value ; *ptx = value ;
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
status = *prx++; // status is 1st byte of receive buffer status = *prx++; // status is 1st byte of receive buffer
endTransaction(); 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); data_len = rf24_min(data_len, payload_size);
uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
//printf("[Writing %u bytes %u blanks]",data_len,blank_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_SERIAL_DEBUG( printf("[Writing %u bytes %u blanks]\n",data_len,blank_len); );
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
beginTransaction(); beginTransaction();
uint8_t * prx = spi_rxbuff; 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++; *ptx++ = *current++;
while ( blank_len-- ) while ( blank_len-- )
*ptx++ = 0; *ptx++ = 0;
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size);
status = *prx; // status is 1st byte of receive buffer status = *prx; // status is 1st byte of receive buffer
endTransaction(); endTransaction();
@ -251,7 +251,7 @@ uint8_t RF24::write_payload(const void* buf, uint8_t data_len, const uint8_t wri
} }
while ( blank_len-- ) { while ( blank_len-- ) {
_SPI.transfer(0); _SPI.transfer(0);
} }
endTransaction(); endTransaction();
#endif #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; if(data_len > payload_size) data_len = payload_size;
uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len;
//printf("[Reading %u bytes %u blanks]",data_len,blank_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_SERIAL_DEBUG( printf("[Reading %u bytes %u blanks]\n",data_len,blank_len); );
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
beginTransaction(); beginTransaction();
uint8_t * prx = spi_rxbuff; 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 size = data_len + blank_len + 1; // Add register value to transmit buffer
*ptx++ = R_RX_PAYLOAD; *ptx++ = R_RX_PAYLOAD;
while(--size) while(--size)
*ptx++ = RF24_NOP; *ptx++ = RF24_NOP;
size = data_len + blank_len + 1; // Size has been lost during while, re affect size = data_len + blank_len + 1; // Size has been lost during while, re affect
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); _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) { if (data_len > 0) {
while ( --data_len ) // Decrement before to skip 1st status byte while ( --data_len ) // Decrement before to skip 1st status byte
*current++ = *prx++; *current++ = *prx++;
*current = *prx; *current = *prx;
} }
endTransaction(); endTransaction();
@ -333,11 +333,11 @@ uint8_t RF24::flush_tx(void)
uint8_t RF24::spiTrans(uint8_t cmd){ uint8_t RF24::spiTrans(uint8_t cmd){
uint8_t status; uint8_t status;
beginTransaction(); beginTransaction();
status = _SPI.transfer( cmd ); status = _SPI.transfer( cmd );
endTransaction(); endTransaction();
return status; return status;
} }
@ -427,7 +427,7 @@ RF24::RF24(uint16_t _cepin, uint16_t _cspin):
#if defined (RF24_LINUX) && !defined (MRAA)//RPi constructor #if defined (RF24_LINUX) && !defined (MRAA)//RPi constructor
RF24::RF24(uint16_t _cepin, uint16_t _cspin, uint32_t _spi_speed): 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; pipe0_reading_address[0]=0;
} }
@ -443,7 +443,7 @@ void RF24::setChannel(uint8_t channel)
uint8_t RF24::getChannel() uint8_t RF24::getChannel()
{ {
return read_register(RF_CH); return read_register(RF_CH);
} }
/****************************************************************************/ /****************************************************************************/
@ -542,7 +542,7 @@ void RF24::printDetails(void)
default : printf("8 Mhz"); break ; default : printf("8 Mhz"); break ;
} }
printf("\n================ NRF Configuration ================\n"); printf("\n================ NRF Configuration ================\n");
#endif //Linux #endif //Linux
print_status(get_status()); print_status(get_status());
@ -577,10 +577,10 @@ bool RF24::begin(void)
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
#if defined (MRAA) #if defined (MRAA)
GPIO(); GPIO();
gpio.begin(ce_pin,csn_pin); gpio.begin(ce_pin,csn_pin);
#endif #endif
#ifdef RF24_RPi #ifdef RF24_RPi
switch(csn_pin){ //Ensure valid hardware CS pin switch(csn_pin){ //Ensure valid hardware CS pin
case 0: break; case 0: break;
@ -591,14 +591,14 @@ bool RF24::begin(void)
default: csn_pin = 0; break; default: csn_pin = 0; break;
} }
#endif #endif
_SPI.begin(csn_pin); _SPI.begin(csn_pin);
pinMode(ce_pin,OUTPUT); pinMode(ce_pin,OUTPUT);
ce(LOW); ce(LOW);
delay(100); delay(100);
#elif defined(LITTLEWIRE) #elif defined(LITTLEWIRE)
pinMode(csn_pin,OUTPUT); pinMode(csn_pin,OUTPUT);
_SPI.begin(); _SPI.begin();
@ -611,13 +611,13 @@ bool RF24::begin(void)
delay(200); delay(200);
#else #else
// Initialize pins // Initialize pins
if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT); if (ce_pin != csn_pin) pinMode(ce_pin,OUTPUT);
#if ! defined(LITTLEWIRE) #if ! defined(LITTLEWIRE)
if (ce_pin != csn_pin) if (ce_pin != csn_pin)
#endif #endif
pinMode(csn_pin,OUTPUT); pinMode(csn_pin,OUTPUT);
_SPI.begin(); _SPI.begin();
ce(LOW); ce(LOW);
csn(HIGH); csn(HIGH);
@ -656,7 +656,7 @@ bool RF24::begin(void)
{ {
p_variant = true ; p_variant = true ;
}*/ }*/
// Then set the data rate to the slowest (and most reliable) speed supported by all // Then set the data rate to the slowest (and most reliable) speed supported by all
// hardware. // hardware.
setDataRate( RF24_1MBPS ) ; setDataRate( RF24_1MBPS ) ;
@ -718,7 +718,7 @@ void RF24::startListening(void)
ce(HIGH); ce(HIGH);
// Restore the pipe0 adddress, if exists // Restore the pipe0 adddress, if exists
if (pipe0_reading_address[0] > 0){ 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{ }else{
closeReadingPipe(0); closeReadingPipe(0);
} }
@ -740,18 +740,18 @@ static const uint8_t child_pipe_enable[] PROGMEM =
}; };
void RF24::stopListening(void) void RF24::stopListening(void)
{ {
ce(LOW); ce(LOW);
delayMicroseconds(txDelay); delayMicroseconds(txDelay);
if(read_register(FEATURE) & _BV(EN_ACK_PAY)){ if(read_register(FEATURE) & _BV(EN_ACK_PAY)){
delayMicroseconds(txDelay); //200 delayMicroseconds(txDelay); //200
flush_tx(); flush_tx();
} }
//flush_rx(); //flush_rx();
write_register(NRF_CONFIG, ( read_register(NRF_CONFIG) ) & ~_BV(PRIM_RX) ); write_register(NRF_CONFIG, ( read_register(NRF_CONFIG) ) & ~_BV(PRIM_RX) );
#if defined (RF24_TINY) || defined (LITTLEWIRE) #if defined (RF24_TINY) || defined (LITTLEWIRE)
// for 3 pins solution TX mode is only left with additonal powerDown/powerUp cycle // for 3 pins solution TX mode is only left with additonal powerDown/powerUp cycle
if (ce_pin == csn_pin) { if (ce_pin == csn_pin) {
@ -760,7 +760,7 @@ void RF24::stopListening(void)
} }
#endif #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) | _BV(pgm_read_byte(&child_pipe_enable[0]))); // Enable RX on pipe0
//delayMicroseconds(100); //delayMicroseconds(100);
} }
@ -815,21 +815,21 @@ bool RF24::write( const void* buf, uint8_t len, const bool multicast )
//Wait until complete or failed //Wait until complete or failed
#if defined (FAILURE_HANDLING) || defined (RF24_LINUX) #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
uint32_t timer = millis(); uint32_t timer = millis();
#endif #endif
while( ! ( get_status() & ( _BV(TX_DS) | _BV(MAX_RT) ))) { while( ! ( get_status() & ( _BV(TX_DS) | _BV(MAX_RT) ))) {
#if defined (FAILURE_HANDLING) || defined (RF24_LINUX) #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
if(millis() - timer > 95){ if(millis() - timer > 95){
errNotify(); errNotify();
#if defined (FAILURE_HANDLING) #if defined (FAILURE_HANDLING)
return 0; return 0;
#else #else
delay(100); delay(100);
#endif #endif
} }
#endif #endif
} }
ce(LOW); ce(LOW);
uint8_t status = write_register(NRF_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); 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(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 defined (FAILURE_HANDLING) || defined (RF24_LINUX)
if(millis() - timer > (timeout+95) ){ if(millis() - timer > (timeout+95) ){
errNotify(); errNotify();
#if defined (FAILURE_HANDLING) #if defined (FAILURE_HANDLING)
return 0; return 0;
#endif #endif
} }
#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) #if defined (FAILURE_HANDLING) || defined (RF24_LINUX)
uint32_t timer = millis(); uint32_t timer = millis();
#endif #endif
while( ( get_status() & ( _BV(TX_FULL) ))) { //Blocking only if FIFO is full. This will loop and block until TX is successful or fail 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)){ 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 //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 defined (FAILURE_HANDLING) || defined (RF24_LINUX)
if(millis() - timer > 95 ){ if(millis() - timer > 95 ){
errNotify(); errNotify();
#if defined (FAILURE_HANDLING) #if defined (FAILURE_HANDLING)
return 0; return 0;
#endif #endif
} }
#endif #endif
@ -989,7 +989,7 @@ bool RF24::txStandBy(){
if( millis() - timeout > 95){ if( millis() - timeout > 95){
errNotify(); errNotify();
#if defined (FAILURE_HANDLING) #if defined (FAILURE_HANDLING)
return 0; return 0;
#endif #endif
} }
#endif #endif
@ -1022,13 +1022,13 @@ bool RF24::txStandBy(uint32_t timeout, bool startTx){
if( millis() - start > (timeout+95)){ if( millis() - start > (timeout+95)){
errNotify(); errNotify();
#if defined (FAILURE_HANDLING) #if defined (FAILURE_HANDLING)
return 0; return 0;
#endif #endif
} }
#endif #endif
} }
ce(LOW); //Set STANDBY-I mode ce(LOW); //Set STANDBY-I mode
return 1; return 1;
@ -1052,12 +1052,12 @@ uint8_t RF24::getDynamicPayloadSize(void)
{ {
uint8_t result = 0; uint8_t result = 0;
#if defined (RF24_LINUX) #if defined (RF24_LINUX)
spi_txbuff[0] = R_RX_PL_WID; spi_txbuff[0] = R_RX_PL_WID;
spi_rxbuff[1] = 0xff; spi_rxbuff[1] = 0xff;
beginTransaction(); beginTransaction();
_SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2);
result = spi_rxbuff[1]; result = spi_rxbuff[1];
endTransaction(); endTransaction();
#else #else
beginTransaction(); beginTransaction();
@ -1132,8 +1132,8 @@ void RF24::openWritingPipe(uint64_t value)
write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), addr_width); write_register(RX_ADDR_P0, reinterpret_cast<uint8_t*>(&value), addr_width);
write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), addr_width); write_register(TX_ADDR, reinterpret_cast<uint8_t*>(&value), addr_width);
//const uint8_t max_payload_size = 32; //const uint8_t max_payload_size = 32;
//write_register(RX_PW_P0,rf24_min(payload_size,max_payload_size)); //write_register(RX_PW_P0,rf24_min(payload_size,max_payload_size));
write_register(RX_PW_P0,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-- ){ while ( data_len-- ){
*ptx++ = *current++; *ptx++ = *current++;
} }
_SPI.transfern( (char *) spi_txbuff, size); _SPI.transfern( (char *) spi_txbuff, size);
endTransaction(); endTransaction();
#else #else
@ -1350,8 +1350,8 @@ void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
while ( data_len-- ) while ( data_len-- )
_SPI.transfer(*current++); _SPI.transfer(*current++);
endTransaction(); endTransaction();
#endif #endif
} }
@ -1446,7 +1446,7 @@ bool RF24::setDataRate(rf24_datarate_e speed)
// HIGH and LOW '00' is 1Mbs - our default // HIGH and LOW '00' is 1Mbs - our default
setup &= ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)) ; setup &= ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)) ;
#if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__) #if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__)
txDelay=250; txDelay=250;
#else //16Mhz Arduino #else //16Mhz Arduino
@ -1472,7 +1472,7 @@ bool RF24::setDataRate(rf24_datarate_e speed)
setup |= _BV(RF_DR_HIGH); setup |= _BV(RF_DR_HIGH);
#if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__) #if defined(__arm__) || defined (RF24_LINUX) || defined (__ARDUINO_X86__)
txDelay=190; txDelay=190;
#else //16Mhz Arduino #else //16Mhz Arduino
txDelay=65; txDelay=65;
#endif #endif
} }
@ -1542,10 +1542,10 @@ void RF24::setCRCLength(rf24_crclength_e length)
rf24_crclength_e RF24::getCRCLength(void) rf24_crclength_e RF24::getCRCLength(void)
{ {
rf24_crclength_e result = RF24_CRC_DISABLED; rf24_crclength_e result = RF24_CRC_DISABLED;
uint8_t config = read_register(NRF_CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ; uint8_t config = read_register(NRF_CONFIG) & ( _BV(CRCO) | _BV(EN_CRC)) ;
uint8_t AA = read_register(EN_AA); uint8_t AA = read_register(EN_AA);
if ( config & _BV(EN_CRC ) || AA) if ( config & _BV(EN_CRC ) || AA)
{ {
if ( config & _BV(CRCO) ) if ( config & _BV(CRCO) )

View File

@ -6,7 +6,7 @@
modify it under the terms of the GNU General Public License modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation. version 2 as published by the Free Software Foundation.
*/ */
/* spaniakos <spaniakos@gmail.com> /* spaniakos <spaniakos@gmail.com>
Added __ARDUINO_X86__ support Added __ARDUINO_X86__ support
*/ */
@ -14,13 +14,13 @@
#ifndef __RF24_CONFIG_H__ #ifndef __RF24_CONFIG_H__
#define __RF24_CONFIG_H__ #define __RF24_CONFIG_H__
/*** USER DEFINES: ***/ /*** USER DEFINES: ***/
//#define FAILURE_HANDLING //#define FAILURE_HANDLING
//#define SERIAL_DEBUG //#define SERIAL_DEBUG
//#define MINIMAL //#define MINIMAL
//#define SPI_UART // Requires library from https://github.com/TMRh20/Sketches/tree/master/SPI_UART //#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 SOFTSPI // Requires library from https://github.com/greiman/DigitalIO
/**********************/ /**********************/
#define rf24_max(a,b) (a>b?a:b) #define rf24_max(a,b) (a>b?a:b)
#define rf24_min(a,b) (a<b?a:b) #define rf24_min(a,b) (a<b?a:b)
@ -28,8 +28,8 @@
#if defined SPI_HAS_TRANSACTION && !defined SPI_UART && !defined SOFTSPI #if defined SPI_HAS_TRANSACTION && !defined SPI_UART && !defined SOFTSPI
#define RF24_SPI_TRANSACTIONS #define RF24_SPI_TRANSACTIONS
#endif #endif
//ATXMega //ATXMega
#if defined(__AVR_ATxmega64D3__) || defined(__AVR_ATxmega128D3__) || defined(__AVR_ATxmega192D3__) || defined(__AVR_ATxmega256D3__) || defined(__AVR_ATxmega384D3__) // In order to be available both in windows and linux this should take presence here. #if defined(__AVR_ATxmega64D3__) || defined(__AVR_ATxmega128D3__) || defined(__AVR_ATxmega192D3__) || defined(__AVR_ATxmega256D3__) || defined(__AVR_ATxmega384D3__) // In order to be available both in windows and linux this should take presence here.
#define XMEGA #define XMEGA
@ -42,28 +42,28 @@
// The includes.h file defines either RF24_RPi, MRAA, LITTLEWIRE or RF24_SPIDEV and includes the correct RF24_arch_config.h file // The includes.h file defines either RF24_RPi, MRAA, LITTLEWIRE or RF24_SPIDEV and includes the correct RF24_arch_config.h file
#include "utility/includes.h" #include "utility/includes.h"
//ATTiny //ATTiny
#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny4313__) || defined(__AVR_ATtiny861__) #elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny4313__) || defined(__AVR_ATtiny861__)
#define RF24_TINY #define RF24_TINY
#include "utility/ATTiny/RF24_arch_config.h" #include "utility/ATTiny/RF24_arch_config.h"
//LittleWire //LittleWire
#elif defined(LITTLEWIRE) #elif defined(LITTLEWIRE)
#include "utility/LittleWire/RF24_arch_config.h" #include "utility/LittleWire/RF24_arch_config.h"
//Teensy //Teensy
#elif defined (TEENSYDUINO) #elif defined (TEENSYDUINO)
#include "utility/Teensy/RF24_arch_config.h" #include "utility/Teensy/RF24_arch_config.h"
//Everything else //Everything else
#else #else
#include <Arduino.h> #include <Arduino.h>
// RF modules support 10 Mhz SPI bus speed // 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 (ARDUINO) && !defined (__arm__) && !defined (__ARDUINO_X86__)
#if defined SPI_UART #if defined SPI_UART
@ -82,11 +82,11 @@
#ifndef SOFT_SPI_SCK_PIN #ifndef SOFT_SPI_SCK_PIN
#define SOFT_SPI_SCK_PIN 7 #define SOFT_SPI_SCK_PIN 7
#endif #endif
const uint8_t SPI_MODE = 0; const uint8_t SPI_MODE = 0;
#define _SPI spi #define _SPI spi
#else #else
#include <SPI.h> #include <SPI.h>
#define _SPI SPI #define _SPI SPI
#endif #endif
@ -108,9 +108,9 @@
#elif !defined(__arm__) && !defined (__ARDUINO_X86__) #elif !defined(__arm__) && !defined (__ARDUINO_X86__)
extern HardwareSPI SPI; extern HardwareSPI SPI;
#endif #endif
#define _BV(x) (1<<(x)) #define _BV(x) (1<<(x))
#endif #endif
#ifdef SERIAL_DEBUG #ifdef SERIAL_DEBUG
@ -121,12 +121,12 @@
#define printf_P(...) #define printf_P(...)
#endif #endif
#endif #endif
#if defined (__ARDUINO_X86__) #if defined (__ARDUINO_X86__)
#define printf_P printf #define printf_P printf
#define _BV(bit) (1<<(bit)) #define _BV(bit) (1<<(bit))
#endif #endif
// Progmem is Arduino-specific // Progmem is Arduino-specific
// Arduino DUE is arm and does not include avr/pgmspace // Arduino DUE is arm and does not include avr/pgmspace
#if defined (ARDUINO_ARCH_ESP8266) #if defined (ARDUINO_ARCH_ESP8266)
@ -146,11 +146,13 @@
typedef uint16_t prog_uint16_t; typedef uint16_t prog_uint16_t;
#if !defined(PSTR)
#define PSTR(x) (x) #define PSTR(x) (x)
#define printf_P printf #define printf_P printf
#define strlen_P strlen #define strlen_P strlen
#define PROGMEM #define PROGMEM
#define pgm_read_word(p) (*(p)) #define pgm_read_word(p) (*(p))
#endif
#define PRIPSTR "%s" #define PRIPSTR "%s"
@ -161,4 +163,3 @@
#endif // __RF24_CONFIG_H__ #endif // __RF24_CONFIG_H__

View File

@ -886,13 +886,13 @@
// Try to be compatible with systems that support yield() and multitasking // Try to be compatible with systems that support yield() and multitasking
// instead of spin-loops // instead of spin-loops
// Recent Arduino IDE or Teensy 3 has yield() // 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(); #define YIELD yield();
#elif (RH_PLATFORM == RH_PLATFORM_ESP8266) #elif (RH_PLATFORM == RH_PLATFORM_ESP8266)
// ESP8266 also hash it // ESP8266 also hash it
// freertos/include/freertos/task.h // freertos/include/freertos/task.h
// #include "freertos/task.h" // #include "freertos/task.h"
#define YIELD #define YIELD
#else #else
#define YIELD #define YIELD
#endif #endif

View File

@ -2,13 +2,14 @@
* Copyright (c) 2014-2017 Cesanta Software Limited * Copyright (c) 2014-2017 Cesanta Software Limited
* All rights reserved * All rights reserved
*/ */
#define RF_RadioHead //#define RF_RadioHead
//#define RF_RF24 //#define RF_RF24
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
//#include <SPI.h>
#ifdef RF_RadioHead #ifdef RF_RadioHead
#include "RadioHead/RH_NRF24.h" #include "RadioHead/RH_NRF24.h"
#include "RadioHead/RHDatagram.h" #include "RadioHead/RHDatagram.h"
@ -67,17 +68,18 @@ RHDatagram Datagram(nrf24, THIS_ADRESS);
#endif #endif
#ifdef RF_RF24 #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. 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 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 sendFailedCounter=0, rxTimer; //Counter and timer for keeping track transfer info
unsigned long receivedCounter=0; unsigned long receivedCounter=0;
unsigned long startTime, stopTime; unsigned long startTime, stopTime, pauseTime;
bool TX=1,RX=0,role=0; bool TX=1,RX=0,role=0;
#endif #endif
//static SPIImpl SPI;
static void setSmallTextSize(void) { display->setFont(&TomThumb); } 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 void setLargeTextSize(void) { display->setFont(&FreeMonoBold9pt7b); }
//static uint8_t getLargeTextHeight() { return FreeMonoBold9pt7b.yAdvance; } //static uint8_t getLargeTextHeight() { return FreeMonoBold9pt7b.yAdvance; }
//static uint8_t getLargeTextCharsPerLine() { return 12; } //static uint8_t getLargeTextCharsPerLine() { return 12; }
// Forward declarations:
static void updateDisplay_cb(void *arg); static void updateDisplay_cb(void *arg);
static void fastclockRF_receive_cb(void *arg); static void fastclockRF_receive_cb(void *arg);
static void fastclockRF_send_cb(void *arg); static void fastclockRF_send_cb(void *arg);
@ -140,19 +144,25 @@ static void initDisplay_cb(void *arg) {
break; break;
} }
++step; ++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) { void setup(void) {
LOG(LL_INFO, ("*** Setup started")); LOG(LL_INFO, ("*** Setup started"));
#ifdef RF_RadioHead
if (!Datagram.init()) #if 0
LOG(LL_ERROR, ("*** Datagram init failed")); 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 #endif
LOG(LL_INFO, ("*** Setting timer")); LOG(LL_INFO, ("*** Setting timer"));
mgos_set_timer(2000 /* ms */, false /* repeat */, initDisplay_cb, NULL); mgos_set_timer(1000 /* ms */, false /* repeat */, initDisplay_cb, NULL);
mgos_set_timer(7000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL); mgos_set_timer(5000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL);
LOG(LL_INFO, ("*** Setup done")); LOG(LL_INFO, ("*** Setup done"));
} }
@ -267,6 +277,7 @@ static void switchToReceiverRole()
static void fastclockRF_receive_cb(void *arg) { static void fastclockRF_receive_cb(void *arg) {
(void) arg; (void) arg;
LOG(LL_INFO, ("*** Rcv RF"));
#ifdef RF_RadioHead #ifdef RF_RadioHead
// check for incoming messages // check for incoming messages
if (Datagram.available()) if (Datagram.available())
@ -287,22 +298,24 @@ static void fastclockRF_receive_cb(void *arg) {
#endif #endif
#ifdef RF_RF24 #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); uint8_t len = sizeof(buf);
unsigned int counger=0; unsigned int counter=0;
switchToReceiverRole(); switchToReceiverRole();
while (radio.available()) { if (radio.available()) {
radio.read(buf, len); radio.read(buf, len);
counter++; 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) { if (millis() - rxTimer > 1000) {
rxTimer = millis(); rxTimer = millis();
receivedCounter += counter; receivedCounter += counter;
unsigned long numBytes = counter*len; unsigned long numBytes = counter*len;
LOG(LL_INFO, ("Bytes: %d, Msg count: %d", numBytes, counter)); LOG(LL_INFO, ("Bytes: %ld, Msg count: %d", numBytes, counter));
counter = 0; counter = 0;
} }
#endif #endif
} }
@ -327,8 +340,8 @@ static void fastclockRF_send_cb(void *arg) {
#ifdef RF_RF24 #ifdef RF_RF24
switchToSenderRole(); switchToSenderRole();
if (!radio.writeFast(&clockMsg,sizeof(clockMsg))) { //Write to the FIFO buffers if (!radio.write(&clockMsg, sizeof(clockMsg), true /*multicast*/)) {
sendFailedCounter++; //Keep count of failed payloads sendFailedCounter++;
} }
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used //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 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 //radio.txStandBy(1000); //Standby, using extended timeout period of 1 second
#endif #endif
LOG(LL_INFO, ("*** send finished"));
} }
static void timeTick_cb(void *arg) { static void timeTick_cb(void *arg) {
@ -352,23 +366,32 @@ static void timeTick_cb(void *arg) {
incrementClockByMilliseconds(fastclockTimeAdvance); incrementClockByMilliseconds(fastclockTimeAdvance);
//lastSentTimeTick += fastclockTimeAdvance * msPerModelSecond/1000; //lastSentTimeTick += fastclockTimeAdvance * msPerModelSecond/1000;
lastSentTimeTick = newTimeTick; lastSentTimeTick = newTimeTick;
LOG(LL_INFO, ("*** tick (adv=%d)", fastclockTimeAdvance));
} }
static void initFastclockRF_cb(void *arg) { static void initFastclockRF_cb(void *arg) {
(void) arg; (void) arg;
LOG(LL_INFO, ("*** Setting up RF"));
lastSentTimeTick = millis(); lastSentTimeTick = millis();
LOG(LL_INFO, ("*** Setting up RF, init lastSentTimeTick=%ld", lastSentTimeTick));
fastclock.day = 0; fastclock.day = 0;
fastclock.hour = 0; fastclock.hour = 0;
fastclock.minute = 0; fastclock.minute = 0;
fastclock.second = 0; fastclock.second = 0;
fastclock.millisecond = 0; fastclock.millisecond = 0;
#ifdef RF_RadioHead
if (!Datagram.init()) {
LOG(LL_ERROR, ("*** Datagram init failed"));
}
#endif
#ifdef RF_RF24 #ifdef RF_RF24
radio.begin(); radio.begin();
if (radio.isChipConnected()) { LOG(LL_INFO, ("*** RF chip found")); }
else { LOG(LL_ERROR, ("*** ERROR: RF chip not found!")); }
radio.setChannel(1); radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX); radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS); radio.setDataRate(RF24_2MBPS);
radio.setAutoAck(0); radio.setAutoAck(0);
//radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries //radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8); radio.setCRCLength(RF24_CRC_8);
@ -376,14 +399,21 @@ static void initFastclockRF_cb(void *arg) {
radio.openReadingPipe(1,pipes[1]); radio.openReadingPipe(1,pipes[1]);
radio.startListening(); radio.startListening();
radio.printDetails(); radio.printDetails();
randomSeed(analogRead(0)); // @TODO: real random seed!
//randomSeed(analogRead(0));
//randomSeed(22);
radio.powerUp(); 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 #endif
LOG(LL_INFO, ("*** Setting up timer tasks"));
mgos_set_timer(100 /* ms */, true /* repeat */, fastclockRF_receive_cb, NULL); mgos_set_timer(100 /* ms */, true /* repeat */, fastclockRF_receive_cb, NULL);
mgos_set_timer(500 /* ms */, true /* repeat */, timeTick_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(3000 /* ms */, true /* repeat */, fastclockRF_send_cb, NULL);
mgos_set_timer(1000 /* ms */, true /* repeat */, updateDisplay_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) { static void updateDisplay_cb(void *arg) {