EFR32 Mighty Gecko 1 Software Documentation  efr32mg1-doc-5.1.2
ezradio_comm.c
Go to the documentation of this file.
1 /**************************************************************************/
33 #include <stdint.h>
34 #include <stdarg.h>
35 #include "em_gpio.h"
36 #include "gpiointerrupt.h"
37 #include "ustimer.h"
38 
39 #include "ezradiodrv_config.h"
40 #include "ezradio_hal.h"
41 #include "ezradio_comm.h"
42 
45 
54 uint8_t ezradio_comm_GetResp(uint8_t byteCount, uint8_t* pData)
55 {
56  uint16_t errCnt = EZRADIODRV_COMM_CTS_RETRY;
57  uint8_t ret = 0;
58 #if !defined(EZRADIODRV_SPI_4WIRE_MODE)
59  uint8_t rxCtsVal;
60 #else
61  uint8_t txCtsBuf[EZRADIODRV_MAX_CTS_BUFF_SIZE];
62  uint8_t rxCtsBuf[EZRADIODRV_MAX_CTS_BUFF_SIZE];
63  uint8_t cnt;
64 
65  txCtsBuf[0] = 0x44;
66  for (cnt = 1; cnt < byteCount+2; cnt++)
67  {
68  txCtsBuf[cnt] = 0xFF;
69  }
70 #endif
71 
72  while (errCnt != 0) //wait until radio IC is ready with the data
73  {
74 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
75  ezradio_hal_SpiWriteReadData(2, txCtsBuf, rxCtsBuf);
76 #else
78  ezradio_hal_SpiWriteByte(0x44); //read CMD buffer
79  ezradio_hal_SpiReadByte(&rxCtsVal);
80 #endif
81 
82 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
83  if (rxCtsBuf[1] == 0xFF)
84 #else
85  if (rxCtsVal == 0xFF)
86 #endif
87  {
88  if (byteCount)
89  {
90 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
91  ezradio_hal_SpiWriteReadData(byteCount+2, txCtsBuf, rxCtsBuf);
92 #else
93  ezradio_hal_SpiReadData(byteCount, pData);
94 #endif
95  }
96 #if !defined(EZRADIODRV_SPI_4WIRE_MODE)
98 #endif
99  break;
100  }
101 #if !defined(EZRADIODRV_SPI_4WIRE_MODE)
103 #endif
104  errCnt--;
105  }
106 
107 #if defined(ezradio_comm_ERROR_CALLBACK)
108  if (errCnt == 0)
109  {
110  ezradio_comm_ERROR_CALLBACK();
111  }
112 #endif
113 
114 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
115  if (rxCtsBuf[1] == 0xFF)
116 #else
117  if (rxCtsVal == 0xFF)
118 #endif
119  {
121  ret = rxCtsVal;
122  }
123 
124 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
125  for (cnt = 0; cnt < byteCount; cnt++)
126  {
127  pData[cnt] = rxCtsBuf[cnt+2];
128  }
129 #endif
130 
131  return ret;
132 }
133 
140 void ezradio_comm_SendCmd(uint8_t byteCount, uint8_t* pData)
141 {
142  while (!ezradio_comm_CtsWentHigh)
143  {
145  }
146 #if !defined(EZRADIODRV_SPI_4WIRE_MODE)
148 #endif
149  ezradio_hal_SpiWriteData(byteCount, pData);
150 #if !defined(EZRADIODRV_SPI_4WIRE_MODE)
152 #endif
154 }
155 
164 void ezradio_comm_ReadData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t* pData)
165 {
166 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
167  uint8_t txBuf[] = {cmd, 0xFF};
168  uint8_t rxBuf[EZRADIODRV_MAX_CTS_BUFF_SIZE];
169  uint8_t cnt;
170 #endif
171 
172  if(pollCts)
173  {
175  {
177  }
178  }
179 
180 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
181  ezradio_hal_SpiWriteReadData(byteCount+1, txBuf, rxBuf);
182 
183  for (cnt = 0; cnt < byteCount; cnt++)
184  {
185  pData[cnt] = rxBuf[cnt+1];
186  }
187 
188 #else
191  ezradio_hal_SpiReadData(byteCount, pData);
193 #endif
194 
196 }
197 
198 
207 void ezradio_comm_WriteData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t* pData)
208 {
209 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
210  uint8_t txBuff[EZRADIODRV_MAX_CTS_BUFF_SIZE];
211  uint8_t cnt;
212 #endif
213 
214  if(pollCts)
215  {
217  {
219  }
220  }
221 
222 #if defined(EZRADIODRV_SPI_4WIRE_MODE)
223  txBuff[0] = cmd;
224 
225  for (cnt = 0; cnt < byteCount; cnt++)
226  {
227  txBuff[cnt+1] = pData[cnt];
228  }
229 
230  ezradio_hal_SpiWriteData(byteCount + 1, txBuff);
231 
232 #else
235  ezradio_hal_SpiWriteData(byteCount, pData);
237 #endif
238 
240 }
241 
247 uint8_t ezradio_comm_PollCTS(void)
248 {
249 #ifdef EZRADIODRV_COMM_USE_GPIO1_FOR_CTS
250  uint8_t ret = 0;
251  uint16_t errCnt = EZRADIODRV_COMM_CTS_RETRY;
252 
253  USTIMER_Init();
254  while(!ezradio_hal_Gpio1Level())
255  {
256  /* Wait 10us before retry */
257  USTIMER_Delay( 10u );
258  errCnt--;
259  }
260  USTIMER_DeInit();
261 
262  /* CTS arrived */
263  if (errCnt)
264  {
266  ret = 0xFF;
267  }
268 
269  return ret;
270 #else
271  return ezradio_comm_GetResp(0, 0);
272 #endif
273 }
274 
279 {
281 }
282 
293 uint8_t ezradio_comm_SendCmdGetResp(uint8_t cmdByteCount, uint8_t* pCmdData, uint8_t respByteCount, uint8_t* pRespData)
294 {
295  ezradio_comm_SendCmd(cmdByteCount, pCmdData);
296  return ezradio_comm_GetResp(respByteCount, pRespData);
297 }
298 
void ezradio_hal_SetNsel(void)
Definition: ezradio_hal.c:165
#define EZRADIODRV_COMM_CTS_RETRY
Number of wait-for-CTS retries.
void ezradio_comm_ClearCTS()
Definition: ezradio_comm.c:278
Ecode_t USTIMER_Init(void)
Activate and initialize the hardware timer used to pace the 1 microsecond delay functions.
Definition: ustimer.c:139
This file contains EZRadio HAL.
void ezradio_hal_SpiWriteReadData(uint8_t byteCount, uint8_t *txData, uint8_t *rxData)
Definition: ezradio_hal.c:241
void ezradio_hal_SpiWriteData(uint8_t byteCount, uint8_t *pData)
Definition: ezradio_hal.c:218
EZRADIODRV configuration file.
General Purpose IO (GPIO) peripheral API.
void ezradio_hal_SpiWriteByte(uint8_t byteToWrite)
Definition: ezradio_hal.c:197
uint8_t ezradio_comm_PollCTS(void)
Definition: ezradio_comm.c:247
void ezradio_hal_SpiReadData(uint8_t byteCount, uint8_t *pData)
Definition: ezradio_hal.c:229
uint8_t ezradio_comm_GetResp(uint8_t byteCount, uint8_t *pData)
Definition: ezradio_comm.c:54
uint8_t ezradio_comm_SendCmdGetResp(uint8_t cmdByteCount, uint8_t *pCmdData, uint8_t respByteCount, uint8_t *pRespData)
Definition: ezradio_comm.c:293
void ezradio_hal_SpiReadByte(uint8_t *readByte)
Definition: ezradio_hal.c:207
Ecode_t USTIMER_DeInit(void)
Deinitialize USTIMER driver.
Definition: ustimer.c:190
void ezradio_comm_WriteData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t *pData)
Definition: ezradio_comm.c:207
GPIOINT API definition.
void ezradio_hal_ClearNsel(void)
Definition: ezradio_hal.c:157
Microsecond delay function API definition.
Ecode_t USTIMER_Delay(uint32_t usec)
Delay a given number of microseconds.
Definition: ustimer.c:221
void ezradio_comm_ReadData(uint8_t cmd, uint8_t pollCts, uint8_t byteCount, uint8_t *pData)
Definition: ezradio_comm.c:164
This file contains the EZRadio communication layer.
void ezradio_comm_SendCmd(uint8_t byteCount, uint8_t *pData)
Definition: ezradio_comm.c:140
uint8_t ezradio_comm_CtsWentHigh
Definition: ezradio_comm.c:44