Diff: RF12B/RF12B.cpp
- Revision:
- 1:0370ea94b64a
- Parent:
- 0:7583de124698
diff -r 7583de124698 -r 0370ea94b64a RF12B/RF12B.cpp
--- a/RF12B/RF12B.cpp Fri Mar 30 19:44:25 2012 +0000
+++ b/RF12B/RF12B.cpp Thu Apr 26 17:50:00 2012 +0000
@@ -2,41 +2,38 @@
#include "RF_defs.h"
#include <algorithm>
-
-//#include "globals.h"
+#include "system.h"
+#include "globals.h"
-//DigitalOut DBG2(LED2);
-//DigitalOut DBG3(LED3);
-//DigitalOut DBG4(LED4);
RF12B::RF12B(PinName _SDI,
PinName _SDO,
PinName _SCK,
PinName _NCS,
PinName _NIRQ):spi(_SDI, _SDO, _SCK),
- NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ){// rfled(LED3) {
+ NCS(_NCS), NIRQ(_NIRQ), NIRQ_in(_NIRQ) {// rfled(LED3) {
- /* SPI frequency, word lenght, polarity and phase */
+ // SPI frequency, word lenght, polarity and phase */
spi.format(16,0);
spi.frequency(2000000);
- /* Set ~CS high */
+ // Set ~CS high
NCS = 1;
- /* Initialise RF Module */
+ // Initialise RF Module
init();
- /* Setup interrupt to happen on falling edge of NIRQ */
+ // Setup interrupt to happen on falling edge of NIRQ
NIRQ.fall(this, &RF12B::rxISR);
}
-/* Returns the packet length if data is available in the receive buffer, 0 otherwise*/
-unsigned int RF12B::available() {
- return fifo.size();
-}
+// Returns the packet length if data is available in the receive buffer, 0 otherwise
+//unsigned int RF12B::available() {
+// return fifo.size();
+//}
-/* Reads a packet of data, with length "size" Returns false if read failed. TODO: make a metafifo to isolate packets*/
-bool RF12B::read(unsigned char* data, unsigned int size) {
+// Reads a packet of data, with length "size" Returns false if read failed. TODO: make a metafifo to isolate packets
+/*bool RF12B::read(unsigned char* data, unsigned int size) {
if (fifo.size() == 0) {
return false;
} else {
@@ -48,8 +45,10 @@
return true;
}
}
+*/
-/* Reads a byte of data from the receive buffer */
+// Reads a byte of data from the receive buffer
+/*
unsigned char RF12B::read() {
if (available()) {
unsigned char data = fifo.front();
@@ -59,12 +58,13 @@
return 0xFF; // Error val although could also be data...
}
}
+*/
-/* Sends a packet of data to the RF module for transmission TODO: Make asych*/
+// 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 */
+
+ // Transmitter mode
changeMode(TX);
writeCmd(0x0000);
@@ -73,12 +73,12 @@
send(0xAA);
send(0x2D); // SYNC
send(0xD4);
- /* Packet Length */
+ // Packet Length
send(length);
crc = crc8(crc, length);
send(crc);
crc = crc8(crc, crc);
- /* Packet Data */
+ // Packet Data
for (unsigned char i=0; i<length; i++) {
send(data[i]);
crc = crc8(crc, data[i]);
@@ -88,31 +88,31 @@
send(0xAA);
send(0xAA);
- /* Back to receiver mode */
+ // Back to receiver mode
changeMode(RX);
status();
-
+
}
-/* Transmit a 1-byte data packet */
+// Transmit a 1-byte data packet
void RF12B::write(unsigned char data) {
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) {
+
+ // -1 means try to transmit everything in the queue
+ if (length == -1) {
length = data.size();
}
-
- /* max length of packet is 255 */
+
+ // max length of packet is 255
length_byte = min(length, 255);
-
- /* Transmitter mode */
+
+ // Transmitter mode
changeMode(TX);
writeCmd(0x0000);
@@ -121,12 +121,12 @@
send(0xAA);
send(0x2D); // SYNC
send(0xD4);
- /* Packet Length */
+ // Packet Length
send(length_byte);
crc = crc8(crc, length_byte);
send(crc);
crc = crc8(crc, crc);
- /* Packet Data */
+ // Packet Data
for (char i=0; i<length_byte; i++) {
send(data.front());
crc = crc8(crc, data.front());
@@ -137,18 +137,18 @@
send(0xAA);
send(0xAA);
- /* Back to receiver mode */
+ // Back to receiver mode
changeMode(RX);
status();
}
-
+*/
/**********************************************************************
* PRIVATE FUNCTIONS
*********************************************************************/
-/* Initialises the RF12B module */
+// Initialises the RF12B module
void RF12B::init() {
- /* writeCmd(0x80E7); //EL,EF,868band,12.0pF
+ // writeCmd(0x80E7); //EL,EF,868band,12.0pF
changeMode(RX);
writeCmd(0xA640); //frequency select
writeCmd(0xC647); //4.8kbps
@@ -161,7 +161,7 @@
writeCmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDIT�CBW0
writeCmd(0xE000); //NOT USED
writeCmd(0xC800); //NOT USED
- writeCmd(0xC040); //1.66MHz,2.2V */
+ writeCmd(0xC040); //1.66MHz,2.2V
writeCmd(
RFM_CONFIG_EL |
@@ -273,25 +273,18 @@
}
}
-/* Interrupt routine for data reception */
+// Interrupt routine for data reception */
void RF12B::rxISR() {
-
- //static int cnt = 0;
- //printf("%d hits\r\n", cnt);
- //cnt++;
-
- //DBG2 = !(cnt%3);
- //DBG3 = !((cnt+1)%3);
- //DBG4 = !((cnt+2)%3);
unsigned int data = 0;
static int i = -2;
static unsigned char packet_length = 0;
static unsigned char crc = 0;
+ static unsigned char temp;
//Loop while interrupt is asserted
while (!NIRQ_in && mode == RX) {
-
+
// Grab the packet's length byte
if (i == -2) {
data = writeCmd(0x0000);
@@ -331,11 +324,12 @@
if (NIRQ_in)
break;
- // Grab the packet's data
+ // Grab the packet's data
if (i >= 0 && i < packet_length) {
data = writeCmd(0x0000);
if ( (data&0x8000) ) {
data = writeCmd(0xB000);
+ temp = data&0x00FF;
//temp.push(data&0x00FF);
crc = crc8(crc, (unsigned char)(data&0x00FF));
i++;
@@ -353,9 +347,16 @@
if ((unsigned char)(data & 0x00FF) == crc) {
//If the checksum is correct, add our data to the end of the output buffer
//while (!temp.empty()) {
- // fifo.push(temp.front());
- // temp.pop();
- //}
+ //fifo.push(temp);
+ // temp.pop();
+#ifdef ROBOT_SECONDARY
+ if (callbackfunc)
+ (*callbackfunc)(temp);
+
+ if (callbackobj && mcallbackfunc)
+ (callbackobj->*mcallbackfunc)(temp);
+#endif
+ // }
}
// Tell RF Module we are finished, and clean up
@@ -367,19 +368,20 @@
}
}
}
+
}
unsigned int RF12B::status() {
return writeCmd(0x0000);
}
-/* Tell the RF Module this packet is received and wait for the next */
+// Tell the RF Module this packet is received and wait for the next */
void RF12B::resetRX() {
writeCmd(0xCA81);
writeCmd(0xCA83);
};
-/* Calculate CRC8 */
+// Calculate CRC8 */
unsigned char RF12B::crc8(unsigned char crc, unsigned char data) {
crc = crc ^ data;
for (int i = 0; i < 8; i++) {