upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
nrf24l01.hpp
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  * BLE Beaconing based on http://dmitry.gr/index.php?r=05.Projects&proj=11.%20Bluetooth%20LE%20fakery
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #pragma once
26 
27 #include <string>
28 #include <mraa/aio.hpp>
29 #include <mraa/common.hpp>
30 
31 #include <mraa/gpio.hpp>
32 
33 #include <mraa/spi.hpp>
34 #include <cstring>
35 
36 #include "Callback.hpp"
37 
38 /* Memory Map */
39 #define CONFIG 0x00
40 #define EN_AA 0x01
41 #define EN_RXADDR 0x02
42 #define SETUP_AW 0x03
43 #define SETUP_RETR 0x04
44 #define RF_CH 0x05
45 #define RF_SETUP 0x06
46 #define STATUS 0x07
47 #define OBSERVE_TX 0x08
48 #define CD 0x09
49 #define RX_ADDR_P0 0x0A
50 #define RX_ADDR_P1 0x0B
51 #define RX_ADDR_P2 0x0C
52 #define RX_ADDR_P3 0x0D
53 #define RX_ADDR_P4 0x0E
54 #define RX_ADDR_P5 0x0F
55 #define TX_ADDR 0x10
56 #define RX_PW_P0 0x11
57 #define RX_PW_P1 0x12
58 #define RX_PW_P2 0x13
59 #define RX_PW_P3 0x14
60 #define RX_PW_P4 0x15
61 #define RX_PW_P5 0x16
62 #define FIFO_STATUS 0x17
63 #define DYNPD 0x1C
64 #define FEATURE 0x1D
65 
66 /* Bit Mnemonics */
67 #define MASK_RX_DR 6
68 #define MASK_TX_DS 5
69 #define MASK_MAX_RT 4
70 #define EN_CRC 3
71 #define CRCO 2
72 #define PWR_UP 1
73 #define PRIM_RX 0
74 #define ENAA_P5 5
75 #define ENAA_P4 4
76 #define ENAA_P3 3
77 #define ENAA_P2 2
78 #define ENAA_P1 1
79 #define ENAA_P0 0
80 #define ERX_P5 5
81 #define ERX_P4 4
82 #define ERX_P3 3
83 #define ERX_P2 2
84 #define ERX_P1 1
85 #define ERX_P0 0
86 #define AW 0
87 #define ARD 4
88 #define ARC 0
89 #define PLL_LOCK 4
90 #define RF_DR 3
91 #define RF_PWR 1
92 #define LNA_HCURR 0
93 #define RX_DR 6
94 #define TX_DS 5
95 #define MAX_RT 4
96 #define RX_P_NO 1
97 #define TX_FULL 0
98 #define PLOS_CNT 4
99 #define ARC_CNT 0
100 #define TX_REUSE 6
101 #define FIFO_FULL 5
102 #define TX_EMPTY 4
103 #define RX_FULL 1
104 #define RX_EMPTY 0
105 
106 /* Instruction Mnemonics */
107 #define R_REGISTER 0x00
108 #define W_REGISTER 0x20
109 #define REGISTER_MASK 0x1F
110 #define R_RX_PAYLOAD 0x61
111 #define W_TX_PAYLOAD 0xA0
112 #define FLUSH_TX 0xE1
113 #define FLUSH_RX 0xE2
114 #define REUSE_TX_PL 0xE3
115 #define NOP 0xFF
116 
117 #define RF_DR_LOW 5
118 #define RF_DR_HIGH 3
119 #define RF_PWR_LOW 1
120 #define RF_PWR_HIGH 2
121 
122 /* Nrf24l settings */
123 #define ADDR_LEN 5
124 #define _CONFIG ((1<<EN_CRC) | (0<<CRCO) )
125 
126 #define MAX_BUFFER 32
127 
128 #define HIGH 1
129 #define LOW 0
130 
131 /* BLE beaconing */
132 #define BLE_MAC_0 0xEF
133 #define BLE_MAC_1 0xFF
134 #define BLE_MAC_2 0xC0
135 #define BLE_MAC_3 0xAA
136 #define BLE_MAC_4 0x18
137 #define BLE_MAC_5 0x00
138 
139 #define BLE_PAYLOAD_OFFSET 13
140 
141 namespace upm {
142 
143 typedef void (* funcPtrVoidVoid) (Callback *);
144 
145 typedef enum {
146  NRF_250KBPS = 0,
147  NRF_1MBPS = 1,
148  NRF_2MBPS = 2,
149 } speed_rate_t;
150 
151 typedef enum {
152  NRF_0DBM = 0,
153  NRF_6DBM = 1,
154  NRF_12DBM = 2,
155  NRF_18DBM = 3,
156 } power_t;
157 
182 class NRF24L01 {
183  public:
189  NRF24L01 (int cs, int ce);
190 
194  std::string name()
195  {
196  return m_name;
197  }
198 
205  void init (int chipSelect, int chipEnable);
206 
210  void configure ();
211 
217  void send (uint8_t * value);
218 
223  void send ();
224 
230  void setSourceAddress (uint8_t * addr);
231 
238  void setDestinationAddress (uint8_t * addr);
239 
245  void setBroadcastAddress (uint8_t * addr);
246 
252  void setPayload (uint8_t load);
253 
259  void setDataReceivedHandler (Callback *call_obj);
260 
264  bool dataReady ();
265 
269  bool dataSending ();
270 
276  void getData (uint8_t * data);
277 
281  uint8_t getStatus ();
282 
286  bool rxFifoEmpty ();
287 
291  void rxPowerUp ();
292 
296  void rxFlushBuffer ();
297 
301  void txPowerUp ();
302 
306  void powerDown ();
307 
308  void setChannel (uint8_t channel);
309 
310  void setPower (power_t power);
311 
312  uint8_t setSpeedRate (speed_rate_t rate);
313 
317  void txFlushBuffer ();
318 
323  void pollListener ();
324 
328  mraa::Result ceHigh ();
329 
333  mraa::Result ceLow ();
334 
338  mraa::Result csOn ();
339 
343  mraa::Result csOff ();
344 
349  void setBeaconingMode ();
350 
356  void sendBeaconingMsg (uint8_t * msg);
357 
358  uint8_t m_rxBuffer[MAX_BUFFER];
359  uint8_t m_txBuffer[MAX_BUFFER];
360  uint8_t m_bleBuffer [32];
362  private:
363 
364  /* Callback object to use for setting the handler from Java */
365  Callback *m_callback_obj;
366 
368  funcPtrVoidVoid dataReceivedHandler;
369 
373  void writeBytes (uint8_t * dataout, uint8_t * datain, uint8_t len);
377  void setRegister (uint8_t reg, uint8_t value);
381  uint8_t getRegister (uint8_t reg);
385  void readRegister (uint8_t reg, uint8_t * value, uint8_t len);
389  void writeRegister (uint8_t reg, uint8_t * value, uint8_t len);
393  void sendCommand (uint8_t cmd);
394 
395  void bleCrc (const uint8_t* data, uint8_t len, uint8_t* dst);
396 
397  void bleWhiten (uint8_t* data, uint8_t len, uint8_t whitenCoeff);
398 
399  void blePacketEncode(uint8_t* packet, uint8_t len, uint8_t chan);
400 
401  uint8_t swapbits (uint8_t a);
402 
403  mraa::Spi m_spi;
404  uint8_t m_ce;
405  uint8_t m_csn;
406  uint8_t m_channel;
407  uint8_t m_power;
408  uint8_t m_ptx;
409  uint8_t m_payload;
410 
411  mraa::Gpio m_csnPinCtx;
412  mraa::Gpio m_cePinCtx;
413 
414  std::string m_name;
415 };
416 
417 }
bool rxFifoEmpty()
Definition: nrf24l01.cxx:198
uint8_t m_bleBuffer[32]
Definition: nrf24l01.hpp:360
bool dataReady()
Definition: nrf24l01.cxx:145
void txFlushBuffer()
Definition: nrf24l01.cxx:317
mraa::Result ceHigh()
Definition: nrf24l01.cxx:288
NRF24L01(int cs, int ce)
Definition: nrf24l01.cxx:37
void configure()
Definition: nrf24l01.cxx:66
void sendBeaconingMsg(uint8_t *msg)
Definition: nrf24l01.cxx:359
void send()
Definition: nrf24l01.cxx:111
void powerDown()
Definition: nrf24l01.cxx:224
void setDataReceivedHandler(Callback *call_obj)
Definition: nrf24l01.cxx:138
void setBroadcastAddress(uint8_t *addr)
Definition: nrf24l01.cxx:128
void rxPowerUp()
Definition: nrf24l01.cxx:204
void getData(uint8_t *data)
Definition: nrf24l01.cxx:173
std::string name()
Definition: nrf24l01.hpp:194
void setDestinationAddress(uint8_t *addr)
Definition: nrf24l01.cxx:123
void rxFlushBuffer()
Definition: nrf24l01.cxx:213
Definition: Callback.hpp:3
mraa::Result ceLow()
Definition: nrf24l01.cxx:293
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
void setBeaconingMode()
Definition: nrf24l01.cxx:322
void init(int chipSelect, int chipEnable)
Definition: nrf24l01.cxx:44
void setPayload(uint8_t load)
Definition: nrf24l01.cxx:133
uint8_t m_txBuffer[MAX_BUFFER]
Definition: nrf24l01.hpp:359
void txPowerUp()
Definition: nrf24l01.cxx:218
bool dataSending()
Definition: nrf24l01.cxx:158
API for the NRF24L01 Transceiver Module.
Definition: nrf24l01.hpp:182
void pollListener()
Definition: nrf24l01.cxx:308
uint8_t getStatus()
Definition: nrf24l01.cxx:193
mraa::Result csOn()
Definition: nrf24l01.cxx:298
void setSourceAddress(uint8_t *addr)
Definition: nrf24l01.cxx:116
Definition: max30100.hpp:35
mraa::Result csOff()
Definition: nrf24l01.cxx:303
uint8_t m_rxBuffer[MAX_BUFFER]
Definition: nrf24l01.hpp:358