00001
00017 #ifndef __pic_rf_24l01_h
00018 #define __pic_rf_24l01_h
00019
00020 #include "config.h"
00021 #include "pic_utils.h"
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00056 typedef struct _rf_config {
00057 uns8 payload_width_ch2;
00058 uns8 payload_width_ch1;
00059 uns8 address_ch2[5];
00060 uns8 address_ch1[5];
00061 uns8 address_width;
00062 uns8 crystal;
00063 uns8 output_power;
00064 uns8 channel;
00065 uns8 options;
00066 } rf_config;
00067
00068
00069
00071 #define OP_ENABLE_RECEIVE 0
00072
00073 #define OP_LONG_CRC 1
00074
00075 #define OP_ENABLE_CRC 2
00076
00077 #define OP_ENABLE_1_MBPS 5
00078
00079 #define OP_ENABLE_SHOCKBURST 6
00080
00081 #define OP_ENABLE_CH2 7
00082
00083
00085 #define RECEIVE_MODE 1
00086
00087 #define TRANSMIT_MODE 0
00088
00090 static bit rf_current_mode_receive = 0;
00092 static uns8 rf_current_channel = 2;
00093
00094
00095
00096
00097
00098 #define RF_WR_REG_CONFIG_REG 0b00100000
00099 #define RF_RD_REG_CONFIG_REG 0b00000000
00100 #define RF_WR_REG_SETUP_RETR 0b00100100
00101 #define RF_WR_REG_SETUP_AW 0b00100011
00102 #define RF_WR_REG_RF_SETUP 0b00100110
00103 #define RF_WR_REG_TX_ADDR 0b00110000
00104 #define RF_WR_REG_RX_ADDR_P0 0b00101010
00105 #define RF_WR_REG_EN_AA 0b00100001
00106 #define RF_WR_REG_RX_PW_P0 0b00110001
00107 #define RF_WR_REG_RF_CH 0b00100101
00108 #define RF_WR_REG_STATUS 0b00100111
00109
00110 #define RF_RD_REG_RX_PW_P0 0b00010001
00111 #define RF_RD_REG_STATUS 0b00000111
00112 #define RF_RD_REG_FIFO_STATUS 0b00010111
00113 #define RF_RD_REG_CD 0b00001001
00114 #define RF_FLUSH_TX 0b11100001
00115 #define RF_FLUSH_RX 0b11100010
00116
00117
00118 #define RF_W_TX_PAYLOAD 0b10100000
00119 #define RF_R_RX_PAYLOAD 0b01100001
00120
00121
00122 #define RF_NOP 0b11111111
00123
00124
00125 #define CONFIG_MASK_RX_DR 6
00126 #define CONFIG_MASK_TX_DS 5
00127 #define CONFIG_MASK_MAX_RT 4
00128 #define CONFIG_EN_CRC 3
00129 #define CONFIG_CRCO 2
00130 #define CONFIG_PWR_UP 1
00131 #define CONFIG_PRIM_RX 0
00132
00133
00134 #define STATUS_RX_DR 6
00135 #define STATUS_TX_DS 5
00136 #define STATUS_MAX_RT 4
00137
00138 #define STATUS_TX_FULL 0
00139
00140
00141 #define FIFO_STATUS_TX_REUSE 6
00142 #define FIFO_STATUS_TX_FULL 5
00143 #define FIFO_STATUS_TX_EMPTY 4
00144 #define FIFO_STATUS_RX_FULL 1
00145 #define FIFO_STATUS_RX_EMPTY 0
00146
00147
00154 void pic_rf_setup();
00155
00163 void pic_rf_init(rf_config *my_config);
00164
00165
00178 void pic_rf_quick_init(char *my_config, uns8 my_channel, bit my_receive_on);
00186 void pic_rf_transmit(uns8 *data, uns8 bytes_to_transmit);
00187
00188
00195 void pic_rf_receive_inline(uns8 *data, uns8 bytes_to_receive);
00196
00197
00210 void pic_rf_set_channel(uns8 channel);
00211
00218 uns8 pic_rf_receive(uns8 *data, uns8 bytes_to_receive);
00219
00230 uns8 pic_rf_read_register(uns8 cmd, uns8 *data, uns8 data_len);
00231
00240 uns8 pic_rf_send_byte(uns8 b);
00249 uns8 pic_rf_send_byte_int(uns8 b);
00250
00260 void pic_rf_set_mode(uns8 mode);
00261
00272 uns8 pic_rf_send_command(uns8 cmd, uns8 *data, uns8 data_len);
00282 uns8 pic_rf_send_command_single(uns8 cmd, uns8 data);
00283
00284
00296 inline uns8 pic_rf_read_register_inline(uns8 cmd, uns8 *data, uns8 data_len) {
00297
00298 uns8 byte_counter, status;
00299
00300 clear_pin(rf_csn_port, rf_csn_pin);
00301
00302 status = pic_rf_send_byte_int(cmd);
00303
00304 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) {
00305 data[byte_counter] = pic_rf_send_byte_int(0);
00306 }
00307
00308 set_pin(rf_csn_port, rf_csn_pin);
00309
00310 return status;
00311 }
00312
00324 inline uns8 pic_rf_send_command_inline(uns8 cmd, uns8 *data, uns8 data_len) {
00325
00326 uns8 byte_counter, status;
00327
00328 clear_pin(rf_csn_port, rf_csn_pin);
00329 status = pic_rf_send_byte_int(cmd);
00330 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) {
00331 pic_rf_send_byte_int(data[byte_counter]);
00332 }
00333
00334 set_pin(rf_csn_port, rf_csn_pin);
00335 return status;
00336 }
00337
00338
00339 #define pic_rf_get_status() \
00340 pic_rf_read_register(RF_NOP, 0, 0)
00341
00342 #define pic_rf_set_status(status) \
00343 pic_rf_send_command(RF_WR_REG_STATUS, status, 1)
00344
00345 #define pic_rf_receive_mode() pic_rf_set_mode(RECEIVE_MODE)
00346 #define pic_rf_transmit_mode() pic_rf_set_mode(TRANSMIT_MODE)
00347
00348
00349
00350
00351
00352 #ifndef rf_ce_port
00353 #error "pic_rf: You haven't defined rf_ce_port in your config.h!"
00354 #endif
00355 #ifndef rf_ce_pin
00356 #error "pic_rf: You haven't defined rf_ce_pin in your config.h!"
00357 #endif
00358
00359 #ifndef rf_csn_port
00360 #error "pic_rf: You haven't defined rf_csn_port in your config.h!"
00361 #endif
00362 #ifndef rf_csn_pin
00363 #error "pic_rf: You haven't defined rf_csn_pin in your config.h!"
00364 #endif
00365
00366 #ifndef rf_irq_port
00367 #error "pic_rf: You haven't defined rf_irq_port in your config.h!"
00368 #endif
00369 #ifndef rf_irq_pin
00370 #error "pic_rf: You haven't defined rf_irq_pin in your config.h!"
00371 #endif
00372
00373 #ifndef rf_mosi_port
00374 #error "pic_rf: You haven't defined rf_mosi_port in your config.h!"
00375 #endif
00376 #ifndef rf_mosi_pin
00377 #error "pic_rf: You haven't defined rf_mosi_pin in your config.h!"
00378 #endif
00379
00380 #ifndef rf_miso_port
00381 #error "pic_rf: You haven't defined rf_miso_port in your config.h!"
00382 #endif
00383 #ifndef rf_miso_pin
00384 #error "pic_rf: You haven't defined rf_miso_pin in your config.h!"
00385 #endif
00386
00387 #ifndef rf_sck_port
00388 #error "pic_rf: You haven't defined rf_sck_port in your config.h!"
00389 #endif
00390 #ifndef rf_sck_pin
00391 #error "pic_rf: You haven't defined rf_sck_pin in your config.h!"
00392 #endif
00393
00394 #endif