RF12B.h@5:12d8175359f2, 2012-03-13 (annotated)
- Committer:
- pangsk
- Date:
- Tue Mar 13 10:21:46 2012 +0000
- Revision:
- 5:12d8175359f2
- Parent:
- 4:cd581c12a4b9
Added ACK macro for mbed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pangsk | 0:66fdbf2cc578 | 1 | #ifndef _RF12B_H |
pangsk | 0:66fdbf2cc578 | 2 | #define _RF12B_H |
pangsk | 0:66fdbf2cc578 | 3 | |
pangsk | 0:66fdbf2cc578 | 4 | #include "mbed.h" |
pangsk | 4:cd581c12a4b9 | 5 | #define RF12_433MHZ 1 |
pangsk | 4:cd581c12a4b9 | 6 | #define RF12_868MHZ 2 |
pangsk | 4:cd581c12a4b9 | 7 | #define RF12_915MHZ 3 |
pangsk | 0:66fdbf2cc578 | 8 | |
pangsk | 0:66fdbf2cc578 | 9 | #define rf12_grp rf12_buf[0] |
pangsk | 0:66fdbf2cc578 | 10 | #define rf12_hdr rf12_buf[1] |
pangsk | 0:66fdbf2cc578 | 11 | #define rf12_len rf12_buf[2] |
pangsk | 0:66fdbf2cc578 | 12 | #define rf12_data (rf12_buf + 3) |
pangsk | 0:66fdbf2cc578 | 13 | |
pangsk | 0:66fdbf2cc578 | 14 | #define RF12_HDR_CTL 0x80 |
pangsk | 0:66fdbf2cc578 | 15 | #define RF12_HDR_DST 0x40 |
pangsk | 0:66fdbf2cc578 | 16 | #define RF12_HDR_ACK 0x20 |
pangsk | 0:66fdbf2cc578 | 17 | #define RF12_HDR_MASK 0x1F |
pangsk | 0:66fdbf2cc578 | 18 | |
pangsk | 0:66fdbf2cc578 | 19 | #define RF12_MAXDATA 66 |
pangsk | 0:66fdbf2cc578 | 20 | // maximum transmit / receive buffer: 3 header + data + 2 crc bytes |
pangsk | 0:66fdbf2cc578 | 21 | #define RF_MAX (RF12_MAXDATA + 5) |
pangsk | 0:66fdbf2cc578 | 22 | #define PACKET_LEN 16 |
pangsk | 0:66fdbf2cc578 | 23 | |
pangsk | 0:66fdbf2cc578 | 24 | // shorthand to simplify sending out the proper ACK when requested |
pangsk | 0:66fdbf2cc578 | 25 | #define RF12_WANTS_ACK ((rf12_hdr & RF12_HDR_ACK) && !(rf12_hdr & RF12_HDR_CTL)) |
pangsk | 0:66fdbf2cc578 | 26 | #define RF12_ACK_REPLY (rf12_hdr & RF12_HDR_DST ? RF12_HDR_CTL : \ |
pangsk | 0:66fdbf2cc578 | 27 | RF12_HDR_CTL | RF12_HDR_DST | (rf12_hdr & RF12_HDR_MASK)) |
pangsk | 5:12d8175359f2 | 28 | |
pangsk | 5:12d8175359f2 | 29 | // New for mbed |
pangsk | 5:12d8175359f2 | 30 | #define RF12_WANTS_ACK_MBED ((radiolink.get_hdr() & RF12_HDR_ACK) && !(radiolink.get_hdr() & RF12_HDR_CTL)) |
pangsk | 5:12d8175359f2 | 31 | #define RF12_ACK_REPLY_MBED (radiolink.get_hdr() & RF12_HDR_DST ? RF12_HDR_CTL : \ |
pangsk | 5:12d8175359f2 | 32 | RF12_HDR_CTL | RF12_HDR_DST | (radiolink.get_hdr() & RF12_HDR_MASK)) |
pangsk | 5:12d8175359f2 | 33 | |
pangsk | 0:66fdbf2cc578 | 34 | |
pangsk | 0:66fdbf2cc578 | 35 | //enum rfmode_t{RX, TX}; |
pangsk | 0:66fdbf2cc578 | 36 | |
pangsk | 0:66fdbf2cc578 | 37 | class RF12B { |
pangsk | 0:66fdbf2cc578 | 38 | public: |
pangsk | 0:66fdbf2cc578 | 39 | /* Constructor */ |
pangsk | 0:66fdbf2cc578 | 40 | RF12B(PinName SDI, |
pangsk | 0:66fdbf2cc578 | 41 | PinName SDO, |
pangsk | 0:66fdbf2cc578 | 42 | PinName SCK, |
pangsk | 0:66fdbf2cc578 | 43 | PinName NCS, |
pangsk | 0:66fdbf2cc578 | 44 | PinName NIRQ); |
pangsk | 0:66fdbf2cc578 | 45 | |
pangsk | 0:66fdbf2cc578 | 46 | |
pangsk | 0:66fdbf2cc578 | 47 | /* Initialises the RF12B module */ |
pangsk | 0:66fdbf2cc578 | 48 | void init(uint8_t id, uint8_t band, uint8_t g); |
pangsk | 0:66fdbf2cc578 | 49 | |
pangsk | 0:66fdbf2cc578 | 50 | |
pangsk | 0:66fdbf2cc578 | 51 | /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/ |
pangsk | 0:66fdbf2cc578 | 52 | unsigned int available(); |
pangsk | 0:66fdbf2cc578 | 53 | void rf12_sendStart (uint8_t hdr, const void* ptr, uint8_t len); |
pangsk | 0:66fdbf2cc578 | 54 | void rf12_sendStart2 (uint8_t hdr); |
pangsk | 0:66fdbf2cc578 | 55 | uint8_t rf12_recvDone (void); |
pangsk | 0:66fdbf2cc578 | 56 | void rf12_recvStart (void); |
pangsk | 0:66fdbf2cc578 | 57 | uint16_t check_crc(void); |
pangsk | 0:66fdbf2cc578 | 58 | uint8_t length(void); |
pangsk | 0:66fdbf2cc578 | 59 | uint8_t* get_data(void); |
pangsk | 5:12d8175359f2 | 60 | uint8_t get_hdr(void); |
pangsk | 0:66fdbf2cc578 | 61 | |
pangsk | 0:66fdbf2cc578 | 62 | protected: |
pangsk | 0:66fdbf2cc578 | 63 | |
pangsk | 0:66fdbf2cc578 | 64 | /* SPI module */ |
pangsk | 0:66fdbf2cc578 | 65 | SPI spi; |
pangsk | 0:66fdbf2cc578 | 66 | |
pangsk | 0:66fdbf2cc578 | 67 | /* Other digital pins */ |
pangsk | 0:66fdbf2cc578 | 68 | DigitalOut NCS; |
pangsk | 0:66fdbf2cc578 | 69 | InterruptIn NIRQ; |
pangsk | 0:66fdbf2cc578 | 70 | DigitalIn NIRQ_in; |
pangsk | 3:e926e54424cb | 71 | |
pangsk | 0:66fdbf2cc578 | 72 | volatile uint16_t rf12_crc; // running crc value |
pangsk | 0:66fdbf2cc578 | 73 | volatile unsigned char rf12_buf[RF_MAX]; // recv/xmit buf, including hdr & crc bytes |
pangsk | 0:66fdbf2cc578 | 74 | volatile uint8_t nodeid; // address of this node |
pangsk | 0:66fdbf2cc578 | 75 | volatile uint8_t group; // network grou |
pangsk | 0:66fdbf2cc578 | 76 | volatile uint8_t rxfill; // number of data bytes in rf12_buf |
pangsk | 0:66fdbf2cc578 | 77 | volatile int8_t rxstate; // current transceiver state |
pangsk | 0:66fdbf2cc578 | 78 | |
pangsk | 0:66fdbf2cc578 | 79 | |
pangsk | 0:66fdbf2cc578 | 80 | /* Write a command to the RF Module */ |
pangsk | 0:66fdbf2cc578 | 81 | unsigned int writeCmd(unsigned int cmd); |
pangsk | 0:66fdbf2cc578 | 82 | |
pangsk | 0:66fdbf2cc578 | 83 | /* Sends a byte of data across RF */ |
pangsk | 0:66fdbf2cc578 | 84 | void send(unsigned char data); |
pangsk | 0:66fdbf2cc578 | 85 | |
pangsk | 0:66fdbf2cc578 | 86 | /* Interrupt routine for data reception */ |
pangsk | 0:66fdbf2cc578 | 87 | void rxISR(); |
pangsk | 0:66fdbf2cc578 | 88 | |
pangsk | 0:66fdbf2cc578 | 89 | |
pangsk | 0:66fdbf2cc578 | 90 | uint16_t _crc16_update(uint16_t crc, uint8_t data); |
pangsk | 0:66fdbf2cc578 | 91 | |
pangsk | 0:66fdbf2cc578 | 92 | uint16_t rf12_xfer (uint16_t cmd); |
pangsk | 0:66fdbf2cc578 | 93 | uint8_t rf12_byte(uint8_t out); |
pangsk | 0:66fdbf2cc578 | 94 | }; |
pangsk | 0:66fdbf2cc578 | 95 | |
pangsk | 0:66fdbf2cc578 | 96 | #endif /* _RF12B_H */ |