Final Project Starter Code

Dependencies:   mbed

Fork of ESE519_Lab6_part3_skeleton by Carter Sharer

Committer:
csharer
Date:
Thu Nov 10 19:20:55 2016 +0000
Revision:
6:ae3e6aefe908
ese519 lab6 part 3;

Who changed what in which revision?

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