BroBot Code for ESE350 Lab6 part 3 (Skeleton)

Dependencies:   MPU6050_V3 mbed-rtos mbed

Fork of BroBot_RTOS_ESE350 by Carter Sharer

Committer:
csharer
Date:
Wed Mar 22 21:59:10 2017 +0000
Revision:
20:a7cba632d0b1
Parent:
6:62cdb7482b50
Final Skeleton code for ESE350

Who changed what in which revision?

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