This library lets you connect an MRF24J40 tranceiver to your mbed. The MRF24J40 is intended for use as a zigbee tranceiver. However, it can also be used to simply send data from one tranceiver to another. The tranceiver is also available as a module on a small PCB with antenna etc. It requires no other components and can be connected to the mbed using 5 pins.

Dependents:   mrf24jclient_vest1

Committer:
hilgo
Date:
Wed Feb 16 07:53:48 2011 +0000
Revision:
1:55d2672c4708
Parent:
0:0630ffe718d3
Fixed @code tag in documentation.

Who changed what in which revision?

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