nRFBareRadio is a library to use the Radio peripheral in a nRF51 or nRF52 Nordic microcontroller in "bare" mode transmitting raw packets, instead of the usual BLE protocols.

Committer:
fbcosentino
Date:
Fri May 17 14:25:38 2019 +0000
Revision:
1:fd37281bcdf7
Parent:
0:123cac2364c4
Examples added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fbcosentino 0:123cac2364c4 1 /**
fbcosentino 0:123cac2364c4 2 * @file nRFBareRadio.h
fbcosentino 0:123cac2364c4 3 * @brief nRFBareRadio is a library to use the Radio peripheral in a
fbcosentino 0:123cac2364c4 4 * nRF51 or nRF52 Nordic microcontroller in "bare" mode transmitting
fbcosentino 0:123cac2364c4 5 * raw packets, instead of the usual BLE protocols.
fbcosentino 0:123cac2364c4 6 *
fbcosentino 0:123cac2364c4 7 * @author Fernando Cosentino
fbcosentino 0:123cac2364c4 8 * @author Uses some code from a version by Manuel Caballero
fbcosentino 0:123cac2364c4 9 * (https://os.mbed.com/users/mcm/)
fbcosentino 1:fd37281bcdf7 10 *
fbcosentino 1:fd37281bcdf7 11 * Transmitter example:
fbcosentino 1:fd37281bcdf7 12 * @code
fbcosentino 1:fd37281bcdf7 13 * #include "mbed.h"
fbcosentino 1:fd37281bcdf7 14 * #include "nRFBareRadio.h"
fbcosentino 1:fd37281bcdf7 15 *
fbcosentino 1:fd37281bcdf7 16 * #ifdef TARGET_NRF52
fbcosentino 1:fd37281bcdf7 17 * DigitalOut led1(P0_17); // nRF52
fbcosentino 1:fd37281bcdf7 18 * #else
fbcosentino 1:fd37281bcdf7 19 * DigitalOut led1(P0_21); // nRF51
fbcosentino 1:fd37281bcdf7 20 * #endif
fbcosentino 1:fd37281bcdf7 21 *
fbcosentino 1:fd37281bcdf7 22 * int main(void) {
fbcosentino 1:fd37281bcdf7 23 * int i = 0;
fbcosentino 1:fd37281bcdf7 24 * char buffer[32];
fbcosentino 1:fd37281bcdf7 25 *
fbcosentino 1:fd37281bcdf7 26 * // Address object
fbcosentino 1:fd37281bcdf7 27 * RadioAddress address = {0xC0, 0x55, 0x42, 0xBB, 0xC2};
fbcosentino 1:fd37281bcdf7 28 *
fbcosentino 1:fd37281bcdf7 29 * // Configuration object
fbcosentino 1:fd37281bcdf7 30 * RadioConfig config;
fbcosentino 1:fd37281bcdf7 31 * config.frequency = 10; // 2400 + 10
fbcosentino 1:fd37281bcdf7 32 * // change other config parameters here if you please
fbcosentino 1:fd37281bcdf7 33 *
fbcosentino 1:fd37281bcdf7 34 * // Radio object
fbcosentino 1:fd37281bcdf7 35 * BareRadio radio;
fbcosentino 1:fd37281bcdf7 36 * radio.Setup(RADIO_MODE_TX, address, config); // This is a transmitter
fbcosentino 1:fd37281bcdf7 37 *
fbcosentino 1:fd37281bcdf7 38 * // Main loop
fbcosentino 1:fd37281bcdf7 39 * while(1) {
fbcosentino 1:fd37281bcdf7 40 * // Put some data in the buffer
fbcosentino 1:fd37281bcdf7 41 * sprintf(buffer, "Value = %d\n", i++);
fbcosentino 1:fd37281bcdf7 42 * // Transmit the buffer
fbcosentino 1:fd37281bcdf7 43 * radio.Transmit(buffer);
fbcosentino 1:fd37281bcdf7 44 * // Toggle the LED and wait a bit
fbcosentino 1:fd37281bcdf7 45 * led1 = !led1;
fbcosentino 1:fd37281bcdf7 46 * wait(1.0);
fbcosentino 1:fd37281bcdf7 47 * }
fbcosentino 1:fd37281bcdf7 48 * }
fbcosentino 1:fd37281bcdf7 49 * @endcode
fbcosentino 1:fd37281bcdf7 50 *
fbcosentino 1:fd37281bcdf7 51 * Receiver example:
fbcosentino 1:fd37281bcdf7 52 * @code
fbcosentino 1:fd37281bcdf7 53 * #include "mbed.h"
fbcosentino 1:fd37281bcdf7 54 * #include "nRFBareRadio.h"
fbcosentino 1:fd37281bcdf7 55 *
fbcosentino 1:fd37281bcdf7 56 * #ifdef TARGET_NRF52
fbcosentino 1:fd37281bcdf7 57 * Serial pc(P0_6, P0_8); // nRF52
fbcosentino 1:fd37281bcdf7 58 * DigitalOut led1(P0_17); // nRF52
fbcosentino 1:fd37281bcdf7 59 * #else
fbcosentino 1:fd37281bcdf7 60 * Serial pc(P0_9, P0_11); // nRF51
fbcosentino 1:fd37281bcdf7 61 * DigitalOut led1(P0_21); // nRF51
fbcosentino 1:fd37281bcdf7 62 * #endif
fbcosentino 1:fd37281bcdf7 63 *
fbcosentino 1:fd37281bcdf7 64 * int main(void) {
fbcosentino 1:fd37281bcdf7 65 * int i = 0;
fbcosentino 1:fd37281bcdf7 66 * char buffer[32];
fbcosentino 1:fd37281bcdf7 67 *
fbcosentino 1:fd37281bcdf7 68 * // Address object
fbcosentino 1:fd37281bcdf7 69 * RadioAddress address = {0xC0, 0x55, 0x42, 0xBB, 0xC2};
fbcosentino 1:fd37281bcdf7 70 *
fbcosentino 1:fd37281bcdf7 71 * // Configuration object
fbcosentino 1:fd37281bcdf7 72 * RadioConfig config;
fbcosentino 1:fd37281bcdf7 73 * config.frequency = 10; // 2400 + 10
fbcosentino 1:fd37281bcdf7 74 * // change other config parameters here if you please
fbcosentino 1:fd37281bcdf7 75 *
fbcosentino 1:fd37281bcdf7 76 * // Radio object
fbcosentino 1:fd37281bcdf7 77 * BareRadio radio;
fbcosentino 1:fd37281bcdf7 78 * radio.Setup(RADIO_MODE_RX, address, config); // This is a receiver
fbcosentino 1:fd37281bcdf7 79 *
fbcosentino 1:fd37281bcdf7 80 * // Main loop
fbcosentino 1:fd37281bcdf7 81 * while(1) {
fbcosentino 1:fd37281bcdf7 82 * // Did we receive a packet?
fbcosentino 1:fd37281bcdf7 83 * if (radio.Receive(buffer)) {
fbcosentino 1:fd37281bcdf7 84 * // Print the packet since the transmitter is sending a string
fbcosentino 1:fd37281bcdf7 85 * pc.printf(buffer);
fbcosentino 1:fd37281bcdf7 86 * // Toggle the LED
fbcosentino 1:fd37281bcdf7 87 * led1 = !led1;
fbcosentino 1:fd37281bcdf7 88 * }
fbcosentino 1:fd37281bcdf7 89 * }
fbcosentino 1:fd37281bcdf7 90 * }
fbcosentino 1:fd37281bcdf7 91 * @endcode
fbcosentino 0:123cac2364c4 92 */
fbcosentino 0:123cac2364c4 93
fbcosentino 0:123cac2364c4 94 #ifndef __NRFBARERADIO
fbcosentino 0:123cac2364c4 95 #define __NRFBARERADIO
fbcosentino 0:123cac2364c4 96
fbcosentino 0:123cac2364c4 97 #include "mbed.h"
fbcosentino 0:123cac2364c4 98
fbcosentino 0:123cac2364c4 99 #define RADIO_MODE_RX 0
fbcosentino 0:123cac2364c4 100 #define RADIO_MODE_TX 1
fbcosentino 0:123cac2364c4 101
fbcosentino 0:123cac2364c4 102 #define RADIO_RATE_1M RADIO_MODE_MODE_Nrf_1Mbit
fbcosentino 0:123cac2364c4 103 #define RADIO_RATE_2M RADIO_MODE_MODE_Nrf_2Mbit
fbcosentino 0:123cac2364c4 104 #define RADIO_RATE_250K RADIO_MODE_MODE_Nrf_250Kbit
fbcosentino 0:123cac2364c4 105
fbcosentino 0:123cac2364c4 106 #define RADIO_WHITENING RADIO_PCNF1_WHITEEN_Enabled
fbcosentino 0:123cac2364c4 107 #define RADIO_NO_WHITENING RADIO_PCNF1_WHITEEN_Disabled
fbcosentino 0:123cac2364c4 108
fbcosentino 0:123cac2364c4 109 #define RADIO_LITTLEENDIAN RADIO_PCNF1_ENDIAN_Little
fbcosentino 0:123cac2364c4 110 #define RADIO_BIGENDIAN RADIO_PCNF1_ENDIAN_Big
fbcosentino 0:123cac2364c4 111
fbcosentino 0:123cac2364c4 112 #define RADIO_TX_0dBm RADIO_TXPOWER_TXPOWER_0dBm
fbcosentino 0:123cac2364c4 113 #define RADIO_TX_N4dBm RADIO_TXPOWER_TXPOWER_Neg4dBm
fbcosentino 0:123cac2364c4 114 #define RADIO_TX_N12dBm RADIO_TXPOWER_TXPOWER_Neg12dBm
fbcosentino 0:123cac2364c4 115 #define RADIO_TX_N20dBm RADIO_TXPOWER_TXPOWER_Neg20dBm
fbcosentino 0:123cac2364c4 116 #define RADIO_TX_N40dBm RADIO_TXPOWER_TXPOWER_Neg40dBm
fbcosentino 0:123cac2364c4 117 #define RADIO_TX_P4dBm RADIO_TXPOWER_TXPOWER_Pos4dBm // WARNING:
fbcosentino 0:123cac2364c4 118 // +4dBm could be against regulations in some areas
fbcosentino 0:123cac2364c4 119
fbcosentino 0:123cac2364c4 120
fbcosentino 0:123cac2364c4 121 /**
fbcosentino 0:123cac2364c4 122 * RadioAddress is a struct type for storing transmit and receive addresses,
fbcosentino 0:123cac2364c4 123 * being used as argument to configure a radio.
fbcosentino 0:123cac2364c4 124 */
fbcosentino 0:123cac2364c4 125 typedef struct RadioAddress {
fbcosentino 0:123cac2364c4 126 unsigned char A0;
fbcosentino 0:123cac2364c4 127 unsigned char A1;
fbcosentino 0:123cac2364c4 128 unsigned char A2;
fbcosentino 0:123cac2364c4 129 unsigned char A3;
fbcosentino 0:123cac2364c4 130 unsigned char A4;
fbcosentino 0:123cac2364c4 131 } RadioAddress;
fbcosentino 0:123cac2364c4 132
fbcosentino 0:123cac2364c4 133 /**
fbcosentino 0:123cac2364c4 134 * RadioConfig holds various setup parameters, being used as argument to
fbcosentino 0:123cac2364c4 135 * configure a radio. All have default values, so you can get a radio up and
fbcosentino 0:123cac2364c4 136 * running without touching anything.
fbcosentino 0:123cac2364c4 137 */
fbcosentino 0:123cac2364c4 138 class RadioConfig {
fbcosentino 0:123cac2364c4 139 public:
fbcosentino 0:123cac2364c4 140 RadioConfig();
fbcosentino 0:123cac2364c4 141
fbcosentino 0:123cac2364c4 142 /** Center frequency the radio will operate at in MHz, with 2400MHz offset
fbcosentino 0:123cac2364c4 143 * (that is, a value of 35 means the radio will operate at 2435MHz).
fbcosentino 0:123cac2364c4 144 * Must be in range 0-100 and defaults to 2 (2402MHz). */
fbcosentino 0:123cac2364c4 145 int frequency;
fbcosentino 0:123cac2364c4 146
fbcosentino 0:123cac2364c4 147 /** Data rate (band width around the center frequency) in MHz.
fbcosentino 0:123cac2364c4 148 * Possible values are RADIO_RATE_1M, RADIO_RATE_2M (default)
fbcosentino 0:123cac2364c4 149 * or RADIO_RATE_250K (deprecated). */
fbcosentino 0:123cac2364c4 150 int rate;
fbcosentino 0:123cac2364c4 151
fbcosentino 0:123cac2364c4 152 /** Packet payload size in bytes, must be in the range 0-32. This library
fbcosentino 0:123cac2364c4 153 * uses static payload size only. If your payload varies, use the largest
fbcosentino 0:123cac2364c4 154 * possible length and leave unused bytes in the packet. */
fbcosentino 0:123cac2364c4 155 int data_length;
fbcosentino 0:123cac2364c4 156
fbcosentino 0:123cac2364c4 157 /** Length of the address field in bytes, must be in the range 3-5. This
fbcosentino 0:123cac2364c4 158 * library supports one endpoint only (logic address 0). */
fbcosentino 0:123cac2364c4 159 int address_length;
fbcosentino 0:123cac2364c4 160
fbcosentino 0:123cac2364c4 161 /** Power level used in transmit mode. Possible values are
fbcosentino 0:123cac2364c4 162 * RADIO_TX_0dBm (default) for 0dBm,
fbcosentino 0:123cac2364c4 163 * RADIO_TX_N4dBm for -4dBm,
fbcosentino 0:123cac2364c4 164 * RADIO_TX_N12dBm for -12 dBm,
fbcosentino 0:123cac2364c4 165 * RADIO_TX_N20dBm for -20 dBm,
fbcosentino 0:123cac2364c4 166 * RADIO_TX_N40dBm for -40 dBm,
fbcosentino 0:123cac2364c4 167 * RADIO_TX_P4dBm for +4dBm,
fbcosentino 0:123cac2364c4 168 * or any constant from the nRF SDK
fbcosentino 0:123cac2364c4 169 * (example: RADIO_TXPOWER_TXPOWER_Neg8dBm). */
fbcosentino 0:123cac2364c4 170 int tx_power;
fbcosentino 0:123cac2364c4 171
fbcosentino 0:123cac2364c4 172 /** Either to use data whitening or not. Possible values are RADIO_WHITENING
fbcosentino 0:123cac2364c4 173 * or RADIO_NO_WHITENING (default). Data whitening is not compatible to
fbcosentino 0:123cac2364c4 174 * nRF24 chipsets. */
fbcosentino 0:123cac2364c4 175 int use_whitening;
fbcosentino 0:123cac2364c4 176
fbcosentino 0:123cac2364c4 177 /** Data endianness for both the address and payload. Possible values are
fbcosentino 0:123cac2364c4 178 * RADIO_LITTLEENDIAN or RADIO_BIGENDIAN (default). For nRF24 compatibility
fbcosentino 0:123cac2364c4 179 * it must be RADIO_BIGENDIAN. */
fbcosentino 0:123cac2364c4 180 int endianness;
fbcosentino 0:123cac2364c4 181
fbcosentino 0:123cac2364c4 182 /** CRC polynomial, fixed at a 16bit length in this library. Default 0x1021
fbcosentino 0:123cac2364c4 183 * (compatible to nRF24). */
fbcosentino 0:123cac2364c4 184 unsigned int crc_poly;
fbcosentino 0:123cac2364c4 185
fbcosentino 0:123cac2364c4 186 /** CRC initial value, fixed at a 16bit length in this library.
fbcosentino 0:123cac2364c4 187 * Default 0xFFFF (compatible to nRF24). */
fbcosentino 0:123cac2364c4 188 unsigned int crc_init;
fbcosentino 0:123cac2364c4 189 };
fbcosentino 0:123cac2364c4 190
fbcosentino 0:123cac2364c4 191 /**
fbcosentino 0:123cac2364c4 192 * BareRadio represents and controls the RADIO peripheral in a nRF51 or nRF52
fbcosentino 0:123cac2364c4 193 * Nordic microcontroller. Should be initialised with a RadioAddress and a
fbcosentino 0:123cac2364c4 194 * RadioConfig objects.
fbcosentino 0:123cac2364c4 195 */
fbcosentino 0:123cac2364c4 196 class BareRadio {
fbcosentino 0:123cac2364c4 197 public:
fbcosentino 0:123cac2364c4 198 /** Initialises a BareRadio instance. */
fbcosentino 0:123cac2364c4 199 BareRadio();
fbcosentino 0:123cac2364c4 200
fbcosentino 0:123cac2364c4 201 /** Configures the high speed clock. You don't have to call this manually
fbcosentino 0:123cac2364c4 202 * since it's called by the constructor on instantiation.
fbcosentino 0:123cac2364c4 203 *
fbcosentino 0:123cac2364c4 204 * @returns 1 if the clock was successfully configured, 0 on task timeout.
fbcosentino 0:123cac2364c4 205 */
fbcosentino 0:123cac2364c4 206 int ConfigClock();
fbcosentino 0:123cac2364c4 207
fbcosentino 0:123cac2364c4 208 /** Configures the radio at either a transmitter or a receiver,
fbcosentino 0:123cac2364c4 209 * using a supplied address and configuration objects.
fbcosentino 0:123cac2364c4 210 *
fbcosentino 0:123cac2364c4 211 * @param mode The radio mode, either RADIO_MODE_RX or RADIO_MODE_TX
fbcosentino 0:123cac2364c4 212 * @param address A RadioAddress object previously filled
fbcosentino 0:123cac2364c4 213 * @param config A RadioConfig object previously configured
fbcosentino 0:123cac2364c4 214 */
fbcosentino 0:123cac2364c4 215 void Setup(int mode, RadioAddress address, RadioConfig& config);
fbcosentino 0:123cac2364c4 216
fbcosentino 0:123cac2364c4 217 /** Transmits a packet using data from a supplied buffer, which must be
fbcosentino 0:123cac2364c4 218 * at least as long as the data length configured.
fbcosentino 0:123cac2364c4 219 *
fbcosentino 0:123cac2364c4 220 * @param data The buffer as array of char or array of unsigned char
fbcosentino 0:123cac2364c4 221 */
fbcosentino 0:123cac2364c4 222 void Transmit(char * data);
fbcosentino 0:123cac2364c4 223 void Transmit(unsigned char * data);
fbcosentino 0:123cac2364c4 224
fbcosentino 0:123cac2364c4 225 /** Checks if a packet was received, and if received fills the supplied
fbcosentino 1:fd37281bcdf7 226 * buffer with the corresponding data. If no packet was received,
fbcosentino 0:123cac2364c4 227 * the buffer is kept unchanged.
fbcosentino 0:123cac2364c4 228 *
fbcosentino 0:123cac2364c4 229 * @param data The buffer to be filled as array of char or array of
fbcosentino 0:123cac2364c4 230 * unsigned char
fbcosentino 0:123cac2364c4 231 * @returns 1 if a packet was received and the buffer was changed,
fbcosentino 0:123cac2364c4 232 * 0 otherwise
fbcosentino 0:123cac2364c4 233 */
fbcosentino 0:123cac2364c4 234 int Receive(char * data);
fbcosentino 0:123cac2364c4 235 int Receive(unsigned char * data);
fbcosentino 0:123cac2364c4 236
fbcosentino 0:123cac2364c4 237
fbcosentino 0:123cac2364c4 238 private:
fbcosentino 0:123cac2364c4 239 unsigned char packet[32];
fbcosentino 0:123cac2364c4 240 int data_len;
fbcosentino 0:123cac2364c4 241 };
fbcosentino 0:123cac2364c4 242
fbcosentino 0:123cac2364c4 243
fbcosentino 0:123cac2364c4 244
fbcosentino 0:123cac2364c4 245
fbcosentino 0:123cac2364c4 246
fbcosentino 0:123cac2364c4 247
fbcosentino 0:123cac2364c4 248
fbcosentino 0:123cac2364c4 249
fbcosentino 0:123cac2364c4 250
fbcosentino 0:123cac2364c4 251
fbcosentino 0:123cac2364c4 252
fbcosentino 0:123cac2364c4 253
fbcosentino 0:123cac2364c4 254
fbcosentino 0:123cac2364c4 255
fbcosentino 0:123cac2364c4 256
fbcosentino 0:123cac2364c4 257
fbcosentino 0:123cac2364c4 258
fbcosentino 0:123cac2364c4 259
fbcosentino 0:123cac2364c4 260
fbcosentino 0:123cac2364c4 261
fbcosentino 0:123cac2364c4 262
fbcosentino 0:123cac2364c4 263
fbcosentino 0:123cac2364c4 264
fbcosentino 0:123cac2364c4 265
fbcosentino 0:123cac2364c4 266
fbcosentino 0:123cac2364c4 267
fbcosentino 0:123cac2364c4 268 #endif