void rfRecvData(void) { uint8 pRxData[128]; int rlen; basicRfReceiveOn(); // Main loop while (TRUE) { while(!basicRfPacketIsReady()); rlen = basicRfReceive(pRxData, sizeof pRxData, NULL); if(rlen > 0) { pRxData[rlen] = 0; printf("%s rssi: %d\r\n", (char *)pRxData, basicRfGetRssi()); } } }
void rfRecvData(void) { uint8 pRxData[128]; int rlen; basicRfReceiveOn(); printf("recv msg start...\r\n"); // Main loop while (TRUE) { while(!basicRfPacketIsReady()); rlen = basicRfReceive(pRxData, sizeof pRxData, NULL); if(rlen > 0) { printf("recv msg ok...\r\n"); pRxData[rlen] = 0; printf((char *)pRxData); } } }
static THD_FUNCTION(rx_thread, arg) { (void)arg; chRegSetThreadName("CC2520 RX"); for(;;) { if (basicRfPacketIsReady()) { static uint8_t buf[130]; unsigned int len = 0; unsigned int ind = 0; len = basicRfReceive(buf, 130, NULL); MOTE_PACKET packet = buf[0]; led_toggle(LED_RED); switch (packet) { case MOTE_PACKET_FILL_RX_BUFFER: memcpy(rx_buffer + buf[1], buf + 2, len - 2); break; case MOTE_PACKET_FILL_RX_BUFFER_LONG: { int rxbuf_ind = (unsigned int)buf[1] << 8; rxbuf_ind |= buf[2]; if (rxbuf_ind < RX_BUFFER_SIZE) { memcpy(rx_buffer + rxbuf_ind, buf + 3, len - 3); } } break; case MOTE_PACKET_PROCESS_RX_BUFFER: { ind = 1; int rxbuf_len = (unsigned int)buf[ind++] << 8; rxbuf_len |= (unsigned int)buf[ind++]; if (rxbuf_len > RX_BUFFER_SIZE) { break; } uint8_t crc_high = buf[ind++]; uint8_t crc_low = buf[ind++]; memcpy(rx_buffer + rxbuf_len - (len - ind), buf + ind, len - ind); if (crc16(rx_buffer, rxbuf_len) == ((unsigned short) crc_high << 8 | (unsigned short) crc_low)) { comm_usb_send_packet(rx_buffer, rxbuf_len); } } break; case MOTE_PACKET_PROCESS_SHORT_BUFFER: comm_usb_send_packet(buf + 1, len - 1); break; default: break; } } chThdSleepMicroseconds(100); } }
/*********************************************************************************** * @fn main */ void main(void) { // Initalise board peripherals halBoardInit(); basicRfSetUp(); // Initalise hal_rf if(halRfInit()==FAILED) { HAL_ASSERT(FALSE); } // Indicate that device is powered halLedSet(1); halMcuWaitMs(350); configureUSART0forUART_ALT1(); uartStartRxForIsr(); while(TRUE) { //---------------------- // INITIALIZE //---------------------- if(initFlag) { while(!start); //waiting for 'a' key from PC //respond to PC -- going to try to start up WRS start=0; pTxData[0] = INIT_COMM_CMD; basicRfReceiveOff(); if(basicRfSendPacket(ROBOT_ADDR, pTxData, APP_PAYLOAD_LENGTH)==SUCCESS) { state=1; } basicRfReceiveOn(); //wait for ACK from WRS pTxData[0] = INIT_COEF_CMD; basicRfReceiveOff(); if(basicRfSendPacket(ROBOT_ADDR, pTxData, APP_PAYLOAD_LENGTH)==SUCCESS) { basicRfReceiveOn(); //WAIT FOR COEFFICIENTS FROM WRS while(!basicRfPacketIsReady());//wait to receive acknowledgement if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) { if(pRxData[0] == 'C') { //Pass to PC for (unsigned int uartTxIndex = 0; uartTxIndex<105; uartTxIndex++) { U0CSR &= ~0x02; //SET U0TX_BYTE to 0 U0DBUF = pRxData[uartTxIndex]; while (!(U0CSR&0x02)); } } } } //finished sending coefficients to PC basicRfReceiveOn(); initFlag=0; } if(turnOnMotorFlag){ if(basicRfSendPacket(ROBOT_ADDR, pTxData, APP_PAYLOAD_LENGTH)==SUCCESS)//send command to WRS { turnOnMotorFlag=0; } } if(sendInitFlag){ if(basicRfSendPacket(ROBOT_ADDR, pTxData, APP_PAYLOAD_LENGTH)==SUCCESS)//send command to WRS { initFlag=1; sendInitFlag=0; //Pass to PC for (unsigned int uartTxIndex = 0; uartTxIndex<105; uartTxIndex++) { pRxData[0] == 'R'; U0CSR &= ~0x02; //SET U0TX_BYTE to 0 U0DBUF = pRxData[uartTxIndex]; while (!(U0CSR&0x02)); } } } //Receive package from WRS if(basicRfPacketIsReady()) { if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, myRSSI)>0) { getRSSI = basicRfGetRssi(); pRxData[104]=getRSSI; if(pRxData[0] == 'D')//||(pRxData[0] == 'I')) { //SEND DATA TO PC for (unsigned int uartTxIndex = 0; uartTxIndex<105; uartTxIndex++) { U0CSR &= ~0x02; //SET U0TX_BYTE to 0 U0DBUF = pRxData[uartTxIndex]; while (!(U0CSR&0x02)); } } } } } }
/*********************************************************************************** * @fn main */ void main(void) { // Initalise board peripherals halBoardInit(); basicRfSetUp(); // Initalise hal_rf if(halRfInit()==FAILED) { HAL_ASSERT(FALSE); } // Indicate that device is powered halLedSet(1); halMcuWaitMs(350); configureUSART0forUART_ALT1(); uartStartRxForIsr(); while(!start); //waiting for 'a' key from PC //respond to PC -- going to try to start up WRS pTxData[0] = INIT_COMM_CMD; basicRfReceiveOff(); if(basicRfSendPacket(ROBOT_ADDR, pTxData, APP_PAYLOAD_LENGTH)==SUCCESS) { state=1; } basicRfReceiveOn(); //wait for ACK from WRS pTxData[0] = INIT_COEF_CMD; basicRfReceiveOff(); if(basicRfSendPacket(ROBOT_ADDR, pTxData, APP_PAYLOAD_LENGTH)==SUCCESS) { basicRfReceiveOn(); state=2;//continuous? //WAIT FOR COEFFICIENTS FROM WRS while(!basicRfPacketIsReady());//wait to receive acknowledgement if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) { if(pRxData[0] == 'C') { //Pass to PC for (unsigned int uartTxIndex = 0; uartTxIndex<105; uartTxIndex++) { U0CSR &= ~0x02; //SET U0TX_BYTE to 0 U0DBUF = pRxData[uartTxIndex]; while (!(U0CSR&0x02)); } // while(!ACK);//waiting for acknowledgement } } } //finished sending coefficients to PC basicRfReceiveOn(); while(TRUE) { //Receive package from WRS if(basicRfPacketIsReady()) { if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, myRSSI)>0) { getRSSI = basicRfGetRssi(); pRxData[104]=getRSSI; if((pRxData[0] == 'P')||(pRxData[0] == 'A')) { //SEND DATA TO PC for (unsigned int uartTxIndex = 0; uartTxIndex<105; uartTxIndex++) { U0CSR &= ~0x02; //SET U0TX_BYTE to 0 U0DBUF = pRxData[uartTxIndex]; while (!(U0CSR&0x02)); } } } } //Receive CMD from PC if(changePWMflag)//have this be set in interrupt { //basicRfReceiveOff(); if(basicRfSendPacket(ROBOT_ADDR, pTxData, APP_PAYLOAD_LENGTH)==SUCCESS)//send PWM info to WRS { changePWMflag=0; } //basicRfReceiveOn(); // while(changePWMflag)//Keep receiving until PWM acknowledge is sent // { // while(!basicRfPacketIsReady());//waiting for acknowledgement -- important here i think // // if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) // { // if(pRxData[0] == 'Z') // { // //receive current duty cycle and send back to PC // for (unsigned int uartTxIndex = 0; uartTxIndex<105; uartTxIndex++) // { // U0CSR &= ~0x02; //SET U0TX_BYTE to 0 // U0DBUF = pRxData[uartTxIndex]; // while (!(U0CSR&0x02)); // } // changePWMflag = 0; // } // //else send pressure? // } // } // } } }//END OF MAIN WHILE LOOP }