📄 micahighspeedradiotinysecm.nc
字号:
/* See below (at call point) why this was moved into a separate function. Basically, register spill/inlining/timing issues. -pal */ void timeSyncFunctionHack() { struct TimeSyncMsg * ptr; tos_time_t tt; // this is a time sync msg ptr = (struct TimeSyncMsg *)send_ptr->data; tt = call Time.get(); //TOSH_CLR_GREEN_LED_PIN(); ptr->timeH = tt.high32; ptr->timeL = tt.low32; ptr->phase = call Time.getUs(); } async event result_t ChannelMon.idleDetect() { uint8_t firstSSByte; uint8_t firstMsgByte; uint16_t timeVal; uint16_t tmpCrc; uint8_t oldSendState; atomic { oldSendState = send_state; if (send_state == SEND_WAITING) { send_state = IDLE_STATE; state = TRANSMITTING_START; } } if (oldSendState == SEND_WAITING){ atomic { firstMsgByte = ((char*)send_ptr)[0]; firstSSByte = TOSH_MHSR_start[0]; buf_end = buf_head = 0; enc_count = 0; call Code.encode(firstMsgByte); rx_count = 0; /**** TinySec ****/ if(send_ptr->length & TINYSEC_ENABLED_BIT) { msg_length = call TinySec.sendInit(send_ptr); } else { msg_length = (unsigned char)(send_ptr->length) + MSG_DATA_SIZE - DATA_LENGTH - 2; } /**** TinySec ****/ /* In the original MicaHighSpeedRadioM, the TimeSync operations were in the handler itself. This causes problems with atomic sections: the large structure allocated by the function causes a lot of registers to spill, making the above initialization code a lot slower. When the time sync code was in this function, the radio stack stopped working. So, I've put it in a separate function, which gets its own stack space, etc. However, nesC loves inlining things, which returns us to the original problem. To prevent inlining, the function has to have two call points: in this case, one of them is never executed (the else if (0) statement). Yay embedded programming. -pal */ if (send_ptr->type == AM_TIMESYNCMSG) { timeSyncFunctionHack(); } else if (0) { timeSyncFunctionHack(); } } // end of timesync change call SpiByteFifo.send(firstSSByte); timeVal = call RadioTiming.currentTime(); tmpCrc = crcByte(0x00, firstMsgByte); atomic { send_ptr->time = timeVal; calc_crc = tmpCrc; } } signal RadioSendCoordinator.startSymbol(); /**** TinySec ****/ if(send_ptr->length & TINYSEC_ENABLED_BIT) { call TinySec.send(); } /**** TinySec ****/ return 1; } async event result_t Code.decodeDone(char data, char error){ result_t rval = SUCCESS; atomic { if(state == IDLE_STATE){ rval = FAIL; } else if (state == HEADER_RX_STATE) { ((char*)rec_ptr)[(int)rec_count] = data; rec_count++; calc_crc = crcByte(calc_crc, data); signal RadioReceiveCoordinator.byte((TOS_MsgPtr) rec_ptr, (uint8_t)rec_count); signal TinySecRadio.byteReceived(data); } else if(state == RX_STATE){ ((char*)rec_ptr)[(int)rec_count] = data; rec_count++; if(rec_count >= MSG_DATA_SIZE){ // TODO: TOSH_RF_COMM_ADC_GET_DATA(0); if(calc_crc == rec_ptr->crc){ rec_ptr->crc = 1; if(rec_ptr->addr == TOS_LOCAL_ADDRESS || rec_ptr->addr == TOS_BCAST_ADDR){ call SpiByteFifo.send(0x55); } }else{ rec_ptr->crc = 0; } state = ACK_SEND_STATE; rval = 0; signal RadioReceiveCoordinator.byte((TOS_MsgPtr) rec_ptr, (uint8_t)rec_count); } else if(rec_count <= MSG_DATA_SIZE-2){ calc_crc = crcByte(calc_crc, data); if(rec_count == msg_length){ rec_count = MSG_DATA_SIZE-2; } } } else if(state == RX_STATE_TINYSEC) { uint8_t rec_count_save = ++rec_count; signal RadioReceiveCoordinator.byte((TOS_MsgPtr) rec_ptr, (uint8_t)rec_count); signal TinySecRadio.byteReceived(data); if(rec_count_save == msg_length + TINYSEC_MAC_LENGTH) { if(rec_ptr->crc == 1 && (rec_ptr->addr == TOS_LOCAL_ADDRESS || rec_ptr->addr == TOS_BCAST_ADDR)){ call SpiByteFifo.send(0x55); } state = ACK_SEND_STATE; } } } return rval; } async event result_t Code.encodeDone(char data1){ atomic { encoded_buffer[(int)buf_end] = data1; buf_end ++; buf_end &= 0x3; enc_count += 1; } return SUCCESS; } async event result_t SpiByteFifo.dataReady(uint8_t data) { atomic { if(state == TRANSMITTING_START){ call SpiByteFifo.send(TOSH_MHSR_start[(int)tx_count]); tx_count ++; if(tx_count == sizeof(TOSH_MHSR_start)){ if(send_ptr->length & TINYSEC_ENABLED_BIT) { // just a dummy call. first byte already encoded signal TinySecRadio.getTransmitByte(); state = TRANSMITTING_TINYSEC; } else { state = TRANSMITTING; } tx_count = 1; } }else if(state == TRANSMITTING){ call SpiByteFifo.send(encoded_buffer[(int)buf_head]); buf_head ++; buf_head &= 0x3; enc_count --; //now check if that was the last byte. if(enc_count >= 2){ ; }else if(tx_count < MSG_DATA_SIZE){ char next_data = ((char*)send_ptr)[(int)tx_count]; call Code.encode(next_data); tx_count ++; if(tx_count <= msg_length){ calc_crc = crcByte(calc_crc, next_data); } if(tx_count == msg_length){ //the last 2 bytes must be the CRC and are //transmitted regardless of the length. tx_count = MSG_DATA_SIZE - 2; send_ptr->crc = calc_crc; } signal RadioSendCoordinator.byte((TOS_MsgPtr) send_ptr, (uint8_t)tx_count); }else if(buf_head != buf_end){ call Code.encode_flush(); }else{ state = SENDING_STRENGTH_PULSE; tx_count = 0; tx_done = TRUE; } }else if(state == TRANSMITTING_TINYSEC) { call SpiByteFifo.send(encoded_buffer[(int)buf_head]); buf_head ++; buf_head &= 0x3; enc_count --; //now check if that was the last byte. if(enc_count >= 2){ ; }else if(tx_count < TINYSEC_MSG_DATA_SIZE){ uint8_t next_data = signal TinySecRadio.getTransmitByte(); call Code.encode(next_data); tx_count ++; if(tx_done){ tx_count = TINYSEC_MSG_DATA_SIZE; } signal RadioSendCoordinator.byte((TOS_MsgPtr) send_ptr, (uint8_t)tx_count); }else if(buf_head != buf_end){ call Code.encode_flush(); }else{ state = SENDING_STRENGTH_PULSE; tx_count = 0; } }else if(state == SENDING_STRENGTH_PULSE){ tx_count ++; if(tx_count == 3){ state = WAITING_FOR_ACK; call SpiByteFifo.phaseShift(); tx_count = 1; call SpiByteFifo.send(0x00); }else{ call SpiByteFifo.send(0xff); } }else if(state == WAITING_FOR_ACK){ data &= 0x7f; call SpiByteFifo.send(0x00); if(tx_count == 1) call SpiByteFifo.rxMode(); tx_count ++; if(tx_count == ACK_CNT + 2) { send_ptr->ack = (data == 0x55); state = IDLE_STATE; /* changes for timesync if (timeSyncFlag) { // clock phase reset call Time.set(tt.high32, tt.low32); timeSyncFlag =0; } // end of change */ //TOSH_CLR_GREEN_LED_PIN(); call SpiByteFifo.idle(); post packetSent(); } }else if(state == RX_STATE || state == RX_STATE_TINYSEC || state == HEADER_RX_STATE){ call Code.decode(data); }else if(state == ACK_SEND_STATE){ ack_count ++; if(ack_count > ACK_CNT + 1){ state = RX_DONE_STATE; call SpiByteFifo.idle(); // this is was set to TRUE for CRC packets if(tinysec_rx_done) { post packetReceived(); } }else{ call SpiByteFifo.txMode(); } } } return 1; }#if 0 char SIG_STRENGTH_READING(short data){ atomic { rec_ptr->strength = data; } return 1; }#endif // 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() { } default async event void RadioSendCoordinator.byte(TOS_MsgPtr msg, uint8_t byteCount) {} default async event void RadioReceiveCoordinator.startSymbol() { } default async event void RadioReceiveCoordinator.byte(TOS_MsgPtr msg, uint8_t byteCount) {}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -