RFM12B radio module driver library

Dependents:   IoTGateway_Basic

Committer:
SomeRandomBloke
Date:
Sat Mar 31 07:11:16 2012 +0000
Revision:
0:e724d8251cdc
Updated and cleaned up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:e724d8251cdc 1 /* RF12B Library. Based on work done by JeeLabs.org ported to mbed by SK Pang.
SomeRandomBloke 0:e724d8251cdc 2 http://jeelabs.net/projects/cafe/wiki/RF12
SomeRandomBloke 0:e724d8251cdc 3
SomeRandomBloke 0:e724d8251cdc 4 http://opensource.org/licenses/mit-license.php
SomeRandomBloke 0:e724d8251cdc 5
SomeRandomBloke 0:e724d8251cdc 6 Jan 2012 skpang.co.uk
SomeRandomBloke 0:e724d8251cdc 7
SomeRandomBloke 0:e724d8251cdc 8 Modified by Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
SomeRandomBloke 0:e724d8251cdc 9
SomeRandomBloke 0:e724d8251cdc 10 Permission is hereby granted, free of charge, to any person obtaining a copy
SomeRandomBloke 0:e724d8251cdc 11 of this software and associated documentation files (the "Software"), to deal
SomeRandomBloke 0:e724d8251cdc 12 in the Software without restriction, including without limitation the rights
SomeRandomBloke 0:e724d8251cdc 13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
SomeRandomBloke 0:e724d8251cdc 14 copies of the Software, and to permit persons to whom the Software is
SomeRandomBloke 0:e724d8251cdc 15 furnished to do so, subject to the following conditions:
SomeRandomBloke 0:e724d8251cdc 16
SomeRandomBloke 0:e724d8251cdc 17 The above copyright notice and this permission notice shall be included in
SomeRandomBloke 0:e724d8251cdc 18 all copies or substantial portions of the Software.
SomeRandomBloke 0:e724d8251cdc 19
SomeRandomBloke 0:e724d8251cdc 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
SomeRandomBloke 0:e724d8251cdc 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
SomeRandomBloke 0:e724d8251cdc 22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
SomeRandomBloke 0:e724d8251cdc 23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
SomeRandomBloke 0:e724d8251cdc 24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SomeRandomBloke 0:e724d8251cdc 25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SomeRandomBloke 0:e724d8251cdc 26 THE SOFTWARE.
SomeRandomBloke 0:e724d8251cdc 27 */
SomeRandomBloke 0:e724d8251cdc 28
SomeRandomBloke 0:e724d8251cdc 29 #ifndef _RF12B_H
SomeRandomBloke 0:e724d8251cdc 30 #define _RF12B_H
SomeRandomBloke 0:e724d8251cdc 31
SomeRandomBloke 0:e724d8251cdc 32 #include "mbed.h"
SomeRandomBloke 0:e724d8251cdc 33
SomeRandomBloke 0:e724d8251cdc 34 #define RF12_433MHZ 1
SomeRandomBloke 0:e724d8251cdc 35 #define RF12_868MHZ 2
SomeRandomBloke 0:e724d8251cdc 36 #define RF12_915MHZ 3
SomeRandomBloke 0:e724d8251cdc 37
SomeRandomBloke 0:e724d8251cdc 38 #define rf12_grp rf12_buf[0]
SomeRandomBloke 0:e724d8251cdc 39 #define rf12_hdr rf12_buf[1]
SomeRandomBloke 0:e724d8251cdc 40 #define rf12_len rf12_buf[2]
SomeRandomBloke 0:e724d8251cdc 41 #define rf12_data (rf12_buf + 3)
SomeRandomBloke 0:e724d8251cdc 42
SomeRandomBloke 0:e724d8251cdc 43 #define RF12_HDR_CTL 0x80
SomeRandomBloke 0:e724d8251cdc 44 #define RF12_HDR_DST 0x40
SomeRandomBloke 0:e724d8251cdc 45 #define RF12_HDR_ACK 0x20
SomeRandomBloke 0:e724d8251cdc 46 #define RF12_HDR_MASK 0x1F
SomeRandomBloke 0:e724d8251cdc 47
SomeRandomBloke 0:e724d8251cdc 48 #define RF12_MAXDATA 66
SomeRandomBloke 0:e724d8251cdc 49 // maximum transmit / receive buffer: 3 header + data + 2 crc bytes
SomeRandomBloke 0:e724d8251cdc 50 #define RF_MAX (RF12_MAXDATA + 5)
SomeRandomBloke 0:e724d8251cdc 51 //#define PACKET_LEN 16
SomeRandomBloke 0:e724d8251cdc 52
SomeRandomBloke 0:e724d8251cdc 53 // shorthand to simplify sending out the proper ACK when requested
SomeRandomBloke 0:e724d8251cdc 54 #define RF12_WANTS_ACK ((rf12_hdr & RF12_HDR_ACK) && !(rf12_hdr & RF12_HDR_CTL))
SomeRandomBloke 0:e724d8251cdc 55 #define RF12_ACK_REPLY (rf12_hdr & RF12_HDR_DST ? RF12_HDR_CTL : \
SomeRandomBloke 0:e724d8251cdc 56 RF12_HDR_CTL | RF12_HDR_DST | (rf12_hdr & RF12_HDR_MASK))
SomeRandomBloke 0:e724d8251cdc 57
SomeRandomBloke 0:e724d8251cdc 58 /** RFM12B Radio module class
SomeRandomBloke 0:e724d8251cdc 59 * Example usage:
SomeRandomBloke 0:e724d8251cdc 60 * @code
SomeRandomBloke 0:e724d8251cdc 61 * #include "RF12B.h"
SomeRandomBloke 0:e724d8251cdc 62 *
SomeRandomBloke 0:e724d8251cdc 63 * RF12B rfm12b(p11, p12, p13, p14, p18);
SomeRandomBloke 0:e724d8251cdc 64 *
SomeRandomBloke 0:e724d8251cdc 65 * int main() {
SomeRandomBloke 0:e724d8251cdc 66 * rfm12b.init(2, 868, 5 ); //id = 2, band 866, group 5
SomeRandomBloke 0:e724d8251cdc 67 * rfm12b.rf12_recvStart();
SomeRandomBloke 0:e724d8251cdc 68 * while (true) {
SomeRandomBloke 0:e724d8251cdc 69 * if ( rfm12b.available() > 0 ) {
SomeRandomBloke 0:e724d8251cdc 70 * // Do something with received data
SomeRandomBloke 0:e724d8251cdc 71 * }
SomeRandomBloke 0:e724d8251cdc 72 * }
SomeRandomBloke 0:e724d8251cdc 73 *
SomeRandomBloke 0:e724d8251cdc 74 * }
SomeRandomBloke 0:e724d8251cdc 75 * @endcode
SomeRandomBloke 0:e724d8251cdc 76 */
SomeRandomBloke 0:e724d8251cdc 77 class RF12B {
SomeRandomBloke 0:e724d8251cdc 78 public:
SomeRandomBloke 0:e724d8251cdc 79 /* Constructor */
SomeRandomBloke 0:e724d8251cdc 80 RF12B(PinName SDI,
SomeRandomBloke 0:e724d8251cdc 81 PinName SDO,
SomeRandomBloke 0:e724d8251cdc 82 PinName SCK,
SomeRandomBloke 0:e724d8251cdc 83 PinName NCS,
SomeRandomBloke 0:e724d8251cdc 84 PinName NIRQ);
SomeRandomBloke 0:e724d8251cdc 85
SomeRandomBloke 0:e724d8251cdc 86 /* Initialises the RF12B module */
SomeRandomBloke 0:e724d8251cdc 87 void init(uint8_t id, uint8_t band, uint8_t g);
SomeRandomBloke 0:e724d8251cdc 88
SomeRandomBloke 0:e724d8251cdc 89 /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/
SomeRandomBloke 0:e724d8251cdc 90 bool havePacket( void );
SomeRandomBloke 0:e724d8251cdc 91 int available();
SomeRandomBloke 0:e724d8251cdc 92 void rf12_sendStart (uint8_t hdr, const void* ptr, uint8_t len);
SomeRandomBloke 0:e724d8251cdc 93 void rf12_sendStart (uint8_t hdr);
SomeRandomBloke 0:e724d8251cdc 94 uint8_t rf12_recvDone (void);
SomeRandomBloke 0:e724d8251cdc 95 void rf12_recvStart (void);
SomeRandomBloke 0:e724d8251cdc 96 uint16_t check_crc(void);
SomeRandomBloke 0:e724d8251cdc 97 uint8_t length(void);
SomeRandomBloke 0:e724d8251cdc 98 uint8_t* get_data(void);
SomeRandomBloke 0:e724d8251cdc 99 uint8_t* get_payload(void);
SomeRandomBloke 0:e724d8251cdc 100
SomeRandomBloke 0:e724d8251cdc 101 protected:
SomeRandomBloke 0:e724d8251cdc 102
SomeRandomBloke 0:e724d8251cdc 103 /* SPI module */
SomeRandomBloke 0:e724d8251cdc 104 SPI spi;
SomeRandomBloke 0:e724d8251cdc 105
SomeRandomBloke 0:e724d8251cdc 106 /* Other digital pins */
SomeRandomBloke 0:e724d8251cdc 107 DigitalOut NCS;
SomeRandomBloke 0:e724d8251cdc 108 InterruptIn NIRQ;
SomeRandomBloke 0:e724d8251cdc 109 DigitalIn NIRQ_in;
SomeRandomBloke 0:e724d8251cdc 110 volatile uint16_t rf12_crc; // running crc value
SomeRandomBloke 0:e724d8251cdc 111 volatile unsigned char rf12_buf[RF_MAX]; // recv/xmit buf, including hdr & crc bytes
SomeRandomBloke 0:e724d8251cdc 112 volatile uint8_t nodeid; // address of this node
SomeRandomBloke 0:e724d8251cdc 113 volatile uint8_t group; // network group
SomeRandomBloke 0:e724d8251cdc 114 volatile uint8_t rxfill; // number of data bytes in rf12_buf
SomeRandomBloke 0:e724d8251cdc 115 volatile int8_t rxstate; // current transceiver state
SomeRandomBloke 0:e724d8251cdc 116
SomeRandomBloke 0:e724d8251cdc 117 /* Write a command to the RF Module */
SomeRandomBloke 0:e724d8251cdc 118 int writeCmd(int cmd);
SomeRandomBloke 0:e724d8251cdc 119
SomeRandomBloke 0:e724d8251cdc 120 /* Sends a byte of data across RF */
SomeRandomBloke 0:e724d8251cdc 121 void send(uint8_t data);
SomeRandomBloke 0:e724d8251cdc 122
SomeRandomBloke 0:e724d8251cdc 123 /* Interrupt routine for data reception */
SomeRandomBloke 0:e724d8251cdc 124 void rxISR();
SomeRandomBloke 0:e724d8251cdc 125
SomeRandomBloke 0:e724d8251cdc 126 uint16_t _crc16_update(uint16_t crc, uint8_t data);
SomeRandomBloke 0:e724d8251cdc 127
SomeRandomBloke 0:e724d8251cdc 128 uint16_t rf12_xfer (uint16_t cmd);
SomeRandomBloke 0:e724d8251cdc 129 uint8_t rf12_byte(uint8_t out);
SomeRandomBloke 0:e724d8251cdc 130 };
SomeRandomBloke 0:e724d8251cdc 131
SomeRandomBloke 0:e724d8251cdc 132 #endif /* _RF12B_H */