Working lib for qq v2
Revision 1:09e016eea608, committed 2012-02-08
- Comitter:
- madcowswe
- Date:
- Wed Feb 08 16:51:49 2012 +0000
- Parent:
- 0:048b717be6c0
- Commit message:
- Working lib for qq v2
Changed in this revision
RF12B.cpp | Show annotated file Show diff for this revision Revisions of this file |
RF12B.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/RF12B.cpp Fri Mar 11 18:02:05 2011 +0000 +++ b/RF12B.cpp Wed Feb 08 16:51:49 2012 +0000 @@ -1,8 +1,7 @@ #include "RF12B.h" #include "RF_defs.h" - -DigitalOut rfled(LED3); +#include <algorithm> RF12B::RF12B(PinName _SDI, PinName _SDO, @@ -58,6 +57,7 @@ /* Sends a packet of data to the RF module for transmission TODO: Make asych*/ void RF12B::write(unsigned char *data, unsigned char length) { unsigned char crc = 0; + /* Transmitter mode */ changeMode(TX); @@ -92,6 +92,48 @@ write(&data, 1); } +void RF12B::write(queue<char> &data, int length) { + char crc = 0; + char length_byte = 0; + + /* -1 means try to transmit everything in the queue */ + if(length == -1) { + length = data.size(); + } + + /* max length of packet is 255 */ + length_byte = min(length, 255); + + /* Transmitter mode */ + changeMode(TX); + + writeCmd(0x0000); + send(0xAA); // PREAMBLE + send(0xAA); + send(0xAA); + send(0x2D); // SYNC + send(0xD4); + /* Packet Length */ + send(length_byte); + crc = crc8(crc, length_byte); + send(crc); + crc = crc8(crc, crc); + /* Packet Data */ + for (char i=0; i<length_byte; i++) { + send(data.front()); + crc = crc8(crc, data.front()); + data.pop(); + } + send(crc); + send(0xAA); // DUMMY BYTES + send(0xAA); + send(0xAA); + + /* Back to receiver mode */ + changeMode(RX); + status(); +} + /********************************************************************** * PRIVATE FUNCTIONS *********************************************************************/ @@ -194,6 +236,7 @@ // I think this will slow down the pll's reaction time. Not sure, check with someone! ); + changeMode(RX); resetRX(); status(); } @@ -213,8 +256,9 @@ } /* Change the mode of the RF module to Transmitting or Receiving */ -void RF12B::changeMode(rfmode_t mode) { - if (mode == TX) { +void RF12B::changeMode(rfmode_t _mode) { + mode = _mode; + if (_mode == TX) { writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC } else { /* mode == RX */ writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC @@ -230,7 +274,7 @@ static queue<unsigned char> temp; //Loop while interrupt is asserted - while (!NIRQ_in) { + while (!NIRQ_in && mode == RX) { /* Grab the packet's length byte */ if (i == -2) {
--- a/RF12B.h Fri Mar 11 18:02:05 2011 +0000 +++ b/RF12B.h Wed Feb 08 16:51:49 2012 +0000 @@ -4,8 +4,6 @@ #include "mbed.h" #include <queue> -#define PACKET_LEN 16 - enum rfmode_t{RX, TX}; class RF12B { @@ -28,9 +26,8 @@ /* Transmits a packet of data */ void write(unsigned char* data, unsigned char length); - - /* Transmits a 1-byte packet of data */ - void write(unsigned char data); + void write(unsigned char data); /* 1-byte packet */ + void write(queue<char> &data, int length = -1); /* sends a whole queue */ /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/ unsigned int available(); @@ -47,6 +44,8 @@ InterruptIn NIRQ; DigitalIn NIRQ_in; DigitalOut rfled; + + rfmode_t mode; /* Initialises the RF12B module */ void init();