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