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