![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
f
Dependencies: mbed
Fork of ESE350-Whack-a-Mole1 by
MRF24J40/MRF24J40.h@2:20e1d264bc3f, 2015-10-22 (annotated)
- Committer:
- srijani987
- Date:
- Thu Oct 22 23:09:41 2015 +0000
- Revision:
- 2:20e1d264bc3f
- Parent:
- 0:ddc820578cb0
aaa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mlab4 | 0:ddc820578cb0 | 1 | /* mbed MRF24J40 (IEEE 802.15.4 tranceiver) Library |
mlab4 | 0:ddc820578cb0 | 2 | * Copyright (c) 2011 Jeroen Hilgers |
mlab4 | 0:ddc820578cb0 | 3 | * |
mlab4 | 0:ddc820578cb0 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mlab4 | 0:ddc820578cb0 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mlab4 | 0:ddc820578cb0 | 6 | * in the Software without restriction, including without limitation the rights |
mlab4 | 0:ddc820578cb0 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mlab4 | 0:ddc820578cb0 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mlab4 | 0:ddc820578cb0 | 9 | * furnished to do so, subject to the following conditions: |
mlab4 | 0:ddc820578cb0 | 10 | * |
mlab4 | 0:ddc820578cb0 | 11 | * The above copyright notice and this permission notice shall be included in |
mlab4 | 0:ddc820578cb0 | 12 | * all copies or substantial portions of the Software. |
mlab4 | 0:ddc820578cb0 | 13 | * |
mlab4 | 0:ddc820578cb0 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mlab4 | 0:ddc820578cb0 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mlab4 | 0:ddc820578cb0 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mlab4 | 0:ddc820578cb0 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mlab4 | 0:ddc820578cb0 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mlab4 | 0:ddc820578cb0 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mlab4 | 0:ddc820578cb0 | 20 | * THE SOFTWARE. |
mlab4 | 0:ddc820578cb0 | 21 | */ |
mlab4 | 0:ddc820578cb0 | 22 | |
mlab4 | 0:ddc820578cb0 | 23 | #ifndef MRF24J40_H |
mlab4 | 0:ddc820578cb0 | 24 | #define MRF25J40_H |
mlab4 | 0:ddc820578cb0 | 25 | |
mlab4 | 0:ddc820578cb0 | 26 | #include "mbed.h" |
mlab4 | 0:ddc820578cb0 | 27 | |
mlab4 | 0:ddc820578cb0 | 28 | /** MRF24J40 class. Provides a simple send/receive API for a microchip |
mlab4 | 0:ddc820578cb0 | 29 | ** MFR24J40 IEEE 802.15.4 tranceiver. The tranceiver is available on a |
mlab4 | 0:ddc820578cb0 | 30 | ** module that can easilly be soldered to some header pins to use it with |
mlab4 | 0:ddc820578cb0 | 31 | ** an mbed on a breadboard. The module is called 'MRF24J40MA' and can be |
mlab4 | 0:ddc820578cb0 | 32 | ** ordered for example by www.farnell.com. |
mlab4 | 0:ddc820578cb0 | 33 | * |
mlab4 | 0:ddc820578cb0 | 34 | * Example: |
mlab4 | 0:ddc820578cb0 | 35 | * @code |
mlab4 | 0:ddc820578cb0 | 36 | * #include "mbed.h" |
mlab4 | 0:ddc820578cb0 | 37 | * #include "MRF24J40.h" |
mlab4 | 0:ddc820578cb0 | 38 | * |
mlab4 | 0:ddc820578cb0 | 39 | * // RF tranceiver to link with handheld. |
mlab4 | 0:ddc820578cb0 | 40 | * MRF24J40 mrf(p11, p12, p13, p14, p21); |
mlab4 | 0:ddc820578cb0 | 41 | * |
mlab4 | 0:ddc820578cb0 | 42 | * // LEDs |
mlab4 | 0:ddc820578cb0 | 43 | * DigitalOut led1(LED1); |
mlab4 | 0:ddc820578cb0 | 44 | * DigitalOut led2(LED2); |
mlab4 | 0:ddc820578cb0 | 45 | * DigitalOut led3(LED3); |
mlab4 | 0:ddc820578cb0 | 46 | * DigitalOut led4(LED4); |
mlab4 | 0:ddc820578cb0 | 47 | * |
mlab4 | 0:ddc820578cb0 | 48 | * // Timer. |
mlab4 | 0:ddc820578cb0 | 49 | * Timer timer; |
mlab4 | 0:ddc820578cb0 | 50 | * |
mlab4 | 0:ddc820578cb0 | 51 | * // Serial port for showing RX data. |
mlab4 | 0:ddc820578cb0 | 52 | * Serial pc(USBTX, USBRX); |
mlab4 | 0:ddc820578cb0 | 53 | * |
mlab4 | 0:ddc820578cb0 | 54 | * // Send / receive buffers. |
mlab4 | 0:ddc820578cb0 | 55 | * // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends |
mlab4 | 0:ddc820578cb0 | 56 | * // to reject data that doesn't have the right header. So the first |
mlab4 | 0:ddc820578cb0 | 57 | * // 8 bytes in txBuffer look like a valid header. The remaining 120 |
mlab4 | 0:ddc820578cb0 | 58 | * // bytes can be used for anything you like. |
mlab4 | 0:ddc820578cb0 | 59 | * uint8_t txBuffer[128]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
mlab4 | 0:ddc820578cb0 | 60 | * |
mlab4 | 0:ddc820578cb0 | 61 | * uint8_t rxBuffer[128]; |
mlab4 | 0:ddc820578cb0 | 62 | * uint8_t rxLen; |
mlab4 | 0:ddc820578cb0 | 63 | * |
mlab4 | 0:ddc820578cb0 | 64 | * int main (void) |
mlab4 | 0:ddc820578cb0 | 65 | * { |
mlab4 | 0:ddc820578cb0 | 66 | * uint8_t count = 0; |
mlab4 | 0:ddc820578cb0 | 67 | * pc.baud(115200); |
mlab4 | 0:ddc820578cb0 | 68 | * timer.start(); |
mlab4 | 0:ddc820578cb0 | 69 | * while(1) |
mlab4 | 0:ddc820578cb0 | 70 | * { |
mlab4 | 0:ddc820578cb0 | 71 | * // Check if any data was received. |
mlab4 | 0:ddc820578cb0 | 72 | * rxLen = mrf.Receive(rxBuffer, 128); |
mlab4 | 0:ddc820578cb0 | 73 | * if(rxLen) |
mlab4 | 0:ddc820578cb0 | 74 | * { |
mlab4 | 0:ddc820578cb0 | 75 | * // Toggle LED 1 upon each reception of data. |
mlab4 | 0:ddc820578cb0 | 76 | * led1 = led1^1; |
mlab4 | 0:ddc820578cb0 | 77 | * // Send to serial. |
mlab4 | 0:ddc820578cb0 | 78 | * // IMPORTANT: The last two bytes of the received data |
mlab4 | 0:ddc820578cb0 | 79 | * // are the checksum used in the transmission. |
mlab4 | 0:ddc820578cb0 | 80 | * for(uint8_t i=0; i<rxLen; i++) |
mlab4 | 0:ddc820578cb0 | 81 | * { |
mlab4 | 0:ddc820578cb0 | 82 | * pc.printf("0x%02X ", rxBuffer[i]); |
mlab4 | 0:ddc820578cb0 | 83 | * } |
mlab4 | 0:ddc820578cb0 | 84 | * pc.printf("\r\n"); |
mlab4 | 0:ddc820578cb0 | 85 | * } |
mlab4 | 0:ddc820578cb0 | 86 | * |
mlab4 | 0:ddc820578cb0 | 87 | * // Each second, send some data. |
mlab4 | 0:ddc820578cb0 | 88 | * if(timer.read_ms() >= 1000) |
mlab4 | 0:ddc820578cb0 | 89 | * { |
mlab4 | 0:ddc820578cb0 | 90 | * timer.reset(); |
mlab4 | 0:ddc820578cb0 | 91 | * // Toggle LED 2. |
mlab4 | 0:ddc820578cb0 | 92 | * led2 = led2^1; |
mlab4 | 0:ddc820578cb0 | 93 | * |
mlab4 | 0:ddc820578cb0 | 94 | * // UART. |
mlab4 | 0:ddc820578cb0 | 95 | * pc.printf("TXD\r\n"); |
mlab4 | 0:ddc820578cb0 | 96 | * |
mlab4 | 0:ddc820578cb0 | 97 | * // Send counter value. |
mlab4 | 0:ddc820578cb0 | 98 | * count++; |
mlab4 | 0:ddc820578cb0 | 99 | * txBuffer[8] = count; |
mlab4 | 0:ddc820578cb0 | 100 | * mrf.Send(txBuffer, 9); |
mlab4 | 0:ddc820578cb0 | 101 | * } |
mlab4 | 0:ddc820578cb0 | 102 | * } |
mlab4 | 0:ddc820578cb0 | 103 | * } |
mlab4 | 0:ddc820578cb0 | 104 | * @endcode |
mlab4 | 0:ddc820578cb0 | 105 | */ |
mlab4 | 0:ddc820578cb0 | 106 | |
mlab4 | 0:ddc820578cb0 | 107 | |
mlab4 | 0:ddc820578cb0 | 108 | class MRF24J40 |
mlab4 | 0:ddc820578cb0 | 109 | { |
mlab4 | 0:ddc820578cb0 | 110 | public: |
mlab4 | 0:ddc820578cb0 | 111 | /** Create a MRF24J40 object and initizalize it. |
mlab4 | 0:ddc820578cb0 | 112 | * |
mlab4 | 0:ddc820578cb0 | 113 | * @param pin mosi Spi MOSI pin connected to MRF's SDI. |
mlab4 | 0:ddc820578cb0 | 114 | * @param pin miso Spi MISO pin connected to MRF's SDO. |
mlab4 | 0:ddc820578cb0 | 115 | * @param pin sck Spi SCK pin connected to MRF's SCK. |
mlab4 | 0:ddc820578cb0 | 116 | * @param pin cs Pin connected to MRF's #CS. |
mlab4 | 0:ddc820578cb0 | 117 | * @param pin reset Pin connected to MRF's #Reset. |
mlab4 | 0:ddc820578cb0 | 118 | */ |
mlab4 | 0:ddc820578cb0 | 119 | MRF24J40(PinName mosi, PinName miso, PinName sck, PinName cs, PinName reset);//, PinName irq, PinName wake); |
mlab4 | 0:ddc820578cb0 | 120 | |
mlab4 | 0:ddc820578cb0 | 121 | /** Reset the MRF24J40 and initialize it. |
mlab4 | 0:ddc820578cb0 | 122 | */ |
mlab4 | 0:ddc820578cb0 | 123 | void Reset(void); // Reset chip and configure it. |
mlab4 | 0:ddc820578cb0 | 124 | |
mlab4 | 0:ddc820578cb0 | 125 | /** Send data. |
mlab4 | 0:ddc820578cb0 | 126 | * |
mlab4 | 0:ddc820578cb0 | 127 | * Note that the MRF24J40 only handles data with a valid IEEE 802.15.4 |
mlab4 | 0:ddc820578cb0 | 128 | * header. See the example how to get around this. |
mlab4 | 0:ddc820578cb0 | 129 | * |
mlab4 | 0:ddc820578cb0 | 130 | * @param data Pointer to data to be send. |
mlab4 | 0:ddc820578cb0 | 131 | * @param length Length of the data to be send in bytes. |
mlab4 | 0:ddc820578cb0 | 132 | */ |
mlab4 | 0:ddc820578cb0 | 133 | void Send(uint8_t *data, uint8_t length); // Send data. |
mlab4 | 0:ddc820578cb0 | 134 | |
mlab4 | 0:ddc820578cb0 | 135 | /** Check if any data was received. |
mlab4 | 0:ddc820578cb0 | 136 | * |
mlab4 | 0:ddc820578cb0 | 137 | * Note that the MRF24J40 appends two bytes of CRC for each packet. |
mlab4 | 0:ddc820578cb0 | 138 | * So you will receive two bytes more than were send with the 'Send' function. |
mlab4 | 0:ddc820578cb0 | 139 | * |
mlab4 | 0:ddc820578cb0 | 140 | * @param data Pointer to buffer where received data can be placed. |
mlab4 | 0:ddc820578cb0 | 141 | * @param maxLength Maximum amount of data to be placed in the buffer. |
mlab4 | 0:ddc820578cb0 | 142 | * @param returns The number of bytes written into the buffer. |
mlab4 | 0:ddc820578cb0 | 143 | */ |
mlab4 | 0:ddc820578cb0 | 144 | uint8_t Receive(uint8_t *data, uint8_t maxLength); // Receive data if ready. |
mlab4 | 0:ddc820578cb0 | 145 | |
mlab4 | 0:ddc820578cb0 | 146 | /** Sets the channel of the MRF24J40 |
mlab4 | 0:ddc820578cb0 | 147 | * |
mlab4 | 0:ddc820578cb0 | 148 | * @param channel A number between 0-15 (0=2405MHz 15=2480MHz) |
mlab4 | 0:ddc820578cb0 | 149 | */ |
mlab4 | 0:ddc820578cb0 | 150 | void SetChannel(uint8_t channel); |
mlab4 | 0:ddc820578cb0 | 151 | |
mlab4 | 0:ddc820578cb0 | 152 | // void DebugDump(Serial &ser); |
mlab4 | 0:ddc820578cb0 | 153 | |
mlab4 | 0:ddc820578cb0 | 154 | private: |
mlab4 | 0:ddc820578cb0 | 155 | SPI mSpi; |
mlab4 | 0:ddc820578cb0 | 156 | DigitalOut mCs; |
mlab4 | 0:ddc820578cb0 | 157 | DigitalOut mReset; |
mlab4 | 0:ddc820578cb0 | 158 | // DigitalIn mIrq; |
mlab4 | 0:ddc820578cb0 | 159 | // DigitalIn mWake; |
mlab4 | 0:ddc820578cb0 | 160 | |
mlab4 | 0:ddc820578cb0 | 161 | uint8_t ReadShort (uint8_t address); |
mlab4 | 0:ddc820578cb0 | 162 | void WriteShort (uint8_t address, uint8_t data); |
mlab4 | 0:ddc820578cb0 | 163 | uint8_t ReadLong (uint16_t address); |
mlab4 | 0:ddc820578cb0 | 164 | void WriteLong (uint16_t address, uint8_t data); |
mlab4 | 0:ddc820578cb0 | 165 | }; |
mlab4 | 0:ddc820578cb0 | 166 | |
mlab4 | 0:ddc820578cb0 | 167 | #endif |