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