Working lib for qq v2
RF12B.h@0:048b717be6c0, 2011-03-11 (annotated)
- Committer:
- madcowswe
- Date:
- Fri Mar 11 18:02:05 2011 +0000
- Revision:
- 0:048b717be6c0
- Child:
- 1:09e016eea608
Made reading asynchronous, and added a block read function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
madcowswe | 0:048b717be6c0 | 1 | #ifndef _RF12B_H |
madcowswe | 0:048b717be6c0 | 2 | #define _RF12B_H |
madcowswe | 0:048b717be6c0 | 3 | |
madcowswe | 0:048b717be6c0 | 4 | #include "mbed.h" |
madcowswe | 0:048b717be6c0 | 5 | #include <queue> |
madcowswe | 0:048b717be6c0 | 6 | |
madcowswe | 0:048b717be6c0 | 7 | #define PACKET_LEN 16 |
madcowswe | 0:048b717be6c0 | 8 | |
madcowswe | 0:048b717be6c0 | 9 | enum rfmode_t{RX, TX}; |
madcowswe | 0:048b717be6c0 | 10 | |
madcowswe | 0:048b717be6c0 | 11 | class RF12B { |
madcowswe | 0:048b717be6c0 | 12 | public: |
madcowswe | 0:048b717be6c0 | 13 | /* Constructor */ |
madcowswe | 0:048b717be6c0 | 14 | RF12B(PinName SDI, |
madcowswe | 0:048b717be6c0 | 15 | PinName SDO, |
madcowswe | 0:048b717be6c0 | 16 | PinName SCK, |
madcowswe | 0:048b717be6c0 | 17 | PinName NCS, |
madcowswe | 0:048b717be6c0 | 18 | PinName NIRQ); |
madcowswe | 0:048b717be6c0 | 19 | |
madcowswe | 0:048b717be6c0 | 20 | |
madcowswe | 0:048b717be6c0 | 21 | |
madcowswe | 0:048b717be6c0 | 22 | /* Reads a packet of data. Returns false if read failed. Use available() to check how much space to allocate for buffer */ |
madcowswe | 0:048b717be6c0 | 23 | bool read(unsigned char* data, unsigned int size); |
madcowswe | 0:048b717be6c0 | 24 | |
madcowswe | 0:048b717be6c0 | 25 | /* Reads a byte of data from the receive buffer |
madcowswe | 0:048b717be6c0 | 26 | Returns 0xFF if there is no data */ |
madcowswe | 0:048b717be6c0 | 27 | unsigned char read(); |
madcowswe | 0:048b717be6c0 | 28 | |
madcowswe | 0:048b717be6c0 | 29 | /* Transmits a packet of data */ |
madcowswe | 0:048b717be6c0 | 30 | void write(unsigned char* data, unsigned char length); |
madcowswe | 0:048b717be6c0 | 31 | |
madcowswe | 0:048b717be6c0 | 32 | /* Transmits a 1-byte packet of data */ |
madcowswe | 0:048b717be6c0 | 33 | void write(unsigned char data); |
madcowswe | 0:048b717be6c0 | 34 | |
madcowswe | 0:048b717be6c0 | 35 | /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/ |
madcowswe | 0:048b717be6c0 | 36 | unsigned int available(); |
madcowswe | 0:048b717be6c0 | 37 | |
madcowswe | 0:048b717be6c0 | 38 | protected: |
madcowswe | 0:048b717be6c0 | 39 | /* Receive FIFO buffer */ |
madcowswe | 0:048b717be6c0 | 40 | queue<unsigned char> fifo; |
madcowswe | 0:048b717be6c0 | 41 | |
madcowswe | 0:048b717be6c0 | 42 | /* SPI module */ |
madcowswe | 0:048b717be6c0 | 43 | SPI spi; |
madcowswe | 0:048b717be6c0 | 44 | |
madcowswe | 0:048b717be6c0 | 45 | /* Other digital pins */ |
madcowswe | 0:048b717be6c0 | 46 | DigitalOut NCS; |
madcowswe | 0:048b717be6c0 | 47 | InterruptIn NIRQ; |
madcowswe | 0:048b717be6c0 | 48 | DigitalIn NIRQ_in; |
madcowswe | 0:048b717be6c0 | 49 | DigitalOut rfled; |
madcowswe | 0:048b717be6c0 | 50 | |
madcowswe | 0:048b717be6c0 | 51 | /* Initialises the RF12B module */ |
madcowswe | 0:048b717be6c0 | 52 | void init(); |
madcowswe | 0:048b717be6c0 | 53 | |
madcowswe | 0:048b717be6c0 | 54 | /* Write a command to the RF Module */ |
madcowswe | 0:048b717be6c0 | 55 | unsigned int writeCmd(unsigned int cmd); |
madcowswe | 0:048b717be6c0 | 56 | |
madcowswe | 0:048b717be6c0 | 57 | /* Sends a byte of data across RF */ |
madcowswe | 0:048b717be6c0 | 58 | void send(unsigned char data); |
madcowswe | 0:048b717be6c0 | 59 | |
madcowswe | 0:048b717be6c0 | 60 | /* Switch module between receive and transmit modes */ |
madcowswe | 0:048b717be6c0 | 61 | void changeMode(rfmode_t mode); |
madcowswe | 0:048b717be6c0 | 62 | |
madcowswe | 0:048b717be6c0 | 63 | /* Interrupt routine for data reception */ |
madcowswe | 0:048b717be6c0 | 64 | void rxISR(); |
madcowswe | 0:048b717be6c0 | 65 | |
madcowswe | 0:048b717be6c0 | 66 | /* Tell the RF Module this packet is received and wait for the next */ |
madcowswe | 0:048b717be6c0 | 67 | void resetRX(); |
madcowswe | 0:048b717be6c0 | 68 | |
madcowswe | 0:048b717be6c0 | 69 | /* Return the RF Module Status word */ |
madcowswe | 0:048b717be6c0 | 70 | unsigned int status(); |
madcowswe | 0:048b717be6c0 | 71 | |
madcowswe | 0:048b717be6c0 | 72 | /* Calculate CRC8 */ |
madcowswe | 0:048b717be6c0 | 73 | unsigned char crc8(unsigned char crc, unsigned char data); |
madcowswe | 0:048b717be6c0 | 74 | }; |
madcowswe | 0:048b717be6c0 | 75 | |
madcowswe | 0:048b717be6c0 | 76 | #endif /* _RF12B_H */ |