コード例 #1
0
ファイル: basic_rf.c プロジェクト: SMACproject/SMAC-IAR
/***********************************************************************************
* @fn          basicRfInit
*
* @brief       Initialise basic RF datastructures. Sets channel, short address and
*              PAN id in the chip and configures interrupt on packet reception
*
* @param       pRfConfig - pointer to BASIC_RF_CONFIG struct.
*                          This struct must be allocated by higher layer
*              txState - file scope variable that keeps tx state info
*              rxi - file scope variable info extracted from the last incoming
*                    frame
*
* @return      none
*/
uint8 basicRfInit(basicRfCfg_t* pRfConfig)
{
    if (halRfInit()==FAILED)
        return FAILED;

    halIntOff();

    // Set the protocol configuration
    pConfig = pRfConfig;
    rxi.pPayload   = NULL;

    txState.receiveOn = TRUE;
    txState.frameCounter = 0;

    // Set channel
    halRfSetChannel(pConfig->channel);

    // Write the short address and the PAN ID to the CC2520 RAM
    halRfSetShortAddr(pConfig->myAddr);
    halRfSetPanId(pConfig->panId);

    // if security is enabled, write key and nonce
    #ifdef SECURITY_CCM
    basicRfSecurityInit(pConfig);
    #endif

    // Set up receive interrupt (received data or acknowlegment)
    halRfRxInterruptConfig(basicRfRxFrmDoneIsr);

    halIntOn();

    return SUCCESS;
}
コード例 #2
0
/***********************************************************************************
* @fn           usbInProcess
*
* @brief        Handle traffic flow from RF to USB.
*
* @param        none
*
* @return       none
*/
static void usbInProcess(void)
{
    //uint8 length;

    // USB ready to accept new IN packet
    halIntOff();

    oldEndpoint = USBFW_GET_SELECTED_ENDPOINT();
    USBFW_SELECT_ENDPOINT(4);

    // The IN endpoint is ready to accept data
    if ( USBFW_IN_ENDPOINT_DISARMED() )
    {
        if (usb_sendack)
        {
          // modify and return received packet
          usbfwWriteFifo(&USBF4, usb_buffer[2], &usb_buffer[0]);
          usb_sendack = 0;
          
          // Flag USB IN buffer as not ready (disarming EP4)
          USBFW_SELECT_ENDPOINT(4);
          USBFW_ARM_IN_ENDPOINT();   // Send data to the host
        }
          
/*        // Number of bytes present in RF buffer
        length= bufNumBytes(&rbTxBuf);

        if (length>0) {

            // Limit the size
            if (length > USB_MAX_PACKET_SIZE)
            {
                length = USB_MAX_PACKET_SIZE;
            }

            // Read from UART TX buffer
            bufGet(&rbTxBuf,buffer,length);

            // Write to USB FIFO
            usbfwWriteFifo(&USBF4, length, buffer);

            // Flag USB IN buffer as not ready (disarming EP4)
            USBFW_SELECT_ENDPOINT(4);
            USBFW_ARM_IN_ENDPOINT();   // Send data to the host

        }*/
    }

    USBFW_SELECT_ENDPOINT(oldEndpoint);
    halIntOn();

}
コード例 #3
0
/***********************************************************************************
* @fn           usbOutProcess
*
* @brief        Handle traffic flow from USB to RF.
*
* @param        none
*
* @return       none
*/
static void usbOutProcess(void)
{
    uint8 length, nToSend;

    // If new packet is ready in USB FIFO
    halIntOff();

    oldEndpoint = USBFW_GET_SELECTED_ENDPOINT();
    USBFW_SELECT_ENDPOINT(4);


    if (USBFW_OUT_ENDPOINT_DISARMED() ) {

        // Get length of USB packet, this operation must not be interrupted.
        length = USBFW_GET_OUT_ENDPOINT_COUNT_LOW();
        length+= (int)(USBFW_GET_OUT_ENDPOINT_COUNT_HIGH()) >> 8;

        // Calculate number of bytes available in RF buffer; and the number
        // of bytes we may transfer in this operation.
        nToSend= MIN(BUF_SIZE - bufNumBytes(&rbRxBuf), length);

        // Space available in UART RX buffer ?
        if (nToSend>0)
        {
            // Read from USB FIFO
            usbfwReadFifo(&USBF4, nToSend, buffer);

            // Write to radio TX buffer
            bufPut(&rbRxBuf,buffer,nToSend);

            // If entire USB packet is read from buffer
            if (length == nToSend)
            {
                USBFW_SELECT_ENDPOINT(4);
                USBFW_ARM_OUT_ENDPOINT();
            }

        }
    }

    USBFW_SELECT_ENDPOINT(oldEndpoint);
    halIntOn();
}
コード例 #4
0
ファイル: basic_rf.c プロジェクト: SMACproject/SMAC-IAR
/**********************************************************************************
* @fn          basicRfReceive
*
* @brief       Copies the payload of the last incoming packet into a buffer
*
* @param       pRxData - pointer to data buffer to fill. This buffer must be
*                        allocated by higher layer.
*              len - Number of bytes to read in to buffer
*              rxi - file scope variable holding the information of the last
*                    incoming packet
*
* @return      uint8 - number of bytes actually copied into buffer
*/
uint8 basicRfReceive(uint8* pRxData, uint8 len, int16* pRssi)
{
    // Accessing shared variables -> this is a critical region
    // Critical region start
    halIntOff();
    memcpy(pRxData, rxi.pPayload, min(rxi.length, len));
    if(pRssi != NULL) {
        if(rxi.rssi < 128){
            *pRssi = rxi.rssi - halRfGetRssiOffset();
        }
        else{
            *pRssi = (rxi.rssi - 256) - halRfGetRssiOffset();
        }
    }
    rxi.isReady = FALSE;
    halIntOn();

    // Critical region end

    return min(rxi.length, len);
}
コード例 #5
0
static void appSwitch()
{
/****************************************************************by boo  
  halLcdWriteLine(HAL_LCD_LINE_1, "Switch");
    halLcdWriteLine(HAL_LCD_LINE_2, "Joystick Push");
    halLcdWriteLine(HAL_LCD_LINE_3, "Send Command");*/
#ifdef ASSY_EXP4618_CC2420
    halLcdClearLine(1);
    halLcdWriteSymbol(HAL_LCD_SYMBOL_TX, 1);
#endif


    // Initialize BasicRF
    basicRfConfig.myAddr = SWITCH_ADDR;
    if(basicRfInit(&basicRfConfig)==FAILED) {
      HAL_ASSERT(FALSE);
    }
    
    pTxData[0] = LIGHT_TOGGLE_CMD;

    // Keep Receiver off when not needed to save power
    basicRfReceiveOff();

    // Main loop
    while (TRUE) {
        //if( halJoystickPushed() )**********************by boo
      if(halButtonPushed()==HAL_BUTTON_1)//**************by boo
        {

            basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);
            P1_1=~P1_1;
            // Put MCU to sleep. It will wake up on joystick interrupt
            halIntOff();
            halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
            // interrupt enable
            halIntOn();

        }
    }
}
コード例 #6
0
ファイル: basic_rf.c プロジェクト: SMACproject/SMAC-IAR
/***********************************************************************************
* @fn          basicRfRxFrmDoneIsr
*
* @brief       Interrupt service routine for received frame from radio
*              (either data or acknowlegdement)
*
* @param       rxi - file scope variable info extracted from the last incoming
*                    frame
*              txState - file scope variable that keeps tx state info
*
* @return      none
*/
static void basicRfRxFrmDoneIsr(void)
{
    basicRfPktHdr_t *pHdr;
    uint8 *pStatusWord;
    #ifdef SECURITY_CCM
    uint8 authStatus=0;
    #endif

    // Map header to packet buffer
    pHdr= (basicRfPktHdr_t*)rxMpdu;

    // Clear interrupt and disable new RX frame done interrupt
    halRfDisableRxInterrupt();

    // Enable all other interrupt sources (enables interrupt nesting)
    halIntOn();

    // Read payload length.
    halRfReadRxBuf(&pHdr->packetLength,1);
    pHdr->packetLength &= BASIC_RF_PLD_LEN_MASK; // Ignore MSB
    
    // Is this an acknowledgment packet?
    // Only ack packets may be 5 bytes in total.
    if (pHdr->packetLength == BASIC_RF_ACK_PACKET_SIZE) {

        // Read the packet
        halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength);

        // Make sure byte fields are changed from network to host byte order
    	UINT16_NTOH(pHdr->panId);
    	UINT16_NTOH(pHdr->destAddr);
    	UINT16_NTOH(pHdr->srcAddr);
        #ifdef SECURITY_CCM
        UINT32_NTOH(pHdr->frameCounter);
        #endif

        rxi.ackRequest = !!(pHdr->fcf0 & BASIC_RF_FCF_ACK_BM_L);

        // Read the status word and check for CRC OK
        pStatusWord= rxMpdu + 4;

        // Indicate the successful ACK reception if CRC and sequence number OK
        if ((pStatusWord[1] & BASIC_RF_CRC_OK_BM) && (pHdr->seqNumber == txState.txSeqNumber)) {
            txState.ackReceived = TRUE;
        }

        // No, it is data
    } else {

        // It is assumed that the radio rejects packets with invalid length.
        // Subtract the number of bytes in the frame overhead to get actual payload.

        rxi.length = pHdr->packetLength - BASIC_RF_PACKET_OVERHEAD_SIZE;

        #ifdef SECURITY_CCM
        rxi.length -= (BASIC_RF_AUX_HDR_LENGTH + BASIC_RF_LEN_MIC);
        authStatus = halRfReadRxBufSecure(&rxMpdu[1], pHdr->packetLength, rxi.length,
                                        BASIC_RF_LEN_AUTH, BASIC_RF_SECURITY_M);
        #else
        halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength);
        #endif

        // Make sure byte fields are changed from network to host byte order
    	UINT16_NTOH(pHdr->panId);
    	UINT16_NTOH(pHdr->destAddr);
    	UINT16_NTOH(pHdr->srcAddr);
        #ifdef SECURITY_CCM
        UINT32_NTOH(pHdr->frameCounter);
        #endif

        rxi.ackRequest = !!(pHdr->fcf0 & BASIC_RF_FCF_ACK_BM_L);

        // Read the source address
        rxi.srcAddr= pHdr->srcAddr;

        // Read the packet payload
        rxi.pPayload = rxMpdu + BASIC_RF_HDR_SIZE;

        // Read the FCS to get the RSSI and CRC
        pStatusWord= rxi.pPayload+rxi.length;
        #ifdef SECURITY_CCM
        pStatusWord+= BASIC_RF_LEN_MIC;
        #endif
        rxi.rssi = pStatusWord[0];

        // Notify the application about the received data packet if the CRC is OK
        // Throw packet if the previous packet had the same sequence number
        if( (pStatusWord[1] & BASIC_RF_CRC_OK_BM) && (rxi.seqNumber != pHdr->seqNumber) ) {
            // If security is used check also that authentication passed
            #ifdef SECURITY_CCM
            if( authStatus==SUCCESS ) {
                if ( (pHdr->fcf0 & BASIC_RF_FCF_BM_L) ==
                    (BASIC_RF_FCF_NOACK_L | BASIC_RF_SEC_ENABLED_FCF_BM_L)) {
                        rxi.isReady = TRUE;
                }
            }
            #else
            if ( ((pHdr->fcf0 & (BASIC_RF_FCF_BM_L)) == BASIC_RF_FCF_NOACK_L) ) {
                rxi.isReady = TRUE;
            }              
            #endif
        }
        rxi.seqNumber = pHdr->seqNumber;
    }
  
    // Enable RX frame done interrupt again
    halIntOff();
    halRfEnableRxInterrupt();
}
コード例 #7
0
/***********************************************************************************
* @fn           usbOutProcess
*
* @brief        Handle traffic flow from USB to RF.
*
* @param        none
*
* @return       none
*/
static void usbOutProcess(void)
{
    uint8 length, /*nToSend,*/ packetlength=0;

    // If new packet is ready in USB FIFO
    halIntOff();

    oldEndpoint = USBFW_GET_SELECTED_ENDPOINT();
    USBFW_SELECT_ENDPOINT(4);


    if (USBFW_OUT_ENDPOINT_DISARMED() ) {

        // Get length of USB packet, this operation must not be interrupted.
        length = USBFW_GET_OUT_ENDPOINT_COUNT_LOW();
        length+= USBFW_GET_OUT_ENDPOINT_COUNT_HIGH() >> 8;
        
        // Avoid overflow
        message_length = USB_MAX_MESSAGE_LENGTH; 
        if (usb_bufferIndex + length > USB_MAX_MESSAGE_LENGTH) usb_bufferIndex=0;
        
        // Copy received bytes from FIFO to buffer
        usbfwReadFifo(&USBF4, length, &usb_buffer[usb_bufferIndex]);
        
        // Increase buffer index
        usb_bufferIndex += length;
        
        // If entire USB packet is read from buffer
        USBFW_SELECT_ENDPOINT(4);
        USBFW_ARM_OUT_ENDPOINT();

        // get packet lenght from byte #2 of received packet
        if ((usb_bufferIndex >= 2) && (packetlength == 0)) packetlength = usb_buffer[2];
        if ((usb_bufferIndex > packetlength-1) && (packetlength >= USB_MIN_MESSAGE_LENGTH)) 
        { 
           //extract data from packet
           usb_decode();
           usb_bufferIndex = 0; 
         }     
        
        /*
        // Calculate number of bytes available in RF buffer; and the number
        // of bytes we may transfer in this operation.
        nToSend= MIN(BUF_SIZE - bufNumBytes(&rbRxBuf), length);

        // Space available in UART RX buffer ?
        if (nToSend>0)
        {
            // Read from USB FIFO
            usbfwReadFifo(&USBF4, nToSend, buffer);

            // Write to radio TX buffer
            bufPut(&rbRxBuf,buffer,nToSend);

            // If entire USB packet is read from buffer
            if (length == nToSend)
            {
                USBFW_SELECT_ENDPOINT(4);
                USBFW_ARM_OUT_ENDPOINT();
            }

        }*/
    }

    USBFW_SELECT_ENDPOINT(oldEndpoint);
    halIntOn();
}