Part 1 + 3

Dependencies:   Joystick_skeleton_Pengineers mbed

Fork of ESE519_Lab6_part1_skeleton by Carter Sharer

Committer:
csharer
Date:
Thu Oct 20 22:16:49 2016 +0000
Revision:
0:0ebe6f55caee
This is the complete implementation for lab6 part 1 controller

Who changed what in which revision?

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