TMRh20 ported to MBED
Fork of TMRh20 by
Diff: RF24.cpp
- Revision:
- 1:8f889354678f
- Parent:
- 0:163155b607df
- Child:
- 2:3332510eebba
--- a/RF24.cpp Thu Mar 10 14:40:30 2016 +0000 +++ b/RF24.cpp Wed Mar 16 11:51:59 2016 +0000 @@ -16,7 +16,6 @@ { csn_pin = mode; wait_us(5); - } /****************************************************************************/ @@ -34,12 +33,14 @@ //_SPI.beginTransaction(SPISettings(RF24_SPI_SPEED, MSBFIRST, SPI_MODE0)); //#endif csn(LOW); + wait_us(5); } /****************************************************************************/ inline void RF24::endTransaction() { + wait_us(10); csn(HIGH); //#if defined (RF24_SPI_TRANSACTIONS) //_SPI.endTransaction(); @@ -72,10 +73,12 @@ beginTransaction(); spi.write( R_REGISTER | ( REGISTER_MASK & reg ) ); - result = spi.write(0xff); + result = spi.write(0x55); // 0xff endTransaction(); + //printf_P(PSTR("read_register(%02x,%02x)\r\n"),reg,result); + return result; } @@ -85,6 +88,8 @@ { uint8_t status; + //printf_P(PSTR("write_register_more(%02x,%d)\r\n"), reg, len); + beginTransaction(); status = spi.write( W_REGISTER | ( REGISTER_MASK & reg ) ); @@ -102,7 +107,7 @@ { uint8_t status; - printf_P(PSTR("write_register(%02x,%02x)\r\n"),reg,value); + //printf_P(PSTR("write_register(%02x,%02x)\r\n"),reg,value); beginTransaction(); @@ -124,7 +129,7 @@ data_len = rf24_min(data_len, payload_size); uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; - printf_P("[Writing %u bytes %u blanks]\n",data_len,blank_len); + //printf_P("[Writing %u bytes %u blanks]\n",data_len,blank_len); beginTransaction(); @@ -149,7 +154,7 @@ if(data_len > payload_size) data_len = payload_size; uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; - printf_P("[Reading %u bytes %u blanks]\n",data_len,blank_len); + //printf_P("[Reading %u bytes %u blanks]\n",data_len,blank_len); beginTransaction(); @@ -185,6 +190,8 @@ uint8_t status; + //printf_P(PSTR("spiTrans(%02x)\r\n"), cmd); + beginTransaction(); status = spi.write(cmd); endTransaction(); @@ -260,11 +267,18 @@ /****************************************************************************/ -RF24::RF24(PinName mosi, PinName miso, PinName sck, PinName _cepin, PinName _cspin): +RF24::RF24(PinName mosi, PinName miso, PinName sck, PinName _cspin, PinName _cepin): spi(mosi, miso, sck), ce_pin(_cepin), csn_pin(_cspin), p_variant(false), payload_size(32), dynamic_payloads_enabled(false), addr_width(5)//,pipe0_reading_address(0) { pipe0_reading_address[0]=0; + + //_SPI.begin(csn_pin); + spi.frequency(10000000/5); // 2Mbit, 1/5th the maximum transfer rate for the spi bus + spi.format(8,0); + + mainTimer.start(); + } /****************************************************************************/ @@ -272,7 +286,7 @@ void RF24::setChannel(uint8_t channel) { const uint8_t max_channel = 125; - write_register(RF_CH,rf24_min(channel,max_channel)); + write_register(RF_CH, rf24_min(channel,max_channel)); } uint8_t RF24::getChannel() @@ -360,16 +374,12 @@ bool RF24::begin(void) { + //printf("RF24::begin\n"); uint8_t setup=0; - //_SPI.begin(csn_pin); - spi.frequency(4000000); // 4Mbit - spi.format(8,0); // 8-bit, ClockPhase = 0, ClockPolarity = 0 - - mainTimer.start(); - ce(LOW); + csn(HIGH); // extra wait_ms(100); @@ -438,6 +448,7 @@ return ( setup != 0 && setup != 0xff ); } + /****************************************************************************/ void RF24::startListening(void) @@ -962,6 +973,8 @@ void RF24::toggle_features(void) { + //printf_P("ACTIVATE"); + beginTransaction(); spi.write( ACTIVATE ); @@ -979,8 +992,7 @@ //toggle_features(); write_register(FEATURE,read_register(FEATURE) | _BV(EN_DPL) ); - - printf_P("FEATURE=%i\r\n",read_register(FEATURE)); + //printf_P("FEATURE=%i\r\n",read_register(FEATURE)); // Enable dynamic payload on all pipes // @@ -1002,7 +1014,7 @@ //toggle_features(); write_register(FEATURE,read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL) ); - printf_P("FEATURE=%i\r\n",read_register(FEATURE)); + //printf_P("FEATURE=%i\r\n",read_register(FEATURE)); // // Enable dynamic payload on pipes 0 & 1 @@ -1022,7 +1034,7 @@ //toggle_features(); write_register(FEATURE,read_register(FEATURE) | _BV(EN_DYN_ACK) ); - printf_P("FEATURE=%i\r\n",read_register(FEATURE)); + //printf_P("FEATURE=%i\r\n",read_register(FEATURE)); }