-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcc1101_spi.c
executable file
·338 lines (290 loc) · 9.05 KB
/
cc1101_spi.c
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2021
*/
#include "cc1101_spi.h"
#include "cc1101_internal.h"
#define MAX_REPEATS 10
#define SPI_BURST 0x40
#define SPI_READ 0x80
/*
* Read a single byte from one of the device's configuration registers via SPI
*
* Arguments:
* cc1101: device struct
* reg: address of CC1101 register to read
*
* Returns:
* Transaction struct containing register contents and device status
*/
spi_transaction_t cc1101_spi_read_config_register(cc1101_t* cc1101, unsigned char reg) {
struct spi_transfer t = {0};
struct spi_message m;
spi_transaction_t transaction;
WARN_ON(reg > 0x2E);
transaction.header = reg | SPI_READ;
transaction.data = 0;
t.tx_buf = &transaction;
t.rx_buf = &transaction;
t.len = 2;
t.cs_change = 0;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
spi_sync(cc1101->spi, &m);
return transaction;
}
/*
* Writes a single byte to one of the device's registers via SPI
*
* Arguments:
* cc1101: device struct
* reg: address of CC1101 register to write
* value: byte to write to the register
*
* Returns:
* Device status
*/
unsigned char cc1101_spi_write_config_register(cc1101_t* cc1101, unsigned char reg, unsigned char value) {
struct spi_transfer t = {0};
struct spi_message m;
spi_transaction_t transaction;
WARN_ON(reg > 0x2E);
transaction.header = reg;
transaction.data = value;
t.tx_buf = &transaction,
t.rx_buf = &transaction,
t.len = 2,
t.cs_change = 0,
spi_message_init(&m);
spi_message_add_tail(&t, &m);
spi_sync(cc1101->spi, &m);
return transaction.header;
}
/*
* Read a sequence of bytes from the device's registers via SPI
*
* Arguments:
* cc1101: device struct
* reg: address of CC1101 registers to start reading from.
* out: pointer to a buffer that will contain the register values read from the device
* len: size of out, number of bytes to read from the device
*
* Returns:
* Device status
*/
static unsigned char read_registers(cc1101_t* cc1101, unsigned char reg, unsigned char* out, unsigned char len) {
struct spi_transfer t = {0};
struct spi_message m;
unsigned char transaction[FIFO_LEN + 1];
transaction[0] = reg | SPI_BURST | SPI_READ;
t.tx_buf = &transaction;
t.rx_buf = &transaction;
t.len = len + 1;
t.cs_change = 0;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
spi_sync(cc1101->spi, &m);
memcpy(out, &transaction[1], len);
return transaction[0];
}
/*
* Read a sequence of bytes from the device's config registers via SPI
*
* Arguments:
* cc1101: device struct
* registers: pointer to a buffer that will contain the values read from the device
* len: size of out, number of bytes to read from the device
*
* Returns:
* Device status
*
*/
unsigned char cc1101_spi_read_config_registers(cc1101_t* cc1101, unsigned char* registers, unsigned char len) {
BUG_ON(len > CONFIG_REGISTERS_LEN);
return read_registers(cc1101, 0x00, registers, len);
}
/*
* Read a sequence of bytes from the device's PATABLE via SPI
*
* Arguments:
* cc1101: device struct
* patable: pointer to a buffer that will contain the values read from the device
* len: size of out, number of bytes to read from the device
*
* Returns:
* Device status
*/
unsigned char cc1101_spi_read_patable(cc1101_t* cc1101, unsigned char* patable, unsigned char len) {
BUG_ON(len > PATABLE_LEN);
return read_registers(cc1101, PATABLE, patable, len);
}
/*
* Read a sequence of bytes from the device's RXFIFO via SPI
*
* Arguments:
* cc1101: device struct
* rx_bytes: pointer to a buffer that will contain the values read from the device
* len: size of out, number of bytes to read from the device
*
* Returns:
* Device status
*/
unsigned char cc1101_spi_read_rxfifo(cc1101_t* cc1101, unsigned char* rx_bytes, unsigned char len) {
BUG_ON(len > FIFO_LEN);
return read_registers(cc1101, FIFO, rx_bytes, len);
}
/*
* Write a sequence of bytes to the device's registers via SPI
*
* Arguments:
* cc1101: device struct
* reg: address of CC1101 registers to start writing to. Must have the burst bit set
* in: pointer to a buffer containing a byte sequence to write to the device registers
* len: number of bytes in buffer that will be written to the device
*
* Returns:
* Device status
*/
static unsigned char write_registers(cc1101_t* cc1101, unsigned char reg, const unsigned char* in, unsigned char len) {
struct spi_transfer t = {0};
struct spi_message m;
unsigned char transaction[FIFO_LEN + 1];
transaction[0] = reg | SPI_BURST;
memcpy(&transaction[1], in, len);
t.tx_buf = &transaction;
t.rx_buf = &transaction;
t.len = len + 1;
t.cs_change = 0;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
spi_sync(cc1101->spi, &m);
return transaction[0];
}
/*
* Write a sequence of bytes to the device's configuration registers via SPI
*
* Arguments:
* cc1101: device struct
* registers: pointer to a buffer containing a byte sequence to write to the device registers
* len: number of bytes in buffer that will be written to the device
*
* Returns:
* Device status
*/
unsigned char cc1101_spi_write_config_registers(cc1101_t* cc1101, const unsigned char* registers, unsigned char len) {
BUG_ON(len > CONFIG_REGISTERS_LEN);
return write_registers(cc1101, 0x00, registers, len);
}
/*
* Write a sequence of bytes to the device's PATABLE registers via SPI
*
* Arguments:
* cc1101: device struct
* patable: pointer to a buffer containing a byte sequence to write to the PATABLE
* len: number of bytes in buffer that will be written to the device
*
* Returns:
* Device status
*/
unsigned char cc1101_spi_write_patable(cc1101_t* cc1101, const unsigned char* patable, unsigned char len) {
BUG_ON(len > PATABLE_LEN);
return write_registers(cc1101, PATABLE, patable, len);
}
/*
* Write a sequence of bytes to the device's TXFIFO via SPI
*
* Arguments:
* cc1101: device struct
* tx_bytes: pointer to a buffer containing a byte sequence to write to the TXFIFO
* len: number of bytes in buffer that will be written to the device
*
* Returns:
* Device status
*/
unsigned char cc1101_spi_write_txfifo(cc1101_t* cc1101, const unsigned char* tx_bytes, unsigned char len) {
BUG_ON(len > FIFO_LEN);
return write_registers(cc1101, FIFO, tx_bytes, len);
}
/*
* Sends a command to the device via SPI
*
* Arguments:
* cc1101: device struct
* command: command to send to the device
*
* Returns:
* Device status
*/
unsigned char cc1101_spi_send_command(cc1101_t* cc1101, unsigned char command) {
struct spi_transfer t = {0};
struct spi_message m;
unsigned char transaction;
WARN_ON(command < 0x30 || command > 0x3D);
transaction = command;
t.tx_buf = &transaction;
t.rx_buf = &transaction;
t.len = 1;
t.cs_change = 0;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
spi_sync(cc1101->spi, &m);
return transaction;
}
/*
* Read a single byte from one of the device's status registers via SPI
*
* Arguments:
* cc1101: device struct
* reg: address of CC1101 status register to read
*
* Returns:
* Transaction struct containing register contents and device status
*/
spi_transaction_t cc1101_spi_read_status_register_once(cc1101_t* cc1101, unsigned char reg) {
struct spi_transfer t = {0};
struct spi_message m;
spi_transaction_t transaction;
WARN_ON(reg < 0x30 || reg > 0x3D);
// Status registers require the burst bit to be set
transaction.header = reg | SPI_BURST | SPI_READ;
transaction.data = 0;
t.tx_buf = &transaction;
t.rx_buf = &transaction;
t.len = 2;
t.cs_change = 0;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
spi_sync(cc1101->spi, &m);
return transaction;
}
/*
* Read a single byte from one of the device's status registers via SPI
*
* The double-read implemented here is recommended by the CC1101 Errata to prevent corrupt values
* from being read from the device.
*
* Arguments:
* cc1101: device struct
* reg: address of CC1101 status register to read
*
* Returns:
* Transaction struct containing register contents and device status
*/
spi_transaction_t cc1101_spi_read_status_register(cc1101_t* cc1101, unsigned char reg) {
spi_transaction_t result, result_check;
// Read status register
result = cc1101_spi_read_status_register_once(cc1101, reg);
while (1) {
// Read again
result_check = cc1101_spi_read_status_register_once(cc1101, reg);
// If the values match, the value was valid
if (result.data == result_check.data) {
break;
}
// If not, continue until two reads agree
else {
result = result_check;
}
}
return result;
}