int setRFBeeMode() { // CCx_MCSM1 is configured to have TX and RX return to proper state on completion or timeout switch (config_get(CONFIG_RFBEE_MODE)) { case TRANSMIT_MODE: ccx_strobe(CCx_SIDLE); _delay_ms(1); ccx_write(CCx_MCSM1, 0x00); //TXOFF_MODE->stay in IDLE ccx_strobe(CCx_SFTX); break; case RECEIVE_MODE: ccx_strobe(CCx_SIDLE); _delay_ms(1); ccx_write(CCx_MCSM1, 0x0C); //RXOFF_MODE->stay in RX ccx_strobe(CCx_SFRX); ccx_strobe(CCx_SRX); break; case TRANSCEIVE_MODE: ccx_strobe(CCx_SIDLE); _delay_ms(1); ccx_write(CCx_MCSM1, 0x0F); //RXOFF_MODE and TXOFF_MODE stay in RX ccx_strobe(CCx_SFTX); ccx_strobe(CCx_SFRX); ccx_strobe(CCx_SRX); break; case LOWPOWER_MODE: ccx_strobe(CCx_SIDLE); break; default: break; } return OK; }
// put the rfbee into sleep int setSleepMode() { #ifdef DEBUG printf("going to sleep\r\n"); #endif ccx_strobe(CCx_SIDLE); ccx_strobe(CCx_SPWD); sleepNow(SLEEP_MODE_IDLE); //sleepNow(SLEEP_MODE_PWR_DOWN); #ifdef DEBUG printf("just woke up\r\n"); #endif setRFBeeMode(); setSerialDataMode(); return NOTHING; }
void readSerialData() { uint8_t len; uint8_t data; uint8_t fifoSize = 0; static uint8_t plus = 0; static uint8_t pos = 0; uint8_t rfBeeMode; int i; // insert any plusses from last round for (i = pos; i < plus; i++) { //be careful, i should start from pos, -changed by Icing serialData[i] = '+'; } len = serial_available() + plus + pos; if (len > BUFFLEN) { len = BUFFLEN; //only process at most BUFFLEN chars } // check how much space we have in the TX fifo fifoSize = txFifoFree(); // the fifoSize should be the number of bytes in TX FIFO if (fifoSize <= 0) { serial_flush(); //ccx_strobe(CCx_SFTX); plus = 0; pos = 0; return; } if (len > fifoSize) { len = fifoSize; // don't overflow the TX fifo } for (i = plus + pos; i < len; i++) { data = serial_read(); serialData[i] = data; //serialData is our global serial buffer if (data == '+') { plus++; } else { plus = 0; } if (plus == 3) { len = i - 2; // do not send the last 2 plusses plus = 0; serialMode = SERIALCMDMODE; ccx_strobe(CCx_SIDLE); printf("ok, starting cmd mode\r\n"); break; // jump out of the loop, but still send the remaining chars in the buffer } } if (plus > 0) { // save any trailing plusses for the next round len -= plus; } // check if we have more input than the transmitThreshold, if we have just switched to commandmode send the current buffer anyway. if ((serialMode != SERIALCMDMODE) && (len < config_get(CONFIG_TX_THRESHOLD))) { pos = len; // keep the current bytes in the buffer and wait till next round. return; } if (len > 0) { rfBeeMode = config_get(CONFIG_RFBEE_MODE); //only when TRANSMIT_MODE or TRANSCEIVE,transmit the buffer data,otherwise ignore if ((rfBeeMode == TRANSMIT_MODE) || (rfBeeMode == TRANSCEIVE_MODE)) { tx_packet.len = len + PAYLOAD_OFFSET; transmitData(&tx_packet); //transmitData(serialData, len, config_get(CONFIG_MY_ADDR), config_get(CONFIG_DEST_ADDR)); } pos = 0; // serial databuffer is free again. } }
void ccx_idle() { ccx_strobe(CCx_SIDLE); while (ccx_strobe(CCx_SNOP) & 0xF0); }