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