RF routines for the Nordic nRF24L01 chip. More...
#include "config.h"
#include "pic_utils.h"
Go to the source code of this file.
Data Structures | |
struct | rf_config |
Defines | |
#define | CONFIG_CRCO 2 |
#define | CONFIG_EN_CRC 3 |
#define | CONFIG_MASK_MAX_RT 4 |
#define | CONFIG_MASK_RX_DR 6 |
#define | CONFIG_MASK_TX_DS 5 |
#define | CONFIG_PRIM_RX 0 |
#define | CONFIG_PWR_UP 1 |
#define | FIFO_STATUS_RX_EMPTY 0 |
#define | FIFO_STATUS_RX_FULL 1 |
#define | FIFO_STATUS_TX_EMPTY 4 |
#define | FIFO_STATUS_TX_FULL 5 |
#define | FIFO_STATUS_TX_REUSE 6 |
#define | OP_ENABLE_1_MBPS 5 |
#define | OP_ENABLE_CH2 7 |
#define | OP_ENABLE_CRC 2 |
#define | OP_ENABLE_RECEIVE 0 |
#define | OP_ENABLE_SHOCKBURST 6 |
#define | OP_LONG_CRC 1 |
#define | pic_rf_get_status() pic_rf_read_register(RF_NOP, 0, 0) |
#define | pic_rf_receive_mode() pic_rf_set_mode(RECEIVE_MODE) |
#define | pic_rf_set_status(status) pic_rf_send_command(RF_WR_REG_STATUS, status, 1) |
#define | pic_rf_transmit_mode() pic_rf_set_mode(TRANSMIT_MODE) |
#define | RECEIVE_MODE 1 |
#define | RF_FLUSH_RX 0b11100010 |
#define | RF_FLUSH_TX 0b11100001 |
#define | RF_NOP 0b11111111 |
#define | RF_R_RX_PAYLOAD 0b01100001 |
#define | RF_RD_REG_CD 0b00001001 |
#define | RF_RD_REG_CONFIG_REG 0b00000000 |
#define | RF_RD_REG_FIFO_STATUS 0b00010111 |
#define | RF_RD_REG_RX_PW_P0 0b00010001 |
#define | RF_RD_REG_STATUS 0b00000111 |
#define | RF_W_TX_PAYLOAD 0b10100000 |
#define | RF_WR_REG_CONFIG_REG 0b00100000 |
#define | RF_WR_REG_EN_AA 0b00100001 |
#define | RF_WR_REG_RF_CH 0b00100101 |
#define | RF_WR_REG_RF_SETUP 0b00100110 |
#define | RF_WR_REG_RX_ADDR_P0 0b00101010 |
#define | RF_WR_REG_RX_PW_P0 0b00110001 |
#define | RF_WR_REG_SETUP_AW 0b00100011 |
#define | RF_WR_REG_SETUP_RETR 0b00100100 |
#define | RF_WR_REG_STATUS 0b00100111 |
#define | RF_WR_REG_TX_ADDR 0b00110000 |
#define | STATUS_MAX_RT 4 |
#define | STATUS_RX_DR 6 |
#define | STATUS_TX_DS 5 |
#define | STATUS_TX_FULL 0 |
#define | TRANSMIT_MODE 0 |
Functions | |
void | pic_rf_init (rf_config *my_config) |
Initialise nRF24L01 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_inline (uns8 cmd, uns8 *data, uns8 data_len) |
Read nRF24L01 register (inline). | |
uns8 | pic_rf_receive (uns8 *data, uns8 bytes_to_receive) |
Receive data from nRF24L01. | |
void | pic_rf_receive_inline (uns8 *data, uns8 bytes_to_receive) |
Receive data from nRF24L01. | |
uns8 | pic_rf_send_byte (uns8 b) |
Clock a byte into the nRF24L01. | |
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_inline (uns8 cmd, uns8 *data, uns8 data_len) |
Send command to the nrf24l01 (inline). | |
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) |
Receive data from nRF24L01 (inline). | |
void | pic_rf_set_mode (uns8 mode) |
Set rf mode to transmit or receive. | |
void | pic_rf_setup () |
Setup ports and pins for communication with nRF24L01. | |
void | pic_rf_transmit (uns8 *data, uns8 bytes_to_transmit) |
Transmit data from nRF24L01. | |
Variables | |
static uns8 | rf_current_channel = 2 |
static bit | rf_current_mode_receive = 0 |
RF routines for the Nordic nRF24L01 chip
Definition in file pic_rf_24l01.h.
#define CONFIG_CRCO 2 |
Definition at line 159 of file pic_rf_24l01.h.
#define CONFIG_EN_CRC 3 |
Definition at line 158 of file pic_rf_24l01.h.
#define CONFIG_MASK_MAX_RT 4 |
Definition at line 157 of file pic_rf_24l01.h.
#define CONFIG_MASK_RX_DR 6 |
Definition at line 155 of file pic_rf_24l01.h.
#define CONFIG_MASK_TX_DS 5 |
Definition at line 156 of file pic_rf_24l01.h.
#define CONFIG_PRIM_RX 0 |
Definition at line 161 of file pic_rf_24l01.h.
Referenced by pic_rf_set_mode().
#define CONFIG_PWR_UP 1 |
Definition at line 160 of file pic_rf_24l01.h.
#define FIFO_STATUS_RX_EMPTY 0 |
Definition at line 175 of file pic_rf_24l01.h.
#define FIFO_STATUS_RX_FULL 1 |
Definition at line 174 of file pic_rf_24l01.h.
#define FIFO_STATUS_TX_EMPTY 4 |
Definition at line 173 of file pic_rf_24l01.h.
#define FIFO_STATUS_TX_FULL 5 |
Definition at line 172 of file pic_rf_24l01.h.
#define FIFO_STATUS_TX_REUSE 6 |
Definition at line 171 of file pic_rf_24l01.h.
#define OP_ENABLE_1_MBPS 5 |
rf_config options - enable 1 MPS transmition (otherwise 250Kbps)
Definition at line 107 of file pic_rf_24l01.h.
#define OP_ENABLE_CH2 7 |
rf_config options - enable channel 2 reception
Definition at line 111 of file pic_rf_24l01.h.
#define OP_ENABLE_CRC 2 |
rf_config options - enable CRC (recommended!)
Definition at line 105 of file pic_rf_24l01.h.
#define OP_ENABLE_RECEIVE 0 |
rf_config options - enable receive
Definition at line 101 of file pic_rf_24l01.h.
#define OP_ENABLE_SHOCKBURST 6 |
rf_config options - enable shockburst transmition
Definition at line 109 of file pic_rf_24l01.h.
#define OP_LONG_CRC 1 |
rf_config options - enable 16 bit CRC (otherwise 8 bit CRC if 0)
Definition at line 103 of file pic_rf_24l01.h.
#define pic_rf_get_status | ( | ) | pic_rf_read_register(RF_NOP, 0, 0) |
Definition at line 369 of file pic_rf_24l01.h.
#define pic_rf_receive_mode | ( | ) | pic_rf_set_mode(RECEIVE_MODE) |
Definition at line 375 of file pic_rf_24l01.h.
#define pic_rf_set_status | ( | status | ) | pic_rf_send_command(RF_WR_REG_STATUS, status, 1) |
Definition at line 372 of file pic_rf_24l01.h.
#define pic_rf_transmit_mode | ( | ) | pic_rf_set_mode(TRANSMIT_MODE) |
Definition at line 376 of file pic_rf_24l01.h.
#define RECEIVE_MODE 1 |
Mode selection - receive
Definition at line 115 of file pic_rf_24l01.h.
#define RF_FLUSH_RX 0b11100010 |
Definition at line 145 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_FLUSH_TX 0b11100001 |
Definition at line 144 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_NOP 0b11111111 |
Definition at line 152 of file pic_rf_24l01.h.
#define RF_R_RX_PAYLOAD 0b01100001 |
Definition at line 149 of file pic_rf_24l01.h.
Referenced by pic_rf_receive(), and pic_rf_receive_inline().
#define RF_RD_REG_CD 0b00001001 |
Definition at line 143 of file pic_rf_24l01.h.
Referenced by pic_rf_transmit().
#define RF_RD_REG_CONFIG_REG 0b00000000 |
Definition at line 129 of file pic_rf_24l01.h.
Referenced by pic_rf_set_mode().
#define RF_RD_REG_FIFO_STATUS 0b00010111 |
Definition at line 142 of file pic_rf_24l01.h.
Referenced by pic_rf_receive().
#define RF_RD_REG_RX_PW_P0 0b00010001 |
Definition at line 140 of file pic_rf_24l01.h.
#define RF_RD_REG_STATUS 0b00000111 |
Definition at line 141 of file pic_rf_24l01.h.
#define RF_W_TX_PAYLOAD 0b10100000 |
Definition at line 148 of file pic_rf_24l01.h.
Referenced by pic_rf_transmit().
#define RF_WR_REG_CONFIG_REG 0b00100000 |
Definition at line 128 of file pic_rf_24l01.h.
Referenced by pic_rf_init(), and pic_rf_set_mode().
#define RF_WR_REG_EN_AA 0b00100001 |
Definition at line 135 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_WR_REG_RF_CH 0b00100101 |
Definition at line 137 of file pic_rf_24l01.h.
Referenced by pic_rf_init(), and pic_rf_set_channel().
#define RF_WR_REG_RF_SETUP 0b00100110 |
Definition at line 132 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_WR_REG_RX_ADDR_P0 0b00101010 |
Definition at line 134 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_WR_REG_RX_PW_P0 0b00110001 |
Definition at line 136 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_WR_REG_SETUP_AW 0b00100011 |
Definition at line 131 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_WR_REG_SETUP_RETR 0b00100100 |
Definition at line 130 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define RF_WR_REG_STATUS 0b00100111 |
Definition at line 138 of file pic_rf_24l01.h.
Referenced by pic_rf_init(), and pic_rf_receive().
#define RF_WR_REG_TX_ADDR 0b00110000 |
Definition at line 133 of file pic_rf_24l01.h.
Referenced by pic_rf_init().
#define STATUS_MAX_RT 4 |
Definition at line 166 of file pic_rf_24l01.h.
#define STATUS_RX_DR 6 |
Definition at line 164 of file pic_rf_24l01.h.
#define STATUS_TX_DS 5 |
Definition at line 165 of file pic_rf_24l01.h.
#define STATUS_TX_FULL 0 |
Definition at line 168 of file pic_rf_24l01.h.
#define TRANSMIT_MODE 0 |
Mode selection - transmit
Definition at line 117 of file pic_rf_24l01.h.
void pic_rf_init | ( | rf_config * | my_config | ) |
Sends the configuration to the Nordic nRF24L01 chip ready to begin communication. This routine assumes you have already set my_config to the correct values.
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.
Definition at line 83 of file pic_rf_2401a.c.
References rf_config::address_ch1, rf_config::address_ch2, rf_config::address_width, rf_config::channel, clear_pin, rf_config::crystal, end_crit_sec, make_output, rf_config::options, rf_config::output_power, rf_config::payload_width_ch1, rf_config::payload_width_ch2, pic_rf_chip_enable, pic_rf_chip_select, pic_rf_send_byte(), pic_rf_send_bytes(), pic_rf_send_command(), pic_rf_send_command_inline(), rf_current_channel, rf_current_mode_receive, RF_FLUSH_RX, RF_FLUSH_TX, RF_WR_REG_CONFIG_REG, RF_WR_REG_EN_AA, RF_WR_REG_RF_CH, RF_WR_REG_RF_SETUP, RF_WR_REG_RX_ADDR_P0, RF_WR_REG_RX_PW_P0, RF_WR_REG_SETUP_AW, RF_WR_REG_SETUP_RETR, RF_WR_REG_STATUS, RF_WR_REG_TX_ADDR, serial_print_int_hex(), serial_print_str(), set_pin, start_crit_sec, and uns8.
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.
Definition at line 62 of file pic_rf_2401a.c.
References clear_pin, make_output, pic_rf_chip_enable, pic_rf_chip_select, pic_rf_send_byte(), rf_current_channel, rf_current_mode_receive, and uns8.
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 |
Definition at line 92 of file pic_rf_24l01.c.
References clear_pin, pic_rf_send_byte(), set_pin, and uns8.
Referenced by pic_rf_set_mode().
uns8 pic_rf_read_register_inline | ( | uns8 | cmd, | |
uns8 * | data, | |||
uns8 | data_len | |||
) | [inline] |
Internal routine to read a particular nRF24L01 register. Clocks out data_len bytes from the chip. Internal routine. Inline version.
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 |
Definition at line 326 of file pic_rf_24l01.h.
References clear_pin, pic_rf_send_byte_int(), set_pin, and uns8.
Referenced by pic_rf_receive(), pic_rf_receive_inline(), and pic_rf_transmit().
uns8 pic_rf_receive | ( | uns8 * | data, | |
uns8 | bytes_to_receive | |||
) |
Having been notified that there is data available, call this routine to clock the data in from the nRF24L01.
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
!pic_rf_chip_enable(0); // save power
pic_rf_chip_enable(1); // turn chip back on
Definition at line 130 of file pic_rf_2401a.c.
References clear_pin, kill_interrupts, make_input, pic_rf_read_register_inline(), pic_rf_send_command_inline(), RF_R_RX_PAYLOAD, RF_RD_REG_FIFO_STATUS, RF_WR_REG_STATUS, set_pin, test_pin, and uns8.
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.
Definition at line 348 of file pic_rf_24l01.c.
References pic_rf_read_register_inline(), and RF_R_RX_PAYLOAD.
Referenced by pic_rf_receive2().
uns8 pic_rf_send_byte | ( | uns8 | b | ) |
Clock one byte into the nRF24L01. Internal routine.
b | The byte to send |
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
Definition at line 41 of file pic_rf_2401a.c.
References change_pin, clear_pin, set_pin, test_pin, and uns8.
Referenced by pic_rf_init(), pic_rf_quick_init(), pic_rf_read_register(), pic_rf_send_bytes(), pic_rf_send_bytes_inline(), pic_rf_send_command(), pic_rf_send_command_single(), and pic_rf_set_channel().
uns8 pic_rf_send_byte_int | ( | uns8 | b | ) |
Clock one byte into the nRF24L01. Internal routine.
b | The byte to send |
Definition at line 140 of file pic_rf_24l01.c.
References change_pin, clear_pin, set_pin, test_pin, and uns8.
Referenced by pic_rf_read_register_inline(), pic_rf_read_register_int(), and pic_rf_send_command_inline().
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 |
Definition at line 66 of file pic_rf_24l01.c.
References clear_pin, pic_rf_send_byte(), set_pin, and uns8.
Referenced by pic_rf_init(), pic_rf_set_channel(), pic_rf_set_mode(), and pic_rf_transmit().
uns8 pic_rf_send_command_inline | ( | uns8 | cmd, | |
uns8 * | data, | |||
uns8 | data_len | |||
) | [inline] |
Send a command and associated data to the nRF24L01. Internal routine. Inline version.
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 |
Definition at line 354 of file pic_rf_24l01.h.
References clear_pin, pic_rf_send_byte_int(), set_pin, and uns8.
Referenced by pic_rf_init(), and pic_rf_receive().
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 |
Definition at line 80 of file pic_rf_24l01.c.
References clear_pin, pic_rf_send_byte(), set_pin, and uns8.
void pic_rf_set_channel | ( | uns8 | channel | ) |
Having been notified that there is data available, call this routine to clock the data in from the nRF24L01. Change channel on the nRF24L01 Changes the current channel used by the nRF24L01.
Receive data from nRF24L01 (inline).
Reclocks the essential config to change the current channel used by the nrf2401a.
Definition at line 199 of file pic_rf_2401a.c.
References clear_pin, end_crit_sec, kill_interrupts, pic_rf_chip_enable, pic_rf_chip_select, pic_rf_send_byte(), pic_rf_send_command(), rf_current_channel, rf_current_mode_receive, RF_WR_REG_RF_CH, set_pin, and start_crit_sec.
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.
Definition at line 178 of file pic_rf_2401a.c.
References change_pin, clear_pin, CONFIG_PRIM_RX, end_crit_sec, kill_interrupts, make_output, pic_rf_chip_enable, pic_rf_chip_select, pic_rf_read_register(), pic_rf_send_command(), RECEIVE_MODE, rf_current_mode_receive, RF_RD_REG_CONFIG_REG, RF_WR_REG_CONFIG_REG, set_pin, start_crit_sec, TRANSMIT_MODE, and uns8.
Referenced by pic_rf_transmit().
void pic_rf_setup | ( | ) |
Set up ports and pins to correct input/output for communication with Nordic nRF24L01
Setup ports and pins for communication with nRF24L01.
Set up ports and pins to correct input/output for communication with Nordif nrf2401a
Definition at line 221 of file pic_rf_2401a.c.
References clear_pin, make_input, make_output, and set_pin.
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.
Definition at line 326 of file pic_rf_24l01.c.
References clear_pin, end_crit_sec, pic_rf_read_register_inline(), pic_rf_send_command(), pic_rf_set_mode(), RECEIVE_MODE, RF_RD_REG_CD, RF_W_TX_PAYLOAD, serial_print_int(), serial_print_nl(), serial_print_str(), set_pin, start_crit_sec, TRANSMIT_MODE, and uns8.
uns8 rf_current_channel = 2 [static] |
Maintain state of current channel
Definition at line 122 of file pic_rf_24l01.h.
bit rf_current_mode_receive = 0 [static] |
Maintain state of current mode (1 = receive mode)
Definition at line 120 of file pic_rf_24l01.h.