Pic Nordic nrf2401a routines. More...
Go to the source code of this file.
Data Structures | |
struct | rf_config |
Defines | |
#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_chip_enable(value) change_pin(rf_ce_port, rf_ce_pin, value); |
#define | pic_rf_chip_select(value) change_pin(rf_cs_port, rf_cs_pin, value); |
#define | pic_rf_init_inline(my_config) |
Inline version of the pic_rf_init routine. | |
#define | pic_rf_receive_mode() pic_rf_set_mode(RECEIVE_MODE) |
#define | pic_rf_transmit_mode() pic_rf_set_mode(TRANSMIT_MODE) |
#define | RECEIVE_MODE 1 |
#define | TRANSMIT_MODE 0 |
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. | |
void | pic_rf_receive (uns8 *data, uns8 bytes_to_receive) |
Receive data from nrf2401a. | |
void | pic_rf_send_byte (uns8 b) |
Internal routine to send a byte to nrf2401a. | |
void | pic_rf_send_bytes (char *bytes, uns8 num_bytes) |
Internal routine to send bytes to nrf2401a. | |
void | pic_rf_send_bytes_inline (char *bytes, uns8 num_bytes) |
Inline version of send_bytes. | |
void | pic_rf_set_channel (uns8 channel) |
Change channel on the nrf2401a. | |
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 nrf2401a. | |
void | pic_rf_transmit (char *data, uns8 bytes_to_transmit) |
Transmit data from nrf2401a. | |
Variables | |
static uns8 | rf_current_channel = 2 |
static bit | rf_current_mode_receive = 0 |
Nordic nrf2401a routines
#define OP_ENABLE_1_MBPS 5 |
rf_config options - enable 1 MPS transmition (otherwise 250Kbps)
#define OP_ENABLE_CH2 7 |
rf_config options - enable channel 2 reception
#define OP_ENABLE_CRC 2 |
rf_config options - enable CRC (recommended!)
#define OP_ENABLE_RECEIVE 0 |
rf_config options - enable receive
#define OP_ENABLE_SHOCKBURST 6 |
rf_config options - enable shockburst transmition
#define OP_LONG_CRC 1 |
rf_config options - enable 16 bit CRC (otherwise 8 bit CRC if 0)
#define pic_rf_chip_enable | ( | value | ) | change_pin(rf_ce_port, rf_ce_pin, value); |
Set chip enable line (CE) to the given value
#define pic_rf_chip_select | ( | value | ) | change_pin(rf_cs_port, rf_cs_pin, value); |
Set chip select line (CS) to the given value
#define pic_rf_init_inline | ( | my_config | ) |
Depending upon your chip, if you're using the programmatic method of configuring the nrf2401a you may find that this routine helps you use less flash and/or stack space
#define pic_rf_receive_mode | ( | ) | pic_rf_set_mode(RECEIVE_MODE) |
Change to receive mode
#define pic_rf_transmit_mode | ( | ) | pic_rf_set_mode(TRANSMIT_MODE) |
Change to transmit mode
#define RECEIVE_MODE 1 |
Mode selection - receive
#define TRANSMIT_MODE 0 |
Mode selection - transmit
void pic_rf_init | ( | rf_config * | my_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.
00084 { 00085 uns8 temp; 00086 uns8 options; 00087 00088 make_output(rf_data_port, rf_data_pin); // make data pin output 00089 clear_pin(rf_clk1_port, rf_clk1_pin); // make sure it's 0 00090 00091 pic_rf_chip_enable(0); 00092 pic_rf_chip_select(1); // config mode 00093 00094 pic_rf_send_byte(my_config->payload_width_ch2); 00095 pic_rf_send_byte(my_config->payload_width_ch1); 00096 pic_rf_send_bytes(my_config->address_ch2, 5); 00097 pic_rf_send_bytes(my_config->address_ch1, 5); 00098 00099 options = my_config->options; 00100 00101 temp = my_config->address_width << 2; 00102 temp.1 = options.OP_LONG_CRC; // |= my_config->long_crc << 1; 00103 temp.0 = options.OP_ENABLE_CRC; // |= my_config->enable_crc; 00104 00105 pic_rf_send_byte(temp); 00106 00107 temp = options & 0b11100000; //pull off top three bits 00108 //temp.7 = options.ENABLE_CH2; // |= my_config->enable_ch2 << 7; 00109 //temp.6 = options.ENABLE_SHOCKBURST; // |= my_config->enable_shockburst << 6; 00110 //temp.5 = options.ENABLE_1_MBPS; //my_config->enable_1_mbps << 5; 00111 temp |= (my_config->crystal & 0b00000111) << 2; // bits 4,3,2 - mask 3 bit range 00112 temp |= (my_config->output_power & 0b00000011); // bits 1,0 - mask 2 bit range 00113 00114 pic_rf_send_byte(temp); 00115 00116 temp = my_config->channel << 1; 00117 rf_current_channel = my_config->channel; 00118 00119 temp |= options.OP_ENABLE_RECEIVE; // my_config->enable_receive; 00120 rf_current_mode_receive = options.OP_ENABLE_RECEIVE; 00121 00122 pic_rf_send_byte(temp); 00123 00124 pic_rf_chip_select(0); // config mode ended 00125 pic_rf_chip_enable(1); // on the air! 00126 00127 }
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.
00062 { 00063 00064 uns8 byte_counter; 00065 make_output(rf_data_port, rf_data_pin); // make data pin output 00066 clear_pin(rf_clk1_port, rf_clk1_pin); // make sure it's 0 00067 00068 pic_rf_chip_enable(0); 00069 pic_rf_chip_select(1); // config mode 00070 00071 for(byte_counter = 0 ; byte_counter < 15 ; byte_counter++) { 00072 pic_rf_send_byte(my_config[byte_counter]); 00073 } 00074 rf_current_channel = my_channel; 00075 rf_current_mode_receive = my_receive_on; 00076 00077 pic_rf_chip_select(0); // config mode ended 00078 pic_rf_chip_enable(1); // on the air! 00079 00080 }
void 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 nrf2401a.
!pic_rf_chip_enable(0); // save power
pic_rf_chip_enable(1); // turn chip back on
00130 { 00131 uns8 byte_count, bit_count, temp; 00132 00134 bit my_store_gie = intcon.GIE; 00135 kill_interrupts(); 00136 00137 make_input(rf_data_port, rf_data_pin); // make data pin input 00138 00139 for (byte_count = 0; byte_count < bytes_to_receive; byte_count++) { 00140 00141 for (bit_count = 0; bit_count < 8; bit_count++) { 00142 temp <<= 1; 00143 temp.0 = test_pin(rf_data_port, rf_data_pin); 00144 set_pin(rf_clk1_port, rf_clk1_pin); // clock it out 00145 clear_pin(rf_clk1_port, rf_clk1_pin); // ready for next bit 00146 } 00147 data[byte_count] = temp; 00148 } 00149 00151 intcon.GIE = my_store_gie; 00152 }
void pic_rf_send_byte | ( | uns8 | b | ) |
Internal routine to send a byte to the nrf2401a. Generally you shouldn't need to use this, see pic_rf_transmit instead
00042 { 00043 uns8 bit_counter; 00044 for(bit_counter = 0 ; bit_counter < 8 ; bit_counter++) { 00045 change_pin(rf_data_port, rf_data_pin, b.7); // Put data on data pin 00046 set_pin(rf_clk1_port, rf_clk1_pin); // clock it in (positive edge) 00047 clear_pin(rf_clk1_port, rf_clk1_pin); // ready for next bit 00048 00049 b <<= 1; // Move all the bits left 00050 } // repeat until finished 00051 00052 } // pic_rf_send_byte
void pic_rf_send_bytes | ( | char * | bytes, | |
uns8 | num_bytes | |||
) |
Internal routine to send bytes to the nrf2401a. Generally you shouldn't need to use this, see pic_rf_transmit instead
00054 { 00055 00056 uns8 byte_counter; 00057 for(byte_counter = 0 ; byte_counter < num_bytes ; byte_counter++) { 00058 pic_rf_send_byte(bytes[byte_counter]); 00059 } 00060 }
void pic_rf_send_bytes_inline | ( | char * | bytes, | |
uns8 | num_bytes | |||
) | [inline] |
Inline version of pc_rf_send_bytes. This is an internal routine, and generally you shouldn't need to call it. Use pic_rf_transmit instead.
00273 { 00274 00275 uns8 byte_counter; 00276 for(byte_counter = 0 ; byte_counter < num_bytes ; byte_counter++) { 00277 pic_rf_send_byte(bytes[byte_counter]); 00278 } 00279 }
void pic_rf_set_channel | ( | uns8 | channel | ) |
Reclocks the essential config to change the current channel used by the nrf2401a.
00200 { 00201 bit my_store_gie = intcon.GIE; 00202 kill_interrupts(); 00203 00204 clear_bit(tris_array[rf_data_port - PORTA], rf_data_pin); // make data pin output 00205 00206 pic_rf_chip_enable(0); 00207 pic_rf_chip_select(1); // config mode 00208 00209 rf_current_channel = channel; 00210 channel <<= 1; 00211 channel |= rf_current_mode_receive; 00212 00213 pic_rf_send_byte(channel); 00214 00215 pic_rf_chip_select(0); // config mode ended 00216 pic_rf_chip_enable(1); 00217 00218 intcon.GIE = my_store_gie; 00219 }
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.
00179 { 00180 bit my_store_gie = intcon.GIE; 00181 kill_interrupts(); 00182 00183 make_output(rf_data_port, rf_data_pin); // make data pin output 00184 pic_rf_chip_enable(0); 00185 pic_rf_chip_select(1); // config mode 00186 00187 change_pin(rf_data_port, rf_data_pin, mode); // send a zero 00188 set_pin(rf_clk1_port, rf_clk1_pin); // clock it in (positive edge) 00189 clear_pin(rf_clk1_port, rf_clk1_pin); // ready for next bit 00190 00191 pic_rf_chip_select(0); // config mode ended 00192 pic_rf_chip_enable(1); 00193 00194 rf_current_mode_receive = mode; 00195 00196 intcon.GIE = my_store_gie; 00197 }
void pic_rf_setup | ( | ) |
Set up ports and pins to correct input/output for communication with Nordif nrf2401a
00221 { 00222 00223 make_output(rf_data_port, rf_data_pin); // make data pin output 00224 make_output(rf_cs_port, rf_cs_pin); // make cs pin output 00225 make_output(rf_ce_port, rf_ce_pin); // make ce pin output 00226 make_input (rf_dr1_port, rf_dr1_pin); // make dr1 pin input 00227 make_output(rf_clk1_port, rf_clk1_pin); // make clk1 pin output 00228 00229 }
void pic_rf_transmit | ( | char * | data, | |
uns8 | bytes_to_transmit | |||
) |
Changes to transmit mode, clocks data into the nRF2401A and hits the shockburst button. Returns to receive mode when finished.
00154 { 00155 00156 uns8 byte_count, bit_count, temp; 00157 00158 bit my_store_gie = intcon.GIE; 00159 kill_interrupts(); 00160 00161 if (rf_current_mode_receive) { 00162 pic_rf_set_mode(TRANSMIT_MODE); 00163 } 00164 00165 make_output(rf_data_port, rf_data_pin); // make data pin output 00166 00167 pic_rf_send_bytes_inline(data, bytes_to_transmit); 00168 00169 pic_rf_chip_enable(0); // On low, enable shockburst 00170 00171 delay_ms(1); 00172 00173 pic_rf_set_mode(RECEIVE_MODE); // Go back to receive mode 00174 00175 intcon.GIE = my_store_gie; 00176 }
uns8 rf_current_channel = 2 [static] |
Maintain state of current channel
bit rf_current_mode_receive = 0 [static] |
Maintain state of current mode (1 = receive mode)