upm  0.1.8
Sensor/Actuator repository for libmraa (v0.4.5)
 All Data Structures Files Functions Variables Macros Pages
nrf24l01.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/aio.h>
28 #include <mraa/gpio.h>
29 #include <mraa/spi.h>
30 
31 /* Memory Map */
32 #define CONFIG 0x00
33 #define EN_AA 0x01
34 #define EN_RXADDR 0x02
35 #define SETUP_AW 0x03
36 #define SETUP_RETR 0x04
37 #define RF_CH 0x05
38 #define RF_SETUP 0x06
39 #define STATUS 0x07
40 #define OBSERVE_TX 0x08
41 #define CD 0x09
42 #define RX_ADDR_P0 0x0A
43 #define RX_ADDR_P1 0x0B
44 #define RX_ADDR_P2 0x0C
45 #define RX_ADDR_P3 0x0D
46 #define RX_ADDR_P4 0x0E
47 #define RX_ADDR_P5 0x0F
48 #define TX_ADDR 0x10
49 #define RX_PW_P0 0x11
50 #define RX_PW_P1 0x12
51 #define RX_PW_P2 0x13
52 #define RX_PW_P3 0x14
53 #define RX_PW_P4 0x15
54 #define RX_PW_P5 0x16
55 #define FIFO_STATUS 0x17
56 
57 /* Bit Mnemonics */
58 #define MASK_RX_DR 6
59 #define MASK_TX_DS 5
60 #define MASK_MAX_RT 4
61 #define EN_CRC 3
62 #define CRCO 2
63 #define PWR_UP 1
64 #define PRIM_RX 0
65 #define ENAA_P5 5
66 #define ENAA_P4 4
67 #define ENAA_P3 3
68 #define ENAA_P2 2
69 #define ENAA_P1 1
70 #define ENAA_P0 0
71 #define ERX_P5 5
72 #define ERX_P4 4
73 #define ERX_P3 3
74 #define ERX_P2 2
75 #define ERX_P1 1
76 #define ERX_P0 0
77 #define AW 0
78 #define ARD 4
79 #define ARC 0
80 #define PLL_LOCK 4
81 #define RF_DR 3
82 #define RF_PWR 1
83 #define LNA_HCURR 0
84 #define RX_DR 6
85 #define TX_DS 5
86 #define MAX_RT 4
87 #define RX_P_NO 1
88 #define TX_FULL 0
89 #define PLOS_CNT 4
90 #define ARC_CNT 0
91 #define TX_REUSE 6
92 #define FIFO_FULL 5
93 #define TX_EMPTY 4
94 #define RX_FULL 1
95 #define RX_EMPTY 0
96 
97 /* Instruction Mnemonics */
98 #define R_REGISTER 0x00
99 #define W_REGISTER 0x20
100 #define REGISTER_MASK 0x1F
101 #define R_RX_PAYLOAD 0x61
102 #define W_TX_PAYLOAD 0xA0
103 #define FLUSH_TX 0xE1
104 #define FLUSH_RX 0xE2
105 #define REUSE_TX_PL 0xE3
106 #define NOP 0xFF
107 
108 /* Nrf24l settings */
109 #define ADDR_LEN 5
110 #define NRF_CONFIG ((1<<EN_CRC) | (0<<CRCO) )
111 
112 #define MAX_BUFFER 32
113 
114 #define HIGH 1
115 #define LOW 0
116 
117 namespace upm {
118 
119 typedef void (* funcPtrVoidVoid) ();
120 
129 class NRF24l01 {
130  public:
136  NRF24l01 (uint8_t cs);
137 
141  ~NRF24l01 ();
142 
146  std::string name()
147  {
148  return m_name;
149  }
150 
157  void nrfInitModule (uint8_t chipSelect, uint8_t chipEnable);
158 
162  void nrfConfigModule ();
163 
169  void nrfSend (uint8_t *value);
170 
175  void nrfSend ();
176 
182  void nrfSetRXaddr (uint8_t * addr);
183 
190  void nrfSetTXaddr (uint8_t * addr);
191 
197  void nrfSetBroadcastAddr (uint8_t * addr);
198 
204  void nrfSetPayload (uint8_t load);
205 
209  bool nrfDataReady ();
210 
214  bool nrfIsSending ();
215 
219  bool nrfRXFifoEmpty ();
220 
224  bool nrfTXFifoEmpty ();
225 
231  void nrfGetData (uint8_t * data);
232 
236  uint8_t nrfGetStatus ();
237 
244  void nrfTransmitSync (uint8_t *dataout, uint8_t len);
245 
254  void nrfTransferSync (uint8_t *dataout ,uint8_t *datain, uint8_t len);
255 
262  void nrfConfigRegister (uint8_t reg, uint8_t value);
263 
271  void nrfReadRegister (uint8_t reg, uint8_t * value, uint8_t len);
272 
280  void nrfWriteRegister (uint8_t reg, uint8_t * value, uint8_t len);
281 
285  void nrfPowerUpRX ();
286 
290  void nrfPowerUpTX ();
291 
295  void nrfPowerDown ();
296 
300  mraa_result_t nrfCEHigh ();
301 
305  mraa_result_t nrfCELow ();
306 
310  mraa_result_t nrfCSOn ();
311 
315  mraa_result_t nrfCSOff ();
316 
320  void nrfFlushRX ();
321 
326  void nrfListenForChannel();
327 
328  uint8_t m_rxBuffer[MAX_BUFFER];
329  uint8_t m_txBuffer[MAX_BUFFER];
331  funcPtrVoidVoid dataRecievedHandler;
332  private:
333  mraa_spi_context m_spi;
334  uint8_t m_ce;
335  uint8_t m_csn;
336  uint8_t m_channel;
337  uint8_t m_ptx;
338  uint8_t m_payload;
339  uint8_t m_localAddress[5];
340 
341  mraa_gpio_context m_csnPinCtx;
342  mraa_gpio_context m_cePinCtx;
343 
344  std::string m_name;
345 };
346 
347 }
mraa_result_t nrfCSOff()
Definition: nrf24l01.cxx:309
bool nrfTXFifoEmpty()
void nrfWriteRegister(uint8_t reg, uint8_t *value, uint8_t len)
Definition: nrf24l01.cxx:160
uint8_t m_txBuffer[MAX_BUFFER]
Definition: nrf24l01.h:329
void nrfTransmitSync(uint8_t *dataout, uint8_t len)
Definition: nrf24l01.cxx:169
void nrfFlushRX()
Definition: nrf24l01.cxx:123
void nrfReadRegister(uint8_t reg, uint8_t *value, uint8_t len)
Definition: nrf24l01.cxx:196
std::string name()
Definition: nrf24l01.h:146
void nrfPowerUpRX()
Definition: nrf24l01.cxx:114
void nrfPowerUpTX()
Definition: nrf24l01.cxx:282
bool nrfRXFifoEmpty()
Definition: nrf24l01.cxx:213
void nrfSetRXaddr(uint8_t *addr)
Definition: nrf24l01.cxx:131
uint8_t nrfGetStatus()
Definition: nrf24l01.cxx:188
uint8_t m_rxBuffer[MAX_BUFFER]
Definition: nrf24l01.h:328
void nrfTransferSync(uint8_t *dataout, uint8_t *datain, uint8_t len)
Definition: nrf24l01.cxx:205
~NRF24l01()
Definition: nrf24l01.cxx:38
void nrfSetBroadcastAddr(uint8_t *addr)
Definition: nrf24l01.cxx:148
mraa_result_t nrfCEHigh()
Definition: nrf24l01.cxx:294
NRF24l01(uint8_t cs)
Definition: nrf24l01.cxx:33
void nrfGetData(uint8_t *data)
Definition: nrf24l01.cxx:221
C++ API for NRF24l01 transceiver module.
Definition: nrf24l01.h:129
void nrfSetTXaddr(uint8_t *addr)
Definition: nrf24l01.cxx:139
void nrfSend()
Definition: nrf24l01.cxx:262
bool nrfIsSending()
Definition: nrf24l01.cxx:267
bool nrfDataReady()
Definition: nrf24l01.cxx:178
void nrfSetPayload(uint8_t load)
Definition: nrf24l01.cxx:155
void nrfConfigModule()
Definition: nrf24l01.cxx:89
void nrfInitModule(uint8_t chipSelect, uint8_t chipEnable)
Definition: nrf24l01.cxx:55
void nrfListenForChannel()
Definition: nrf24l01.cxx:314
mraa_result_t nrfCSOn()
Definition: nrf24l01.cxx:304
funcPtrVoidVoid dataRecievedHandler
Definition: nrf24l01.h:331
void nrfPowerDown()
Definition: nrf24l01.cxx:288
mraa_result_t nrfCELow()
Definition: nrf24l01.cxx:299
void nrfConfigRegister(uint8_t reg, uint8_t value)
Definition: nrf24l01.cxx:106