BroBot Code for ESE350 Lab6 part 3 (Skeleton)
Dependencies: MPU6050_V3 mbed-rtos mbed
Fork of BroBot_RTOS_ESE350 by
MRF24J40/MRF24J40.cpp@20:a7cba632d0b1, 2017-03-22 (annotated)
- 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?
User | Revision | Line number | New 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 | #include "MRF24J40.h" |
csharer | 6:62cdb7482b50 | 24 | |
csharer | 6:62cdb7482b50 | 25 | // MRF20J40 Short address control register mapping. |
csharer | 6:62cdb7482b50 | 26 | #define RXMCR 0x00 |
csharer | 6:62cdb7482b50 | 27 | #define PANIDL 0x01 |
csharer | 6:62cdb7482b50 | 28 | #define PANIDH 0x02 |
csharer | 6:62cdb7482b50 | 29 | #define SADRL 0x03 |
csharer | 6:62cdb7482b50 | 30 | #define SADRH 0x04 |
csharer | 6:62cdb7482b50 | 31 | #define EADR0 0x05 |
csharer | 6:62cdb7482b50 | 32 | #define EADR1 0x06 |
csharer | 6:62cdb7482b50 | 33 | #define EADR2 0x07 |
csharer | 6:62cdb7482b50 | 34 | #define EADR3 0x08 |
csharer | 6:62cdb7482b50 | 35 | #define EADR4 0x09 |
csharer | 6:62cdb7482b50 | 36 | #define EADR5 0x0a |
csharer | 6:62cdb7482b50 | 37 | #define EADR6 0x0b |
csharer | 6:62cdb7482b50 | 38 | #define EADR7 0x0c |
csharer | 6:62cdb7482b50 | 39 | #define RXFLUSH 0x0d |
csharer | 6:62cdb7482b50 | 40 | |
csharer | 6:62cdb7482b50 | 41 | #define TXNMTRIG 0x1b |
csharer | 6:62cdb7482b50 | 42 | #define TXSR 0x24 |
csharer | 6:62cdb7482b50 | 43 | |
csharer | 6:62cdb7482b50 | 44 | #define ISRSTS 0x31 |
csharer | 6:62cdb7482b50 | 45 | #define INTMSK 0x32 |
csharer | 6:62cdb7482b50 | 46 | #define GPIO 0x33 |
csharer | 6:62cdb7482b50 | 47 | #define TRISGPIO 0x34 |
csharer | 6:62cdb7482b50 | 48 | |
csharer | 6:62cdb7482b50 | 49 | #define RFCTL 0x36 |
csharer | 6:62cdb7482b50 | 50 | |
csharer | 6:62cdb7482b50 | 51 | #define BBREG2 0x3A |
csharer | 6:62cdb7482b50 | 52 | |
csharer | 6:62cdb7482b50 | 53 | #define BBREG6 0x3E |
csharer | 6:62cdb7482b50 | 54 | #define RSSITHCCA 0x3F |
csharer | 6:62cdb7482b50 | 55 | |
csharer | 6:62cdb7482b50 | 56 | // MRF20J40 Long address control register mapping. |
csharer | 6:62cdb7482b50 | 57 | #define RFCTRL0 0x200 |
csharer | 6:62cdb7482b50 | 58 | |
csharer | 6:62cdb7482b50 | 59 | #define RFCTRL2 0x202 |
csharer | 6:62cdb7482b50 | 60 | #define RFCTRL3 0x203 |
csharer | 6:62cdb7482b50 | 61 | |
csharer | 6:62cdb7482b50 | 62 | #define RFCTRL6 0x206 |
csharer | 6:62cdb7482b50 | 63 | #define RFCTRL7 0x207 |
csharer | 6:62cdb7482b50 | 64 | #define RFCTRL8 0x208 |
csharer | 6:62cdb7482b50 | 65 | |
csharer | 6:62cdb7482b50 | 66 | #define CLKINTCR 0x211 |
csharer | 6:62cdb7482b50 | 67 | #define CLCCTRL 0x220 |
csharer | 6:62cdb7482b50 | 68 | |
csharer | 6:62cdb7482b50 | 69 | MRF24J40::MRF24J40(PinName mosi, PinName miso, PinName sck, PinName cs, PinName reset) ://, PinName irq, PinName wake) : |
csharer | 6:62cdb7482b50 | 70 | mSpi(mosi, miso, sck), // mosi, miso, sclk |
csharer | 6:62cdb7482b50 | 71 | mCs(cs), |
csharer | 6:62cdb7482b50 | 72 | mReset(reset) |
csharer | 6:62cdb7482b50 | 73 | // mIrq(irq), |
csharer | 6:62cdb7482b50 | 74 | // mWake(wake) |
csharer | 6:62cdb7482b50 | 75 | { |
csharer | 6:62cdb7482b50 | 76 | mSpi.format(8, 0); // 8 bits, cpol=0; cpha=0 |
csharer | 6:62cdb7482b50 | 77 | mSpi.frequency(500000); |
csharer | 6:62cdb7482b50 | 78 | Reset(); |
csharer | 6:62cdb7482b50 | 79 | } |
csharer | 6:62cdb7482b50 | 80 | |
csharer | 6:62cdb7482b50 | 81 | /* |
csharer | 6:62cdb7482b50 | 82 | void MRF24J40::DebugDump(Serial &ser) |
csharer | 6:62cdb7482b50 | 83 | { |
csharer | 6:62cdb7482b50 | 84 | ser.printf("MRF24J40 registers:\r"); |
csharer | 6:62cdb7482b50 | 85 | ser.printf("RXMCR=0x%X\r", MrfReadShort(RXMCR)); |
csharer | 6:62cdb7482b50 | 86 | ser.printf("RXFLUSH=0x%X\r", MrfReadShort(RXFLUSH)); |
csharer | 6:62cdb7482b50 | 87 | ser.printf("TXNMTRIG=0x%X\r", MrfReadShort(TXNMTRIG)); |
csharer | 6:62cdb7482b50 | 88 | ser.printf("TXSR=0x%X\r", MrfReadShort(TXSR)); |
csharer | 6:62cdb7482b50 | 89 | ser.printf("ISRSTS=0x%X\r", MrfReadShort(ISRSTS)); |
csharer | 6:62cdb7482b50 | 90 | ser.printf("INTMSK=0x%X\r", MrfReadShort(INTMSK)); |
csharer | 6:62cdb7482b50 | 91 | ser.printf("GPIO=0x%X\r", MrfReadShort(GPIO)); |
csharer | 6:62cdb7482b50 | 92 | ser.printf("TRISGPIO=0x%X\r", MrfReadShort(TRISGPIO)); |
csharer | 6:62cdb7482b50 | 93 | ser.printf("RFCTL=0x%X\r", MrfReadShort(RFCTL)); |
csharer | 6:62cdb7482b50 | 94 | ser.printf("BBREG2=0x%X\r", MrfReadShort(BBREG2)); |
csharer | 6:62cdb7482b50 | 95 | ser.printf("BBREG6=0x%X\r", MrfReadShort(BBREG6)); |
csharer | 6:62cdb7482b50 | 96 | ser.printf("RSSITHCCA=0x%X\r", MrfReadShort(RSSITHCCA)); |
csharer | 6:62cdb7482b50 | 97 | |
csharer | 6:62cdb7482b50 | 98 | |
csharer | 6:62cdb7482b50 | 99 | ser.printf("RFCTRL0=0x%X\r", MrfReadLong(RFCTRL0)); |
csharer | 6:62cdb7482b50 | 100 | ser.printf("RFCTRL2=0x%X\r", MrfReadLong(RFCTRL2)); |
csharer | 6:62cdb7482b50 | 101 | ser.printf("RFCTRL3=0x%X\r", MrfReadLong(RFCTRL3)); |
csharer | 6:62cdb7482b50 | 102 | ser.printf("RFCTRL6=0x%X\r", MrfReadLong(RFCTRL6)); |
csharer | 6:62cdb7482b50 | 103 | ser.printf("RFCTRL7=0x%X\r", MrfReadLong(RFCTRL7)); |
csharer | 6:62cdb7482b50 | 104 | ser.printf("RFCTRL8=0x%X\r", MrfReadLong(RFCTRL8)); |
csharer | 6:62cdb7482b50 | 105 | ser.printf("CLKINTCR=0x%X\r", MrfReadLong(CLKINTCR)); |
csharer | 6:62cdb7482b50 | 106 | ser.printf("CLCCTRL=0x%X\r", MrfReadLong(CLCCTRL)); |
csharer | 6:62cdb7482b50 | 107 | ser.printf("\r"); |
csharer | 6:62cdb7482b50 | 108 | } |
csharer | 6:62cdb7482b50 | 109 | */ |
csharer | 6:62cdb7482b50 | 110 | |
csharer | 6:62cdb7482b50 | 111 | void MRF24J40::Reset(void) |
csharer | 6:62cdb7482b50 | 112 | { |
csharer | 6:62cdb7482b50 | 113 | mCs = 1; |
csharer | 6:62cdb7482b50 | 114 | // Pulse hardware reset. |
csharer | 6:62cdb7482b50 | 115 | mReset = 0; |
csharer | 6:62cdb7482b50 | 116 | wait_us(100); |
csharer | 6:62cdb7482b50 | 117 | mReset = 1; |
csharer | 6:62cdb7482b50 | 118 | wait_us(100); |
csharer | 6:62cdb7482b50 | 119 | |
csharer | 6:62cdb7482b50 | 120 | // Reset RF module. |
csharer | 6:62cdb7482b50 | 121 | WriteShort(RFCTL, 0x04); |
csharer | 6:62cdb7482b50 | 122 | WriteShort(RFCTL, 0x00); |
csharer | 6:62cdb7482b50 | 123 | |
csharer | 6:62cdb7482b50 | 124 | WriteShort(RFCTL, 0x00); |
csharer | 6:62cdb7482b50 | 125 | |
csharer | 6:62cdb7482b50 | 126 | WriteShort(PANIDL, 0xAA); |
csharer | 6:62cdb7482b50 | 127 | WriteShort(PANIDH, 0xAA); |
csharer | 6:62cdb7482b50 | 128 | WriteShort(SADRL, 0xAA); |
csharer | 6:62cdb7482b50 | 129 | WriteShort(SADRH, 0xAA); |
csharer | 6:62cdb7482b50 | 130 | |
csharer | 6:62cdb7482b50 | 131 | // Flush RX fifo. |
csharer | 6:62cdb7482b50 | 132 | WriteShort(RXFLUSH, 0x01); |
csharer | 6:62cdb7482b50 | 133 | |
csharer | 6:62cdb7482b50 | 134 | // Write MAC addresses here. We don't care. |
csharer | 6:62cdb7482b50 | 135 | |
csharer | 6:62cdb7482b50 | 136 | WriteLong(RFCTRL2, 0x80); // Enable RF PLL. |
csharer | 6:62cdb7482b50 | 137 | |
csharer | 6:62cdb7482b50 | 138 | WriteLong(RFCTRL3, 0x00); // Full power. |
csharer | 6:62cdb7482b50 | 139 | WriteLong(RFCTRL6, 0x80); // Enable TX filter (recommended) |
csharer | 6:62cdb7482b50 | 140 | WriteLong(RFCTRL8, 0x10); // Enhanced VCO (recommended) |
csharer | 6:62cdb7482b50 | 141 | |
csharer | 6:62cdb7482b50 | 142 | WriteShort(BBREG2,0x78); // Clear Channel Assesment use carrier sense. |
csharer | 6:62cdb7482b50 | 143 | WriteShort(BBREG6,0x40); // Calculate RSSI for Rx packet. |
csharer | 6:62cdb7482b50 | 144 | WriteShort(RSSITHCCA,0x00);// RSSI threshold for CCA. |
csharer | 6:62cdb7482b50 | 145 | |
csharer | 6:62cdb7482b50 | 146 | WriteLong(RFCTRL0, 0x00); // Channel 11. |
csharer | 6:62cdb7482b50 | 147 | |
csharer | 6:62cdb7482b50 | 148 | WriteShort(RXMCR, 0x01); // Don't check address upon reception. |
csharer | 6:62cdb7482b50 | 149 | // MrfWriteShort(RXMCR, 0x00); // Check address upon reception. |
csharer | 6:62cdb7482b50 | 150 | |
csharer | 6:62cdb7482b50 | 151 | // Reset RF module with new settings. |
csharer | 6:62cdb7482b50 | 152 | WriteShort(RFCTL, 0x04); |
csharer | 6:62cdb7482b50 | 153 | WriteShort(RFCTL, 0x00); |
csharer | 6:62cdb7482b50 | 154 | } |
csharer | 6:62cdb7482b50 | 155 | |
csharer | 6:62cdb7482b50 | 156 | void MRF24J40::Send(uint8_t *data, uint8_t length) |
csharer | 6:62cdb7482b50 | 157 | { |
csharer | 6:62cdb7482b50 | 158 | uint8_t i; |
csharer | 6:62cdb7482b50 | 159 | |
csharer | 6:62cdb7482b50 | 160 | WriteLong(0x000, 0); // No addresses in header. |
csharer | 6:62cdb7482b50 | 161 | WriteLong(0x001, length); // 11 bytes |
csharer | 6:62cdb7482b50 | 162 | for(i=0; i<length; i++) |
csharer | 6:62cdb7482b50 | 163 | WriteLong(0x002+i, data[i]); |
csharer | 6:62cdb7482b50 | 164 | |
csharer | 6:62cdb7482b50 | 165 | WriteShort(TXNMTRIG, 0x01); |
csharer | 6:62cdb7482b50 | 166 | } |
csharer | 6:62cdb7482b50 | 167 | |
csharer | 6:62cdb7482b50 | 168 | uint8_t MRF24J40::Receive(uint8_t *data, uint8_t maxLength) |
csharer | 6:62cdb7482b50 | 169 | { |
csharer | 6:62cdb7482b50 | 170 | uint8_t i, length; |
csharer | 6:62cdb7482b50 | 171 | uint8_t lqi, rssi; |
csharer | 6:62cdb7482b50 | 172 | |
csharer | 6:62cdb7482b50 | 173 | if(ReadShort(ISRSTS)& 0x08) |
csharer | 6:62cdb7482b50 | 174 | { |
csharer | 6:62cdb7482b50 | 175 | length = ReadLong(0x300); |
csharer | 6:62cdb7482b50 | 176 | lqi = ReadLong(0x301 + length); |
csharer | 6:62cdb7482b50 | 177 | rssi = ReadLong(0x302 + length); |
csharer | 6:62cdb7482b50 | 178 | for(i=0; i<length; i++) |
csharer | 6:62cdb7482b50 | 179 | if(i<maxLength) |
csharer | 6:62cdb7482b50 | 180 | *data++ = ReadLong(0x301 + (uint16_t)i); |
csharer | 6:62cdb7482b50 | 181 | else |
csharer | 6:62cdb7482b50 | 182 | ReadLong(0x301 + (uint16_t)i); |
csharer | 6:62cdb7482b50 | 183 | if(length < maxLength) |
csharer | 6:62cdb7482b50 | 184 | return length; |
csharer | 6:62cdb7482b50 | 185 | } |
csharer | 6:62cdb7482b50 | 186 | return 0; |
csharer | 6:62cdb7482b50 | 187 | } |
csharer | 6:62cdb7482b50 | 188 | |
csharer | 6:62cdb7482b50 | 189 | uint8_t MRF24J40::ReadShort (uint8_t address) |
csharer | 6:62cdb7482b50 | 190 | { |
csharer | 6:62cdb7482b50 | 191 | uint8_t value; |
csharer | 6:62cdb7482b50 | 192 | mCs = 0; |
csharer | 6:62cdb7482b50 | 193 | wait_us(1); |
csharer | 6:62cdb7482b50 | 194 | mSpi.write((address<<1) & 0x7E); |
csharer | 6:62cdb7482b50 | 195 | wait_us(1); |
csharer | 6:62cdb7482b50 | 196 | value = mSpi.write(0xFF); |
csharer | 6:62cdb7482b50 | 197 | wait_us(1); |
csharer | 6:62cdb7482b50 | 198 | mCs = 1; |
csharer | 6:62cdb7482b50 | 199 | wait_us(1); |
csharer | 6:62cdb7482b50 | 200 | return value; |
csharer | 6:62cdb7482b50 | 201 | } |
csharer | 6:62cdb7482b50 | 202 | |
csharer | 6:62cdb7482b50 | 203 | void MRF24J40::WriteShort (uint8_t address, uint8_t data) |
csharer | 6:62cdb7482b50 | 204 | { |
csharer | 6:62cdb7482b50 | 205 | mCs = 0; |
csharer | 6:62cdb7482b50 | 206 | wait_us(1); |
csharer | 6:62cdb7482b50 | 207 | mSpi.write(((address<<1) & 0x7E) | 0x01); |
csharer | 6:62cdb7482b50 | 208 | wait_us(1); |
csharer | 6:62cdb7482b50 | 209 | mSpi.write(data); |
csharer | 6:62cdb7482b50 | 210 | wait_us(1); |
csharer | 6:62cdb7482b50 | 211 | mCs = 1; |
csharer | 6:62cdb7482b50 | 212 | wait_us(1); |
csharer | 6:62cdb7482b50 | 213 | } |
csharer | 6:62cdb7482b50 | 214 | |
csharer | 6:62cdb7482b50 | 215 | uint8_t MRF24J40::ReadLong (uint16_t address) |
csharer | 6:62cdb7482b50 | 216 | { |
csharer | 6:62cdb7482b50 | 217 | uint8_t value; |
csharer | 6:62cdb7482b50 | 218 | mCs = 0; |
csharer | 6:62cdb7482b50 | 219 | wait_us(1); |
csharer | 6:62cdb7482b50 | 220 | mSpi.write((address>>3) | 0x80); |
csharer | 6:62cdb7482b50 | 221 | wait_us(1); |
csharer | 6:62cdb7482b50 | 222 | mSpi.write((address<<5) & 0xE0); |
csharer | 6:62cdb7482b50 | 223 | wait_us(1); |
csharer | 6:62cdb7482b50 | 224 | value = mSpi.write(0xFF); |
csharer | 6:62cdb7482b50 | 225 | wait_us(1); |
csharer | 6:62cdb7482b50 | 226 | mCs = 1; |
csharer | 6:62cdb7482b50 | 227 | wait_us(1); |
csharer | 6:62cdb7482b50 | 228 | return value; |
csharer | 6:62cdb7482b50 | 229 | } |
csharer | 6:62cdb7482b50 | 230 | |
csharer | 6:62cdb7482b50 | 231 | void MRF24J40::WriteLong (uint16_t address, uint8_t data) |
csharer | 6:62cdb7482b50 | 232 | { |
csharer | 6:62cdb7482b50 | 233 | mCs = 0; |
csharer | 6:62cdb7482b50 | 234 | wait_us(1); |
csharer | 6:62cdb7482b50 | 235 | mSpi.write((address>>3) | 0x80); |
csharer | 6:62cdb7482b50 | 236 | wait_us(1); |
csharer | 6:62cdb7482b50 | 237 | mSpi.write(((address<<5) & 0xE0) | 0x10); |
csharer | 6:62cdb7482b50 | 238 | wait_us(1); |
csharer | 6:62cdb7482b50 | 239 | mSpi.write(data); |
csharer | 6:62cdb7482b50 | 240 | wait_us(1); |
csharer | 6:62cdb7482b50 | 241 | mCs = 1; |
csharer | 6:62cdb7482b50 | 242 | wait_us(1); |
csharer | 6:62cdb7482b50 | 243 | } |
csharer | 6:62cdb7482b50 | 244 | |
csharer | 6:62cdb7482b50 | 245 | void MRF24J40::SetChannel(uint8_t channel) |
csharer | 6:62cdb7482b50 | 246 | { |
csharer | 6:62cdb7482b50 | 247 | WriteLong(RFCTRL0, (channel & 0x0F)<<4 | 0x03); // Set channel, leave RFOPT bits at recommended |
csharer | 6:62cdb7482b50 | 248 | |
csharer | 6:62cdb7482b50 | 249 | //Reset the board by first writing a 4 to RFCTL, then writing a 0 |
csharer | 6:62cdb7482b50 | 250 | WriteShort(RFCTL, 0x04); |
csharer | 6:62cdb7482b50 | 251 | WriteShort(RFCTL, 0x00); |
csharer | 6:62cdb7482b50 | 252 | wait(0.5); |
csharer | 6:62cdb7482b50 | 253 | } |