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