ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Thu Apr 07 02:07:33 2016 +0000
Revision:
14:64b06476d943
msoch update - compiles!

Who changed what in which revision?

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