void appMain(void) { static uint8_t buffer[100]; radioSetTxPower(TX_POWER); radioSetReceiveHandle(rcvRadio); radioOn(); uint32_t lastTime = getTimeMs(); for (;;) { int8_t rssi = radioGetLastRSSI(); if ((int32_t) getTimeMs() - (int32_t) lastTime > 1000) { radioSend(buffer, sizeof(buffer)); PRINTF("rssi=%d\n", rssi); lastTime = getTimeMs(); if (rxOk) --rxOk; if (!rxOk) redLedOn(); else redLedOff(); } greenLedToggle(); busyWait(getWaitInterval(rssi)); } }
//Recieved radio packet, send over UART void xbeeHandleRx(MacPacket packet) { int i; unsigned char checksum; unsigned char pld_len = payGetPayloadLength(packet->payload); unsigned char* pld_str = payToString(packet->payload); unsigned char xb_frame_len = pld_len + XB_OVERHEAD_LENGTH; WordVal src_addr = packet->src_addr; dmaTxReady = 0; checksum = XB_API_ID; checksum += src_addr.byte.HB; checksum += src_addr.byte.LB; //CRITICAL_SECTION_START txBufferA[0] = XB_RX_START; //Start Byte txBufferA[1] = 0x00; //Length High Byte txBufferA[2] = xb_frame_len; //Length Low Byte txBufferA[3] = XB_API_ID; //API Identifier - Currently only support the RX type txBufferA[4] = src_addr.byte.HB; //Source Address High byte txBufferA[5] = src_addr.byte.LB; //Source Address Low Byte txBufferA[6] = radioGetLastRSSI(); //'RSSI' Not currently implemented txBufferA[7] = 0x00; //'Options' Not currently implemented for(i = 0; i < pld_len; i++) { checksum += pld_str[i]; txBufferA[RX_FRAME_OFFSET + i] = pld_str[i]; } txBufferA[RX_FRAME_OFFSET + pld_len] = 0xFF - checksum; //set Checksum byte //CRITICAL_SECTION_END // pld_len + RX_FRAME_OFFSET + length(CHKSUM) - 1 DMA6CNT = pld_len + RX_FRAME_OFFSET; DMA6CONbits.CHEN = 1; DMA6REQbits.FORCE = 1; }
// -------------------------------------------- // -------------------------------------------- void onRadioRecv(void) { static bool flRxProcessing=false; if(flRxProcessing){ #ifdef PRINT_PACKETS PRINTF("RX Locked\n"); #endif return; } flRxProcessing=true; // There is a chance for a small race condition #ifdef PRINT_PACKETS uint32_t rxTime = getTimeMs(); #endif int16_t rxLen; rssi_t rssi; lqi_t lqi; rxIdx++; if( rxIdx < 0 ) rxIdx=0; led1Toggle(); // rxLen = radioRecv( &(DB_REC(db)), DB_REC_SIZE(db)); // DB_REC(db).recLen = rxLen; rxLen = radioRecv(&radioBuffer, sizeof(radioBuffer)); rssi = radioGetLastRSSI(); lqi = radioGetLastLQI(); #ifdef PRINT_PACKETS PRINTF("%d\t%d\t%d\t%d\t%ld\t", (int)rxIdx, (int)rxLen, (int)rssi, (int)lqi, (long)rxTime); #endif #ifdef PRINT_PACKETS if (rxLen < 0) { PRINTF("RX failed\n"); } else if (rxLen > 0 ) { debugHexdump((uint8_t *) &radioBuffer, rxLen); // debugHexdump((uint8_t *) &(DB_REC(db)), rxLen); } #endif if (rxLen < 0) { led2Toggle(); flRxProcessing=false; return; } if( ! MSG_SIGNATURE_OK(radioBuffer) ) { flRxProcessing = false; return; } // Anticipated payload types. MSG_NEW_PAYLOAD_PTR(radioBuffer, phaser_ping_t, test_data_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, phaser_control_t, ctrl_data_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, msg_text_data_t, msg_text_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, test_config_t, test_config_p); int act = MSG_ACT_CLEAR; bool flOK=true; switch( radioBuffer.id ){ case PH_MSG_Test: MSG_CHECK_FOR_PAYLOAD(radioBuffer, phaser_ping_t, flOK=false ); if( !flOK ){ PRINTF("BadChk\n"); break; } // Check if new experiment iteration started. if(lastExpIdx != test_data_p->expIdx && curExp){ sendTestResults(); } processTestMsg(test_data_p, rssi, lqi); break; case PH_MSG_Angle: if(curExp) sendTestResults(); if( flRestart ){ // Best time to resend the restart message after the angle change send_ctrl_msg(MSG_ACT_RESTART); flRestart = false; } break; case PH_MSG_Control: MSG_CHECK_FOR_PAYLOAD(radioBuffer, phaser_control_t, break); if(curExp) sendTestResults(); act = ctrl_data_p->action; if(act == MSG_ACT_START ){ flRestart = false; // Clear restart command attempt } printAction(act); break; case PH_MSG_Text: MSG_CHECK_FOR_PAYLOAD(radioBuffer, msg_text_data_t, break ); PRINTF(msg_text_p->text); PRINTF("\n"); break; case PH_MSG_Config: MSG_CHECK_FOR_PAYLOAD(radioBuffer, test_config_t, break ); PRINTF("Config received:\n"); // TODO: parse the config and print print_test_config(test_config_p); } flRxProcessing=false; }