-
Notifications
You must be signed in to change notification settings - Fork 5
/
tm_stm32_nrf24l01.h
371 lines (325 loc) · 10.8 KB
/
tm_stm32_nrf24l01.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* @author Tilen Majerle
* @email tilen@majerle.eu
* @website http://stm32f4-discovery.net
* @link http://stm32f4-discovery.net/2015/09/hal-library-25-nrf24l01-for-stm32fxxx/
* @version v1.0
* @ide Keil uVision
* @license MIT
* @brief Library template
*
\verbatim
----------------------------------------------------------------------
Copyright (c) 2016 Tilen Majerle
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------
\endverbatim
*/
#ifndef TM_NRF24L01_H
#define TM_NRF24L01_H 100
/* C++ detection */
#ifdef __cplusplus
extern "C" {
#endif
/**
* @addtogroup TM_STM32Fxxx_HAL_Libraries
* @{
*/
/**
* @defgroup TM_NRF24L01P
* @brief nRF24L01+ library for STM32xxx devices - http://stm32f4-discovery.net/2015/09/hal-library-25-nrf24l01-for-stm32fxxx/
* @{
*
* This library allows you to work with nRF24L01+ modules.
*
* You can send and receive data from nRF24L01+ modules.
*
* \par Default pinout
*
\verbatim
NRF24L01+ STM32Fxxx DESCRIPTION
GND GND Ground
VCC 3.3V 3.3V
CE PD8 RF activated pin
CSN PD7 Chip select pin for SPI
SCK PC10 SCK pin for SPI
MOSI PC12 MOSI pin for SPI
MISO PC11 MISO pin for SPI
IRQ Not used Interrupt pin. Goes low when active. Pin functionality is active, but not used in library
\endverbatim
*
* IRQ pin is not used in this library, but its functionality is enabled by this software.
*
* You can still set any pin on Fxxx to be an external interrupt and handle interrupts from nRF24L01+ module.
*
* The easiest way to that is to use @ref TM_EXTI library and attach interrupt functionality to this pin
*
* \par Custom pinout
*
* Add lines below in your defines.h file if you want to change default pinout:
*
\code
//Change SPI used. Refer to TM SPI library to check which pins are for SPI
#define NRF24L01_SPI SPI3
#define NRF24L01_SPI_PINS TM_SPI_PinsPack_2
//Change CSN pin. This is for SPI communication
#define NRF24L01_CSN_PORT GPIOD
#define NRF24L01_CSN_PIN GPIO_Pin_7
//Change CE pin. This pin is used to enable/disable transmitter/receiver functionality
#define NRF24L01_CE_PORT GPIOD
#define NRF24L01_CE_PIN GPIO_Pin_8
\endcode
*
* \par Changelog
*
\verbatim
Version 1.0
- First release
\endverbatim
*
* \par Dependencies
*
\verbatim
- STM32Fxxx HAL
- defines.h
- TM SPI
- TM GPIO
\endverbatim
*/
#include "stm32f4xx_hal.h"
//#include "defines.h"
#include "tm_stm32_spi.h"
#include "tm_stm32_gpio.h"
/**
* @defgroup TM_NRF24L01P_Macros
* @brief Library defines
* @{
*/
/* Default SPI used */
#ifndef NRF24L01_SPI
#define NRF24L01_SPI SPI1
#define NRF24L01_SPI_PINS TM_SPI_PinsPack_2
#endif
/* SPI chip enable pin */
#ifndef NRF24L01_CSN_PIN
#define NRF24L01_CSN_PORT GPIOC
#define NRF24L01_CSN_PIN GPIO_PIN_1
#endif
/* Chip enable for transmitting */
#ifndef NRF24L01_CE_PIN
#define NRF24L01_CE_PORT GPIOC
#define NRF24L01_CE_PIN GPIO_PIN_0
#endif
/* Pins configuration */
#define NRF24L01_CE_LOW TM_GPIO_SetPinLow(NRF24L01_CE_PORT, NRF24L01_CE_PIN)
#define NRF24L01_CE_HIGH TM_GPIO_SetPinHigh(NRF24L01_CE_PORT, NRF24L01_CE_PIN)
#define NRF24L01_CSN_LOW TM_GPIO_SetPinLow(NRF24L01_CSN_PORT, NRF24L01_CSN_PIN)
#define NRF24L01_CSN_HIGH TM_GPIO_SetPinHigh(NRF24L01_CSN_PORT, NRF24L01_CSN_PIN)
/* Interrupt masks */
#define NRF24L01_IRQ_DATA_READY 0x40 /*!< Data ready for receive */
#define NRF24L01_IRQ_TRAN_OK 0x20 /*!< Transmission went OK */
#define NRF24L01_IRQ_MAX_RT 0x10 /*!< Max retransmissions reached, last transmission failed */
/**
* @}
*/
/**
* @defgroup TM_NRF24L01P_Typedefs
* @brief Library Typedefs
* @{
*/
/**
* @brief Interrupt structure
*/
typedef union _TM_NRF24L01_IRQ_t {
struct {
uint8_t reserved0:4;
uint8_t MaxRT:1; /*!< Set to 1 if MAX retransmissions flag is set */
uint8_t DataSent:1; /*!< Set to 1 if last transmission is OK */
uint8_t DataReady:1; /*!< Set to 1 if data are ready to be read */
uint8_t reserved1:1;
} F;
uint8_t Status; /*!< NRF status register value */
} TM_NRF24L01_IRQ_t;
/**
* @brief Transmission status enumeration
*/
typedef enum _TM_NRF24L01_Transmit_Status_t {
TM_NRF24L01_Transmit_Status_Lost = 0x00, /*!< Message is lost, reached maximum number of retransmissions */
TM_NRF24L01_Transmit_Status_Ok = 0x01, /*!< Message sent successfully */
TM_NRF24L01_Transmit_Status_Sending = 0xFF /*!< Message is still sending */
} TM_NRF24L01_Transmit_Status_t;
/**
* @brief Data rate enumeration
*/
typedef enum _TM_NRF24L01_DataRate_t {
TM_NRF24L01_DataRate_2M = 0x00, /*!< Data rate set to 2Mbps */
TM_NRF24L01_DataRate_1M, /*!< Data rate set to 1Mbps */
TM_NRF24L01_DataRate_250k /*!< Data rate set to 250kbps */
} TM_NRF24L01_DataRate_t;
/**
* @brief Output power enumeration
*/
typedef enum _TM_NRF24L01_OutputPower_t {
TM_NRF24L01_OutputPower_M18dBm = 0x00,/*!< Output power set to -18dBm */
TM_NRF24L01_OutputPower_M12dBm, /*!< Output power set to -12dBm */
TM_NRF24L01_OutputPower_M6dBm, /*!< Output power set to -6dBm */
TM_NRF24L01_OutputPower_0dBm /*!< Output power set to 0dBm */
} TM_NRF24L01_OutputPower_t;
/**
* @}
*/
/**
* @defgroup TM_NRF24L01P_Functions
* @brief Library Functions
*
* Here are listed very basic functions to work with NRF modules
*
* @{
*/
/**
* @brief Initializes NRF24L01+ module
* @param channel: channel you will use for communication, from 0 to 125 eg. working frequency from 2.4 to 2.525 GHz
* @param payload_size: maximum data to be sent in one packet from one NRF to another.
* @note Maximal payload size is 32bytes
* @retval 1
*/
uint8_t TM_NRF24L01_Init(uint8_t channel, uint8_t payload_size);
/**
* @brief Sets own address. This is used for settings own id when communication with other modules
* @note "Own" address of one device must be the same as "TX" address of other device (and vice versa),
* if you want to get successful communication
* @param *adr: Pointer to 5-bytes length array with address
* @retval None
*/
void TM_NRF24L01_SetMyAddress(uint8_t* adr);
/**
* @brief Sets address you will communicate with
* @note "Own" address of one device must be the same as "TX" address of other device (and vice versa),
* if you want to get successful communication
* @param *adr: Pointer to 5-bytes length array with address
* @retval None
*/
void TM_NRF24L01_SetTxAddress(uint8_t* adr);
/**
* @brief Gets number of retransmissions needed in last transmission
* @param None
* @retval Number of retransmissions, between 0 and 15.
*/
uint8_t TM_NRF24L01_GetRetransmissionsCount(void);
/**
* @brief Sets NRF24L01+ to TX mode
* @note In this mode is NRF able to send data to another NRF module
* @param None
* @retval None
*/
void TM_NRF24L01_PowerUpTx(void);
/**
* @brief Sets NRF24L01+ to RX mode
* @note In this mode is NRF able to receive data from another NRF module.
* This is default mode and should be used all the time, except when sending data
* @param None
* @retval None
*/
void TM_NRF24L01_PowerUpRx(void);
/**
* @brief Sets NRF24L01+ to power down mode
* @note In power down mode, you are not able to transmit/receive data.
* You can wake up device using @ref TM_NRF24L01_PowerUpTx() or @ref TM_NRF24L01_PowerUpRx() functions
* @param None
* @retval None
*/
void TM_NRF24L01_PowerDown(void);
/**
* @brief Gets transmissions status
* @param None
* @retval Transmission status. Return is based on @ref TM_NRF24L01_Transmit_Status_t enumeration
*/
TM_NRF24L01_Transmit_Status_t TM_NRF24L01_GetTransmissionStatus(void);
/**
* @brief Transmits data with NRF24L01+ to another NRF module
* @param *data: Pointer to 8-bit array with data.
* Maximum length of array can be the same as "payload_size" parameter on initialization
* @retval None
*/
void TM_NRF24L01_Transmit(uint8_t *data);
/**
* @brief Checks if data is ready to be read from NRF24L01+
* @param None
* @retval Data ready status:
* - 0: No data available for receive in bufferReturns
* - > 0: Data is ready to be collected
*/
uint8_t TM_NRF24L01_DataReady(void);
/**
* @brief Gets data from NRF24L01+
* @param *data: Pointer to 8-bits array where data from NRF will be saved
* @retval None
*/
void TM_NRF24L01_GetData(uint8_t *data);
/**
* @brief Sets working channel
* @note Channel value is just an offset in units MHz from 2.4GHz
* For example, if you select channel 65, then operation frequency will be set to 2.465GHz.
* @param channel: RF channel where device will operate
* @retval None
*/
void TM_NRF24L01_SetChannel(uint8_t channel);
/**
* @brief Sets RF parameters for NRF24L01+
* @param DataRate: Data rate selection for NRF module. This parameter can be a value of @ref TM_NRF24L01_DataRate_t enumeration
* @param OutPwr: Output power selection for NRF module. This parameter can be a value of @ref TM_NRF24L01_OutputPower_t enumeration
* @retval None
*/
void TM_NRF24L01_SetRF(TM_NRF24L01_DataRate_t DataRate, TM_NRF24L01_OutputPower_t OutPwr);
/**
* @brief Gets NRLF+ status register value
* @param None
* @retval Status register from NRF
*/
uint8_t TM_NRF24L01_GetStatus(void);
/**
* @brief Reads interrupts from NRF
* @param *IRQ: Pointer to @ref TM_NRF24L01_IRQ_t where IRQ status will be saved
* @retval IRQ status
* - 0: No interrupts are active
* - > 0: At least one interrupt is active
*/
uint8_t TM_NRF24L01_Read_Interrupts(TM_NRF24L01_IRQ_t* IRQ);
/**
* @brief Clears interrupt status
* @param None
* @retval None
*/
void TM_NRF24L01_Clear_Interrupts(void);
/* Private */
void TM_NRF24L01_WriteRegister(uint8_t reg, uint8_t value);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/* C++ detection */
#ifdef __cplusplus
}
#endif
#endif