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.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRFBareRadio.h Source File

nRFBareRadio.h

Go to the documentation of this file.
00001 /** 
00002  *  @file nRFBareRadio.h
00003  *  @brief nRFBareRadio is a library to use the Radio peripheral in a 
00004  *  nRF51 or nRF52 Nordic microcontroller in "bare" mode transmitting
00005  *  raw packets, instead of the usual BLE protocols.
00006  * 
00007  *  @author Fernando Cosentino
00008  *  @author Uses some code from a version by Manuel Caballero
00009  *  (https://os.mbed.com/users/mcm/)
00010  *
00011  *  Transmitter example:
00012  *  @code
00013  *  #include "mbed.h"
00014  *  #include "nRFBareRadio.h"
00015  *
00016  *  #ifdef TARGET_NRF52
00017  *  DigitalOut led1(P0_17); // nRF52
00018  *  #else
00019  *  DigitalOut led1(P0_21); // nRF51
00020  *  #endif
00021  *
00022  *  int main(void) {
00023  *      int i = 0;
00024  *      char buffer[32]; 
00025  *
00026  *      // Address object
00027  *      RadioAddress address = {0xC0, 0x55, 0x42, 0xBB, 0xC2};
00028  *
00029  *      // Configuration object
00030  *      RadioConfig config;
00031  *      config.frequency = 10; // 2400 + 10
00032  *      // change other config parameters here if you please
00033  *   
00034  *      // Radio object
00035  *      BareRadio radio;
00036  *      radio.Setup(RADIO_MODE_TX, address, config); // This is a transmitter
00037  *
00038  *      // Main loop
00039  *      while(1) {
00040  *          // Put some data in the buffer     
00041  *          sprintf(buffer, "Value = %d\n", i++);
00042  *          // Transmit the buffer
00043  *          radio.Transmit(buffer);
00044  *          // Toggle the LED and wait a bit
00045  *          led1 = !led1;
00046  *          wait(1.0);
00047  *      }
00048  *  }
00049  *  @endcode
00050  *
00051  *  Receiver example:
00052  *  @code
00053  *  #include "mbed.h"
00054  *  #include "nRFBareRadio.h"
00055  *
00056  *  #ifdef TARGET_NRF52
00057  *  Serial pc(P0_6, P0_8);  // nRF52
00058  *  DigitalOut led1(P0_17); // nRF52
00059  *  #else
00060  *  Serial pc(P0_9, P0_11); // nRF51
00061  *  DigitalOut led1(P0_21); // nRF51
00062  *  #endif
00063  *
00064  *  int main(void) {
00065  *      int i = 0;
00066  *      char buffer[32]; 
00067  *
00068  *      // Address object
00069  *      RadioAddress address = {0xC0, 0x55, 0x42, 0xBB, 0xC2};
00070  *
00071  *      // Configuration object
00072  *      RadioConfig config;
00073  *      config.frequency = 10; // 2400 + 10
00074  *      // change other config parameters here if you please
00075  *   
00076  *      // Radio object
00077  *      BareRadio radio;
00078  *      radio.Setup(RADIO_MODE_RX, address, config); // This is a receiver
00079  *
00080  *      // Main loop
00081  *      while(1) {
00082  *          // Did we receive a packet?
00083  *          if (radio.Receive(buffer)) {
00084  *              // Print the packet since the transmitter is sending a string
00085  *              pc.printf(buffer);
00086  *              // Toggle the LED
00087  *              led1 = !led1;
00088  *          }
00089  *      }
00090  *  }
00091  *  @endcode
00092  */
00093 
00094 #ifndef __NRFBARERADIO
00095 #define __NRFBARERADIO
00096 
00097 #include "mbed.h"
00098 
00099 #define RADIO_MODE_RX 0
00100 #define RADIO_MODE_TX 1
00101 
00102 #define RADIO_RATE_1M   RADIO_MODE_MODE_Nrf_1Mbit
00103 #define RADIO_RATE_2M   RADIO_MODE_MODE_Nrf_2Mbit
00104 #define RADIO_RATE_250K RADIO_MODE_MODE_Nrf_250Kbit
00105 
00106 #define RADIO_WHITENING    RADIO_PCNF1_WHITEEN_Enabled
00107 #define RADIO_NO_WHITENING RADIO_PCNF1_WHITEEN_Disabled
00108 
00109 #define RADIO_LITTLEENDIAN RADIO_PCNF1_ENDIAN_Little
00110 #define RADIO_BIGENDIAN    RADIO_PCNF1_ENDIAN_Big
00111 
00112 #define RADIO_TX_0dBm     RADIO_TXPOWER_TXPOWER_0dBm
00113 #define RADIO_TX_N4dBm    RADIO_TXPOWER_TXPOWER_Neg4dBm
00114 #define RADIO_TX_N12dBm   RADIO_TXPOWER_TXPOWER_Neg12dBm
00115 #define RADIO_TX_N20dBm   RADIO_TXPOWER_TXPOWER_Neg20dBm
00116 #define RADIO_TX_N40dBm   RADIO_TXPOWER_TXPOWER_Neg40dBm
00117 #define RADIO_TX_P4dBm    RADIO_TXPOWER_TXPOWER_Pos4dBm // WARNING: 
00118                              // +4dBm could be against regulations in some areas
00119 
00120 
00121 /**
00122  *  RadioAddress is a struct type for storing transmit and receive addresses,
00123  *  being used as argument to configure a radio.
00124  */
00125 typedef struct RadioAddress {
00126     unsigned char A0;
00127     unsigned char A1;
00128     unsigned char A2;
00129     unsigned char A3;
00130     unsigned char A4;
00131 } RadioAddress;
00132 
00133 /**
00134  *  RadioConfig holds various setup parameters, being used as argument to 
00135  *  configure a radio. All have default values, so you can get a radio up and 
00136  *  running without touching anything.
00137  */
00138 class RadioConfig {
00139 public:
00140     RadioConfig();
00141     
00142     /** Center frequency the radio will operate at in MHz, with 2400MHz offset
00143      *  (that is, a value of 35 means the radio will operate at 2435MHz).
00144      *  Must be in range 0-100 and defaults to 2 (2402MHz). */
00145     int frequency;
00146     
00147     /** Data rate (band width around the center frequency) in MHz.
00148      *  Possible values are RADIO_RATE_1M, RADIO_RATE_2M (default) 
00149      *  or RADIO_RATE_250K (deprecated). */
00150     int rate;
00151     
00152     /** Packet payload size in bytes, must be in the range 0-32. This library
00153      *  uses static payload size only. If your payload varies, use the largest
00154      *  possible length and leave unused bytes in the packet. */
00155     int data_length;
00156     
00157     /** Length of the address field in bytes, must be in the range 3-5. This
00158      *  library supports one endpoint only (logic address 0). */
00159     int address_length;
00160     
00161     /** Power level used in transmit mode. Possible values are
00162      *  RADIO_TX_0dBm (default) for 0dBm, 
00163      *  RADIO_TX_N4dBm for -4dBm,
00164      *  RADIO_TX_N12dBm for -12 dBm,
00165      *  RADIO_TX_N20dBm for -20 dBm,
00166      *  RADIO_TX_N40dBm for -40 dBm,
00167      *  RADIO_TX_P4dBm for +4dBm,
00168      *  or any constant from the nRF SDK 
00169      *  (example: RADIO_TXPOWER_TXPOWER_Neg8dBm). */
00170     int tx_power;
00171     
00172     /** Either to use data whitening or not. Possible values are RADIO_WHITENING
00173      *  or RADIO_NO_WHITENING (default). Data whitening is not compatible to 
00174      *  nRF24 chipsets. */
00175     int use_whitening;
00176     
00177     /** Data endianness for both the address and payload. Possible values are
00178      *  RADIO_LITTLEENDIAN or RADIO_BIGENDIAN (default). For nRF24 compatibility
00179      *  it must be RADIO_BIGENDIAN. */
00180     int endianness;
00181     
00182     /** CRC polynomial, fixed at a 16bit length in this library. Default 0x1021
00183      *  (compatible to nRF24). */
00184     unsigned int crc_poly;
00185     
00186     /** CRC initial value, fixed at a 16bit length in this library.
00187      *  Default 0xFFFF (compatible to nRF24). */
00188     unsigned int crc_init;
00189 };
00190 
00191 /** 
00192  *  BareRadio represents and controls the RADIO peripheral in a nRF51 or nRF52
00193  *  Nordic microcontroller. Should be initialised with a RadioAddress and a
00194  *  RadioConfig objects.
00195  */
00196 class BareRadio {
00197 public:
00198     /** Initialises a BareRadio instance. */
00199     BareRadio();
00200     
00201     /** Configures the high speed clock. You don't have to call this manually
00202      *  since it's called by the constructor on instantiation. 
00203      *
00204      *  @returns 1 if the clock was successfully configured, 0 on task timeout.
00205      */
00206     int ConfigClock();
00207     
00208     /** Configures the radio at either a transmitter or a receiver,
00209      *  using a supplied address and configuration objects.
00210      *
00211      *  @param mode The radio mode, either RADIO_MODE_RX or RADIO_MODE_TX
00212      *  @param address A RadioAddress object previously filled
00213      *  @param config A RadioConfig object previously configured
00214      */
00215     void Setup(int mode, RadioAddress address, RadioConfig& config);
00216     
00217     /** Transmits a packet using data from a supplied buffer, which must be 
00218      *  at least as long as the data length configured.
00219      *
00220      *  @param data The buffer as array of char or array of unsigned char
00221      */
00222     void Transmit(char * data);
00223     void Transmit(unsigned char * data);
00224     
00225     /** Checks if a packet was received, and if received fills the supplied
00226      *  buffer with the corresponding data. If no packet was received,
00227      *  the buffer is kept unchanged.
00228      *
00229      *  @param data The buffer to be filled as array of char or array of
00230      *  unsigned char
00231      *  @returns 1 if a packet was received and the buffer was changed, 
00232      *  0 otherwise
00233      */
00234     int Receive(char * data);
00235     int Receive(unsigned char * data);
00236     
00237 
00238 private:
00239     unsigned char packet[32];
00240     int data_len;
00241 };
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 #endif