/*! * File: * radio_comm.h * * Description: * This file contains the RADIO communication layer. * * Silicon Laboratories Confidential * Copyright 2012 Silicon Laboratories, Inc. */ /* ======================================= * * I N C L U D E * * ======================================= */ #include "..\..\..\bsp.h" /* ======================================= * * D E F I N I T I O N S * * ======================================= */ /* ======================================= * * G L O B A L V A R I A B L E S * * ======================================= */ BIT ctsWentHigh = 0; /* ======================================= * * L O C A L F U N C T I O N S * * ======================================= */ /* ======================================= * * P U B L I C F U N C T I O N S * * ======================================= */ /*! * Gets a command response from the radio chip * * @param byteCount Number of bytes to get from the radio chip * @param pData Pointer to where to put the data * * @return CTS value */ U8 radio_comm_GetResp(U8 byteCount, U8* pData) { SEGMENT_VARIABLE(ctsVal = 0u, U8, SEG_DATA); SEGMENT_VARIABLE(errCnt = RADIO_CTS_TIMEOUT, U16, SEG_DATA); while (errCnt != 0) //wait until radio IC is ready with the data { radio_hal_ClearNsel(); radio_hal_SpiWriteByte(0x44); //read CMD buffer ctsVal = radio_hal_SpiReadByte(); if (ctsVal == 0xFF) { if (byteCount) { radio_hal_SpiReadData(byteCount, pData); } radio_hal_SetNsel(); break; } radio_hal_SetNsel(); errCnt--; } if( errCnt == 0 ) { while(1) { /* ERROR!!!! CTS should never take this long. */ } } if (ctsVal == 0xFF) { ctsWentHigh = 1; } return ctsVal; } /*! * Waits for CTS to be high * * @return CTS value */ U8 radio_comm_PollCTS(void) { #ifdef RADIO_USER_CFG_USE_GPIO1_FOR_CTS while(!radio_hal_Gpio1Level()) { /* Wait...*/ } ctsWentHigh = 1; return 0xFF; #else return radio_comm_GetResp(0, 0); #endif } /*! * Sends a command to the radio chip * * @param byteCount Number of bytes in the command to send to the radio device * @param pData Pointer to the command to send. */ void radio_comm_SendCmd(U8 byteCount, U8* pData) { /* There was a bug in A1 hardware that will not handle 1 byte commands. It was supposedly fixed in B0 but the fix didn't make it at the last minute, so here we go again */ if (byteCount == 1) byteCount++; #warning CHECK IT 2!!!!! while (!ctsWentHigh) { radio_comm_PollCTS(); } radio_hal_ClearNsel(); radio_hal_SpiWriteData(byteCount, pData); radio_hal_SetNsel(); ctsWentHigh = 0; } /*! * Sends a command to the radio chip and gets a response * * @param cmdByteCount Number of bytes in the command to send to the radio device * @param pCmdData Pointer to the command data * @param respByteCount Number of bytes in the response to fetch * @param pRespData Pointer to where to put the response data * * @return CTS value */ U8 radio_comm_SendCmdGetResp(U8 cmdByteCount, U8* pCmdData, U8 respByteCount, U8* pRespData) { radio_comm_SendCmd(cmdByteCount, pCmdData); return radio_comm_GetResp(respByteCount, pRespData); } /*! * Gets a command response from the radio chip * * @param cmd Command ID * @param pollCts Set to poll CTS * @param byteCount Number of bytes to get from the radio chip. * @param pData Pointer to where to put the data. */ void radio_comm_ReadData(U8 cmd, BIT pollCts, U8 byteCount, U8* pData) { if(pollCts) { while(!ctsWentHigh) { radio_comm_PollCTS(); } } radio_hal_ClearNsel(); radio_hal_SpiWriteByte(cmd); radio_hal_SpiReadData(byteCount, pData); radio_hal_SetNsel(); ctsWentHigh = 0; } /*! * Gets a command response from the radio chip * * @param cmd Command ID * @param pollCts Set to poll CTS * @param byteCount Number of bytes to get from the radio chip * @param pData Pointer to where to put the data */ void radio_comm_WriteData(U8 cmd, BIT pollCts, U8 byteCount, U8* pData) { if(pollCts) { while(!ctsWentHigh) { radio_comm_PollCTS(); } } radio_hal_ClearNsel(); radio_hal_SpiWriteByte(cmd); radio_hal_SpiWriteData(byteCount, pData); radio_hal_SetNsel(); ctsWentHigh = 0; }