//------------------------------------------------------------------------------ // void main (void) // // DESCRIPTION: // Startup routine and main loop //------------------------------------------------------------------------------ int main (void) { uint8_t cnt,i,length; nrk_setup_ports(); nrk_setup_uart (UART_BAUDRATE_115K2); printf( "Basic TX...\r\n" ); nrk_led_set(0); nrk_led_set(1); nrk_led_clr(2); nrk_led_clr(3); /* while(1) { for(i=0; i<40; i++ ) halWait(10000); nrk_led_toggle(1); } */ rfRxInfo.pPayload = rx_buf; rfRxInfo.max_length = RF_MAX_PAYLOAD_SIZE; nrk_int_enable(); rf_init (&rfRxInfo, 13, 0x2420, 0x1214); cnt=0; while(1){ nrk_led_set(GREEN_LED); rfTxInfo.pPayload=tx_buf; sprintf( tx_buf, "This is my string counter %d", cnt); rfTxInfo.length= strlen(tx_buf) + 1; rfTxInfo.destAddr = 0x1215; rfTxInfo.cca = 0; rfTxInfo.ackRequest = 0; printf( "Sending\r\n" ); nrk_gpio_set(NRK_DEBUG_0); if(rf_tx_packet(&rfTxInfo) != 1) printf("--- RF_TX ERROR ---\r\n"); nrk_gpio_clr(NRK_DEBUG_0); cnt++; for(i=0; i<80; i++ ) halWait(10000); nrk_led_clr(GREEN_LED); for(i=0; i<20; i++ ) halWait(10000); } }
void sendAck(SPP_STRUCT* receivedPacket) { SIDLE(); // Debug: halWait(1); DMA_ABORT_CHANNEL(dmaNumberTx); RFTXRXIF = 0; STX(); RFIF &= ~IRQ_DONE; while(RFTXRXIF == 0); RFD = SPP_HEADER_AND_FOOTER_LENGTH + SPP_ACK_LENGTH; RFTXRXIF = 0; while(RFTXRXIF == 0); RFTXRXIF = 0; RFD = receivedPacket->srcAddress; while(RFTXRXIF == 0); RFTXRXIF = 0; RFD = myAddress; while(RFTXRXIF == 0); RFTXRXIF = 0; RFD = ACK; while(!(RFIF & IRQ_DONE)); RFIF &= ~IRQ_DONE; return; }
//------------------------------------------------------------------------------ // void main (void) // // DESCRIPTION: // Startup routine and main loop //------------------------------------------------------------------------------ int main (void) { uint8_t i,length; uint32_t cnt; nrk_setup_ports(); nrk_setup_uart (UART_BAUDRATE_115K2); printf( "Basic TX...\r\n" ); nrk_led_set(0); nrk_led_set(1); nrk_led_clr(2); nrk_led_clr(3); /* while(1) { for(i=0; i<40; i++ ) halWait(10000); nrk_led_toggle(1); } */ rfRxInfo.pPayload = rx_buf; rfRxInfo.max_length = RF_MAX_PAYLOAD_SIZE; nrk_int_enable(); rf_init (&rfRxInfo, 26, 0x2420, 0x1214); cnt=0; while(1){ DPDS1 |= 0x3; DDRG |= 0x1; PORTG |= 0x1; DDRE|=0xE0; PORTE|=0xE0; rfTxInfo.pPayload=tx_buf; sprintf( tx_buf, "%lu", cnt); rfTxInfo.length= strlen(tx_buf) + 1; rfTxInfo.destAddr = 0x1215; rfTxInfo.cca = 0; rfTxInfo.ackRequest = 0; printf( "Sending\r\n" ); // nrk_gpio_set(NRK_DEBUG_0); if(rf_tx_packet(&rfTxInfo) != 1) printf("--- RF_TX ERROR ---\r\n"); // nrk_gpio_clr(NRK_DEBUG_0); cnt++; for(i=0; i<10; i++ ) halWait(10000); nrk_led_toggle(RED_LED); } }
//------------------------------------------------------------------------------ // void main (void) // // DESCRIPTION: // Startup routine and main loop //------------------------------------------------------------------------------ int main (void) { uint8_t cnt,i,length; nrk_setup_ports(); nrk_setup_uart (UART_BAUDRATE_115K2); printf( "Basic TX...\r\n" ); nrk_led_clr(0); nrk_led_clr(1); nrk_led_clr(2); nrk_led_clr(3); nrk_gpio_set(NRK_DEBUG_0); nrk_gpio_set(NRK_DEBUG_1); rfRxInfo.pPayload = rx_buf; rfRxInfo.max_length = RF_MAX_PAYLOAD_SIZE; rf_init (&rfRxInfo, 25, 0x2420, 0x1215); cnt=0; while(1) { nrk_led_set(1); rfTxInfo.pPayload=tx_buf; sprintf( tx_buf, "This is my string counter %d", cnt); rfTxInfo.length=strlen(&tx_buf); rfTxInfo.cca=0; nrk_gpio_set(NRK_DEBUG_0); printf( "Sending\r\n" ); rf_tx_packet (&rfTxInfo); nrk_gpio_clr(NRK_DEBUG_0); cnt++; for(i=0; i<10; i++ ) halWait(10000); nrk_led_clr(1); for(i=0; i<10; i++ ) halWait(10000); } }
/****************************************************************************** * @fn updateCounter * * @brief * Function for updating counting speed for binary counter * * Parameters: * * @param INT8 delay * New counting delay * * @return void * ******************************************************************************/ void updateCounter(INT8 delay) { static UINT8 counter = 0; UINT16 i = 0; i = ((delay > 0) ? 0x7F - delay : 0x7F); halWait( i ); counter++; SET_LED_MASK( (BYTE)counter ); }
uint8_t rf_tx_packet(RF_TX_INFO *pRTI) { uint16_t frameControlField; uint8_t packetLength; uint8_t success, i; // Note: checksum is automatic in HW LPC_GPIO1->FIOPIN ^= 1<<31; packetLength = pRTI->length + RF_PACKET_OVERHEAD_SIZE; frameControlField = RF_FCF_NOACK; if(auto_ack_enable || pRTI->ackRequest) frameControlField |= RF_ACK_BM; if(security_enable) frameControlField |= 0x0000; // TODO: Paul Gurniak, add security mrf_write_long(TXNFIFO, RF_PACKET_OVERHEAD_SIZE); // No checksum, overhead is all header mrf_write_long(TXNFIFO+1, packetLength); // Write header bytes mrf_write_long(TXNFIFO+2, frameControlField & 0xFF); mrf_write_long(TXNFIFO+3, frameControlField >> 8); mrf_write_long(TXNFIFO+4, rfSettings.txSeqNumber); mrf_write_long(TXNFIFO+5, rfSettings.panId & 0xFF); mrf_write_long(TXNFIFO+6, rfSettings.panId >> 8); mrf_write_long(TXNFIFO+7, pRTI->destAddr & 0xFF); mrf_write_long(TXNFIFO+8, pRTI->destAddr >> 8); mrf_write_long(TXNFIFO+9, rfSettings.myAddr & 0xFF); mrf_write_long(TXNFIFO+10, rfSettings.myAddr >> 8); // Write payload for(i = 0; i < pRTI->length; i++) { mrf_write_long(TXNFIFO+11+i, pRTI->pPayload[i]); // pPayload is the user defined part of data packet right ? } if(pRTI->cca) { uint8_t cnt = 0; if(!rfSettings.receiveOn) { rf_rx_on(); } while(!rf_rx_check_cca()) { cnt++; if(cnt > 100) { return FALSE; } halWait(100); } } tx_status_ready = 0; mrf_write_short(TXNCON, auto_ack_enable ? 0x05 : 0x01); // Send contents of TXNFIFO as packet //putchar(auto_ack_enable); // Wait for Tx to finish while(!tx_status_ready); wait_ms(50); success = 1; //putchar(auto_ack_enable); if(auto_ack_enable || pRTI->ackRequest) { uint8_t k = mrf_read_short(TXSTAT); success = !(k & 0x01); //printf("ACK%c",k); } //printf("tx_pkt success = %d\r\n",success); // Transmission works fine : Checked by Madhur, success = 1only when the receiver is enabled // Increment sequence, return result rfSettings.txSeqNumber++; return success; }
//------------------------------------------------------------------------------------------------------- // BYTE rf_tx_packet(RF_TX_INFO *pRTI) // // DESCRIPTION: // Transmits a packet using the IEEE 802.15.4 MAC data packet format with short addresses. CCA is // measured only once before backet transmission (not compliant with 802.15.4 CSMA-CA). // The function returns: // - When pRTI->ackRequest is FALSE: After the transmission has begun (SFD gone high) // - When pRTI->ackRequest is TRUE: After the acknowledgment has been received/declared missing. // The acknowledgment is received through the FIFOP interrupt. // // ARGUMENTS: // RF_TX_INFO *pRTI // The transmission structure, which contains all relevant info about the packet. // // RETURN VALUE: // uint8_t // Successful transmission (acknowledgment received) //------------------------------------------------------------------------------------------------------- uint8_t rf_tx_packet(RF_TX_INFO *pRTI) { uint16_t frameControlField; uint8_t packetLength, length; uint8_t success; uint8_t spiStatusByte; uint8_t checksum,i; #ifdef RADIO_PRIORITY_CEILING nrk_sem_pend(radio_sem); #endif #ifdef CC2420_OSC_OPT FASTSPI_STROBE(CC2420_SXOSCON); nrk_spin_wait_us(OSC_STARTUP_DELAY); #endif if(security_enable) FASTSPI_STROBE(CC2420_STXENC); checksum=0; for(i=0; i<pRTI->length; i++ ) { // lets do our own payload checksum because we don't trust the CRC checksum+=pRTI->pPayload[i]; } // Write the packet to the TX FIFO (the FCS is appended automatically when AUTOCRC is enabled) // These are only the MAC AGNOSTIC parameters... // Slots for example are at a slighly higher later since they assume TDMA packetLength = pRTI->length + RF_PACKET_OVERHEAD_SIZE + CHECKSUM_OVERHEAD; if(security_enable) packetLength+=4; // for CTR counter // XXX 2 below are hacks... FASTSPI_STROBE(CC2420_SFLUSHRX); FASTSPI_STROBE(CC2420_SFLUSHRX); // Wait until the transceiver is idle while (FIFOP_IS_1 || SFD_IS_1); // Turn off global interrupts to avoid interference on the SPI interface DISABLE_GLOBAL_INT(); // Flush the TX FIFO just in case... FASTSPI_STROBE(CC2420_SFLUSHTX); FASTSPI_STROBE(CC2420_SFLUSHTX); /* // Turn on RX if necessary if (!rfSettings.receiveOn) { FASTSPI_STROBE(CC2420_SRXON); } // Wait for the RSSI value to become valid do { FASTSPI_UPD_STATUS(spiStatusByte); } while (!(spiStatusByte & BM(CC2420_RSSI_VALID))); // TX begins after the CCA check has passed do { FASTSPI_STROBE(CC2420_STXONCCA); FASTSPI_UPD_STATUS(spiStatusByte); halWait(100); } while (!(spiStatusByte & BM(CC2420_TX_ACTIVE))); */ FASTSPI_WRITE_FIFO((uint8_t*)&packetLength, 1); // Packet length frameControlField = RF_FCF_NOACK; // default if(auto_ack_enable) frameControlField |= RF_ACK_BM; if(security_enable) frameControlField |= RF_SEC_BM; FASTSPI_WRITE_FIFO((uint8_t*) &frameControlField, 2); // Frame control field FASTSPI_WRITE_FIFO((uint8_t*) &rfSettings.txSeqNumber, 1); // Sequence number FASTSPI_WRITE_FIFO((uint8_t*) &rfSettings.panId, 2); // Dest. PAN ID FASTSPI_WRITE_FIFO((uint8_t*) &pRTI->destAddr, 2); // Dest. address FASTSPI_WRITE_FIFO((uint8_t*) &rfSettings.myAddr, 2); // Source address if(security_enable) FASTSPI_WRITE_FIFO((uint8_t*) &tx_ctr, 4); // CTR counter FASTSPI_WRITE_FIFO((uint8_t*) pRTI->pPayload, pRTI->length); // Payload FASTSPI_WRITE_FIFO((uint8_t*) &checksum, 1); // Checksum if (pRTI->cca == TRUE) { uint8_t cnt; if (!rfSettings.receiveOn) { FASTSPI_STROBE (CC2420_SRXON); } // Wait for the RSSI value to become valid do { FASTSPI_UPD_STATUS (spiStatusByte); } while (!(spiStatusByte & BM (CC2420_RSSI_VALID))); // TX begins after the CCA check has passed cnt = 0; do { FASTSPI_STROBE (CC2420_STXONCCA); FASTSPI_UPD_STATUS (spiStatusByte); cnt++; if (cnt > 100) { ENABLE_GLOBAL_INT (); nrk_sem_post(radio_sem); return FALSE; } halWait (100); } while (!(spiStatusByte & BM (CC2420_TX_ACTIVE))); } else FASTSPI_STROBE (CC2420_STXON); ENABLE_GLOBAL_INT(); // Wait for the transmission to begin before exiting (makes sure that this function cannot be called // a second time, and thereby cancelling the first transmission (observe the FIFOP + SFD test above). while (!SFD_IS_1); success = TRUE; // Turn interrupts back on // ENABLE_GLOBAL_INT(); while (SFD_IS_1); // wait for packet to finish // Wait for the acknowledge to be received, if any if (auto_ack_enable) { // rfSettings.ackReceived = FALSE; // Wait for the SFD to go low again // while (SFD_IS_1); // We'll enter RX automatically, so just wait until we can be sure that the // ack reception should have finished // The timeout consists of a 12-symbol turnaround time, the ack packet duration, // and a small margin halWait((12 * RF_SYMBOL_DURATION) + (RF_ACK_DURATION) + (2 * RF_SYMBOL_DURATION) + 100); if(FIFO_IS_1) { FASTSPI_READ_FIFO_BYTE(length); length &= RF_LENGTH_MASK; // Ignore MSB success = TRUE; } else { FASTSPI_STROBE(CC2420_SFLUSHRX); FASTSPI_STROBE(CC2420_SFLUSHRX); success = FALSE; } } // Turn off the receiver if it should not continue to be enabled DISABLE_GLOBAL_INT(); //FASTSPI_STROBE(CC2420_SFLUSHRX); //FASTSPI_STROBE(CC2420_SFLUSHRX); //FASTSPI_STROBE(CC2420_SFLUSHTX); //FASTSPI_STROBE(CC2420_SFLUSHTX); #ifdef CC2420_OSC_OPT FASTSPI_STROBE(CC2420_SXOSCOFF); #endif FASTSPI_STROBE(CC2420_SRFOFF); // shut off radio ENABLE_GLOBAL_INT(); // agr XXX hack to test time issue //rf_rx_on(); // Increment the sequence number, and return the result rfSettings.txSeqNumber++; // while (SFD_IS_1); #ifdef RADIO_PRIORITY_CEILING nrk_sem_post(radio_sem); #endif return success; }
/************************************************************************** This function is the same as normal TX, only it waits until the last second to send the duty out with the high speed timer. And by duty, I mean the packet BIATCH... **************************************************************************/ uint8_t rf_tx_tdma_packet(RF_TX_INFO *pRTI, uint16_t slot_start_time, uint16_t tx_guard_time) { uint16_t frameControlField; uint8_t packetLength; uint8_t success; uint8_t spiStatusByte; uint8_t checksum,i; uint8_t timestamp; #ifdef RADIO_PRIORITY_CEILING nrk_sem_pend (radio_sem); #endif timestamp=_nrk_os_timer_get(); // XXX 2 below are hacks... #ifdef CC2420_OSC_OPT FASTSPI_STROBE(CC2420_SXOSCON); nrk_spin_wait_us(OSC_STARTUP_DELAY); #endif FASTSPI_STROBE(CC2420_SFLUSHRX); FASTSPI_STROBE(CC2420_SFLUSHRX); // Wait until the transceiver is idle while (FIFOP_IS_1 || SFD_IS_1); // Turn off global interrupts to avoid interference on the SPI interface DISABLE_GLOBAL_INT(); // Flush the TX FIFO just in case... FASTSPI_STROBE(CC2420_SFLUSHTX); FASTSPI_STROBE(CC2420_SFLUSHTX); checksum=0; for(i=0; i<pRTI->length; i++ ) { // lets do our own payload checksum because we don't trust the CRC checksum+=pRTI->pPayload[i]; } packetLength = pRTI->length + RF_PACKET_OVERHEAD_SIZE + CHECKSUM_OVERHEAD; //nrk_set_led(3); //do { } while(_nrk_get_high_speed_timer()<(tx_guard_time)); // Write the packet to the TX FIFO (the FCS is appended automatically when AUTOCRC is enabled) // These are only the MAC AGNOSTIC parameters... // Slots for example are at a higher layer since they assume TDMA FASTSPI_WRITE_FIFO((uint8_t*)&packetLength, 1); // Packet length frameControlField = pRTI->ackRequest ? RF_FCF_ACK : RF_FCF_NOACK; FASTSPI_WRITE_FIFO((uint8_t*) &frameControlField, 2); // Frame control field FASTSPI_WRITE_FIFO((uint8_t*) &rfSettings.txSeqNumber, 1); // Sequence number FASTSPI_WRITE_FIFO((uint8_t*) &rfSettings.panId, 2); // Dest. PAN ID FASTSPI_WRITE_FIFO((uint8_t*) &pRTI->destAddr, 2); // Dest. address FASTSPI_WRITE_FIFO((uint8_t*) &rfSettings.myAddr, 2); // Source address nrk_high_speed_timer_wait(slot_start_time,tx_guard_time); //nrk_clr_led(3); /* DISABLE_GLOBAL_INT(); nrk_set_led(3); last=0; do { if(last==_nrk_get_high_speed_timer()) { //while(1) //printf( "TX ERROR %d vs %d\r\n",_nrk_get_high_speed_timer(),tx_guard_time ); break; } last=_nrk_get_high_speed_timer(); } while((volatile)last<(tx_guard_time)); ENABLE_GLOBAL_INT(); nrk_clr_led(3); */ /* // Turn on RX if necessary if (!rfSettings.receiveOn) { FASTSPI_STROBE(CC2420_SRXON); } // Wait for the RSSI value to become valid do { FASTSPI_UPD_STATUS(spiStatusByte); } while (!(spiStatusByte & BM(CC2420_RSSI_VALID))); // TX begins after the CCA check has passed do { FASTSPI_STROBE(CC2420_STXONCCA); FASTSPI_UPD_STATUS(spiStatusByte); halWait(100); } while (!(spiStatusByte & BM(CC2420_TX_ACTIVE))); */ if (pRTI->cca == TRUE) { uint8_t cnt; if (!rfSettings.receiveOn) { FASTSPI_STROBE (CC2420_SRXON); } // Wait for the RSSI value to become valid do { FASTSPI_UPD_STATUS (spiStatusByte); } while (!(spiStatusByte & BM (CC2420_RSSI_VALID))); // TX begins after the CCA check has passed cnt = 0; do { FASTSPI_STROBE (CC2420_STXONCCA); FASTSPI_UPD_STATUS (spiStatusByte); cnt++; if (cnt > 100) { ENABLE_GLOBAL_INT (); nrk_sem_post(radio_sem); return FALSE; } halWait (100); } while (!(spiStatusByte & BM (CC2420_TX_ACTIVE))); } else FASTSPI_STROBE (CC2420_STXON); //nrk_gpio_set(DEBUG_0); // Fill in the rest of the packet now FASTSPI_WRITE_FIFO((uint8_t*) pRTI->pPayload, pRTI->length); // Payload FASTSPI_WRITE_FIFO((uint8_t*) &checksum, 1); // Checksum //nrk_spin_wait_us(200); // FASTSPI_STROBE(CC2420_STXON); // Wait for the transmission to begin before exiting (makes sure that this function cannot be called // a second time, and thereby cancelling the first transmission (observe the FIFOP + SFD test above). while (!SFD_IS_1); success = TRUE; // Turn interrupts back on // ENABLE_GLOBAL_INT(); // Wait for the acknowledge to be received, if any /*if (pRTI->ackRequest) { rfSettings.ackReceived = FALSE; // Wait for the SFD to go low again while (SFD_IS_1); // We'll enter RX automatically, so just wait until we can be sure that the ack reception should have finished // The timeout consists of a 12-symbol turnaround time, the ack packet duration, and a small margin halWait((12 * RF_SYMBOL_DURATION) + (RF_ACK_DURATION) + (2 * RF_SYMBOL_DURATION) + 100); // If an acknowledgment has been received (by the FIFOP interrupt), the ackReceived flag should be set success = rfSettings.ackReceived; }*/ // Turn off the receiver if it should not continue to be enabled DISABLE_GLOBAL_INT(); // XXX hack, temp out //if (!rfSettings.receiveOn) { while (SFD_IS_1); /*FASTSPI_STROBE(CC2420_SRFOFF);*/ } // while (SFD_IS_1); while (SFD_IS_1); // wait for packet to finish FASTSPI_STROBE(CC2420_SFLUSHRX); FASTSPI_STROBE(CC2420_SFLUSHRX); FASTSPI_STROBE(CC2420_SFLUSHTX); FASTSPI_STROBE(CC2420_SFLUSHTX); #ifdef CC2420_OSC_OPT FASTSPI_STROBE(CC2420_SXOSCOFF); #endif FASTSPI_STROBE(CC2420_SRFOFF); // shut off radio ENABLE_GLOBAL_INT(); // Increment the sequence number, and return the result rfSettings.txSeqNumber++; // while (SFD_IS_1); #ifdef RADIO_PRIORITY_CEILING nrk_sem_post(radio_sem); #endif return success; }
//------------------------------------------------------------------------------------------------------- // void rf_init(RF_RX_INFO *pRRI, uint8_t channel, WORD panId, WORD myAddr) // // DESCRIPTION: // Initializes CC2420 for radio communication via the basic RF library functions. Turns on the // voltage regulator, resets the CC2420, turns on the crystal oscillator, writes all necessary // registers and protocol addresses (for automatic address recognition). Note that the crystal // oscillator will remain on (forever). // // ARGUMENTS: // RF_RX_INFO *pRRI // A pointer the RF_RX_INFO data structure to be used during the first packet reception. // The structure can be switched upon packet reception. // uint8_t channel // The RF channel to be used (11 = 2405 MHz to 26 = 2480 MHz) // WORD panId // The personal area network identification number // WORD myAddr // The 16-bit short address which is used by this node. Must together with the PAN ID form a // unique 32-bit identifier to avoid addressing conflicts. Normally, in a 802.15.4 network, the // short address will be given to associated nodes by the PAN coordinator. //------------------------------------------------------------------------------------------------------- void rf_init(RF_RX_INFO *pRRI, uint8_t channel, uint16_t panId, uint16_t myAddr) { uint8_t n; #ifdef RADIO_PRIORITY_CEILING int8_t v; radio_sem = nrk_sem_create(1,RADIO_PRIORITY_CEILING); if (radio_sem == NULL) nrk_kernel_error_add (NRK_SEMAPHORE_CREATE_ERROR, nrk_get_pid ()); v = nrk_sem_pend (radio_sem); if (v == NRK_ERROR) { nrk_kprintf (PSTR ("CC2420 ERROR: Access to semaphore failed\r\n")); } #endif // Make sure that the voltage regulator is on, and that the reset pin is inactive SET_VREG_ACTIVE(); halWait(1000); SET_RESET_ACTIVE(); halWait(1); SET_RESET_INACTIVE(); halWait(100); // Initialize the FIFOP external interrupt //FIFOP_INT_INIT(); //ENABLE_FIFOP_INT(); // Turn off all interrupts while we're accessing the CC2420 registers DISABLE_GLOBAL_INT(); FASTSPI_STROBE(CC2420_SXOSCON); mdmctrl0=0x02E2; FASTSPI_SETREG(CC2420_MDMCTRL0, mdmctrl0); // Std Preamble, CRC, no auto ack, no hw addr decoding //FASTSPI_SETREG(CC2420_MDMCTRL0, 0x0AF2); // Turn on automatic packet acknowledgment // Turn on hw addre decoding FASTSPI_SETREG(CC2420_MDMCTRL1, 0x0500); // Set the correlation threshold = 20 FASTSPI_SETREG(CC2420_IOCFG0, 0x007F); // Set the FIFOP threshold to maximum FASTSPI_SETREG(CC2420_SECCTRL0, 0x01C4); // Turn off "Security" FASTSPI_SETREG(CC2420_RXCTRL1, 0x1A56); // All default except // reference bias current to RX // bandpass filter is set to 3uA /* // FIXME: remove later for auto ack myAddr=MY_MAC; panId=0x02; FASTSPI_SETREG(CC2420_MDMCTRL0, 0x0AF2); // Turn on automatic packet acknowledgment // FASTSPI_SETREG(CC2420_MDMCTRL0, 0x0AE2); // Turn on automatic packet acknowledgment nrk_spin_wait_us(500); nrk_spin_wait_us(500); FASTSPI_WRITE_RAM_LE(&myAddr, CC2420RAM_SHORTADDR, 2, n); nrk_spin_wait_us(500); FASTSPI_WRITE_RAM_LE(&panId, CC2420RAM_PANID, 2, n); nrk_spin_wait_us(500); printf( "myAddr=%d\r\n",myAddr ); */ nrk_spin_wait_us(500); FASTSPI_WRITE_RAM_LE(&panId, CC2420RAM_PANID, 2, n); nrk_spin_wait_us(500); ENABLE_GLOBAL_INT(); // Set the RF channel halRfSetChannel(channel); // Turn interrupts back on ENABLE_GLOBAL_INT(); // Set the protocol configuration rfSettings.pRxInfo = pRRI; rfSettings.panId = panId; rfSettings.myAddr = myAddr; rfSettings.txSeqNumber = 0; rfSettings.receiveOn = FALSE; // Wait for the crystal oscillator to become stable halRfWaitForCrystalOscillator(); // Write the short address and the PAN ID to the CC2420 RAM (requires that the XOSC is on and stable) // DISABLE_GLOBAL_INT(); // FASTSPI_WRITE_RAM_LE(&myAddr, CC2420RAM_SHORTADDR, 2, n); // FASTSPI_WRITE_RAM_LE(&panId, CC2420RAM_PANID, 2, n); // ENABLE_GLOBAL_INT(); #ifdef RADIO_PRIORITY_CEILING v = nrk_sem_post (radio_sem); if (v == NRK_ERROR) { nrk_kprintf (PSTR ("CC2420 ERROR: Release of semaphore failed\r\n")); _nrk_errno_set (2); } #endif auto_ack_enable=0; security_enable=0; last_pkt_encrypted=0; } // rf_init()
//------------------------------------------------------------------------------ // void main (void) // // DESCRIPTION: // Startup routine and main loop //------------------------------------------------------------------------------ int main (void) { uint8_t cnt,i,length,n; nrk_setup_ports(); nrk_setup_uart (UART_BAUDRATE_115K2); printf( "Receiver\r\n" ); nrk_led_clr(0); nrk_led_clr(1); nrk_led_clr(2); nrk_led_clr(3); rfRxInfo.pPayload = rx_buf; rfRxInfo.max_length = RF_MAX_PAYLOAD_SIZE; rfRxInfo.ackRequest= 0; nrk_int_enable(); rf_init (&rfRxInfo, 13, 0x2420, 0x1215); printf( "Waiting for packet...\r\n" ); nrk_led_set(ORANGE_LED); while(1) { nrk_led_set(BLUE_LED); rf_polling_rx_on(); while ((n = rf_rx_check_sfd()) == 0) continue; if (n != 0) { nrk_led_toggle(ORANGE_LED); n = 0; // Packet on its way cnt=0; while ((n = rf_polling_rx_packet ()) == 0) { nrk_led_toggle(GREEN_LED); if (cnt > 50) { //printf( "PKT Timeout\r\n" ); break; // huge timeout as failsafe } halWait(10000); cnt++; } } nrk_led_clr(BLUE_LED); if (n == 1) { nrk_led_clr(RED_LED); // nrk_led_toggle(BLUE_LED); // CRC and checksum passed // printf("packet received\r\n"); // printf("SEQNUM: %d SRCADDR: 0x%x SNR: %d\r\n[",rfRxInfo.seqNumber, rfRxInfo.srcAddr, rfRxInfo.rssi); // printf("\r\n%d, ",rfRxInfo.seqNumber); for(i=0; i<rfRxInfo.length; i++ ) // printf( "%c", rfRxInfo.pPayload[i]); putchar(rfRxInfo.pPayload[i]); // printf( "]\r\n\r\n" ); // printf("\r\nR%d = %d",rfRxInfo.seqNumber,rfRxInfo.length); } else if(n != 0) { printf( "CRC failed!\r\n" ); nrk_led_set(RED_LED); } } }
//----------------------------------------------------------------------------- // See cul.h for a description of this function. //----------------------------------------------------------------------------- BYTE sppSend(SPP_STRUCT* pPacketPointer){ BYTE res = TRUE; // Checking that length is not too long if (pPacketPointer->payloadLength > SPP_MAX_PAYLOAD_LENGTH) { res = TOO_LONG; sppTxStatus = TX_IDLE; } // Flipping the sequence bit, writing total packet length and address if the transfer is not a retransmission. // If it is a retransmission, the fields are correct if(!(pPacketPointer->flags & RETRANSMISSION)) { pPacketPointer->flags ^= SEQUENCE_BIT; pPacketPointer->payloadLength += SPP_HEADER_AND_FOOTER_LENGTH; pPacketPointer->srcAddress = myAddress; } // Setting up the DMA DMA_ABORT_CHANNEL(dmaNumberTx); SET_DMA_SOURCE(dmaTx,pPacketPointer); // Proceed if the packet length is OK. if (res == TRUE) { // Clearing RF interrupt flags and enabling RF interrupts. RFIF &= ~IRQ_DONE; RFIM &= ~IRQ_SFD; INT_SETFLAG(INUM_RF, INT_CLR); #ifdef CCA_ENABLE if(!CCA) { SRX(); // Turning on Rx and waiting to make the RSSI value become valid. halWait(1); } if(CCA) #endif { // Setting up radio DMA_ABORT_CHANNEL(dmaNumberRx); SIDLE(); RFTXRXIF = 0; INT_GLOBAL_ENABLE(FALSE); DMA_ARM_CHANNEL(dmaNumberTx); STX(); INT_GLOBAL_ENABLE(TRUE); sppTxStatus = TX_IN_PROGRESS; if(pPacketPointer->flags & DO_ACK) { pAckData = pPacketPointer; waitForAck(); } else { pAckData = NULL; } RFIM |= IRQ_DONE; } #ifdef CCA_ENABLE // The "air" is busy else { res = CHANNEL_BUSY; RFIM &= ~IRQ_DONE; // De-flipping the sequence bit. if(!(pPacketPointer->flags & RETRANSMISSION)) { pPacketPointer->flags ^= SEQUENCE_BIT; } } #endif } return res; } // ends sppSend
void stop_watch_main(void){ #else void main(void){ #endif STATE state = START_STATE; initStopWatch(); TIMER3_RUN(FALSE); ClearScreen(); Print(0,5,"--STOP WATCH--",1); Rectangle(2 , 4 , 108 , 7); while(!stopApplication()){ switch (state) { case START_STATE: { t.h = t.m = t.s = 0; overflow = 0; printTime(); if(LanguageSel == 1) { Print6(2,10," OK for START ",1); } else { Print(2,8,"按OK键开始:",1); } if(ScanKey() == K_OK) { while(ScanKey() != 0xff); halWait(5); TIMER3_RUN(TRUE); state = RUN_STATE; if(LanguageSel == 1) { Print6(2,10," OK for STOP ",1); } else { Print(2,8,"按OK键停止:",1); } } }break; case RUN_STATE: { INT_GLOBAL_ENABLE(INT_OFF); if(overflow > 0 && overflow < 0x09) { GLED = LED_ON; } else if(overflow > (UINT16)1000) { //overflow = 0; overflow -= 1000; incrementTime(); printTime(); } else { GLED = LED_OFF; } if(ScanKey() == K_OK) { while(ScanKey() != 0xff); halWait(5); TIMER3_RUN(FALSE); state = STOP_STATE; GLED = LED_OFF; } INT_GLOBAL_ENABLE(INT_ON); }break; case STOP_STATE: { printTime(); if(LanguageSel == 1) { Print6(2,10," Total time is:",1); } else { Print(2,8,"总计时间为:",1); } if(ScanKey() == K_OK) { while(ScanKey() != 0xff); halWait(5); state = START_STATE; } }break; default: break; } } while(ScanKey() != 0xff); halWait(5); INT_GLOBAL_ENABLE(INT_OFF); return; }
void flash_main(void){ #else void main(void){ #endif BYTE buffer[30]; char inputBuffer[STRING_LENGTH]; INT8 pointer = 0; BOOL stop = FALSE; BOOL write = FALSE; char c; char *menuText[] = {(char*)" CPU write?", (char*)" DMA write?"}; BYTE command; BOOL unUsed; initFlash(); // Clearing buffers memset(buffer,0,sizeof(buffer)); memset(inputBuffer,0,sizeof(inputBuffer)); // Setting up UART UART_SETUP(0,57600,HIGH_STOP); UTX0IF = 1; // Set UART 0 TX interrupt flag while(getJoystickDirection() != CENTRED); //Displaying the stored flash message. lcdUpdateLine(LINE1,(char*)"Last written:"); if((unUsed = flashUnused((BYTE*)testData, STRING_LENGTH))) { lcdUpdateLine(LINE2,(char*)"Unused"); } else { scrollText((char*) testData, STRING_LENGTH); } while(getJoystickDirection() != CENTRED); while(getJoystickDirection() == CENTRED); while(getJoystickDirection() != CENTRED); // User decides whether to use CPU or DMA to write flash or to abort. command = lcdMenu(menuText,2); if(command == ABORT_MENU) { return; } // Uart communication lcdUpdate((char*)"Enter UART", (char*)"data"); printf((char*)"\n\nFlash Programming\n"); printf((char*)"Press a key\n\n"); uartGetkey (); // wait for a key to be pressed or the application to be ended if (stopApplication() ) return; else { inputBuffer[0] = U0DBUF; halWait(5); USART0_FLUSH(); inputBuffer[1] = U0DBUF; } // Printing the previously written data printf((char*)"\nLast written:\n"); if(unUsed) { printf((char*)"Unused\n"); } else { printf((char*)"%s\n",&testData); } //Aquiring new data: printf((char*)"\n\nType data to be written.\nWill be printed to the LCD next time."); printf((char*)"\n(ENTER: store in flash, ESC: abort)\n\n"); memset(inputBuffer,0,STRING_LENGTH); while(!stop) { c = getkey(); U0DBUF = c; switch (c){ case ENTER: inputBuffer[pointer] = 0; printf((char*)"\n\nTo write: %s\nENTER if OK.\n",inputBuffer); if(getkey() == ENTER) { // Write data to flash; stop = TRUE; write = TRUE; } else { // Reaquire data. printf((char*)"\nEnter text:\n"); pointer = 0; } break; case BACK_SPACE: // Erasing the last typed data. if (pointer > 0) { pointer--; inputBuffer[pointer] = ' '; } break; case ESC: // Abort Flash write. stop = TRUE; write = FALSE; break; default: // Add typed data to buffer. if (pointer < STRING_LENGTH-1) { inputBuffer[pointer] = c; pointer++; } break; } } INT_GLOBAL_ENABLE(INT_OFF); // Updating the flash if asked to. if(write == TRUE) { if(command == 0) { halFlashWritePage((BYTE*) &inputBuffer, buffer, PAGE_NUMBER); } else { writeFlashUsingDMA((BYTE*) &inputBuffer, STRING_LENGTH, PAGE_ADDRESS, TRUE); } printf((char*)"\nUpdated:"); printf((char*)" %s\n",(char __code*) (PAGE_NUMBER << 10)); lcdUpdateLine(LINE1,(char*)"Updated"); } else { printf((char*)"\nNot updated\n"); lcdUpdateLine(LINE1,(char*)"Not updated"); } lcdUpdateLine(LINE2,(char*)"LEFT to continue"); // Done haltApplicationWithLED(); return; }
/****************************************************************************** * @fn writeFlashUsingDMA * * @brief * Writes data to flash using DMA. Erases the page in advance if told to. * * Parameters: * * @param BYTE* pSrcAddr * The start of the data to be written to flash. * * INT16 length * The number of bytes to be written to flash. * * WORD flashAddress * The address in flash the data is to be written to. * * BOOL erase * Indicating whether the flash is to be erased or not. * * @return void * ******************************************************************************/ void writeFlashUsingDMA(BYTE* pSrcAddr, INT16 length, WORD flashAddress, BOOL erase) { BYTE buffer[10]; INT_GLOBAL_ENABLE(INT_OFF); // Setting up the flash address, // erasing the page if required. SET_WORD(FADDRH, FADDRL, (int)(flashAddress >> 1)); if(erase == TRUE) { halFlashErasePage(buffer, PAGE_NUMBER); } halWait(0xFF); // Making sure a multiplum of 4 bytes is transferred. while(length & 0x0003){ length++; } SET_WORD(dmaChannel.SRCADDRH, dmaChannel.SRCADDRL, pSrcAddr); // The start address of the segment SET_WORD(dmaChannel.DESTADDRH, dmaChannel.DESTADDRL, &X_FWDATA); // Input of the AES module SET_WORD(dmaChannel.LENH, dmaChannel.LENL, length); // Setting the length of the transfer (bytes) dmaChannel.VLEN = VLEN_USE_LEN; // Using the length field dmaChannel.PRIORITY = PRI_LOW; // High priority dmaChannel.M8 = M8_USE_8_BITS; // Transferring all 8 bits in each byte. dmaChannel.IRQMASK = FALSE; // The DMA complete interrupt flag is set at completion. dmaChannel.DESTINC = DESTINC_0; // The destination address is constant dmaChannel.SRCINC = SRCINC_1; // The address for data fetch is inremented by 1 byte dmaChannel.TRIG = DMATRIG_FLASH; // Setting the FLASH module to generate the DMA trigger dmaChannel.TMODE = TMODE_SINGLE; // A single byte is transferred each time. dmaChannel.WORDSIZE = WORDSIZE_BYTE; // Set to count bytes. // Setting up the DMA. // Clearing all DMA complete flags and arming the channel. DMA_SET_ADDR_DESC0(&dmaChannel); DMA_ABORT_CHANNEL(0); DMAIRQ &= ~DMA_CHANNEL_0; DMA_ARM_CHANNEL(0); asm("NOP"); // Starting to write FLASH_CONFIG(WRITE); // Waiting for the DMA to finish. while(!(DMAIRQ & DMA_CHANNEL_0)); DMAIRQ &= ~DMA_CHANNEL_0; return; }