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