Functions | |
void | pic_rf_init (rf_config *my_config) |
Initialise nrf2401a chip with config. | |
void | pic_rf_quick_init (char *my_config, uns8 my_channel, bit my_receive_on) |
Initialise nrf2401a chip with quick config. | |
uns8 | pic_rf_read_register (uns8 cmd, uns8 *data, uns8 data_len) |
Read nRF24L01 register. | |
uns8 | pic_rf_read_register_int (uns8 cmd, uns8 *data, uns8 data_len) |
uns8 | pic_rf_receive (uns8 *data, uns8 bytes_to_receive) |
Receive data from nrf2401a. | |
void | pic_rf_receive2 (uns8 *data, uns8 bytes_to_receive) |
void | pic_rf_receive_inline (uns8 *data, uns8 bytes_to_receive) |
Receive data from nRF24L01. | |
uns8 | pic_rf_send_byte (uns8 b) |
Internal routine to send a byte to nrf2401a. | |
uns8 | pic_rf_send_byte_int (uns8 b) |
Clock a byte into the nRF24L01. | |
uns8 | pic_rf_send_command (uns8 cmd, uns8 *data, uns8 data_len) |
Send command to the nrf24l01. | |
uns8 | pic_rf_send_command_single (uns8 cmd, uns8 data) |
Send a single data byte command to the nrf24l01. | |
void | pic_rf_set_channel (uns8 channel) |
Change channel on the nrf2401a. | |
void | pic_rf_set_mode (uns8 requested_mode) |
Set rf mode to transmit or receive. | |
void | pic_rf_setup () |
Setup ports and pins for communication with nrf2401a. | |
void | pic_rf_transmit (uns8 *data, uns8 bytes_to_transmit) |
Transmit data from nRF24L01. |
void pic_rf_init | ( | rf_config * | my_config | ) |
Initialise nRF24L01 chip with config.
Sends the configuration to the Nordic nrf2401a chip ready to begin communication. This routine assumes you have already set my_config to the correct values.
00254 { 00255 uns8 temp; 00256 uns8 options; 00257 uns8 data[5]; 00258 00259 start_crit_sec(); 00260 00261 clear_pin(rf_ce_port, rf_ce_pin); // go into standby, so we can configure 00262 delay_ms(100); 00263 //data[0] = 0b00111100; 00264 temp = pic_rf_send_command(RF_WR_REG_CONFIG_REG, // write register 0x00 = CONFIG_REG 00265 "\x3c", 1); // 7-reserved=0,6-MASK_RX_DR=0,5-MASK_TX_DS=1,4-MASK_MAX_RT=1, 00266 // 3-EN_CRC=1,2-CRC0=1 (1=2 byte) 00267 // 1-PWR_UP=0,0-PRIM_RX=0 00268 //data[0] = 0b00000000; 00269 pic_rf_send_command(RF_WR_REG_SETUP_RETR, // write register 0x04 = SETUP_RETR 00270 "\x00", 1); // 7-4 retry delay=0, 00271 // 3-0 Auto retransmit count=0 00272 //data[0] = 0b00000001; 00273 pic_rf_send_command(RF_WR_REG_SETUP_AW, // write register 0x03 = SETUP_AW Address widths 00274 "\x01", 1); // 7-2 reserved=0, 1-0 *01*-3 bytes 10-4 bytes 11-5 bytes 00275 00276 //data[0] = 0b00000111; 00277 pic_rf_send_command(RF_WR_REG_RF_SETUP, // write register 6 = RF_SETUP 00278 "\x07", 1); // 7-5 reserved=0, 4 PLL_LOCK=0, 3 RF_DR=0, 2-1 RF_PWR=11, 0 LNA_HCURR=1 00279 //data[0] = 0b00000010; 00280 pic_rf_send_command (RF_WR_REG_RF_CH, // write register 0x05 = RF_CH 00281 "\x02", 1); // 7 reserved=0, 6:0 RF_CH = 2 00282 00283 data[0] = 0b11100111; // 0xe7 LSB 00284 data[1] = 0b11100111; // 0xe7 00285 data[2] = 0b11100111; // 0xe7 MSB 00286 pic_rf_send_command(RF_WR_REG_TX_ADDR, // write register 0x10 TX_ADDR 00287 &data, 3); 00288 00289 00290 pic_rf_send_command(RF_WR_REG_RX_ADDR_P0, // write register 0x0a = RX_ADDR_P0 00291 &data, 3); 00292 00293 pic_rf_send_command(RF_WR_REG_EN_AA, // write register 0x01 = EN_AA 00294 "\x00", 1); // 7:6 reserved=0, 5 ENAA_P5=0, 4 ENAA_P4=0, ... 0 ENAA_P0=0 00295 00296 // Set payload to 21 bytes 00297 // 0b00010101 00298 //PKT_PACKET_SIZE 00299 pic_rf_send_command(RF_WR_REG_RX_PW_P0, // write register 0x11 = RX_PW_P0 receive payload width 00300 "\x15", 1); // 7:6 reserved=0 5:0 bytes in payload 00301 // 0b00111111 00302 pic_rf_send_command(RF_WR_REG_CONFIG_REG, // write register 0x00 = CONFIG_REG 00303 "\x3f", 1); // 7-reserved=0,6-MASK_RX_DR=0,5-MASK_TX_DS=1,4-MASK_MAX_RT=1, 00304 // 3-EN_CRC=1,2-CRC0=1 (1=2 byte) 00305 // 1-PWR_UP=1,0-PRIM_RX=1 00306 delay_ms(2); // 1.5ms settling after power up 00307 pic_rf_send_command (RF_FLUSH_TX, 00308 0, 0 ); 00309 pic_rf_send_command (RF_FLUSH_RX, 00310 0, 0 ); 00311 //clear interrupts 00312 pic_rf_send_command_inline(RF_WR_REG_STATUS, "\x40", 1); 00313 delay_ms(2); // 1.5ms settling after power up 00314 00315 set_pin(rf_ce_port, rf_ce_pin); // We're on the air (receive) 00316 00317 end_crit_sec(); 00318 serial_print_str("got="); 00319 serial_print_int_hex(temp); 00320 serial_print_str(" "); 00321 00322 rf_current_mode_receive = 1; 00323 }
void pic_rf_quick_init | ( | char * | my_config, | |
uns8 | my_channel, | |||
bit | my_receive_on | |||
) |
While the usual pic_rf_init() routine is excellent when you want to programatically change the 2401a config, if you're only doing this once (at the start) then it's likely you're burning a lot of instructions (154 words on a PIC16 device) just to send some bytes of config out to the 2401a. If you know your config in advance, then you can just send the byte-stream config using this routine. Use the nrf2401a_config.pl script in the tools directory to generate this string.
uns8 pic_rf_read_register | ( | uns8 | cmd, | |
uns8 * | data, | |||
uns8 | data_len | |||
) |
Internal routine to read a particular nRF24L01 register. Clocks out data_len bytes from the chip. Internal routine.
cmd | Read register command, eg RF_RD_REG_STATUS | |
data | Pointer to array of bytes where data will be put | |
data_len | Number of bytes to clock out |
00092 { // returns status 00093 00094 uns8 byte_counter, status; 00095 00096 clear_pin(rf_csn_port, rf_csn_pin); 00097 status = pic_rf_send_byte(cmd); 00098 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) { 00099 data[byte_counter] = pic_rf_send_byte(0); 00100 } 00101 00102 set_pin(rf_csn_port, rf_csn_pin); 00103 return status; 00104 }
uns8 pic_rf_read_register_int | ( | uns8 | cmd, | |
uns8 * | data, | |||
uns8 | data_len | |||
) |
00106 { // returns status 00107 00108 uns8 byte_counter, status; 00109 00110 clear_pin(rf_csn_port, rf_csn_pin); 00111 status = pic_rf_send_byte_int(cmd); 00112 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) { 00113 data[byte_counter] = pic_rf_send_byte_int(0); 00114 } 00115 00116 set_pin(rf_csn_port, rf_csn_pin); 00117 return status; 00118 }
uns8 pic_rf_receive | ( | uns8 * | data, | |
uns8 | bytes_to_receive | |||
) |
Receive data from nRF24L01.
Having been notified that there is data available, call this routine to clock the data in from the nrf2401a.
!pic_rf_chip_enable(0); // save power
pic_rf_chip_enable(1); // turn chip back on
00041 { 00042 00043 uns8 fifo_status; 00044 uns8 res; 00045 00046 res = 0; 00047 00048 // Could check status here, but we know why it is... 00049 //clear_pin(rf_ce_port, rf_ce_pin); // Go into standby 00050 00051 pic_rf_read_register_inline(RF_RD_REG_FIFO_STATUS, &fifo_status, 1); 00052 while (!test_bit(fifo_status, 0)) { // Fifo has something in it 00053 //serial_putc('|'); 00054 pic_rf_read_register_inline(RF_R_RX_PAYLOAD, data, bytes_to_receive); // receive 00055 pic_rf_send_command_inline(RF_WR_REG_STATUS, "\x40", 1); 00056 res++; 00057 pic_rf_read_register_inline(RF_RD_REG_FIFO_STATUS, &fifo_status, 1); 00058 } 00059 // clear rf interrupt 00060 //pic_rf_send_command_inline (RF_FLUSH_RX, 0, 0 ); 00061 //set_pin(rf_ce_port, rf_ce_pin); // Back on the air 00062 return res; 00063 }
void pic_rf_receive2 | ( | uns8 * | data, | |
uns8 | bytes_to_receive | |||
) |
00352 { 00353 00354 pic_rf_receive_inline(data, bytes_to_receive); 00355 }
void pic_rf_receive_inline | ( | uns8 * | data, | |
uns8 | bytes_to_receive | |||
) | [inline] |
Having been notified that there is data available, call this routine to clock the data in from the nRF24L01.
00348 { 00349 pic_rf_read_register_inline(RF_R_RX_PAYLOAD, data, bytes_to_receive); 00350 }
uns8 pic_rf_send_byte | ( | uns8 | b | ) |
Clock a byte into the nRF24L01.
Internal routine to send a byte to the nrf2401a. Generally you shouldn't need to use this, see pic_rf_transmit instead
00124 { 00125 uns8 bit_counter, status; 00126 // - For debug: print_int_hex(b); putc(' '); 00127 for(bit_counter = 0 ; bit_counter < 8 ; bit_counter++) { 00128 change_pin(rf_mosi_port, rf_mosi_pin, b.7); // Put data on data pin 00129 set_pin(rf_sck_port, rf_sck_pin); // clock it in (positive edge) 00130 status <<= 1; 00131 status.0 = test_pin(rf_miso_port, rf_miso_pin); 00132 clear_pin(rf_sck_port, rf_sck_pin); // ready for next bit 00133 00134 b <<= 1; // Move all the bits left 00135 } // repeat until finished 00136 return status; 00137 } // pic_rf_send_byte
uns8 pic_rf_send_byte_int | ( | uns8 | b | ) |
Clock one byte into the nRF24L01. Internal routine.
b | The byte to send |
00141 { 00142 uns8 bit_counter, status; 00143 // - For debug: print_int_hex(b); putc(' '); 00144 for(bit_counter = 0 ; bit_counter < 8 ; bit_counter++) { 00145 change_pin(rf_mosi_port, rf_mosi_pin, b.7); // Put data on data pin 00146 set_pin(rf_sck_port, rf_sck_pin); // clock it in (positive edge) 00147 status <<= 1; 00148 status.0 = test_pin(rf_miso_port, rf_miso_pin); 00149 clear_pin(rf_sck_port, rf_sck_pin); // ready for next bit 00150 00151 b <<= 1; // Move all the bits left 00152 } // repeat until finished 00153 return status; 00154 } // pic_rf_send_byte
uns8 pic_rf_send_command | ( | uns8 | cmd, | |
uns8 * | data, | |||
uns8 | data_len | |||
) |
Send a command and associated data to the nRF24L01
cmd | Command to send, eg, RF_WR_REG_SETUP_RETR | |
data | Pointer to an array of bytes to send as data for the command | |
data_len | Number of bytes in the array |
00066 { 00067 00068 uns8 byte_counter, status; 00069 00070 clear_pin(rf_csn_port, rf_csn_pin); 00071 status = pic_rf_send_byte(cmd); 00072 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) { 00073 pic_rf_send_byte(data[byte_counter]); 00074 } 00075 00076 set_pin(rf_csn_port, rf_csn_pin); 00077 return status; 00078 }
uns8 pic_rf_send_command_single | ( | uns8 | cmd, | |
uns8 | data | |||
) |
Send a command and 1 byte of data to the nRF24L01
cmd | Command to send, eg, RF_WR_REG_SETUP_RETR | |
data | One byte of data for the command |
00080 { 00081 00082 uns8 byte_counter, status; 00083 00084 clear_pin(rf_csn_port, rf_csn_pin); 00085 status = pic_rf_send_byte(cmd); 00086 pic_rf_send_byte(data); 00087 set_pin(rf_csn_port, rf_csn_pin); 00088 return status; 00089 }
void pic_rf_set_channel | ( | uns8 | channel | ) |
Receive data from nRF24L01 (inline).
Reclocks the essential config to change the current channel used by the nrf2401a.
00386 { 00387 start_crit_sec(); 00388 00389 clear_pin(rf_ce_port, rf_ce_pin); 00390 pic_rf_send_command (RF_WR_REG_RF_CH, // write register 0x05 = RF_CH 00391 &channel, 1); // 7 reserved=0, 6:0 RF_CH = channel 00392 if (rf_current_mode_receive) { 00393 set_pin(rf_ce_port, rf_ce_pin); // Go back on the air! 00394 } 00395 rf_current_channel = channel; 00396 00397 end_crit_sec(); 00398 }
void pic_rf_set_mode | ( | uns8 | mode | ) |
Pass RECEIVE_MODE or TRANSMIT_MODE to change current mode. Generally, you shouldn't need to call this routine. The library assumes you want to receive until you transmit, in which case it switches automatically to transmit mode and back to receive afterwards.
00359 { 00360 uns8 config_reg; 00361 00362 start_crit_sec(); 00363 00364 if ((requested_mode == TRANSMIT_MODE) && (rf_current_mode_receive)) { 00365 // RX -> TX 00366 // need to lower CE to go into stand by mode 00367 clear_pin(rf_ce_port, rf_ce_pin); // Go into standby 00368 pic_rf_read_register(RF_RD_REG_CONFIG_REG, &config_reg, 1); 00369 clear_bit(config_reg, CONFIG_PRIM_RX); 00370 pic_rf_send_command(RF_WR_REG_CONFIG_REG, &config_reg, 1); 00371 rf_current_mode_receive = 0; 00372 } else if ((requested_mode == RECEIVE_MODE) && (!rf_current_mode_receive)) { 00373 // TX -> RX 00374 // CE should already be low, so we're in standby mode 00375 pic_rf_read_register(RF_RD_REG_CONFIG_REG, &config_reg, 1); 00376 set_bit(config_reg, CONFIG_PRIM_RX); 00377 pic_rf_send_command(RF_WR_REG_CONFIG_REG, &config_reg, 1); 00378 set_pin(rf_ce_port, rf_ce_pin); // we're on the air! (rx) 00379 rf_current_mode_receive = 1; 00380 } 00381 00382 end_crit_sec(); 00383 }
void pic_rf_setup | ( | ) |
Setup ports and pins for communication with nRF24L01.
Set up ports and pins to correct input/output for communication with Nordif nrf2401a
00400 { 00401 00402 make_output(rf_ce_port, rf_ce_pin); 00403 make_output(rf_csn_port, rf_csn_pin); 00404 make_output(rf_sck_port, rf_sck_pin); 00405 make_output(rf_mosi_port, rf_mosi_pin); 00406 make_input (rf_miso_port, rf_miso_pin); 00407 make_input (rf_irq_port, rf_irq_pin); 00408 00409 set_pin(rf_csn_port, rf_csn_pin); 00410 clear_pin(rf_ce_port, rf_ce_pin); 00411 }
void pic_rf_transmit | ( | uns8 * | data, | |
uns8 | bytes_to_transmit | |||
) |
Changes to transmit mode, clocks data into the nrf24L01 and hits the shockburst button. Returns to receive mode when finished.
00326 { 00327 00328 uns8 byte_count, bit_count, temp, cd; 00329 start_crit_sec(); 00330 00331 pic_rf_set_mode(TRANSMIT_MODE); 00332 00333 //pic_rf_send_command (RF_FLUSH_TX, 0, 0 ); 00334 pic_rf_read_register_inline(RF_RD_REG_CD, &cd, 1); 00335 serial_print_str("\n cd="); 00336 serial_print_int(cd); 00337 serial_print_nl(); 00338 pic_rf_send_command(RF_W_TX_PAYLOAD, data, bytes_to_transmit); 00339 00340 set_pin(rf_ce_port, rf_ce_pin); 00341 delay_us(10); // 10us pulse - send packet out on airways 00342 clear_pin(rf_ce_port, rf_ce_pin); 00343 delay_us(130); // TX settling 00344 pic_rf_set_mode(RECEIVE_MODE); // go back to receive mode 00345 00346 end_crit_sec(); 00347 }