Library for the nRF2401A Transceiver

Dependents:   nRF2401A_Hello_World nRF2401A_Wireless_Accelerometer_joypad nRF2401A_Gameduino_Invaders

Committer:
TheChrisyd
Date:
Sun Oct 06 16:17:32 2013 +0000
Revision:
4:e8523ef6e472
Parent:
3:7ae3a5e53a1f
Child:
5:18ce8a56b248
hello world example updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 2:ca8602ee9e1d 1 /**
TheChrisyd 2:ca8602ee9e1d 2 *@section DESCRIPTION
TheChrisyd 2:ca8602ee9e1d 3 * mbed nRF2401A Library
TheChrisyd 2:ca8602ee9e1d 4 *@section LICENSE
TheChrisyd 0:17cb4be1e37f 5 * Copyright (c) 2011, Per Söderstam
TheChrisyd 0:17cb4be1e37f 6 *
TheChrisyd 0:17cb4be1e37f 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
TheChrisyd 0:17cb4be1e37f 8 * of this software and associated documentation files (the "Software"), to deal
TheChrisyd 0:17cb4be1e37f 9 * in the Software without restriction, including without limitation the rights
TheChrisyd 0:17cb4be1e37f 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
TheChrisyd 0:17cb4be1e37f 11 * copies of the Software, and to permit persons to whom the Software is
TheChrisyd 0:17cb4be1e37f 12 * furnished to do so, subject to the following conditions:
TheChrisyd 0:17cb4be1e37f 13 *
TheChrisyd 0:17cb4be1e37f 14 * The above copyright notice and this permission notice shall be included in
TheChrisyd 0:17cb4be1e37f 15 * all copies or substantial portions of the Software.
TheChrisyd 0:17cb4be1e37f 16 *
TheChrisyd 0:17cb4be1e37f 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
TheChrisyd 0:17cb4be1e37f 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
TheChrisyd 0:17cb4be1e37f 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
TheChrisyd 0:17cb4be1e37f 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
TheChrisyd 0:17cb4be1e37f 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
TheChrisyd 0:17cb4be1e37f 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
TheChrisyd 0:17cb4be1e37f 23 * THE SOFTWARE.
TheChrisyd 2:ca8602ee9e1d 24 * @file "nRF2401A.h"
TheChrisyd 0:17cb4be1e37f 25 */
TheChrisyd 3:7ae3a5e53a1f 26
TheChrisyd 2:ca8602ee9e1d 27 #ifndef _nRF2401A_H
TheChrisyd 2:ca8602ee9e1d 28 #define _nRF2401A_H
TheChrisyd 0:17cb4be1e37f 29
TheChrisyd 0:17cb4be1e37f 30 #include <mbed.h>
TheChrisyd 0:17cb4be1e37f 31 #include <inttypes.h>
TheChrisyd 0:17cb4be1e37f 32 #define Ts 1 /**< Setup time from data to rising clock edge on write (accualy 500 ns). */
TheChrisyd 0:17cb4be1e37f 33 #define Th 1 /**< Hold time from rising clock to data toggle/falling clock (accualy 500 ns). */
TheChrisyd 0:17cb4be1e37f 34 #define Tcs2data 5 /**< Min delay from CS assert to data, in us. */
TheChrisyd 0:17cb4be1e37f 35 #define Tce2data 5 /**< Min delay from CE assert to data, in us. */
TheChrisyd 0:17cb4be1e37f 36 #define Td 1 /**< Minimum delay between edges (actually 50 ns). */
TheChrisyd 0:17cb4be1e37f 37 #define Tpd2cfgm 3 /**< Minimum delay from power up of tranciever to configuration. */
TheChrisyd 0:17cb4be1e37f 38 #define Tsby2txSB 195 /**< Minimum delay from tx initation to air, in us. */
TheChrisyd 0:17cb4be1e37f 39 #define Thmin 5 /**< */
TheChrisyd 0:17cb4be1e37f 40 #define Tclk2data 1 /**< */
TheChrisyd 0:17cb4be1e37f 41
TheChrisyd 3:7ae3a5e53a1f 42 /** ISR handler prototype for receiving messages.
TheChrisyd 3:7ae3a5e53a1f 43 * A function of this type is registered and called when the DR pin on the
TheChrisyd 3:7ae3a5e53a1f 44 * nRF tranciever signals the reception of a message. The void * argument
TheChrisyd 3:7ae3a5e53a1f 45 * is likewise supplied when registering and is returned at call time.
TheChrisyd 3:7ae3a5e53a1f 46 */
TheChrisyd 3:7ae3a5e53a1f 47 typedef void (*nRF2401A_rx_handler_t)(void *);
TheChrisyd 3:7ae3a5e53a1f 48
TheChrisyd 1:8c57f88ff574 49 /** nRF2401 Class
TheChrisyd 0:17cb4be1e37f 50 *
TheChrisyd 0:17cb4be1e37f 51 * Example:
TheChrisyd 0:17cb4be1e37f 52 * @code
TheChrisyd 0:17cb4be1e37f 53 * #include "mbed.h"
TheChrisyd 0:17cb4be1e37f 54 * #include "nRF2401A.h"
TheChrisyd 3:7ae3a5e53a1f 55 *
TheChrisyd 3:7ae3a5e53a1f 56 * // comment these out depending on the job of the mbed. If your only using one mbed leave both uncommented.
TheChrisyd 3:7ae3a5e53a1f 57 * #define TX
TheChrisyd 3:7ae3a5e53a1f 58 * #define RX
TheChrisyd 3:7ae3a5e53a1f 59 *
TheChrisyd 4:e8523ef6e472 60 * Serial pc(USBTX, USBRX);
TheChrisyd 0:17cb4be1e37f 61 * DigitalOut myled(LED1);
TheChrisyd 4:e8523ef6e472 62 *
TheChrisyd 3:7ae3a5e53a1f 63 * #ifdef TX
TheChrisyd 0:17cb4be1e37f 64 * nRF2401A rf1(p10, p11, p12, p13, p14);
TheChrisyd 3:7ae3a5e53a1f 65 * #endif
TheChrisyd 4:e8523ef6e472 66 *
TheChrisyd 3:7ae3a5e53a1f 67 * #ifdef RX
TheChrisyd 0:17cb4be1e37f 68 * nRF2401A rf2(p21, p22, p23, p24, p25);
TheChrisyd 3:7ae3a5e53a1f 69 *
TheChrisyd 4:e8523ef6e472 70 * bool rx_recieved = false;
TheChrisyd 3:7ae3a5e53a1f 71 *
TheChrisyd 4:e8523ef6e472 72 * void nRF2401A_rx (void *arg)
TheChrisyd 4:e8523ef6e472 73 * {
TheChrisyd 4:e8523ef6e472 74 * rx_recieved = true;
TheChrisyd 4:e8523ef6e472 75 * }
TheChrisyd 4:e8523ef6e472 76 * #endif
TheChrisyd 4:e8523ef6e472 77 * int main()
TheChrisyd 4:e8523ef6e472 78 * {
TheChrisyd 0:17cb4be1e37f 79 * wait(0.005);
TheChrisyd 0:17cb4be1e37f 80 * pc.printf("Hello nRF2401A\n\r");
TheChrisyd 4:e8523ef6e472 81 *
TheChrisyd 3:7ae3a5e53a1f 82 * #ifdef TX
TheChrisyd 0:17cb4be1e37f 83 * rf1.setDataPayloadLength(4 << 3)
TheChrisyd 0:17cb4be1e37f 84 * .setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3)
TheChrisyd 0:17cb4be1e37f 85 * .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:17cb4be1e37f 86 * .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:17cb4be1e37f 87 * .setChannel(0x02);
TheChrisyd 0:17cb4be1e37f 88 *
TheChrisyd 0:17cb4be1e37f 89 * rf1.printControlPacket(pc);
TheChrisyd 4:e8523ef6e472 90 * rf1.flushControlPacket();
TheChrisyd 4:e8523ef6e472 91 *
TheChrisyd 4:e8523ef6e472 92 * nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53};
TheChrisyd 4:e8523ef6e472 93 * uint8_t msg[] = {0x01, 0x01, 0x01, 0x01};
TheChrisyd 4:e8523ef6e472 94 * uint32_t *msg32 = (uint32_t *) msg;
TheChrisyd 4:e8523ef6e472 95 * #endif
TheChrisyd 3:7ae3a5e53a1f 96 *
TheChrisyd 3:7ae3a5e53a1f 97 * #ifdef RX
TheChrisyd 0:17cb4be1e37f 98 * rf2.setDataPayloadLength(4 << 3)
TheChrisyd 0:17cb4be1e37f 99 * .setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3)
TheChrisyd 0:17cb4be1e37f 100 * .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:17cb4be1e37f 101 * .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:17cb4be1e37f 102 * .setChannel(0x02);
TheChrisyd 0:17cb4be1e37f 103 *
TheChrisyd 0:17cb4be1e37f 104 * rf2.printControlPacket(pc);
TheChrisyd 4:e8523ef6e472 105 * rf2.flushControlPacket();
TheChrisyd 4:e8523ef6e472 106 * rf2.attachRXHandler(&nRF2401A_rx, 0);
TheChrisyd 3:7ae3a5e53a1f 107 * #endif
TheChrisyd 4:e8523ef6e472 108 *
TheChrisyd 4:e8523ef6e472 109 * while(1)
TheChrisyd 4:e8523ef6e472 110 * {
TheChrisyd 4:e8523ef6e472 111 *
TheChrisyd 3:7ae3a5e53a1f 112 * #ifdef TX
TheChrisyd 0:17cb4be1e37f 113 * rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
TheChrisyd 0:17cb4be1e37f 114 * *msg32 += 1;
TheChrisyd 3:7ae3a5e53a1f 115 * #endif
TheChrisyd 4:e8523ef6e472 116 *
TheChrisyd 0:17cb4be1e37f 117 * myled = 1;
TheChrisyd 0:17cb4be1e37f 118 * wait(0.25);
TheChrisyd 4:e8523ef6e472 119 *
TheChrisyd 4:e8523ef6e472 120 * #ifdef RX
TheChrisyd 4:e8523ef6e472 121 * if (rx_recieved)
TheChrisyd 4:e8523ef6e472 122 * {
TheChrisyd 4:e8523ef6e472 123 * rf2.printDataPacket(pc);
TheChrisyd 4:e8523ef6e472 124 * rx_recieved = false;
TheChrisyd 4:e8523ef6e472 125 * }
TheChrisyd 4:e8523ef6e472 126 * #endif
TheChrisyd 4:e8523ef6e472 127 *
TheChrisyd 0:17cb4be1e37f 128 * myled = 0;
TheChrisyd 0:17cb4be1e37f 129 * wait(0.25);
TheChrisyd 0:17cb4be1e37f 130 * }
TheChrisyd 0:17cb4be1e37f 131 * }
TheChrisyd 3:7ae3a5e53a1f 132 *
TheChrisyd 4:e8523ef6e472 133 *
TheChrisyd 0:17cb4be1e37f 134 * @endcode
TheChrisyd 0:17cb4be1e37f 135 */
TheChrisyd 0:17cb4be1e37f 136
TheChrisyd 0:17cb4be1e37f 137
TheChrisyd 0:17cb4be1e37f 138 /**
TheChrisyd 0:17cb4be1e37f 139 *
TheChrisyd 0:17cb4be1e37f 140 *
TheChrisyd 0:17cb4be1e37f 141 */
TheChrisyd 0:17cb4be1e37f 142 class nRF2401A
TheChrisyd 0:17cb4be1e37f 143 {
TheChrisyd 0:17cb4be1e37f 144 public:
TheChrisyd 3:7ae3a5e53a1f 145 /** Class constructor.
TheChrisyd 3:7ae3a5e53a1f 146 * The constructor assigns the specified pinout, attatch the
TheChrisyd 3:7ae3a5e53a1f 147 * DR1 to a pin interrupt and sets up inmutable control packet
TheChrisyd 3:7ae3a5e53a1f 148 * fields.
TheChrisyd 3:7ae3a5e53a1f 149 * @param ce Chip Enable (CE) pin of the nRF2401A.
TheChrisyd 3:7ae3a5e53a1f 150 * @param c2 Chip Select (CS) pin of the nRF2401A.
TheChrisyd 3:7ae3a5e53a1f 151 * @param dr1 Data Ready 1 (DR1) pin of the nRF2401A.
TheChrisyd 3:7ae3a5e53a1f 152 * @param clk1 Clock 1 (CLK1) pin of the nRF2401A.
TheChrisyd 3:7ae3a5e53a1f 153 * @param data Data (DATA) pin of the nRF2401A.
TheChrisyd 3:7ae3a5e53a1f 154 */
TheChrisyd 0:17cb4be1e37f 155 nRF2401A(PinName ce,
TheChrisyd 0:17cb4be1e37f 156 PinName cs,
TheChrisyd 0:17cb4be1e37f 157 PinName dr1,
TheChrisyd 0:17cb4be1e37f 158 PinName clk1,
TheChrisyd 0:17cb4be1e37f 159 PinName data);
TheChrisyd 0:17cb4be1e37f 160
TheChrisyd 3:7ae3a5e53a1f 161 /** Class destructor.
TheChrisyd 3:7ae3a5e53a1f 162 * Pretty much useless in the embedded world...
TheChrisyd 3:7ae3a5e53a1f 163 */
TheChrisyd 0:17cb4be1e37f 164 virtual ~nRF2401A() { return; }
TheChrisyd 0:17cb4be1e37f 165
TheChrisyd 3:7ae3a5e53a1f 166 /** Set the payload length, in bits.
TheChrisyd 3:7ae3a5e53a1f 167 * Set the control packet field for length, in number of bits, of the message payload.
TheChrisyd 3:7ae3a5e53a1f 168 * @param n Number of bits of the message payload.
TheChrisyd 3:7ae3a5e53a1f 169 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 170 */
TheChrisyd 0:17cb4be1e37f 171 nRF2401A& setDataPayloadLength(uint8_t n)
TheChrisyd 0:17cb4be1e37f 172 {
TheChrisyd 0:17cb4be1e37f 173 _ctrl_packet_buf.channel_1_data_payload_len = n;
TheChrisyd 0:17cb4be1e37f 174 return *this;
TheChrisyd 0:17cb4be1e37f 175 }
TheChrisyd 0:17cb4be1e37f 176
TheChrisyd 3:7ae3a5e53a1f 177 /** Set the address of channel 1.
TheChrisyd 3:7ae3a5e53a1f 178 * The channel address is a up to 40 bit number identifying the tranceiver.
TheChrisyd 3:7ae3a5e53a1f 179 * @param addr4 Bits 39-32 of the address.
TheChrisyd 3:7ae3a5e53a1f 180 * @param addr4 Bits 31-24 of the address.
TheChrisyd 3:7ae3a5e53a1f 181 * @param addr4 Bits 23-16 of the address.
TheChrisyd 3:7ae3a5e53a1f 182 * @param addr4 Bits 15-8 of the address.
TheChrisyd 3:7ae3a5e53a1f 183 * @param addr4 Bits 7-0 of the address.
TheChrisyd 3:7ae3a5e53a1f 184 * @param n_bits Number of bits used in the address.
TheChrisyd 3:7ae3a5e53a1f 185 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 186 */
TheChrisyd 0:17cb4be1e37f 187 nRF2401A& setAddress(uint8_t addr4, uint8_t addr3, uint8_t addr2, uint8_t addr1, uint8_t addr0, uint8_t n_bits)
TheChrisyd 0:17cb4be1e37f 188 {
TheChrisyd 0:17cb4be1e37f 189 _ctrl_packet_buf.channel_1_address[0] = addr4;
TheChrisyd 0:17cb4be1e37f 190 _ctrl_packet_buf.channel_1_address[1] = addr3;
TheChrisyd 0:17cb4be1e37f 191 _ctrl_packet_buf.channel_1_address[2] = addr2;
TheChrisyd 0:17cb4be1e37f 192 _ctrl_packet_buf.channel_1_address[3] = addr1;
TheChrisyd 0:17cb4be1e37f 193 _ctrl_packet_buf.channel_1_address[4] = addr0;
TheChrisyd 0:17cb4be1e37f 194 _ctrl_packet_buf.channel_address_len = n_bits;
TheChrisyd 0:17cb4be1e37f 195
TheChrisyd 0:17cb4be1e37f 196 return *this;
TheChrisyd 0:17cb4be1e37f 197 }
TheChrisyd 0:17cb4be1e37f 198
TheChrisyd 3:7ae3a5e53a1f 199 /** CRC settings.
TheChrisyd 3:7ae3a5e53a1f 200 * Type covering the allowed settings for use of CRC.
TheChrisyd 3:7ae3a5e53a1f 201 */
TheChrisyd 0:17cb4be1e37f 202 typedef enum
TheChrisyd 0:17cb4be1e37f 203 {
TheChrisyd 0:17cb4be1e37f 204 NO_CRC = 0x0, /**< Do not use CRC. */
TheChrisyd 0:17cb4be1e37f 205 CRC_8 = 0x1, /**< Use a 8-bit CRC. */
TheChrisyd 0:17cb4be1e37f 206 CRC_16 = 0x3 /**< Use a 16-bit CRC. */
TheChrisyd 0:17cb4be1e37f 207 } CRC_T;
TheChrisyd 0:17cb4be1e37f 208
TheChrisyd 3:7ae3a5e53a1f 209 /** Set CRC use.
TheChrisyd 3:7ae3a5e53a1f 210 * Set the CRC mode field of the control packet.
TheChrisyd 3:7ae3a5e53a1f 211 * @param mode The CRC mode of choise.
TheChrisyd 3:7ae3a5e53a1f 212 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 213 */
TheChrisyd 0:17cb4be1e37f 214 nRF2401A& setCRCMode(CRC_T mode)
TheChrisyd 0:17cb4be1e37f 215 {
TheChrisyd 0:17cb4be1e37f 216 _ctrl_packet_buf.crc_config = mode;
TheChrisyd 0:17cb4be1e37f 217 return *this;
TheChrisyd 0:17cb4be1e37f 218 }
TheChrisyd 0:17cb4be1e37f 219
TheChrisyd 3:7ae3a5e53a1f 220 /** Data rate settings.
TheChrisyd 3:7ae3a5e53a1f 221 * Type covering the allowed settings for the tranceiver data rate.
TheChrisyd 3:7ae3a5e53a1f 222 */
TheChrisyd 0:17cb4be1e37f 223 typedef enum
TheChrisyd 0:17cb4be1e37f 224 {
TheChrisyd 0:17cb4be1e37f 225 BIT_RATE_250KBITS = 0x0, /**< */
TheChrisyd 0:17cb4be1e37f 226 BIT_RATE_1MBITS = 0x1 /**< */
TheChrisyd 0:17cb4be1e37f 227 } DATA_RATE_T;
TheChrisyd 0:17cb4be1e37f 228
TheChrisyd 3:7ae3a5e53a1f 229 /** Set tranceiver data rate.
TheChrisyd 3:7ae3a5e53a1f 230 * Sets the data rate field to either 250 kbit/s or 1 Mbit/s data transfer rate.
TheChrisyd 3:7ae3a5e53a1f 231 * @param mode The data rate of choise.
TheChrisyd 3:7ae3a5e53a1f 232 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 233 */
TheChrisyd 0:17cb4be1e37f 234 nRF2401A& setDataRate(DATA_RATE_T data_rate)
TheChrisyd 0:17cb4be1e37f 235 {
TheChrisyd 0:17cb4be1e37f 236 _ctrl_packet_buf.rf_data_rate = data_rate;
TheChrisyd 0:17cb4be1e37f 237 return *this;
TheChrisyd 0:17cb4be1e37f 238 }
TheChrisyd 0:17cb4be1e37f 239
TheChrisyd 3:7ae3a5e53a1f 240 /** Set RF channel.
TheChrisyd 3:7ae3a5e53a1f 241 * Sets the control packet field for channel number. Channel numbers are from 0 to 127
TheChrisyd 3:7ae3a5e53a1f 242 * representing channel frequencies equal to (2400 + channel number) MHz.
TheChrisyd 3:7ae3a5e53a1f 243 * @param ch Channel number, from the range [0, 127].
TheChrisyd 3:7ae3a5e53a1f 244 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 245 */
TheChrisyd 0:17cb4be1e37f 246 nRF2401A& setChannel(uint8_t ch)
TheChrisyd 0:17cb4be1e37f 247 {
TheChrisyd 0:17cb4be1e37f 248 _ctrl_packet_buf.rf_channel = ch;
TheChrisyd 0:17cb4be1e37f 249 return *this;
TheChrisyd 0:17cb4be1e37f 250 }
TheChrisyd 0:17cb4be1e37f 251
TheChrisyd 3:7ae3a5e53a1f 252 /** Send the control packet to the nRF2401A.
TheChrisyd 3:7ae3a5e53a1f 253 * This function transfer the control packet image to the nRF2401A.
TheChrisyd 3:7ae3a5e53a1f 254 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 255 */
TheChrisyd 0:17cb4be1e37f 256 nRF2401A& flushControlPacket();
TheChrisyd 0:17cb4be1e37f 257
TheChrisyd 3:7ae3a5e53a1f 258 /**
TheChrisyd 3:7ae3a5e53a1f 259 *
TheChrisyd 3:7ae3a5e53a1f 260 */
TheChrisyd 0:17cb4be1e37f 261 void activate(bool active = true);
TheChrisyd 0:17cb4be1e37f 262
TheChrisyd 3:7ae3a5e53a1f 263 /**
TheChrisyd 3:7ae3a5e53a1f 264 *
TheChrisyd 3:7ae3a5e53a1f 265 */
TheChrisyd 0:17cb4be1e37f 266 typedef uint8_t address_t[5];
TheChrisyd 0:17cb4be1e37f 267
TheChrisyd 3:7ae3a5e53a1f 268 /** Send a message.
TheChrisyd 3:7ae3a5e53a1f 269 * This routine will transfer the data from the supplied buffer and send
TheChrisyd 3:7ae3a5e53a1f 270 * it to the specified address using the current control packet settings.
TheChrisyd 3:7ae3a5e53a1f 271 * @param addr The address to send to.
TheChrisyd 3:7ae3a5e53a1f 272 * @param addr_len Length of address, in bits.
TheChrisyd 3:7ae3a5e53a1f 273 * @param msg_buf Message body.
TheChrisyd 3:7ae3a5e53a1f 274 * @param msg_len Length of message, in bits.
TheChrisyd 3:7ae3a5e53a1f 275 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 276 */
TheChrisyd 0:17cb4be1e37f 277 nRF2401A& sendMsg(address_t addr, uint8_t addr_len, uint8_t *msg_buf, uint8_t msg_len);
TheChrisyd 0:17cb4be1e37f 278
TheChrisyd 3:7ae3a5e53a1f 279 /** Register a receive action callback.
TheChrisyd 3:7ae3a5e53a1f 280 * Attach a callback that will be called when the tranceiver intercept a
TheChrisyd 3:7ae3a5e53a1f 281 * message. This callback will be called in the context of an interrupt
TheChrisyd 3:7ae3a5e53a1f 282 * routine and should act accordingly.
TheChrisyd 3:7ae3a5e53a1f 283 * @param handler The callback, of type nRF2401_rx_handler_t.
TheChrisyd 3:7ae3a5e53a1f 284 * @param arg Pointer to data supplied to the handler at call time.
TheChrisyd 3:7ae3a5e53a1f 285 * @return Reference to the invoked object (for chaining operations).
TheChrisyd 3:7ae3a5e53a1f 286 */
TheChrisyd 0:17cb4be1e37f 287 nRF2401A& attachRXHandler(nRF2401A_rx_handler_t handler, void *arg);
TheChrisyd 0:17cb4be1e37f 288
TheChrisyd 0:17cb4be1e37f 289 void printControlPacket(Serial& port);
TheChrisyd 0:17cb4be1e37f 290 void printDataPacket(Serial& port);
TheChrisyd 0:17cb4be1e37f 291
TheChrisyd 0:17cb4be1e37f 292 private:
TheChrisyd 0:17cb4be1e37f 293
TheChrisyd 0:17cb4be1e37f 294 DigitalOut _ce; /**< Chip Enable pin. */
TheChrisyd 0:17cb4be1e37f 295 DigitalOut _cs; /**< Chip select pin. */
TheChrisyd 0:17cb4be1e37f 296 DigitalIn _dr1; /**< Data Ready 1 pin. */
TheChrisyd 0:17cb4be1e37f 297 DigitalOut _clk1; /**< Clock 1 pin. */
TheChrisyd 0:17cb4be1e37f 298 DigitalInOut _data; /**< Data pin. */
TheChrisyd 0:17cb4be1e37f 299
TheChrisyd 3:7ae3a5e53a1f 300 /**
TheChrisyd 3:7ae3a5e53a1f 301 *
TheChrisyd 3:7ae3a5e53a1f 302 */
TheChrisyd 0:17cb4be1e37f 303 typedef enum
TheChrisyd 0:17cb4be1e37f 304 {
TheChrisyd 0:17cb4be1e37f 305 UNDEF, /**< The start state. */
TheChrisyd 1:8c57f88ff574 306 RX, /**< The tranceiver is in receive mode. */
TheChrisyd 1:8c57f88ff574 307 TX, /**< The tranceiver is transmitting. */
TheChrisyd 1:8c57f88ff574 308 STANDBY /**< The tranceiver goes into stanby mode. */
TheChrisyd 0:17cb4be1e37f 309 } STATE_T;
TheChrisyd 0:17cb4be1e37f 310
TheChrisyd 0:17cb4be1e37f 311 STATE_T _state;
TheChrisyd 0:17cb4be1e37f 312
TheChrisyd 3:7ae3a5e53a1f 313 /** Contol packet data.
TheChrisyd 3:7ae3a5e53a1f 314 *
TheChrisyd 3:7ae3a5e53a1f 315 */
TheChrisyd 0:17cb4be1e37f 316 struct nRF2401A_ctrl_packet_t
TheChrisyd 0:17cb4be1e37f 317 {
TheChrisyd 0:17cb4be1e37f 318 uint8_t channel_2_data_payload_len; /**< */
TheChrisyd 0:17cb4be1e37f 319 uint8_t channel_1_data_payload_len; /**< */
TheChrisyd 0:17cb4be1e37f 320 uint8_t channel_2_address[5]; /**< */
TheChrisyd 0:17cb4be1e37f 321 uint8_t channel_1_address[5]; /**< */
TheChrisyd 0:17cb4be1e37f 322
TheChrisyd 0:17cb4be1e37f 323 uint8_t crc_config : 2; /**< */
TheChrisyd 0:17cb4be1e37f 324 uint8_t channel_address_len : 6; /**< */
TheChrisyd 0:17cb4be1e37f 325
TheChrisyd 0:17cb4be1e37f 326 uint8_t rf_power : 2; /**< */
TheChrisyd 0:17cb4be1e37f 327 uint8_t xo_frequency : 3; /**< */
TheChrisyd 0:17cb4be1e37f 328 uint8_t rf_data_rate : 1; /**< */
TheChrisyd 0:17cb4be1e37f 329 uint8_t communication_mode : 1; /**< */
TheChrisyd 0:17cb4be1e37f 330 uint8_t enable_dual_channel_mode : 1; /**< */
TheChrisyd 0:17cb4be1e37f 331
TheChrisyd 0:17cb4be1e37f 332 uint8_t txr_switch : 1; /**< */
TheChrisyd 0:17cb4be1e37f 333 uint8_t rf_channel : 7; /**< */
TheChrisyd 0:17cb4be1e37f 334
TheChrisyd 0:17cb4be1e37f 335 } _ctrl_packet_buf; /**< */
TheChrisyd 0:17cb4be1e37f 336
TheChrisyd 0:17cb4be1e37f 337 uint8_t *_ctrl_packet; /**< */
TheChrisyd 0:17cb4be1e37f 338
TheChrisyd 0:17cb4be1e37f 339 uint8_t _data_buf[32]; /**< */
TheChrisyd 0:17cb4be1e37f 340
TheChrisyd 0:17cb4be1e37f 341 nRF2401A_rx_handler_t _rx_handler; /**< */
TheChrisyd 0:17cb4be1e37f 342 void *_rx_handler_arg; /**< */
TheChrisyd 0:17cb4be1e37f 343
TheChrisyd 3:7ae3a5e53a1f 344 /** Receive ISR.
TheChrisyd 3:7ae3a5e53a1f 345 * This handler is attached to the rising flank of the DR1 pin. It
TheChrisyd 3:7ae3a5e53a1f 346 * will thus be called when the nRF2401A receives a packet in ShockBurst
TheChrisyd 3:7ae3a5e53a1f 347 * mode (the mode used). It will in turn call the attached handler.
TheChrisyd 3:7ae3a5e53a1f 348 */
TheChrisyd 0:17cb4be1e37f 349 void dataReadyHandler(void);
TheChrisyd 0:17cb4be1e37f 350
TheChrisyd 3:7ae3a5e53a1f 351 /**
TheChrisyd 3:7ae3a5e53a1f 352 *
TheChrisyd 3:7ae3a5e53a1f 353 */
TheChrisyd 0:17cb4be1e37f 354 InterruptIn _dr1_isr;
TheChrisyd 0:17cb4be1e37f 355
TheChrisyd 3:7ae3a5e53a1f 356 /*
TheChrisyd 3:7ae3a5e53a1f 357 *
TheChrisyd 3:7ae3a5e53a1f 358 */
TheChrisyd 0:17cb4be1e37f 359 typedef enum { RX_MODE = 0x1, TX_MODE = 0x0 } TXR_T;
TheChrisyd 0:17cb4be1e37f 360
TheChrisyd 3:7ae3a5e53a1f 361 /** Write to the data bus.
TheChrisyd 3:7ae3a5e53a1f 362 * Write n_bits bits on the DATA line.
TheChrisyd 3:7ae3a5e53a1f 363 * @param buf Data buffer.
TheChrisyd 3:7ae3a5e53a1f 364 * @param n_bits Number of bits to transfer.
TheChrisyd 3:7ae3a5e53a1f 365 * @param is_ctrl True if the tranfered data is control word, false if data.
TheChrisyd 3:7ae3a5e53a1f 366 */
TheChrisyd 0:17cb4be1e37f 367 void pushCtrl(uint8_t *buf, uint8_t n_bits, bool is_ctrl = true);
TheChrisyd 0:17cb4be1e37f 368
TheChrisyd 3:7ae3a5e53a1f 369 /** Read a message from the tranceiver.
TheChrisyd 3:7ae3a5e53a1f 370 * Read until DR1 goes low.
TheChrisyd 3:7ae3a5e53a1f 371 * @param buf Data buffer.
TheChrisyd 3:7ae3a5e53a1f 372 * @return Number of bits read.
TheChrisyd 3:7ae3a5e53a1f 373 */
TheChrisyd 0:17cb4be1e37f 374 int pull(uint8_t *buf);
TheChrisyd 2:ca8602ee9e1d 375 };
TheChrisyd 2:ca8602ee9e1d 376
TheChrisyd 2:ca8602ee9e1d 377 #endif