Committer:
pangsk
Date:
Tue Feb 28 10:53:46 2012 +0000
Revision:
4:cd581c12a4b9
Parent:
3:e926e54424cb
Child:
5:12d8175359f2
Added frequency band defines.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:66fdbf2cc578 28
pangsk 0:66fdbf2cc578 29
pangsk 0:66fdbf2cc578 30 //enum rfmode_t{RX, TX};
pangsk 0:66fdbf2cc578 31
pangsk 0:66fdbf2cc578 32 class RF12B {
pangsk 0:66fdbf2cc578 33 public:
pangsk 0:66fdbf2cc578 34 /* Constructor */
pangsk 0:66fdbf2cc578 35 RF12B(PinName SDI,
pangsk 0:66fdbf2cc578 36 PinName SDO,
pangsk 0:66fdbf2cc578 37 PinName SCK,
pangsk 0:66fdbf2cc578 38 PinName NCS,
pangsk 0:66fdbf2cc578 39 PinName NIRQ);
pangsk 0:66fdbf2cc578 40
pangsk 0:66fdbf2cc578 41
pangsk 0:66fdbf2cc578 42 /* Initialises the RF12B module */
pangsk 0:66fdbf2cc578 43 void init(uint8_t id, uint8_t band, uint8_t g);
pangsk 0:66fdbf2cc578 44
pangsk 0:66fdbf2cc578 45
pangsk 0:66fdbf2cc578 46 /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/
pangsk 0:66fdbf2cc578 47 unsigned int available();
pangsk 0:66fdbf2cc578 48 void rf12_sendStart (uint8_t hdr, const void* ptr, uint8_t len);
pangsk 0:66fdbf2cc578 49 void rf12_sendStart2 (uint8_t hdr);
pangsk 0:66fdbf2cc578 50 uint8_t rf12_recvDone (void);
pangsk 0:66fdbf2cc578 51 void rf12_recvStart (void);
pangsk 0:66fdbf2cc578 52 uint16_t check_crc(void);
pangsk 0:66fdbf2cc578 53 uint8_t length(void);
pangsk 0:66fdbf2cc578 54 uint8_t* get_data(void);
pangsk 0:66fdbf2cc578 55
pangsk 0:66fdbf2cc578 56 protected:
pangsk 0:66fdbf2cc578 57
pangsk 0:66fdbf2cc578 58 /* SPI module */
pangsk 0:66fdbf2cc578 59 SPI spi;
pangsk 0:66fdbf2cc578 60
pangsk 0:66fdbf2cc578 61 /* Other digital pins */
pangsk 0:66fdbf2cc578 62 DigitalOut NCS;
pangsk 0:66fdbf2cc578 63 InterruptIn NIRQ;
pangsk 0:66fdbf2cc578 64 DigitalIn NIRQ_in;
pangsk 3:e926e54424cb 65
pangsk 0:66fdbf2cc578 66 volatile uint16_t rf12_crc; // running crc value
pangsk 0:66fdbf2cc578 67 volatile unsigned char rf12_buf[RF_MAX]; // recv/xmit buf, including hdr & crc bytes
pangsk 0:66fdbf2cc578 68 volatile uint8_t nodeid; // address of this node
pangsk 0:66fdbf2cc578 69 volatile uint8_t group; // network grou
pangsk 0:66fdbf2cc578 70 volatile uint8_t rxfill; // number of data bytes in rf12_buf
pangsk 0:66fdbf2cc578 71 volatile int8_t rxstate; // current transceiver state
pangsk 0:66fdbf2cc578 72
pangsk 0:66fdbf2cc578 73
pangsk 0:66fdbf2cc578 74 /* Write a command to the RF Module */
pangsk 0:66fdbf2cc578 75 unsigned int writeCmd(unsigned int cmd);
pangsk 0:66fdbf2cc578 76
pangsk 0:66fdbf2cc578 77 /* Sends a byte of data across RF */
pangsk 0:66fdbf2cc578 78 void send(unsigned char data);
pangsk 0:66fdbf2cc578 79
pangsk 0:66fdbf2cc578 80 /* Interrupt routine for data reception */
pangsk 0:66fdbf2cc578 81 void rxISR();
pangsk 0:66fdbf2cc578 82
pangsk 0:66fdbf2cc578 83
pangsk 0:66fdbf2cc578 84 uint16_t _crc16_update(uint16_t crc, uint8_t data);
pangsk 0:66fdbf2cc578 85
pangsk 0:66fdbf2cc578 86 uint16_t rf12_xfer (uint16_t cmd);
pangsk 0:66fdbf2cc578 87 uint8_t rf12_byte(uint8_t out);
pangsk 0:66fdbf2cc578 88 };
pangsk 0:66fdbf2cc578 89
pangsk 0:66fdbf2cc578 90 #endif /* _RF12B_H */