ezradio_comm.c

Go to the documentation of this file.
00001 /**************************************************************************/
00033 #include <stdint.h>
00034 #include <stdarg.h>
00035 #include "em_gpio.h"
00036 #include "gpiointerrupt.h"
00037 
00038 #include "ezradio_hal.h"
00039 #include "ezradio_comm.h"
00040 
00042 uint8_t ezradio_comm_CtsWentHigh = 0;
00043 
00052 uint8_t ezradio_comm_GetResp(uint8_t byteCount, uint8_t* pData)
00053 {
00054   uint8_t ctsVal = 0;
00055   uint16_t errCnt = EZRADIO_CTS_TIMEOUT;
00056 
00057   while (errCnt != 0)      //wait until radio IC is ready with the data
00058   {
00059     ezradio_hal_ClearNsel();
00060     ezradio_hal_SpiWriteByte(0x44);    //read CMD buffer
00061     ezradio_hal_SpiReadByte(&ctsVal);
00062     if (ctsVal == 0xFF)
00063     {
00064       if (byteCount)
00065       {
00066         ezradio_hal_SpiReadData(byteCount, pData);
00067       }
00068       ezradio_hal_SetNsel();
00069       break;
00070     }
00071     ezradio_hal_SetNsel();
00072     errCnt--;
00073   }
00074 
00075   if (errCnt == 0)
00076   {
00077     while(1)
00078     {
00079       /* ERROR!!!!  CTS should never take this long. */
00080       #ifdef ezradio_comm_ERROR_CALLBACK
00081         ezradio_comm_ERROR_CALLBACK();
00082       #endif
00083     }
00084   }
00085 
00086   if (ctsVal == 0xFF)
00087   {
00088     ezradio_comm_CtsWentHigh = 1;
00089   }
00090 
00091   return ctsVal;
00092 }
00093 
00100 void ezradio_comm_SendCmd(uint8_t byteCount, uint8_t* pData)
00101 {
00102     while (!ezradio_comm_CtsWentHigh)
00103     {
00104         ezradio_comm_PollCTS();
00105     }
00106     ezradio_hal_ClearNsel();
00107     ezradio_hal_SpiWriteData(byteCount, pData);
00108     ezradio_hal_SetNsel();
00109     ezradio_comm_CtsWentHigh = 0;
00110 }
00111 
00120 void ezradio_comm_ReadData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t* pData)
00121 {
00122     if(pollCts)
00123     {
00124         while(!ezradio_comm_CtsWentHigh)
00125         {
00126             ezradio_comm_PollCTS();
00127         }
00128     }
00129     ezradio_hal_ClearNsel();
00130     ezradio_hal_SpiWriteByte(cmd);
00131     ezradio_hal_SpiReadData(byteCount, pData);
00132     ezradio_hal_SetNsel();
00133     ezradio_comm_CtsWentHigh = 0;
00134 }
00135 
00136 
00145 void ezradio_comm_WriteData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t* pData)
00146 {
00147     if(pollCts)
00148     {
00149         while(!ezradio_comm_CtsWentHigh)
00150         {
00151             ezradio_comm_PollCTS();
00152         }
00153     }
00154     ezradio_hal_ClearNsel();
00155     ezradio_hal_SpiWriteByte(cmd);
00156     ezradio_hal_SpiWriteData(byteCount, pData);
00157     ezradio_hal_SetNsel();
00158     ezradio_comm_CtsWentHigh = 0;
00159 }
00160 
00166 uint8_t ezradio_comm_PollCTS(void)
00167 {
00168 #ifdef RADIO_USER_CFG_USE_GPIO1_FOR_CTS
00169     while(!ezradio_hal_Gpio1Level())
00170     {
00171         /* Wait...*/
00172     }
00173     ezradio_comm_CtsWentHigh = 1;
00174     return 0xFF;
00175 #else
00176     return ezradio_comm_GetResp(0, 0);
00177 #endif
00178 }
00179 
00183 void ezradio_comm_ClearCTS()
00184 {
00185   ezradio_comm_CtsWentHigh = 0;
00186 }
00187 
00198 uint8_t ezradio_comm_SendCmdGetResp(uint8_t cmdByteCount, uint8_t* pCmdData, uint8_t respByteCount, uint8_t* pRespData)
00199 {
00200     ezradio_comm_SendCmd(cmdByteCount, pCmdData);
00201     return ezradio_comm_GetResp(respByteCount, pRespData);
00202 }
00203