00001 #include "pic_rf_24l01.h"
00002 #include "pic_serial.h"
00003 #include <stdlib.h>
00004
00005 uns8 pic_rf_handle_irq_receive(uns8 *data, uns8 bytes_to_receive) {
00006
00007 uns8 fifo_status;
00008 uns8 res;
00009
00010 res = 0;
00011
00012
00013 clear_pin(rf_ce_port, rf_ce_pin);
00014 pic_rf_read_register_inline(RF_RD_REG_FIFO_STATUS, &fifo_status, 1);
00015 if (!test_bit(fifo_status, 0)) {
00016 pic_rf_read_register_inline(RF_R_RX_PAYLOAD, data, bytes_to_receive);
00017 res = 1;
00018 }
00019
00020 pic_rf_send_command_inline(RF_WR_REG_STATUS, "\x40", 1);
00021 set_pin(rf_ce_port, rf_ce_pin);
00022 return res;
00023 }
00024
00025
00026 uns8 pic_rf_send_command(uns8 cmd, uns8 *data, uns8 data_len) {
00027
00028 uns8 byte_counter, status;
00029
00030 clear_pin(rf_csn_port, rf_csn_pin);
00031
00032
00033 status = pic_rf_send_byte(cmd);
00034 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) {
00035
00036
00037
00038 pic_rf_send_byte(data[byte_counter]);
00039 }
00040
00041 set_pin(rf_csn_port, rf_csn_pin);
00042 return status;
00043 }
00044
00045 uns8 pic_rf_read_register(uns8 cmd, uns8 *data, uns8 data_len) {
00046
00047 uns8 byte_counter, status;
00048
00049 clear_pin(rf_csn_port, rf_csn_pin);
00050
00051
00052 status = pic_rf_send_byte(cmd);
00053 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) {
00054
00055
00056
00057 data[byte_counter] = pic_rf_send_byte(0);
00058 }
00059
00060 set_pin(rf_csn_port, rf_csn_pin);
00061
00062 return status;
00063 }
00064
00065 uns8 pic_rf_read_register_int(uns8 cmd, uns8 *data, uns8 data_len) {
00066
00067 uns8 byte_counter, status;
00068
00069 clear_pin(rf_csn_port, rf_csn_pin);
00070
00071
00072 status = pic_rf_send_byte_int(cmd);
00073 for(byte_counter = 0 ; byte_counter < data_len ; byte_counter++) {
00074
00075
00076
00077 data[byte_counter] = pic_rf_send_byte_int(0);
00078 }
00079
00080 set_pin(rf_csn_port, rf_csn_pin);
00081
00082 return status;
00083 }
00084
00085
00086
00087
00088 uns8 pic_rf_send_byte(uns8 b)
00089 {
00090 uns8 bit_counter, status;
00091
00092 for(bit_counter = 0 ; bit_counter < 8 ; bit_counter++) {
00093 change_pin(rf_mosi_port, rf_mosi_pin, b.7);
00094 set_pin(rf_sck_port, rf_sck_pin);
00095 status <<= 1;
00096 status.0 = test_pin(rf_miso_port, rf_miso_pin);
00097 clear_pin(rf_sck_port, rf_sck_pin);
00098
00099 b <<= 1;
00100 }
00101
00102
00103
00104 return status;
00105 }
00106
00107
00108 uns8 pic_rf_send_byte_int(uns8 b)
00109 {
00110 uns8 bit_counter, status;
00111
00112 for(bit_counter = 0 ; bit_counter < 8 ; bit_counter++) {
00113 change_pin(rf_mosi_port, rf_mosi_pin, b.7);
00114 set_pin(rf_sck_port, rf_sck_pin);
00115 status <<= 1;
00116 status.0 = test_pin(rf_miso_port, rf_miso_pin);
00117 clear_pin(rf_sck_port, rf_sck_pin);
00118
00119 b <<= 1;
00120 }
00121 return status;
00122 }
00123
00124
00125 void pic_rf_send_bytes(char *bytes, uns8 num_bytes) {
00126
00127 uns8 byte_counter;
00128 for(byte_counter = 0 ; byte_counter < num_bytes ; byte_counter++) {
00129 pic_rf_send_byte(bytes[byte_counter]);
00130 }
00131 }
00132
00133 void pic_rf_quick_init(char *my_config, uns8 my_channel, bit my_receive_on) {
00134
00135
00136 }
00137
00138
00139 void pic_rf_init(rf_config *my_config)
00140 {
00141 uns8 temp;
00142 uns8 options;
00143 uns8 data[5];
00144 print_str("init\n");
00145
00146 start_crit_sec();
00147
00148 clear_pin(rf_ce_port, rf_ce_pin);
00149 delay_ms(100);
00150
00151 temp = pic_rf_send_command(RF_WR_REG_CONFIG_REG,
00152 "\x3c", 1);
00153
00154
00155
00156 pic_rf_send_command(RF_WR_REG_SETUP_RETR,
00157 "\x00", 1);
00158
00159
00160 pic_rf_send_command(RF_WR_REG_SETUP_AW,
00161 "\x01", 1);
00162
00163
00164 pic_rf_send_command(RF_WR_REG_RF_SETUP,
00165 "\x07", 1);
00166
00167 pic_rf_send_command (RF_WR_REG_RF_CH,
00168 "\x02", 1);
00169
00170 data[0] = 0b11100111;
00171 data[1] = 0b11100111;
00172 data[2] = 0b11100111;
00173 pic_rf_send_command(RF_WR_REG_TX_ADDR,
00174 &data, 3);
00175
00176
00177 pic_rf_send_command(RF_WR_REG_RX_ADDR_P0,
00178 &data, 3);
00179
00180 pic_rf_send_command(RF_WR_REG_EN_AA,
00181 "\x00", 1);
00182
00183
00184
00185 pic_rf_send_command(RF_WR_REG_RX_PW_P0,
00186 "\x11", 1);
00187
00188 pic_rf_send_command(RF_WR_REG_CONFIG_REG,
00189 "\x3f", 1);
00190
00191
00192 delay_ms(2);
00193 pic_rf_send_command (RF_FLUSH_TX,
00194 0, 0 );
00195 pic_rf_send_command (RF_FLUSH_RX,
00196 0, 0 );
00197 delay_ms(2);
00198
00199 set_pin(rf_ce_port, rf_ce_pin);
00200
00201 end_crit_sec();
00202 print_str("got=");
00203 print_int(temp);
00204 print_str(" ");
00205 }
00206
00207
00208 void pic_rf_transmit(uns8 *data, uns8 bytes_to_transmit) {
00209
00210 uns8 byte_count, bit_count, temp;
00211
00212 start_crit_sec();
00213
00214 if (rf_current_mode_receive) {
00215 pic_rf_set_mode(TRANSMIT_MODE);
00216 }
00217
00218 pic_rf_send_command (RF_FLUSH_TX, 0, 0 );
00219
00220 pic_rf_send_command(RF_W_TX_PAYLOAD, data, bytes_to_transmit);
00221
00222 set_pin(rf_ce_port, rf_ce_pin);
00223 delay_us(10);
00224 clear_pin(rf_ce_port, rf_ce_pin);
00225
00226 pic_rf_set_mode(RECEIVE_MODE);
00227
00228 end_crit_sec();
00229 }
00230
00231 void pic_rf_set_mode(uns8 requested_mode)
00232 {
00233 uns8 config_reg;
00234
00235 start_crit_sec();
00236
00237 if ((requested_mode == TRANSMIT_MODE) && (rf_current_mode_receive)) {
00238
00239
00240 clear_pin(rf_ce_port, rf_ce_pin);
00241 pic_rf_read_register(RF_RD_REG_CONFIG_REG, &config_reg, 1);
00242 clear_bit(config_reg, CONF_PRIM_RX);
00243 pic_rf_send_command(RF_WR_REG_CONFIG_REG, &config_reg, 1);
00244 rf_current_mode_receive = 0;
00245 } else if ((requested_mode == RECEIVE_MODE) && (!rf_current_mode_receive)) {
00246
00247
00248 pic_rf_read_register(RF_RD_REG_CONFIG_REG, &config_reg, 1);
00249 set_bit(config_reg, CONF_PRIM_RX);
00250 pic_rf_send_command(RF_WR_REG_CONFIG_REG, &config_reg, 1);
00251 set_pin(rf_ce_port, rf_ce_pin);
00252 rf_current_mode_receive = 1;
00253 }
00254
00255 end_crit_sec();
00256 }
00257
00258 void pic_rf_set_channel(uns8 channel)
00259 {
00260 start_crit_sec();
00261
00262 clear_pin(rf_ce_port, rf_ce_pin);
00263 pic_rf_send_command (RF_WR_REG_RF_CH,
00264 &channel, 1);
00265 if (rf_current_mode_receive) {
00266 set_pin(rf_ce_port, rf_ce_pin);
00267 }
00268 rf_current_channel = channel;
00269
00270 end_crit_sec();
00271 }
00272
00273 void pic_rf_setup() {
00274 print_str("pic_rf_setup\n");
00275 make_output(rf_ce_port, rf_ce_pin);
00276 make_output(rf_csn_port, rf_csn_pin);
00277 make_output(rf_sck_port, rf_sck_pin);
00278 make_output(rf_mosi_port, rf_mosi_pin);
00279 make_input (rf_miso_port, rf_miso_pin);
00280 make_input (rf_irq_port, rf_irq_pin);
00281
00282 set_pin(rf_csn_port, rf_csn_pin);
00283 clear_pin(rf_ce_port, rf_ce_pin);
00284 }
00285