70 #include <util/delay.h>
74 #include "sicslowmac.h"
78 #define RADIO_CCA_DONE_MASK (1 << 7)
79 #define RADIO_CCA_IDLE_MASK (1 << 6)
81 #define RADIO_START_CCA (1)
83 #define RADIO_TRANSMISSION_SUCCESS (0)
84 #define RADIO_BUSY_CHANNEL (3)
85 #define RADIO_MIN_IEEE_FRAME_LENGTH (5)
111 static radio_rx_callback rx_frame_callback;
112 static uint8_t rssi_val;
113 static uint8_t rx_mode;
119 uint8_t RF230_radio_on, RF230_rsigsi;
120 uint16_t RF230_sendpackets,RF230_receivepackets,RF230_sendfail,RF230_receivefail;
124 static parsed_frame_t parsed_frame;
128 static void radio_rx_start_event(uint32_t
const isr_timestamp, uint8_t
const frame_length);
129 static void radio_trx_end_event(uint32_t
const isr_timestamp);
164 radio_rx_callback rx_callback)
190 if ((version_number != RF230_REVA) && (version_number != RF230_REVB))
205 user_rx_event = rx_event;
206 user_trx_end_event = trx_end_event;
210 rx_frame_callback = rx_callback;
217 radio_frame_data(
void)
219 return rx_frame.
data;
223 radio_frame_length(
void)
230 radio_rx_start_event(uint32_t
const isr_timestamp, uint8_t
const frame_length)
237 user_rx_event(isr_timestamp, frame_length);
242 radio_get_saved_rssi_value(
void)
249 radio_trx_end_event(uint32_t
const isr_timestamp)
251 volatile uint8_t status;
254 if (user_trx_end_event){
255 user_trx_end_event(isr_timestamp);
261 RF230_rsigsi=rssi_val;
262 RF230_receivepackets++;
264 parsed_frame.time = isr_timestamp;
265 parsed_frame.rssi = rssi_val;
277 event_object_t event;
283 case TRAC_SUCCESS_DATA_PENDING:
284 event.event = MAC_EVENT_ACK;
287 case TRAC_CHANNEL_ACCESS_FAILURE:
288 event.event = MAC_EVENT_NACK;
293 case TRAC_SUCCESS_WAIT_FOR_ACK:
333 if ((channel < RF230_MIN_CHANNEL) ||
334 (channel > RF230_MAX_CHANNEL)){
353 if ((trx_state ==
RX_ON) ||
365 return channel_set_status;
396 if (power_level > TX_PWR_17_2DBM){
459 if (ed_threshold > RF230_MAX_ED_THRESHOLD){
493 if ((current_state ==
RX_ON) ||
548 if (voltage_threshold > BATTERY_MONITOR_HIGHEST_VOLTAGE){
584 BATTERY_MONITOR_VOLTAGE_UNDER_THRESHOLD){
588 return batmon_status;
631 if (clock_speed > CLKM_16MHZ){
640 if (direct ==
false){
681 return filter_calibration_status;
718 return pll_calibration_status;
779 bool sleeping =
false;
812 uint8_t original_state;
815 if (!((new_state ==
TRX_OFF) ||
816 (new_state ==
RX_ON) ||
838 if (new_state == original_state){
872 if (original_state ==
TRX_OFF){
885 if (new_state ==
RX_ON ||
893 return set_state_status;
922 return enter_sleep_status;
953 return leave_sleep_status;
994 if (auto_crc_on ==
true){
1045 RF230_sendpackets++;
1088 uint16_t pan_id = ((uint16_t)(pan_id_15_8 << 8)) | pan_id_7_0;
1102 uint8_t pan_byte = new_pan_id & 0xFF;
1105 pan_byte = (new_pan_id >> 8*1) & 0xFF;
1122 uint16_t short_address = ((uint16_t)(short_address_15_8 << 8)) | short_address_7_0;
1124 return short_address;
1136 uint8_t short_address_byte = new_short_address & 0xFF;
1139 short_address_byte = (new_short_address >> 8*1) & 0xFF;
1214 uint8_t back_off_exponent = (be_csma_seed1 & 0xC0) >> 6;
1215 uint8_t csma_retries = (be_csma_seed1 & 0x38) >> 3;
1216 uint8_t seed1 = (be_csma_seed1 & 0x07);
1241 bool success =
false;
1244 uint16_t temp, counter;
1245 uint8_t osccal_saved;
1246 uint8_t tccr2b, tccr1b, tccr1a;
1252 #define TARGETVAL ((1000000ULL * 256 * 32) / F_CPU)
1255 osccal_saved = OSCCAL;
1271 for (counter = 0; counter < 1000; counter++){
1279 TCCR2B = (1 << CS21) | (1 << CS20);
1281 TCCR1B = (1 << CS12) | (1 << CS11);
1284 while (!(TIFR2 & (1 << TOV2))){
1295 if (temp < (uint16_t)(0.995 * TARGETVAL)){
1298 }
else if (temp > (uint16_t)(1.005 * TARGETVAL)){
1317 OSCCAL = osccal_saved;
1342 uint8_t osccal_original = OSCCAL;
1343 volatile uint16_t temp;
1358 while (ASSR & ((1 << TCN2UB)|(1 << OCR2AUB)|(1 << TCR2AUB)|(1 << TCR2BUB))) { ; }
1363 uint8_t counter = 128;
1364 bool cal_ok =
false;
1369 while (ASSR & ((1 << TCN2UB)|(1 << OCR2AUB)|(1 << TCR2AUB)|(1 << TCR2BUB))) { ; }
1380 while (!(TIFR2 & (1 << TOV2))){
1387 #define cal_upper (31250*1.05) // 32812 = 0x802c
1388 #define cal_lower (31250*0.95) // 29687 = 0x73f7
1390 if (temp < cal_lower) {
1393 }
else if (temp > cal_upper) {
1402 }
while ((counter != 0) && (
false == cal_ok));
1404 if (
true != cal_ok) {
1406 OSCCAL = osccal_original;
1411 ASSR &= ~(1 << AS2);