Library for the nRF2401A Transceiver
Dependents: nRF2401A_Hello_World nRF2401A_Wireless_Accelerometer_joypad nRF2401A_Gameduino_Invaders
nRF2401A.h@9:7245524e37e4, 2014-03-09 (annotated)
- Committer:
- TheChrisyd
- Date:
- Sun Mar 09 11:57:33 2014 +0000
- Revision:
- 9:7245524e37e4
- Parent:
- 8:fb7cb88e80a4
Update after feedback, parameters set to defaults, removed references ti this and added error checking to the functions instead, moved defines and updated documentation.
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:7245524e37e4 | 32 | |
TheChrisyd | 9:7245524e37e4 | 33 | #define DATA_BUFFER_SIZE 32 |
TheChrisyd | 9:7245524e37e4 | 34 | |
TheChrisyd | 0:17cb4be1e37f | 35 | |
TheChrisyd | 3:7ae3a5e53a1f | 36 | /** ISR handler prototype for receiving messages. |
TheChrisyd | 3:7ae3a5e53a1f | 37 | * A function of this type is registered and called when the DR pin on the |
TheChrisyd | 3:7ae3a5e53a1f | 38 | * nRF tranciever signals the reception of a message. The void * argument |
TheChrisyd | 3:7ae3a5e53a1f | 39 | * is likewise supplied when registering and is returned at call time. |
TheChrisyd | 3:7ae3a5e53a1f | 40 | */ |
TheChrisyd | 3:7ae3a5e53a1f | 41 | typedef void (*nRF2401A_rx_handler_t)(void *); |
TheChrisyd | 3:7ae3a5e53a1f | 42 | |
TheChrisyd | 1:8c57f88ff574 | 43 | /** nRF2401 Class |
TheChrisyd | 0:17cb4be1e37f | 44 | * |
TheChrisyd | 9:7245524e37e4 | 45 | * A class supporting the nRF2401A nordic 2.4Ghz transciever |
TheChrisyd | 9:7245524e37e4 | 46 | * Supports shock burts mode only. |
TheChrisyd | 9:7245524e37e4 | 47 | * multi channel mode is not supported as it is not on the breakout header. |
TheChrisyd | 9:7245524e37e4 | 48 | * |
TheChrisyd | 0:17cb4be1e37f | 49 | * Example: |
TheChrisyd | 0:17cb4be1e37f | 50 | * @code |
TheChrisyd | 0:17cb4be1e37f | 51 | * #include "mbed.h" |
TheChrisyd | 0:17cb4be1e37f | 52 | * #include "nRF2401A.h" |
TheChrisyd | 3:7ae3a5e53a1f | 53 | * |
TheChrisyd | 6:ad8242a1379a | 54 | * // comment these out depending on the job of the mbed. If your only using one mbed leave both uncommented. |
TheChrisyd | 9:7245524e37e4 | 55 | * |
TheChrisyd | 9:7245524e37e4 | 56 | * #define TX |
TheChrisyd | 3:7ae3a5e53a1f | 57 | * #define RX |
TheChrisyd | 3:7ae3a5e53a1f | 58 | * |
TheChrisyd | 6:ad8242a1379a | 59 | * // If using the FRDM-KL25Z uncomment this line |
TheChrisyd | 5:18ce8a56b248 | 60 | * //#define FRDMKL25Z |
TheChrisyd | 5:18ce8a56b248 | 61 | * |
TheChrisyd | 4:e8523ef6e472 | 62 | * Serial pc(USBTX, USBRX); |
TheChrisyd | 0:17cb4be1e37f | 63 | * DigitalOut myled(LED1); |
TheChrisyd | 4:e8523ef6e472 | 64 | * |
TheChrisyd | 3:7ae3a5e53a1f | 65 | * #ifdef TX |
TheChrisyd | 5:18ce8a56b248 | 66 | * #ifdef FRDMKL25Z |
TheChrisyd | 8:fb7cb88e80a4 | 67 | * nRF2401A rf1(PTD0, PTD5, PTA13, PTC12, PTC13); //ce, cs, dr1, clk1, data |
TheChrisyd | 5:18ce8a56b248 | 68 | * #else |
TheChrisyd | 0:17cb4be1e37f | 69 | * nRF2401A rf1(p10, p11, p12, p13, p14); |
TheChrisyd | 3:7ae3a5e53a1f | 70 | * #endif |
TheChrisyd | 5:18ce8a56b248 | 71 | * #endif |
TheChrisyd | 4:e8523ef6e472 | 72 | * |
TheChrisyd | 3:7ae3a5e53a1f | 73 | * #ifdef RX |
TheChrisyd | 5:18ce8a56b248 | 74 | * #ifdef FRDMKL25Z |
TheChrisyd | 5:18ce8a56b248 | 75 | * nRF2401A rf2(PTD0, PTD5, PTA13, PTC12, PTC13); |
TheChrisyd | 5:18ce8a56b248 | 76 | * #else |
TheChrisyd | 9:7245524e37e4 | 77 | * nRF2401A rf2(p25, p24, p23, p22, p21); |
TheChrisyd | 5:18ce8a56b248 | 78 | * #endif |
TheChrisyd | 3:7ae3a5e53a1f | 79 | * |
TheChrisyd | 4:e8523ef6e472 | 80 | * bool rx_recieved = false; |
TheChrisyd | 3:7ae3a5e53a1f | 81 | * |
TheChrisyd | 4:e8523ef6e472 | 82 | * void nRF2401A_rx (void *arg) |
TheChrisyd | 4:e8523ef6e472 | 83 | * { |
TheChrisyd | 4:e8523ef6e472 | 84 | * rx_recieved = true; |
TheChrisyd | 4:e8523ef6e472 | 85 | * } |
TheChrisyd | 4:e8523ef6e472 | 86 | * #endif |
TheChrisyd | 4:e8523ef6e472 | 87 | * int main() |
TheChrisyd | 4:e8523ef6e472 | 88 | * { |
TheChrisyd | 0:17cb4be1e37f | 89 | * wait(0.005); |
TheChrisyd | 0:17cb4be1e37f | 90 | * pc.printf("Hello nRF2401A\n\r"); |
TheChrisyd | 4:e8523ef6e472 | 91 | * |
TheChrisyd | 5:18ce8a56b248 | 92 | * #ifdef TX |
TheChrisyd | 9:7245524e37e4 | 93 | * // initialise the nRF2401A with payload size and address |
TheChrisyd | 9:7245524e37e4 | 94 | * rf1.setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3); |
TheChrisyd | 9:7245524e37e4 | 95 | * |
TheChrisyd | 0:17cb4be1e37f | 96 | * rf1.printControlPacket(pc); |
TheChrisyd | 4:e8523ef6e472 | 97 | * rf1.flushControlPacket(); |
TheChrisyd | 4:e8523ef6e472 | 98 | * |
TheChrisyd | 9:7245524e37e4 | 99 | * // initialise variables to use for tranmission |
TheChrisyd | 4:e8523ef6e472 | 100 | * nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53}; |
TheChrisyd | 4:e8523ef6e472 | 101 | * uint8_t msg[] = {0x01, 0x01, 0x01, 0x01}; |
TheChrisyd | 4:e8523ef6e472 | 102 | * uint32_t *msg32 = (uint32_t *) msg; |
TheChrisyd | 4:e8523ef6e472 | 103 | * #endif |
TheChrisyd | 3:7ae3a5e53a1f | 104 | * |
TheChrisyd | 3:7ae3a5e53a1f | 105 | * #ifdef RX |
TheChrisyd | 9:7245524e37e4 | 106 | * // initialise the nRF2401A with payload size and address |
TheChrisyd | 9:7245524e37e4 | 107 | * rf2.setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3); |
TheChrisyd | 0:17cb4be1e37f | 108 | * |
TheChrisyd | 0:17cb4be1e37f | 109 | * rf2.printControlPacket(pc); |
TheChrisyd | 4:e8523ef6e472 | 110 | * rf2.flushControlPacket(); |
TheChrisyd | 5:18ce8a56b248 | 111 | * |
TheChrisyd | 6:ad8242a1379a | 112 | * // attach receive callback |
TheChrisyd | 4:e8523ef6e472 | 113 | * rf2.attachRXHandler(&nRF2401A_rx, 0); |
TheChrisyd | 3:7ae3a5e53a1f | 114 | * #endif |
TheChrisyd | 5:18ce8a56b248 | 115 | * |
TheChrisyd | 4:e8523ef6e472 | 116 | * while(1) |
TheChrisyd | 4:e8523ef6e472 | 117 | * { |
TheChrisyd | 4:e8523ef6e472 | 118 | * |
TheChrisyd | 5:18ce8a56b248 | 119 | * #ifdef TX |
TheChrisyd | 5:18ce8a56b248 | 120 | * myled = 0; |
TheChrisyd | 5:18ce8a56b248 | 121 | * wait(0.25); |
TheChrisyd | 5:18ce8a56b248 | 122 | * |
TheChrisyd | 9:7245524e37e4 | 123 | * // send the message to the nRF2401A |
TheChrisyd | 0:17cb4be1e37f | 124 | * rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3); |
TheChrisyd | 0:17cb4be1e37f | 125 | * *msg32 += 1; |
TheChrisyd | 5:18ce8a56b248 | 126 | * |
TheChrisyd | 5:18ce8a56b248 | 127 | * myled = 1; |
TheChrisyd | 5:18ce8a56b248 | 128 | * wait(0.25); |
TheChrisyd | 3:7ae3a5e53a1f | 129 | * #endif |
TheChrisyd | 4:e8523ef6e472 | 130 | * |
TheChrisyd | 4:e8523ef6e472 | 131 | * |
TheChrisyd | 4:e8523ef6e472 | 132 | * #ifdef RX |
TheChrisyd | 4:e8523ef6e472 | 133 | * if (rx_recieved) |
TheChrisyd | 4:e8523ef6e472 | 134 | * { |
TheChrisyd | 6:ad8242a1379a | 135 | * // send the read buffer directly to the serial port |
TheChrisyd | 4:e8523ef6e472 | 136 | * rf2.printDataPacket(pc); |
TheChrisyd | 5:18ce8a56b248 | 137 | * |
TheChrisyd | 6:ad8242a1379a | 138 | * // send a single byte from the read buffer to the serial port |
TheChrisyd | 5:18ce8a56b248 | 139 | * uint8_t rx_msg = 0; |
TheChrisyd | 9:7245524e37e4 | 140 | * rx_msg = rf2.readMsg_byte( 0 ); |
TheChrisyd | 5:18ce8a56b248 | 141 | * pc.printf("\n\r%d\n\r", rx_msg); |
TheChrisyd | 5:18ce8a56b248 | 142 | * |
TheChrisyd | 6:ad8242a1379a | 143 | * // read the read buffer , then send to the serial port |
TheChrisyd | 5:18ce8a56b248 | 144 | * uint8_t rx_buffer[32] = {0}; |
TheChrisyd | 5:18ce8a56b248 | 145 | * rf2.readMsg( &rx_buffer[0], 32); |
TheChrisyd | 5:18ce8a56b248 | 146 | * for(int i = 0; i < sizeof(rx_buffer); i++) |
TheChrisyd | 5:18ce8a56b248 | 147 | * { |
TheChrisyd | 5:18ce8a56b248 | 148 | * pc.printf("%02x ", rx_buffer[i]); |
TheChrisyd | 5:18ce8a56b248 | 149 | * } |
TheChrisyd | 5:18ce8a56b248 | 150 | * pc.printf("\r\n"); |
TheChrisyd | 5:18ce8a56b248 | 151 | * |
TheChrisyd | 6:ad8242a1379a | 152 | * // clear flags and flash the led |
TheChrisyd | 4:e8523ef6e472 | 153 | * rx_recieved = false; |
TheChrisyd | 5:18ce8a56b248 | 154 | * myled = !myled; |
TheChrisyd | 4:e8523ef6e472 | 155 | * } |
TheChrisyd | 4:e8523ef6e472 | 156 | * #endif |
TheChrisyd | 4:e8523ef6e472 | 157 | * |
TheChrisyd | 0:17cb4be1e37f | 158 | * } |
TheChrisyd | 0:17cb4be1e37f | 159 | * } |
TheChrisyd | 3:7ae3a5e53a1f | 160 | * |
TheChrisyd | 0:17cb4be1e37f | 161 | * @endcode |
TheChrisyd | 0:17cb4be1e37f | 162 | */ |
TheChrisyd | 0:17cb4be1e37f | 163 | |
TheChrisyd | 0:17cb4be1e37f | 164 | |
TheChrisyd | 0:17cb4be1e37f | 165 | /** |
TheChrisyd | 0:17cb4be1e37f | 166 | * |
TheChrisyd | 0:17cb4be1e37f | 167 | * |
TheChrisyd | 0:17cb4be1e37f | 168 | */ |
TheChrisyd | 0:17cb4be1e37f | 169 | class nRF2401A |
TheChrisyd | 0:17cb4be1e37f | 170 | { |
TheChrisyd | 0:17cb4be1e37f | 171 | public: |
TheChrisyd | 3:7ae3a5e53a1f | 172 | /** Class constructor. |
TheChrisyd | 3:7ae3a5e53a1f | 173 | * The constructor assigns the specified pinout, attatch the |
TheChrisyd | 3:7ae3a5e53a1f | 174 | * DR1 to a pin interrupt and sets up inmutable control packet |
TheChrisyd | 5:18ce8a56b248 | 175 | * fields. for the KL25Z, the Data Ready pin must be on ports A or C |
TheChrisyd | 3:7ae3a5e53a1f | 176 | * @param ce Chip Enable (CE) pin of the nRF2401A. |
TheChrisyd | 3:7ae3a5e53a1f | 177 | * @param c2 Chip Select (CS) pin of the nRF2401A. |
TheChrisyd | 3:7ae3a5e53a1f | 178 | * @param dr1 Data Ready 1 (DR1) pin of the nRF2401A. |
TheChrisyd | 3:7ae3a5e53a1f | 179 | * @param clk1 Clock 1 (CLK1) pin of the nRF2401A. |
TheChrisyd | 3:7ae3a5e53a1f | 180 | * @param data Data (DATA) pin of the nRF2401A. |
TheChrisyd | 3:7ae3a5e53a1f | 181 | */ |
TheChrisyd | 0:17cb4be1e37f | 182 | nRF2401A(PinName ce, |
TheChrisyd | 0:17cb4be1e37f | 183 | PinName cs, |
TheChrisyd | 0:17cb4be1e37f | 184 | PinName dr1, |
TheChrisyd | 0:17cb4be1e37f | 185 | PinName clk1, |
TheChrisyd | 0:17cb4be1e37f | 186 | PinName data); |
TheChrisyd | 0:17cb4be1e37f | 187 | |
TheChrisyd | 3:7ae3a5e53a1f | 188 | /** Class destructor. |
TheChrisyd | 3:7ae3a5e53a1f | 189 | * Pretty much useless in the embedded world... |
TheChrisyd | 3:7ae3a5e53a1f | 190 | */ |
TheChrisyd | 0:17cb4be1e37f | 191 | virtual ~nRF2401A() { return; } |
TheChrisyd | 0:17cb4be1e37f | 192 | |
TheChrisyd | 9:7245524e37e4 | 193 | |
TheChrisyd | 9:7245524e37e4 | 194 | /** CRC settings. |
TheChrisyd | 9:7245524e37e4 | 195 | * Type covering the allowed settings for use of CRC. |
TheChrisyd | 9:7245524e37e4 | 196 | */ |
TheChrisyd | 9:7245524e37e4 | 197 | typedef enum |
TheChrisyd | 9:7245524e37e4 | 198 | { |
TheChrisyd | 9:7245524e37e4 | 199 | NO_CRC = 0x0, /**< Do not use CRC. */ |
TheChrisyd | 9:7245524e37e4 | 200 | CRC_8 = 0x1, /**< Use a 8-bit CRC. */ |
TheChrisyd | 9:7245524e37e4 | 201 | CRC_16 = 0x3, /**< Use a 16-bit CRC. */ |
TheChrisyd | 9:7245524e37e4 | 202 | INVALID_CRC /* must be last in the list for error check in setCRCMode */ |
TheChrisyd | 9:7245524e37e4 | 203 | } CRC_T; |
TheChrisyd | 9:7245524e37e4 | 204 | |
TheChrisyd | 9:7245524e37e4 | 205 | /** Transceiver state |
TheChrisyd | 9:7245524e37e4 | 206 | * type covering the states of the transceiver |
TheChrisyd | 9:7245524e37e4 | 207 | */ |
TheChrisyd | 9:7245524e37e4 | 208 | typedef enum |
TheChrisyd | 9:7245524e37e4 | 209 | { |
TheChrisyd | 9:7245524e37e4 | 210 | RX, /**< The tranceiver is in receive mode. Default mode. */ |
TheChrisyd | 9:7245524e37e4 | 211 | TX, /**< The tranceiver is transmitting. */ |
TheChrisyd | 9:7245524e37e4 | 212 | STANDBY, /**< The tranceiver goes into stanby mode. */ |
TheChrisyd | 9:7245524e37e4 | 213 | INVALID_STATE |
TheChrisyd | 9:7245524e37e4 | 214 | } STATE_T; |
TheChrisyd | 9:7245524e37e4 | 215 | |
TheChrisyd | 9:7245524e37e4 | 216 | /** Data rate settings. |
TheChrisyd | 9:7245524e37e4 | 217 | * Type covering the allowed settings for the tranceiver data rate. |
TheChrisyd | 9:7245524e37e4 | 218 | */ |
TheChrisyd | 9:7245524e37e4 | 219 | typedef enum |
TheChrisyd | 9:7245524e37e4 | 220 | { |
TheChrisyd | 9:7245524e37e4 | 221 | BIT_RATE_250KBITS = 0x0, /**< 250kbits data rate default */ |
TheChrisyd | 9:7245524e37e4 | 222 | BIT_RATE_1MBITS = 0x1, /**< 1Mbit data rate */ |
TheChrisyd | 9:7245524e37e4 | 223 | INVALID_RATE /* must be last in the list for error check on set data rate */ |
TheChrisyd | 9:7245524e37e4 | 224 | } DATA_RATE_T; |
TheChrisyd | 9:7245524e37e4 | 225 | |
TheChrisyd | 9:7245524e37e4 | 226 | /** RF power settings |
TheChrisyd | 9:7245524e37e4 | 227 | * Type covering the allowed settings for RF power |
TheChrisyd | 9:7245524e37e4 | 228 | */ |
TheChrisyd | 9:7245524e37e4 | 229 | typedef enum |
TheChrisyd | 9:7245524e37e4 | 230 | { |
TheChrisyd | 9:7245524e37e4 | 231 | MINUS_TWENTY_DB = 0, /**< -20dB */ |
TheChrisyd | 9:7245524e37e4 | 232 | MINUS_TEN_DB = 1, /**< -10dB */ |
TheChrisyd | 9:7245524e37e4 | 233 | MINUS_FIVE_DB = 2, /**< -5dB */ |
TheChrisyd | 9:7245524e37e4 | 234 | ZERO_DB = 3, /**< 0dB */ |
TheChrisyd | 9:7245524e37e4 | 235 | INVALID_POWER /* must be last in the list for error check on set RF power */ |
TheChrisyd | 9:7245524e37e4 | 236 | } RF_POWER_T; |
TheChrisyd | 9:7245524e37e4 | 237 | |
TheChrisyd | 9:7245524e37e4 | 238 | /** Put the tranceiver into, or bring out of standby. |
TheChrisyd | 9:7245524e37e4 | 239 | * Tx mode 10.5mA, RX mode 18mA, Standby 400nA. |
TheChrisyd | 9:7245524e37e4 | 240 | * @param active set standby state |
TheChrisyd | 9:7245524e37e4 | 241 | */ |
TheChrisyd | 9:7245524e37e4 | 242 | STATE_T standby_mode(bool active = true); |
TheChrisyd | 9:7245524e37e4 | 243 | |
TheChrisyd | 9:7245524e37e4 | 244 | /** |
TheChrisyd | 9:7245524e37e4 | 245 | * |
TheChrisyd | 9:7245524e37e4 | 246 | */ |
TheChrisyd | 9:7245524e37e4 | 247 | typedef uint8_t address_t[5]; |
TheChrisyd | 9:7245524e37e4 | 248 | |
TheChrisyd | 9:7245524e37e4 | 249 | /** Print control packet |
TheChrisyd | 9:7245524e37e4 | 250 | * Print the control packet to a serial port |
TheChrisyd | 9:7245524e37e4 | 251 | * @param arg Pointer to the port to transmit on |
TheChrisyd | 9:7245524e37e4 | 252 | * @return bool for correct parameters supplied |
TheChrisyd | 9:7245524e37e4 | 253 | */ |
TheChrisyd | 9:7245524e37e4 | 254 | bool printControlPacket(Serial& port); |
TheChrisyd | 9:7245524e37e4 | 255 | |
TheChrisyd | 9:7245524e37e4 | 256 | /** Print data packet |
TheChrisyd | 9:7245524e37e4 | 257 | * Print the data packet to a serial port |
TheChrisyd | 9:7245524e37e4 | 258 | * @param arg Pointer to the port to transmit on |
TheChrisyd | 9:7245524e37e4 | 259 | * @return bool for correct parameters supplied |
TheChrisyd | 9:7245524e37e4 | 260 | */ |
TheChrisyd | 9:7245524e37e4 | 261 | bool printDataPacket(Serial& port); |
TheChrisyd | 9:7245524e37e4 | 262 | |
TheChrisyd | 9:7245524e37e4 | 263 | /** Send the control packet to the nRF2401A. |
TheChrisyd | 9:7245524e37e4 | 264 | * This function transfer the control packet image to the nRF2401A. |
TheChrisyd | 9:7245524e37e4 | 265 | * @return bool for successfull flushing of the packet |
TheChrisyd | 9:7245524e37e4 | 266 | */ |
TheChrisyd | 9:7245524e37e4 | 267 | bool flushControlPacket(); |
TheChrisyd | 9:7245524e37e4 | 268 | |
TheChrisyd | 9:7245524e37e4 | 269 | /** Register a receive action callback. |
TheChrisyd | 9:7245524e37e4 | 270 | * Attach a callback that will be called when the tranceiver intercept a |
TheChrisyd | 9:7245524e37e4 | 271 | * message. This callback will be called in the context of an interrupt |
TheChrisyd | 9:7245524e37e4 | 272 | * routine and should act accordingly. |
TheChrisyd | 9:7245524e37e4 | 273 | * @param handler The callback, of type nRF2401_rx_handler_t. |
TheChrisyd | 9:7245524e37e4 | 274 | * @param arg Pointer to data supplied to the handler at call time. |
TheChrisyd | 9:7245524e37e4 | 275 | * @return Reference to the invoked object (for chaining operations). |
TheChrisyd | 9:7245524e37e4 | 276 | */ |
TheChrisyd | 9:7245524e37e4 | 277 | bool attachRXHandler(nRF2401A_rx_handler_t handler, void *arg); |
TheChrisyd | 9:7245524e37e4 | 278 | |
TheChrisyd | 3:7ae3a5e53a1f | 279 | /** Set the payload length, in bits. |
TheChrisyd | 3:7ae3a5e53a1f | 280 | * Set the control packet field for length, in number of bits, of the message payload. |
TheChrisyd | 3:7ae3a5e53a1f | 281 | * @param n Number of bits of the message payload. |
TheChrisyd | 9:7245524e37e4 | 282 | * @return void |
TheChrisyd | 3:7ae3a5e53a1f | 283 | */ |
TheChrisyd | 9:7245524e37e4 | 284 | void setDataPayloadLength(uint8_t n); |
TheChrisyd | 9:7245524e37e4 | 285 | |
TheChrisyd | 3:7ae3a5e53a1f | 286 | /** Set the address of channel 1. |
TheChrisyd | 3:7ae3a5e53a1f | 287 | * The channel address is a up to 40 bit number identifying the tranceiver. |
TheChrisyd | 3:7ae3a5e53a1f | 288 | * @param addr4 Bits 39-32 of the address. |
TheChrisyd | 3:7ae3a5e53a1f | 289 | * @param addr4 Bits 31-24 of the address. |
TheChrisyd | 3:7ae3a5e53a1f | 290 | * @param addr4 Bits 23-16 of the address. |
TheChrisyd | 3:7ae3a5e53a1f | 291 | * @param addr4 Bits 15-8 of the address. |
TheChrisyd | 3:7ae3a5e53a1f | 292 | * @param addr4 Bits 7-0 of the address. |
TheChrisyd | 3:7ae3a5e53a1f | 293 | * @param n_bits Number of bits used in the address. |
TheChrisyd | 3:7ae3a5e53a1f | 294 | * @return Reference to the invoked object (for chaining operations). |
TheChrisyd | 3:7ae3a5e53a1f | 295 | */ |
TheChrisyd | 9:7245524e37e4 | 296 | bool setAddress(uint8_t addr4, uint8_t addr3, uint8_t addr2, uint8_t addr1, uint8_t addr0, uint8_t n_bits); |
TheChrisyd | 9:7245524e37e4 | 297 | |
TheChrisyd | 9:7245524e37e4 | 298 | /** Set CRC use. |
TheChrisyd | 9:7245524e37e4 | 299 | * Set the CRC mode field of the control packet. Defaults to no CRC. |
TheChrisyd | 9:7245524e37e4 | 300 | * @param mode The CRC mode of choise. |
TheChrisyd | 9:7245524e37e4 | 301 | * @return bool for correct parameter supplied |
TheChrisyd | 3:7ae3a5e53a1f | 302 | */ |
TheChrisyd | 9:7245524e37e4 | 303 | bool setCRCMode(CRC_T mode); |
TheChrisyd | 9:7245524e37e4 | 304 | |
TheChrisyd | 9:7245524e37e4 | 305 | /** Set RF power use. |
TheChrisyd | 9:7245524e37e4 | 306 | * Set the RF power field of the control packet. Defaults to full power. |
TheChrisyd | 9:7245524e37e4 | 307 | * @param power The RF power of choice. |
TheChrisyd | 9:7245524e37e4 | 308 | * @return bool for correct parameter supplied |
TheChrisyd | 3:7ae3a5e53a1f | 309 | */ |
TheChrisyd | 9:7245524e37e4 | 310 | bool setRFpower(RF_POWER_T power); |
TheChrisyd | 9:7245524e37e4 | 311 | |
TheChrisyd | 3:7ae3a5e53a1f | 312 | /** Set tranceiver data rate. |
TheChrisyd | 3:7ae3a5e53a1f | 313 | * Sets the data rate field to either 250 kbit/s or 1 Mbit/s data transfer rate. |
TheChrisyd | 9:7245524e37e4 | 314 | * Defaults to 250 kbit/s. |
TheChrisyd | 3:7ae3a5e53a1f | 315 | * @param mode The data rate of choise. |
TheChrisyd | 9:7245524e37e4 | 316 | * @return bool for correct parameter supplied |
TheChrisyd | 3:7ae3a5e53a1f | 317 | */ |
TheChrisyd | 9:7245524e37e4 | 318 | bool setDataRate(DATA_RATE_T data_rate); |
TheChrisyd | 9:7245524e37e4 | 319 | |
TheChrisyd | 0:17cb4be1e37f | 320 | |
TheChrisyd | 3:7ae3a5e53a1f | 321 | /** Set RF channel. |
TheChrisyd | 3:7ae3a5e53a1f | 322 | * Sets the control packet field for channel number. Channel numbers are from 0 to 127 |
TheChrisyd | 9:7245524e37e4 | 323 | * representing channel frequencies equal to (2400 + channel number) MHz. Defaults to channel 1. |
TheChrisyd | 3:7ae3a5e53a1f | 324 | * @param ch Channel number, from the range [0, 127]. |
TheChrisyd | 9:7245524e37e4 | 325 | * @return boolean to confirm valid parameters have been supplied |
TheChrisyd | 3:7ae3a5e53a1f | 326 | */ |
TheChrisyd | 9:7245524e37e4 | 327 | bool setChannel(uint8_t ch); |
TheChrisyd | 5:18ce8a56b248 | 328 | |
TheChrisyd | 5:18ce8a56b248 | 329 | /** Read a message. |
TheChrisyd | 5:18ce8a56b248 | 330 | * This routine will transfer the data from the receive buffer to the buffer |
TheChrisyd | 5:18ce8a56b248 | 331 | * supplied. It will transfer a number of Bytes equal to the specified length. |
TheChrisyd | 5:18ce8a56b248 | 332 | * @param msg_buf Message buffer. |
TheChrisyd | 9:7245524e37e4 | 333 | * @param msg_len Length of message, in bytes. |
TheChrisyd | 9:7245524e37e4 | 334 | * @return boolean to confirm if valid parameters have been supplied |
TheChrisyd | 5:18ce8a56b248 | 335 | */ |
TheChrisyd | 9:7245524e37e4 | 336 | bool readMsg( uint8_t *msg_buf, uint8_t msg_len ); |
TheChrisyd | 5:18ce8a56b248 | 337 | |
TheChrisyd | 5:18ce8a56b248 | 338 | /** Read a byte from message. |
TheChrisyd | 5:18ce8a56b248 | 339 | * This routine will transfer the data from the receive buffer to the buffer |
TheChrisyd | 5:18ce8a56b248 | 340 | * supplied. It will transfer one Bytes at index buf_index. |
TheChrisyd | 5:18ce8a56b248 | 341 | * @param msg_buf Message body. |
TheChrisyd | 5:18ce8a56b248 | 342 | * @param buf_index index of byte to be read. |
TheChrisyd | 9:7245524e37e4 | 343 | * @return one Byte of the message buffer |
TheChrisyd | 5:18ce8a56b248 | 344 | */ |
TheChrisyd | 9:7245524e37e4 | 345 | uint8_t readMsg_byte( uint8_t buf_index ); //uint8_t *msg_buf, |
TheChrisyd | 5:18ce8a56b248 | 346 | |
TheChrisyd | 3:7ae3a5e53a1f | 347 | /** Send a message. |
TheChrisyd | 3:7ae3a5e53a1f | 348 | * This routine will transfer the data from the supplied buffer and send |
TheChrisyd | 3:7ae3a5e53a1f | 349 | * it to the specified address using the current control packet settings. |
TheChrisyd | 3:7ae3a5e53a1f | 350 | * @param addr The address to send to. |
TheChrisyd | 3:7ae3a5e53a1f | 351 | * @param addr_len Length of address, in bits. |
TheChrisyd | 3:7ae3a5e53a1f | 352 | * @param msg_buf Message body. |
TheChrisyd | 3:7ae3a5e53a1f | 353 | * @param msg_len Length of message, in bits. |
TheChrisyd | 3:7ae3a5e53a1f | 354 | * @return Reference to the invoked object (for chaining operations). |
TheChrisyd | 3:7ae3a5e53a1f | 355 | */ |
TheChrisyd | 9:7245524e37e4 | 356 | bool sendMsg(address_t addr, uint8_t addr_len, uint8_t *msg_buf, uint8_t msg_len); |
TheChrisyd | 9:7245524e37e4 | 357 | |
TheChrisyd | 0:17cb4be1e37f | 358 | private: |
TheChrisyd | 0:17cb4be1e37f | 359 | |
TheChrisyd | 0:17cb4be1e37f | 360 | DigitalOut _ce; /**< Chip Enable pin. */ |
TheChrisyd | 0:17cb4be1e37f | 361 | DigitalOut _cs; /**< Chip select pin. */ |
TheChrisyd | 0:17cb4be1e37f | 362 | DigitalIn _dr1; /**< Data Ready 1 pin. */ |
TheChrisyd | 0:17cb4be1e37f | 363 | DigitalOut _clk1; /**< Clock 1 pin. */ |
TheChrisyd | 0:17cb4be1e37f | 364 | DigitalInOut _data; /**< Data pin. */ |
TheChrisyd | 0:17cb4be1e37f | 365 | |
TheChrisyd | 9:7245524e37e4 | 366 | |
TheChrisyd | 9:7245524e37e4 | 367 | /* |
TheChrisyd | 3:7ae3a5e53a1f | 368 | * |
TheChrisyd | 3:7ae3a5e53a1f | 369 | */ |
TheChrisyd | 0:17cb4be1e37f | 370 | typedef enum |
TheChrisyd | 0:17cb4be1e37f | 371 | { |
TheChrisyd | 9:7245524e37e4 | 372 | RX_MODE = 0x1, |
TheChrisyd | 9:7245524e37e4 | 373 | TX_MODE = 0x0 |
TheChrisyd | 9:7245524e37e4 | 374 | } TXR_T; |
TheChrisyd | 9:7245524e37e4 | 375 | |
TheChrisyd | 3:7ae3a5e53a1f | 376 | /** Contol packet data. |
TheChrisyd | 3:7ae3a5e53a1f | 377 | * |
TheChrisyd | 3:7ae3a5e53a1f | 378 | */ |
TheChrisyd | 0:17cb4be1e37f | 379 | struct nRF2401A_ctrl_packet_t |
TheChrisyd | 0:17cb4be1e37f | 380 | { |
TheChrisyd | 0:17cb4be1e37f | 381 | uint8_t channel_2_data_payload_len; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 382 | uint8_t channel_1_data_payload_len; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 383 | uint8_t channel_2_address[5]; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 384 | uint8_t channel_1_address[5]; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 385 | |
TheChrisyd | 0:17cb4be1e37f | 386 | uint8_t crc_config : 2; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 387 | uint8_t channel_address_len : 6; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 388 | |
TheChrisyd | 0:17cb4be1e37f | 389 | uint8_t rf_power : 2; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 390 | uint8_t xo_frequency : 3; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 391 | uint8_t rf_data_rate : 1; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 392 | uint8_t communication_mode : 1; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 393 | uint8_t enable_dual_channel_mode : 1; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 394 | |
TheChrisyd | 0:17cb4be1e37f | 395 | uint8_t txr_switch : 1; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 396 | uint8_t rf_channel : 7; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 397 | |
TheChrisyd | 9:7245524e37e4 | 398 | } _ctrl_packet_buf; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 399 | |
TheChrisyd | 0:17cb4be1e37f | 400 | uint8_t *_ctrl_packet; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 401 | |
TheChrisyd | 9:7245524e37e4 | 402 | uint8_t _data_buf[DATA_BUFFER_SIZE]; /**< */ |
TheChrisyd | 9:7245524e37e4 | 403 | |
TheChrisyd | 9:7245524e37e4 | 404 | STATE_T _state; |
TheChrisyd | 0:17cb4be1e37f | 405 | |
TheChrisyd | 0:17cb4be1e37f | 406 | nRF2401A_rx_handler_t _rx_handler; /**< */ |
TheChrisyd | 0:17cb4be1e37f | 407 | void *_rx_handler_arg; /**< */ |
TheChrisyd | 9:7245524e37e4 | 408 | |
TheChrisyd | 9:7245524e37e4 | 409 | |
TheChrisyd | 3:7ae3a5e53a1f | 410 | /** Receive ISR. |
TheChrisyd | 3:7ae3a5e53a1f | 411 | * This handler is attached to the rising flank of the DR1 pin. It |
TheChrisyd | 3:7ae3a5e53a1f | 412 | * will thus be called when the nRF2401A receives a packet in ShockBurst |
TheChrisyd | 3:7ae3a5e53a1f | 413 | * mode (the mode used). It will in turn call the attached handler. |
TheChrisyd | 3:7ae3a5e53a1f | 414 | */ |
TheChrisyd | 0:17cb4be1e37f | 415 | void dataReadyHandler(void); |
TheChrisyd | 9:7245524e37e4 | 416 | |
TheChrisyd | 3:7ae3a5e53a1f | 417 | /** |
TheChrisyd | 3:7ae3a5e53a1f | 418 | * |
TheChrisyd | 3:7ae3a5e53a1f | 419 | */ |
TheChrisyd | 0:17cb4be1e37f | 420 | InterruptIn _dr1_isr; |
TheChrisyd | 9:7245524e37e4 | 421 | |
TheChrisyd | 3:7ae3a5e53a1f | 422 | /** Write to the data bus. |
TheChrisyd | 3:7ae3a5e53a1f | 423 | * Write n_bits bits on the DATA line. |
TheChrisyd | 3:7ae3a5e53a1f | 424 | * @param buf Data buffer. |
TheChrisyd | 3:7ae3a5e53a1f | 425 | * @param n_bits Number of bits to transfer. |
TheChrisyd | 3:7ae3a5e53a1f | 426 | * @param is_ctrl True if the tranfered data is control word, false if data. |
TheChrisyd | 3:7ae3a5e53a1f | 427 | */ |
TheChrisyd | 0:17cb4be1e37f | 428 | void pushCtrl(uint8_t *buf, uint8_t n_bits, bool is_ctrl = true); |
TheChrisyd | 0:17cb4be1e37f | 429 | |
TheChrisyd | 3:7ae3a5e53a1f | 430 | /** Read a message from the tranceiver. |
TheChrisyd | 3:7ae3a5e53a1f | 431 | * Read until DR1 goes low. |
TheChrisyd | 3:7ae3a5e53a1f | 432 | * @param buf Data buffer. |
TheChrisyd | 3:7ae3a5e53a1f | 433 | * @return Number of bits read. |
TheChrisyd | 3:7ae3a5e53a1f | 434 | */ |
TheChrisyd | 0:17cb4be1e37f | 435 | int pull(uint8_t *buf); |
TheChrisyd | 7:22f69cf045d9 | 436 | /** Set the tranceiver into transmit mode |
TheChrisyd | 7:22f69cf045d9 | 437 | */ |
TheChrisyd | 7:22f69cf045d9 | 438 | void transmit_mode( void ); |
TheChrisyd | 7:22f69cf045d9 | 439 | /** Set the tranceiver into receive mode |
TheChrisyd | 7:22f69cf045d9 | 440 | */ |
TheChrisyd | 7:22f69cf045d9 | 441 | void receive_mode( void ); |
TheChrisyd | 2:ca8602ee9e1d | 442 | }; |
TheChrisyd | 2:ca8602ee9e1d | 443 | |
TheChrisyd | 2:ca8602ee9e1d | 444 | #endif |