int8_t _bmac_rx() { int8_t n; uint8_t cnt; rf_set_rx (&bmac_rfRxInfo, g_chan); rf_polling_rx_on (); cnt=0; while ((n = rf_rx_check_fifop()) == 0) { cnt++; nrk_wait(_bmac_check_period); if(cnt>2) { #ifdef DEBUG printf( "rx timeout 1 %d\r\n",cnt ); #endif if(rx_failure_cnt<65535) rx_failure_cnt++; rf_rx_off(); return 0; } } if (n != 0) { n = 0; // Packet on its way cnt=0; while ((n = rf_polling_rx_packet ()) == 0) { cnt++; nrk_spin_wait_us(100); if (cnt > 50) { #ifdef DEBUG printf( "rx timeout 2\r\n" ); #endif rx_failure_cnt++; rf_rx_off(); return 0; } } } rf_rx_off(); if (n == 1) { // CRC and checksum passed rx_buf_empty=0; #ifdef DEBUG printf( "BMAC: SNR= %d [",bmac_rfRxInfo.rssi ); for(uint8_t i=0; i<bmac_rfRxInfo.length; i++ ) printf( "%c", bmac_rfRxInfo.pPayload[i]); printf( "]\r\n" ); #endif return 1; } else { #ifdef DEBUG printf( "CRC failed!\r\n" ); #endif rx_failure_cnt++; return 0; } rx_failure_cnt++; return 0; }
int8_t _bmac_channel_check() { int8_t val; rf_polling_rx_on(); nrk_spin_wait_us(250); val=CCA_IS_1; if(val) rf_rx_off(); return val; }
int8_t _bmac_channel_check() { int8_t val; rf_polling_rx_on(); nrk_spin_wait_us(250); rf_rx_check_cca(); // Run once to throw out (possibly) stale data val = rf_rx_check_cca(); if(val) rf_rx_off(); return val; }
void _isa_rx (uint8_t slot) { uint8_t n; uint32_t node_mask; volatile uint8_t timeout; #ifdef LED_DEBUG nrk_led_set(1); #endif rf_set_rx (&isa_rfRxInfo, isa_param.channel); // sets rx buffer and channel rf_polling_rx_on (); // Timing for waiting for sfd timeout = _nrk_os_timer_get(); timeout+=4; // 4ms n = 0; //nrk_gpio_set(NRK_DEBUG_3); while ((n = rf_rx_check_sfd()) == 0) { if (_nrk_os_timer_get() > timeout) { //spend too much time on waiting for a pkt's arrival rf_rx_off (); #ifdef LED_DEBUG nrk_led_clr(1); #endif #ifdef RX_DEBUG printf("sfd times out.\n\r"); #endif return; } } //printf("%d\n\r",_nrk_high_speed_timer_get()); // sfd received, start receiving packet and record start time rx_start_time = _nrk_high_speed_timer_get(); // Timing for waiting for finishing packet receiving timeout = _nrk_os_timer_get(); timeout += 5; // 5ms if (n != 0) { n = 0; //printf("Packet on its way\n\r"); while ((n = rf_polling_rx_packet (false,128)) == 0) { //printf("%d\n\r",_nrk_os_timer_get()); if (_nrk_os_timer_get () > timeout) { #ifdef RX_DEBUG printf("packet is too long, times out.\n\r"); #endif // spend too much time on receiving pkt. return; // huge timeout as fail safe } } } rf_rx_off (); if (n == 1) {// successfully received packet isa_rx_data_ready = 1; //potential problem: if repeater or recipient receives noise, the DHDR would be changed. And it is highly possible that the highest bit of DHDR would be set to 0 /*if(isa_node_mode != ISA_GATEWAY) DHDR = isa_rfRxInfo.pPayload[DHDR_INDEX];*/ #ifdef RX_DEBUG printf("Repeater slot = %d, local slot is %d.\n\r", isa_rfRxInfo.pPayload[SLOT_INDEX],global_slot); #endif RX_DEBUG nrk_event_signal(isa_rx_pkt_signal); //_nrk_high_speed_timer_reset(); //nrk_high_speed_timer_wait(0,CPU_PROCESS_TIME); //nrk_gpio_set(NRK_DEBUG_3); node_mask = ((uint32_t) 1) << isa_rfRxInfo.pPayload[SRC_INDEX]; if( !(node_mask & child_list)) return; //FIXME change // ACK required if(DHDR & (1<<7)){ // Transmit ACK packet DHR = configDHR(); isa_ack_buf[DHR_INDEX]= DHR; #ifdef ACK_DEBUG //printf("DHR is %d.\n\r",DHR); #endif isa_ack_tx.pPayload = isa_ack_buf; if (DHDR & (1<<2)){ // recipient , only reply explicit ACK //isa_ack_tx.length = PKT_DATA_START-1; isa_ack_tx.length = 2; } else { //reply ACK with time offsetX offsetX = rx_start_time - slot_start_time; //printf("slot_start_time is %d,rx_start_time is %d.\n\r",slot_start_time,rx_start_time); uint8_t temp1,temp2; temp1 = (offsetX & 0xFF00)>>8; isa_ack_buf[OFFSET_HIGH]=temp1; temp2 = (offsetX & 0x00FF); isa_ack_buf[OFFSET_LOW]=temp2; #ifdef ACK_DEBUG printf("offsetX is %d\n\r", offsetX); #endif //isa_ack_tx.length = PKT_DATA_START + 1; isa_ack_tx.length = 4; } rf_tx_tdma_packet (&isa_ack_tx,slot_start_time,isa_param.tx_guard_time,&tx_start_time); }
//------------------------------------------------------------------------------ // void main (void) // // DESCRIPTION: // Startup routine and main loop //------------------------------------------------------------------------------ int main (void) { uint8_t cnt,i,length,n; nrk_setup_ports(); nrk_setup_uart (UART_BAUDRATE_115K2); printf( "Receiver\r\n" ); nrk_led_clr(0); nrk_led_clr(1); nrk_led_clr(2); nrk_led_clr(3); rfRxInfo.pPayload = rx_buf; rfRxInfo.max_length = RF_MAX_PAYLOAD_SIZE; rfRxInfo.ackRequest= 0; nrk_int_enable(); rf_init (&rfRxInfo, 13, 0x2420, 0x1215); printf( "Waiting for packet...\r\n" ); nrk_led_set(ORANGE_LED); while(1) { nrk_led_set(BLUE_LED); rf_polling_rx_on(); while ((n = rf_rx_check_sfd()) == 0) continue; if (n != 0) { nrk_led_toggle(ORANGE_LED); n = 0; // Packet on its way cnt=0; while ((n = rf_polling_rx_packet ()) == 0) { nrk_led_toggle(GREEN_LED); if (cnt > 50) { //printf( "PKT Timeout\r\n" ); break; // huge timeout as failsafe } halWait(10000); cnt++; } } nrk_led_clr(BLUE_LED); if (n == 1) { nrk_led_clr(RED_LED); // nrk_led_toggle(BLUE_LED); // CRC and checksum passed // printf("packet received\r\n"); // printf("SEQNUM: %d SRCADDR: 0x%x SNR: %d\r\n[",rfRxInfo.seqNumber, rfRxInfo.srcAddr, rfRxInfo.rssi); // printf("\r\n%d, ",rfRxInfo.seqNumber); for(i=0; i<rfRxInfo.length; i++ ) // printf( "%c", rfRxInfo.pPayload[i]); putchar(rfRxInfo.pPayload[i]); // printf( "]\r\n\r\n" ); // printf("\r\nR%d = %d",rfRxInfo.seqNumber,rfRxInfo.length); } else if(n != 0) { printf( "CRC failed!\r\n" ); nrk_led_set(RED_LED); } } }