multihop protocol
Dependencies: mbed
Revision 8:fd217a8f8658, committed 2015-11-17
- Comitter:
- mfrede
- Date:
- Tue Nov 17 19:25:53 2015 +0000
- Parent:
- 7:67b40640ddb6
- Commit message:
- add rssi
Changed in this revision
MRF24J40/MRF24J40.cpp | Show annotated file Show diff for this revision Revisions of this file |
MRF24J40/MRF24J40.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MRF24J40/MRF24J40.cpp Tue Nov 17 19:25:53 2015 +0000 @@ -0,0 +1,253 @@ +/* mbed MRF24J40 (IEEE 802.15.4 tranceiver) Library + * Copyright (c) 2011 Jeroen Hilgers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "MRF24J40.h" + +// MRF20J40 Short address control register mapping. +#define RXMCR 0x00 +#define PANIDL 0x01 +#define PANIDH 0x02 +#define SADRL 0x03 +#define SADRH 0x04 +#define EADR0 0x05 +#define EADR1 0x06 +#define EADR2 0x07 +#define EADR3 0x08 +#define EADR4 0x09 +#define EADR5 0x0a +#define EADR6 0x0b +#define EADR7 0x0c +#define RXFLUSH 0x0d + +#define TXNMTRIG 0x1b +#define TXSR 0x24 + +#define ISRSTS 0x31 +#define INTMSK 0x32 +#define GPIO 0x33 +#define TRISGPIO 0x34 + +#define RFCTL 0x36 + +#define BBREG2 0x3A + +#define BBREG6 0x3E +#define RSSITHCCA 0x3F + +// MRF20J40 Long address control register mapping. +#define RFCTRL0 0x200 + +#define RFCTRL2 0x202 +#define RFCTRL3 0x203 + +#define RFCTRL6 0x206 +#define RFCTRL7 0x207 +#define RFCTRL8 0x208 + +#define CLKINTCR 0x211 +#define CLCCTRL 0x220 + +MRF24J40::MRF24J40(PinName mosi, PinName miso, PinName sck, PinName cs, PinName reset) ://, PinName irq, PinName wake) : + mSpi(mosi, miso, sck), // mosi, miso, sclk + mCs(cs), + mReset(reset) +// mIrq(irq), +// mWake(wake) +{ + mSpi.format(8, 0); // 8 bits, cpol=0; cpha=0 + mSpi.frequency(500000); + Reset(); +} + +/* +void MRF24J40::DebugDump(Serial &ser) +{ + ser.printf("MRF24J40 registers:\r"); + ser.printf("RXMCR=0x%X\r", MrfReadShort(RXMCR)); + ser.printf("RXFLUSH=0x%X\r", MrfReadShort(RXFLUSH)); + ser.printf("TXNMTRIG=0x%X\r", MrfReadShort(TXNMTRIG)); + ser.printf("TXSR=0x%X\r", MrfReadShort(TXSR)); + ser.printf("ISRSTS=0x%X\r", MrfReadShort(ISRSTS)); + ser.printf("INTMSK=0x%X\r", MrfReadShort(INTMSK)); + ser.printf("GPIO=0x%X\r", MrfReadShort(GPIO)); + ser.printf("TRISGPIO=0x%X\r", MrfReadShort(TRISGPIO)); + ser.printf("RFCTL=0x%X\r", MrfReadShort(RFCTL)); + ser.printf("BBREG2=0x%X\r", MrfReadShort(BBREG2)); + ser.printf("BBREG6=0x%X\r", MrfReadShort(BBREG6)); + ser.printf("RSSITHCCA=0x%X\r", MrfReadShort(RSSITHCCA)); + + + ser.printf("RFCTRL0=0x%X\r", MrfReadLong(RFCTRL0)); + ser.printf("RFCTRL2=0x%X\r", MrfReadLong(RFCTRL2)); + ser.printf("RFCTRL3=0x%X\r", MrfReadLong(RFCTRL3)); + ser.printf("RFCTRL6=0x%X\r", MrfReadLong(RFCTRL6)); + ser.printf("RFCTRL7=0x%X\r", MrfReadLong(RFCTRL7)); + ser.printf("RFCTRL8=0x%X\r", MrfReadLong(RFCTRL8)); + ser.printf("CLKINTCR=0x%X\r", MrfReadLong(CLKINTCR)); + ser.printf("CLCCTRL=0x%X\r", MrfReadLong(CLCCTRL)); + ser.printf("\r"); +} +*/ + +void MRF24J40::Reset(void) +{ + mCs = 1; + // Pulse hardware reset. + mReset = 0; + wait_us(100); + mReset = 1; + wait_us(100); + + // Reset RF module. + WriteShort(RFCTL, 0x04); + WriteShort(RFCTL, 0x00); + + WriteShort(RFCTL, 0x00); + + WriteShort(PANIDL, 0xAA); + WriteShort(PANIDH, 0xAA); + WriteShort(SADRL, 0xAA); + WriteShort(SADRH, 0xAA); + + // Flush RX fifo. + WriteShort(RXFLUSH, 0x01); + + // Write MAC addresses here. We don't care. + + WriteLong(RFCTRL2, 0x80); // Enable RF PLL. + + WriteLong(RFCTRL3, 0x00); // Full power. + WriteLong(RFCTRL6, 0x80); // Enable TX filter (recommended) + WriteLong(RFCTRL8, 0x10); // Enhanced VCO (recommended) + + WriteShort(BBREG2,0x78); // Clear Channel Assesment use carrier sense. + WriteShort(BBREG6,0x40); // Calculate RSSI for Rx packet. + WriteShort(RSSITHCCA,0x00);// RSSI threshold for CCA. + + WriteLong(RFCTRL0, 0x00); // Channel 11. + + WriteShort(RXMCR, 0x01); // Don't check address upon reception. +// MrfWriteShort(RXMCR, 0x00); // Check address upon reception. + + // Reset RF module with new settings. + WriteShort(RFCTL, 0x04); + WriteShort(RFCTL, 0x00); +} + +void MRF24J40::Send(uint8_t *data, uint8_t length) +{ + uint8_t i; + + WriteLong(0x000, 0); // No addresses in header. + WriteLong(0x001, length); // 11 bytes + for(i=0; i<length; i++) + WriteLong(0x002+i, data[i]); + + WriteShort(TXNMTRIG, 0x01); +} + +uint8_t MRF24J40::Receive(uint8_t *data, uint8_t maxLength, uint8_t *rssi) +{ + uint8_t i, length; + uint8_t lqi; + + if(ReadShort(ISRSTS)& 0x08) + { + length = ReadLong(0x300); + lqi = ReadLong(0x301 + length); + *rssi = ReadLong(0x302 + length); + for(i=0; i<length; i++) + if(i<maxLength) + *data++ = ReadLong(0x301 + (uint16_t)i); + else + ReadLong(0x301 + (uint16_t)i); + if(length < maxLength) + return length; + } + return 0; +} + +uint8_t MRF24J40::ReadShort (uint8_t address) +{ + uint8_t value; + mCs = 0; + wait_us(1); + mSpi.write((address<<1) & 0x7E); + wait_us(1); + value = mSpi.write(0xFF); + wait_us(1); + mCs = 1; + wait_us(1); + return value; +} + +void MRF24J40::WriteShort (uint8_t address, uint8_t data) +{ + mCs = 0; + wait_us(1); + mSpi.write(((address<<1) & 0x7E) | 0x01); + wait_us(1); + mSpi.write(data); + wait_us(1); + mCs = 1; + wait_us(1); +} + +uint8_t MRF24J40::ReadLong (uint16_t address) +{ + uint8_t value; + mCs = 0; + wait_us(1); + mSpi.write((address>>3) | 0x80); + wait_us(1); + mSpi.write((address<<5) & 0xE0); + wait_us(1); + value = mSpi.write(0xFF); + wait_us(1); + mCs = 1; + wait_us(1); + return value; +} + +void MRF24J40::WriteLong (uint16_t address, uint8_t data) +{ + mCs = 0; + wait_us(1); + mSpi.write((address>>3) | 0x80); + wait_us(1); + mSpi.write(((address<<5) & 0xE0) | 0x10); + wait_us(1); + mSpi.write(data); + wait_us(1); + mCs = 1; + wait_us(1); +} + +void MRF24J40::SetChannel(uint8_t channel) +{ + WriteLong(RFCTRL0, (channel & 0x0F)<<4 | 0x03); // Set channel, leave RFOPT bits at recommended + + //Reset the board by first writing a 4 to RFCTL, then writing a 0 + WriteShort(RFCTL, 0x04); + WriteShort(RFCTL, 0x00); + wait(0.5); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MRF24J40/MRF24J40.h Tue Nov 17 19:25:53 2015 +0000 @@ -0,0 +1,167 @@ +/* mbed MRF24J40 (IEEE 802.15.4 tranceiver) Library + * Copyright (c) 2011 Jeroen Hilgers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MRF24J40_H +#define MRF25J40_H + +#include "mbed.h" + +/** MRF24J40 class. Provides a simple send/receive API for a microchip + ** MFR24J40 IEEE 802.15.4 tranceiver. The tranceiver is available on a + ** module that can easilly be soldered to some header pins to use it with + ** an mbed on a breadboard. The module is called 'MRF24J40MA' and can be + ** ordered for example by www.farnell.com. + * + * Example: + * @code + * #include "mbed.h" + * #include "MRF24J40.h" + * + * // RF tranceiver to link with handheld. + * MRF24J40 mrf(p11, p12, p13, p14, p21); + * + * // LEDs + * DigitalOut led1(LED1); + * DigitalOut led2(LED2); + * DigitalOut led3(LED3); + * DigitalOut led4(LED4); + * + * // Timer. + * Timer timer; + * + * // Serial port for showing RX data. + * Serial pc(USBTX, USBRX); + * + * // Send / receive buffers. + * // IMPORTANT: The MRF24J40 is intended as zigbee tranceiver; it tends + * // to reject data that doesn't have the right header. So the first + * // 8 bytes in txBuffer look like a valid header. The remaining 120 + * // bytes can be used for anything you like. + * uint8_t txBuffer[128]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; + * + * uint8_t rxBuffer[128]; + * uint8_t rxLen; + * + * int main (void) + * { + * uint8_t count = 0; + * pc.baud(115200); + * timer.start(); + * while(1) + * { + * // Check if any data was received. + * rxLen = mrf.Receive(rxBuffer, 128); + * if(rxLen) + * { + * // Toggle LED 1 upon each reception of data. + * led1 = led1^1; + * // Send to serial. + * // IMPORTANT: The last two bytes of the received data + * // are the checksum used in the transmission. + * for(uint8_t i=0; i<rxLen; i++) + * { + * pc.printf("0x%02X ", rxBuffer[i]); + * } + * pc.printf("\r\n"); + * } + * + * // Each second, send some data. + * if(timer.read_ms() >= 1000) + * { + * timer.reset(); + * // Toggle LED 2. + * led2 = led2^1; + * + * // UART. + * pc.printf("TXD\r\n"); + * + * // Send counter value. + * count++; + * txBuffer[8] = count; + * mrf.Send(txBuffer, 9); + * } + * } + * } + * @endcode + */ + + +class MRF24J40 +{ + public: + /** Create a MRF24J40 object and initizalize it. + * + * @param pin mosi Spi MOSI pin connected to MRF's SDI. + * @param pin miso Spi MISO pin connected to MRF's SDO. + * @param pin sck Spi SCK pin connected to MRF's SCK. + * @param pin cs Pin connected to MRF's #CS. + * @param pin reset Pin connected to MRF's #Reset. + */ + MRF24J40(PinName mosi, PinName miso, PinName sck, PinName cs, PinName reset);//, PinName irq, PinName wake); + + /** Reset the MRF24J40 and initialize it. + */ + void Reset(void); // Reset chip and configure it. + + /** Send data. + * + * Note that the MRF24J40 only handles data with a valid IEEE 802.15.4 + * header. See the example how to get around this. + * + * @param data Pointer to data to be send. + * @param length Length of the data to be send in bytes. + */ + void Send(uint8_t *data, uint8_t length); // Send data. + + /** Check if any data was received. + * + * Note that the MRF24J40 appends two bytes of CRC for each packet. + * So you will receive two bytes more than were send with the 'Send' function. + * + * @param data Pointer to buffer where received data can be placed. + * @param maxLength Maximum amount of data to be placed in the buffer. + * @param returns The number of bytes written into the buffer. + */ + uint8_t Receive(uint8_t *data, uint8_t maxLength, uint8_t *rssi); // Receive data if ready. + + /** Sets the channel of the MRF24J40 + * + * @param channel A number between 0-15 (0=2405MHz 15=2480MHz) + */ + void SetChannel(uint8_t channel); + + // void DebugDump(Serial &ser); + + private: + SPI mSpi; + DigitalOut mCs; + DigitalOut mReset; + // DigitalIn mIrq; + // DigitalIn mWake; + + uint8_t ReadShort (uint8_t address); + void WriteShort (uint8_t address, uint8_t data); + uint8_t ReadLong (uint16_t address); + void WriteLong (uint16_t address, uint8_t data); +}; + +#endif \ No newline at end of file