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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MRF24J40.h Source File

MRF24J40.h

00001 /* mbed MRF24J40 (IEEE 802.15.4 tranceiver) Library
00002  * Copyright (c) 2011 Jeroen Hilgers
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022   
00023 #ifndef MRF24J40_H
00024 #define MRF25J40_H
00025 
00026 #include "mbed.h"
00027 
00028 /** MRF24J40 class. Provides a simple send/receive API for a microchip
00029  ** MFR24J40 IEEE 802.15.4 tranceiver. The tranceiver is available on a 
00030  ** module that can easilly be soldered to some header pins to use it with
00031  ** an mbed on a breadboard. The module is called 'MRF24J40MA' and can be 
00032  ** ordered for example by www.farnell.com.
00033  *
00034  * Example:
00035  * @code
00036  * #include "mbed.h"
00037  * #include "MRF24J40.h"
00038  * 
00039  * // RF tranceiver to link with handheld.
00040  * MRF24J40 mrf(p11, p12, p13, p14, p21);
00041  * 
00042  * // LEDs
00043  * DigitalOut led1(LED1);
00044  * DigitalOut led2(LED2);
00045  * DigitalOut led3(LED3);
00046  * DigitalOut led4(LED4);
00047  * 
00048  * // Timer.
00049  * Timer timer;
00050  * 
00051  * // Serial port for showing RX data.
00052  * Serial pc(USBTX, USBRX); 
00053  * 
00054  * // Send / receive buffers.
00055  * // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends
00056  * // to reject data that doesn't have the right header. So the first 
00057  * // 8 bytes in txBuffer look like a valid header. The remaining 120
00058  * // bytes can be used for anything you like.
00059  * uint8_t txBuffer[128]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
00060  * 
00061  * uint8_t rxBuffer[128];
00062  * uint8_t rxLen;
00063  * 
00064  * int main (void)
00065  * {
00066  *     uint8_t count = 0;
00067  *     pc.baud(115200);
00068  *     timer.start();
00069  *     while(1)
00070  *     {
00071  *         // Check if any data was received. 
00072  *         rxLen = mrf.Receive(rxBuffer, 128);
00073  *         if(rxLen) 
00074  *         {
00075  *             // Toggle LED 1 upon each reception of data.
00076  *             led1 = led1^1;
00077  *             // Send to serial.
00078  *             // IMPORTANT: The last two bytes of the received data
00079  *             // are the checksum used in the transmission. 
00080  *             for(uint8_t i=0; i<rxLen; i++)
00081  *             {
00082  *                 pc.printf("0x%02X ", rxBuffer[i]);
00083  *             }
00084  *             pc.printf("\r\n");
00085  *         }
00086  *         
00087  *         // Each second, send some data.
00088  *         if(timer.read_ms() >= 1000) 
00089  *         {
00090  *             timer.reset();
00091  *             // Toggle LED 2.
00092  *             led2 = led2^1;
00093  *             
00094  *             // UART.
00095  *             pc.printf("TXD\r\n");
00096  *             
00097  *             // Send counter value.
00098  *             count++;
00099  *             txBuffer[8] = count;
00100  *             mrf.Send(txBuffer, 9);
00101  *         }
00102  *     }
00103  * }
00104  * @endcode
00105  */
00106 
00107 
00108 class MRF24J40
00109 {
00110   public:
00111     /** Create a MRF24J40 object and initizalize it.
00112      *
00113      * @param pin mosi Spi MOSI pin connected to MRF's SDI.
00114      * @param pin miso Spi MISO pin connected to MRF's SDO.
00115      * @param pin sck  Spi SCK pin connected to MRF's SCK.
00116      * @param pin cs Pin connected to MRF's #CS.
00117      * @param pin reset Pin connected to MRF's #Reset.
00118      */
00119     MRF24J40(PinName mosi, PinName miso, PinName sck, PinName cs, PinName reset);//, PinName irq, PinName wake);
00120     
00121     /** Reset the MRF24J40 and initialize it.
00122      */
00123     void Reset(void);                             // Reset chip and configure it.
00124     
00125     /** Send data.
00126      *
00127      * Note that the MRF24J40 only handles data with a valid IEEE 802.15.4
00128      * header. See the example how to get around this.
00129      *
00130      * @param data Pointer to data to be send.
00131      * @param length Length of the data to be send in bytes.
00132      */
00133     void Send(uint8_t *data, uint8_t length);         // Send data.
00134 
00135     /** Check if any data was received.
00136      *
00137      * Note that the MRF24J40 appends two bytes of CRC for each packet.
00138      * So you will receive two bytes more than were send with the 'Send' function.
00139      *
00140      * @param data Pointer to buffer where received data can be placed.
00141      * @param maxLength Maximum amount of data to be placed in the buffer.
00142      * @param returns The number of bytes written into the buffer.
00143      */
00144     uint8_t Receive(uint8_t *data, uint8_t maxLength);  // Receive data if ready.
00145     
00146     // void DebugDump(Serial &ser);
00147 
00148   private:
00149     SPI mSpi;
00150     DigitalOut mCs;
00151     DigitalOut mReset;
00152     // DigitalIn mIrq;
00153     // DigitalIn mWake;
00154 
00155     uint8_t ReadShort (uint8_t address);
00156     void WriteShort (uint8_t address, uint8_t data);
00157     uint8_t ReadLong (uint16_t address);
00158     void WriteLong (uint16_t address, uint8_t data);
00159 };
00160 
00161 #endif