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