📄 cc1000radiointm.nc.svn-base
字号:
search_word <<= 1; if(data_in & 0x80) search_word |= 0x1; data_in <<= 1; if (search_word == 0xba83){ txbufptr->ack = 1; RadioTxState = TXSTATE_DONE; return SUCCESS; } } } if(TxByteCnt == MAX_ACK_WAIT){ txbufptr->ack = 0; RadioTxState = TXSTATE_DONE; } break; case TXSTATE_DONE: default: call RSSIADC.getData(); bTxPending = FALSE; if (post PacketSent()) { // If the post operation succeeds, goto Idle // otherwise, we'll try again. RadioState = IDLE_STATE; } break; } } break; case DISABLED_STATE: break; case IDLE_STATE: { if (((data_in == (0xaa)) || (data_in == (0x55)))) { PreambleCount++; if (PreambleCount > CC1K_ValidPrecursor) { PreambleCount = SOFCount = 0; RxBitOffset = RxByteCnt = 0; usRunningCRC = 0; rxlength = MSG_DATA_SIZE-2; /**** TinySec ****/ call TinySec.receiveInit(rxbufptr); /**** TinySec ****/ RadioState = SYNC_STATE; } } else if (bTxPending && (--sMacDelay <= 0)) { bRSSIValid = FALSE; RadioState = PRETX_STATE; iRSSIcount = 0; PreambleCount = 0; call RSSIADC.getData(); } } break; case PRETX_STATE: { if (((data_in == (0xaa)) || (data_in == (0x55)))) { // Back to the penalty box. sMacDelay = (call Random.rand() & 0xF) + 1; RadioState = IDLE_STATE; }/* else if (bRSSIValid) { if (usRSSIVal > (usSquelchVal + CC1K_SquelchBuffer)) { // ROCK AND ROLL!!!!! call CC1000Control.TxMode(); call SpiByteFifo.txMode(); TxByteCnt = 0; usRunningCRC = 0; RadioState = TX_STATE; RadioTxState = TXSTATE_PREAMBLE; NextTxByte = 0xaa; call SpiByteFifo.writeByte(0xaa); } else { // Russin frussin freakin frick o frack sMacDelay = (call Random.rand() & 0xF) + 1; RadioState = IDLE_STATE; } }*/ } break; case SYNC_STATE: { // draw in the preamble bytes and look for a sync byte // save the data in a short with last byte received as msbyte // and current byte received as the lsbyte. // use a bit shift compare to find the byte boundary for the sync byte // retain the shift value and use it to collect all of the packet data // check for data inversion, and restore proper polarity // XXX-PB: Don't do this. uint8_t i; if ((data_in == 0xaa) || (data_in == 0x55)) { // It is actually possible to have the LAST BIT of the incoming // data be part of the Sync Byte. SO, we need to store that // However, the next byte should definitely not have this pattern. // XXX-PB: Do we need to check for excessive preamble? RxShiftBuf.MSB = data_in; } else { // TODO: Modify to be tolerant of bad bits in the preamble... uint16_t usTmp; switch (SOFCount) { case 0: RxShiftBuf.LSB = data_in; break; case 1: case 2: // bit shift the data in with previous sample to find sync usTmp = RxShiftBuf.W; RxShiftBuf.W <<= 8; RxShiftBuf.LSB = data_in; for(i=0;i<8;i++) { usTmp <<= 1; if(data_in & 0x80) usTmp |= 0x1; data_in <<= 1; // check for sync bytes if (usTmp == SYNC_WORD) { if (rxbufptr->length !=0) { call Leds.redToggle(); RadioState = IDLE_STATE; } else { RadioState = HEADER_RX_STATE; call RSSIADC.getData(); RxBitOffset = 7-i; // For time sync services signal RadioReceiveCoordinator.startSymbol(8, RxBitOffset, (TOS_MsgPtr) rxbufptr); } break; } } break; default: // We didn't find it after a reasonable number of tries, so.... RadioState = IDLE_STATE; // Ensures we wait till the end of the transmission break; } SOFCount++; } } break; /**** TinySec ****/ case HEADER_RX_STATE: { char Byte; RxShiftBuf.W <<=8; RxShiftBuf.LSB = data_in; Byte = (RxShiftBuf.W >> RxBitOffset); ((char*)rxbufptr)[(int)RxByteCnt] = Byte; RxByteCnt++; signal RadioReceiveCoordinator.byte((TOS_MsgPtr) rxbufptr, (uint8_t)RxByteCnt-1); usRunningCRC = crcByte(usRunningCRC,Byte); signal TinySecRadio.byteReceived(Byte); } break; case RX_STATE_TINYSEC: { char Byte; RxShiftBuf.W <<=8; RxShiftBuf.LSB = data_in; Byte = (RxShiftBuf.W >> RxBitOffset); RxByteCnt++; signal RadioReceiveCoordinator.byte((TOS_MsgPtr) rxbufptr, (uint8_t)RxByteCnt-1); signal TinySecRadio.byteReceived(Byte); } break; /**** TinySec ****/ // collect the data and shift into double buffer // shift out data by correct offset // invert the data if necessary // stop after the correct packet length is read // return notification to upper levels // go back to idle state case RX_STATE: { char Byte; RxShiftBuf.W <<=8; RxShiftBuf.LSB = data_in; Byte = (RxShiftBuf.W >> RxBitOffset); ((char*)rxbufptr)[(int)RxByteCnt] = Byte; RxByteCnt++; // Time Sync: substract one to start counting from zero signal RadioReceiveCoordinator.byte((TOS_MsgPtr) rxbufptr, (uint8_t)RxByteCnt-1); if (RxByteCnt < rxlength) { usRunningCRC = crcByte(usRunningCRC,Byte); } else if (RxByteCnt == rxlength) { usRunningCRC = crcByte(usRunningCRC,Byte); // Shift index ahead to the crc field. RxByteCnt = offsetof(struct TOS_Msg,crc); } else if (RxByteCnt >= MSG_DATA_SIZE) { // Packet filtering based on bad CRC's is done at higher layers. // So sayeth the TOS weenies. if (rxbufptr->crc == usRunningCRC) { rxbufptr->crc = 1; if(rxbufptr->addr == TOS_LOCAL_ADDRESS || rxbufptr->addr == TOS_BCAST_ADDR){ RadioState = SENDING_ACK; call CC1000Control.TxMode(); call SpiByteFifo.txMode(); call SpiByteFifo.writeByte(0xaa); RxByteCnt = 0; return SUCCESS; } } else { rxbufptr->crc = 0; } call SpiByteFifo.disableIntr(); RadioState = IDLE_STATE; //DISABLED_STATE; rxbufptr->strength = usRSSIVal; if (!(post PacketRcvd())) { // If there are insufficient resources to process the incoming packet // we drop it rxbufptr->length = 0; RadioState = IDLE_STATE; call SpiByteFifo.enableIntr(); } } } break; case SENDING_ACK: { RxByteCnt++; if (RxByteCnt >= ACK_LENGTH) { call CC1000Control.RxMode(); call SpiByteFifo.rxMode(); call SpiByteFifo.disableIntr(); RadioState = IDLE_STATE; //DISABLED_STATE; rxbufptr->strength = usRSSIVal; if (!(post PacketRcvd())) { rxbufptr->length = 0; RadioState = IDLE_STATE; call SpiByteFifo.enableIntr(); } }else if(RxByteCnt >= ACK_LENGTH - sizeof(ack_code) - 2){ call SpiByteFifo.writeByte(ack_code[RxByteCnt + sizeof(ack_code) + 2- ACK_LENGTH]); } } break; default: break; } return SUCCESS;}async event result_t RSSIADC.dataReady(uint16_t data) { uint8_t currentRadioState; atomic currentRadioState = RadioState; // find the maximum RSSI value over CC1K_MAX_RSSI_SAMPLES switch(currentRadioState) { case PRETX_STATE: iRSSIcount++; // if the channel is clear, GO GO GO! if (data > (usSquelchVal + CC1K_SquelchBuffer)) { call SpiByteFifo.writeByte(0xaa); call CC1000Control.TxMode(); call SpiByteFifo.txMode(); atomic { usRSSIVal = data; iRSSIcount = CC1K_MaxRSSISamples; bRSSIValid = TRUE; TxByteCnt = 0; usRunningCRC = 0; RadioState = TX_STATE; RadioTxState = TXSTATE_PREAMBLE; NextTxByte = 0xaa; // start encrypting packet - re-enables interrupts // check if encrypt enabled bit is set /**** TinySec ****/ if(txbufptr->length & TINYSEC_ENABLED_BIT) { call TinySec.send(); } /**** TinySec ****/ } return SUCCESS; } if (iRSSIcount == 1) atomic usRSSIVal = data; else if (data > usRSSIVal) atomic usRSSIVal = data; if (iRSSIcount == CC1K_MaxRSSISamples) { atomic { bRSSIValid = TRUE; sMacDelay = (call Random.rand() & 0xF) + 1; RadioState = IDLE_STATE; } } else call RSSIADC.getData(); break; case IDLE_STATE: atomic usTempSquelch = data; post adjustSquelch(); break; case RX_STATE: case HEADER_RX_STATE: case RX_STATE_TINYSEC: atomic usRSSIVal = data; break; default: } return SUCCESS;} // XXX:JP- for testing the mac layer squlech value command uint16_t GetSquelch() { return usSquelchVal; }// Default events for radio send/receive coordinators do nothing.// Be very careful using these, you'll break the stack.default async event void RadioSendCoordinator.startSymbol(uint8_t bitsPerBlock, uint8_t offset, TOS_MsgPtr msgBuff) { }default async event void RadioSendCoordinator.byte(TOS_MsgPtr msg, uint8_t byteCount) { }default async event void RadioSendCoordinator.blockTimer() { }default async event void RadioReceiveCoordinator.startSymbol(uint8_t bitsPerBlock, uint8_t offset, TOS_MsgPtr msgBuff) { }default async event void RadioReceiveCoordinator.byte(TOS_MsgPtr msg, uint8_t byteCount) { }default async event void RadioReceiveCoordinator.blockTimer() { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -