-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShockBurst.h
281 lines (229 loc) · 6.1 KB
/
ShockBurst.h
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
/*
ShockBurst.h
Copyright 2011 Sebastien Besombes <[email protected]>
This file is part of the ShockBurst arduino library.
Shockburst is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ShockBurst is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with simavr. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _nRF24L01SB_H_
#define _nRF24L01SB_H_
#include <Arduino.h>
#include <nRF24L01.h>
#include <SpiDriver.h>
class ShockBurst
{
public:
struct
{
volatile uint8_t dataReceived:1 ;
volatile uint8_t transmitOk:1 ;
volatile uint8_t transmitErr:1 ;
volatile uint8_t pipe:3 ;
//Error control data
volatile uint8_t losts:4 ; //Number of lost packets
volatile uint8_t trys:4 ; //Number of try during one packet send
} state ;
struct
{
/**
* Pipe 0 and pipe 1 have once up to 5 bytes mac adress.
*/
uint8_t pipe0[5] ;
uint8_t pipe1[5] ;
/**
* Pipe 2 to 5 shares the 4th upper bytes with pipe1 and has only the first byte configurable.
*/
uint8_t pipe[4] ;
} rxmac ;
/*
* CE Pin controls RX / TX, default 8.
*/
uint8_t cePin ;
/*
* CSN Pin Chip Select Not, default 7.
*/
uint8_t csnPin ;
/*
* Spi interface (must extend spi).
* Hardware SPI by default.
*/
SpiDriver *spi ;
/**
* Payload length. Values 1 to 32 are valid for static payload.
* 0 indicates a dynamic payload.
* 0 default value.
*/
uint8_t payload ;
/**
* Channel. 0 to 127.
* 100 default value.
*/
uint8_t channel ;
/**
* Mac adress of the device.
* 0x1F1F1F1F1F default value.
*/
uint8_t mac[5] ;
/**
* Width of mac address.
* 5 bytes default value. Possible values: see AW_xB enumeration.
*/
uint8_t mac_size ;
ShockBurst() ;
/**
* Init of the nrf24l01+. Must be called once at the beginning of the init part.
*/
void init() ;
/**
* Set whole nrf24l01+ configuration. Must be called after each configuration change.
*/
void config() ;
/**
* Add a new pipe.
* @param pipe number. Valid range is from 0 to 5.
* @param pipemac mac adress of the pipe. Note that only pipe 0 and one have a complete mac.
* for pipe 2 to 5 only last byte is used and the 4 first bytes are taken from pipe 1.
*/
void setPipe(uint8_t pipe, uint8_t pipemac[5]) ;
/**
* Send data.
* @param data buffer to send.
* @param size size of the data buffer. Data buffer must be of 1 to 32 bytes maximum.
*/
void send(uint8_t* data, uint8_t size) ;
/**
* Send data with no waited ACK.
* @param data buffer to send.
* @param size size of the data buffer. Data buffer must be of 1 to 32 bytes maximum.
*/
void sendNoAck(uint8_t* data, uint8_t size) ;
/**
* Send and ACK on a pipe.
* @param pipe pipe to use for sending ACK.
* @param data associated buffer to send. May be NULL if no payload associated with ACK.
* @param size size of the data buffer. Data buffer must be of 1 to 32 bytes maximum.
*/
void ack(uint8_t pipe, uint8_t* data, uint8_t size) ;
/**
* Read data for the nrf fifo.
* @param data buffer to fill with data, must be at least 32 bytes len.
* @param pipe get the pipe number of the data read.
* @return number of bytes read.
* If the receive buffer is empty state.dataReceived is set to 0 on exit.
* The pipe containing the read buffer is set in state.pipe.
*/
uint8_t getData(uint8_t* data) ;
/**
* Read the payload width at the top of the RX payload.
*/
uint8_t getPlWidth() ;
/**
* Switch to RX mode so the nrf is ready to receive data.
*/
void powerUpRx() ;
/**
* Switch to TX mode so the nrf is ready to send data contained in the payload.
*/
void powerUpTx() ;
/**
* Indicate if TX fifo is full.
* @return true if full.
*/
bool txFifoFull() ;
/**
* Indicate if TX fifo is empty.
* @return true if empty.
*/
bool txFifoEmpty() ;
/**
* Indicate if RX fifo is full.
* @return true if full.
*/
bool rxFifoFull() ;
/**
* Indicate if RX fifo is empty.
* @return true if empty.
*/
bool rxFifoEmpty() ;
/**
* Indicates if something is beeing curently sent on the channel. Usefull to check
* if another nrf is already sending data to avoid colliding.
* @return true if the channel is already occupied.
*/
bool rpd() ;
/**
* Clear status.
*/
void clearStatus() ;
/**
* Clear state.
*/
void clearState() ;
/**
* Flush RX fifo.
*/
void flushRx() ;
/**
* Flush TX fifo.
*/
void flushTx() ;
/**
* flush RX fifo and TX fifo and clear state.
*/
void flush() ;
/**
* this function must be called inside the IRQ function for the nrf.
*/
void nrf24l_irq() ;
/**
* Read a 8 bits register.
* @param reg Register number.
* @return Register value. No error control is made.
*/
uint8_t getRegister(uint8_t reg) ;
/**
* Sets a 8 bits register.
* @param reg Register number.
* @param value Value to set.
*/
void setRegister(uint8_t reg, uint8_t value) ;
/**
* Sets bits in a 8 bits register.
* @param reg Register number.
* @param mask bit mask, only bits at 1 will be set.
*/
void setRegisterBits(uint8_t reg, uint8_t mask) ;
/**
* Unsets bits in a 8 bits register.
* @param reg Register number.
* @param mask bit mask, only bits at 1 will be unset.
*/
void unsetRegisterBits(uint8_t reg, uint8_t mask) ;
/**
* Write a buffer inside a register.
* @param reg Register number.
* @param data Data to write.
* @param size Size of the data to write.
*/
void writeRegister(uint8_t reg, uint8_t* data, int size) ;
/**
* Read a register content inside a buffer.
* @param reg Register number.
* @param data Data to read.
* @param size Size of the data to read.
*/
void readRegister(uint8_t reg, uint8_t* data, int size) ;
#ifdef DEBUG
void dispMac(uint8_t mac[]) ;
void status() ;
#endif
} ;
#endif