Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*!
* 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 *
* ======================================= */
#if (defined SILABS_RADIO_SI446X) || (defined SILABS_RADIO_SI4455)
BIT ctsWentHigh = 0;
#endif
/* ======================================= *
* 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 *
* ======================================= */
#if (defined SILABS_RADIO_SI446X) || (defined SILABS_RADIO_SI4455)
/*!
* 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. */
#ifdef RADIO_COMM_ERROR_CALLBACK
RADIO_COMM_ERROR_CALLBACK();
#endif
}
}
if (ctsVal == 0xFF)
{
ctsWentHigh = 1;
}
return ctsVal;
}
/*!
* 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)
{
while (!ctsWentHigh)
{
radio_comm_PollCTS();
}
radio_hal_ClearNsel();
radio_hal_SpiWriteData(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_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;
}
/*!
* 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
}
/**
* Clears the CTS state variable.
*/
void radio_comm_ClearCTS()
{
ctsWentHigh = 0;
}
#elif (defined SILABS_RADIO_SI4012)
/*!
* 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);
if (qSmbus_SMBusRead(SI4012_SMBUS_ADDRESS, byteCount, pData) != \
SMBUS_RX_FINISHED) {
return FALSE;
}
if (pData[0] == 0x80) {
return TRUE;
}
return FALSE;
}
/*!
* 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.
*/
U8 radio_comm_SendCmd(U8 byteCount, U8* pData)
{
if (qSmbus_SMBusWrite(SI4012_SMBUS_ADDRESS, byteCount, pData) != \
SMBUS_TRANSMISSION_OK) {
return FALSE;
}
return TRUE;
}
#endif
/*!
* 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);
}