Now that we're in cpp land, remove the mrf_ prefixes from all methods.

Also, add a proper set_channel method, and a method for promiscuous mode.
This commit is contained in:
Karl Palsson 2011-03-13 18:35:18 +00:00
parent 0a2c207146
commit fd58b93767
3 changed files with 98 additions and 88 deletions

View File

@ -20,10 +20,10 @@ long tx_interval = 1000;
void setup() {
Serial.begin(9600);
Serial.println("Starting to reset and startup...");
mrf.mrf_pan_write(0xcafe);
mrf.mrf_address16_write(0x6001);
mrf.set_pan(0xcafe);
// This is _our_ address
mrf.address16_write(0x6001);
attachInterrupt(0, interrupt_routine, CHANGE);
last_time = millis();
}
@ -34,7 +34,7 @@ volatile uint8_t last_interrupt;
void interrupt_routine() {
// read and clear from the radio
last_interrupt = mrf.mrf_read_short(MRF_INTSTAT);
last_interrupt = mrf.read_short(MRF_INTSTAT);
if (last_interrupt & MRF_I_RXIF) {
gotrx = 1;
}
@ -44,19 +44,19 @@ void interrupt_routine() {
}
void loop() {
//mrf.mrf_write_short(MRF_RXMCR, 0x01); // promiscuous!
//mrf.set_promiscuous(true);
int tmp;
interrupts();
unsigned long current_time = millis();
if (current_time - last_time > tx_interval) {
last_time = current_time;
Serial.println("txxxing...");
mrf.mrf_send16(0x4202, 4, "abcd");
mrf.send16(0x4202, 4, "abcd");
}
if (txok) {
txok = 0;
Serial.print("tx went ok:");
tmp = mrf.mrf_read_short(MRF_TXSTAT);
tmp = mrf.read_short(MRF_TXSTAT);
Serial.print(tmp);
if (!(tmp & ~(1<<TXNSTAT))) { // 1 = failed
Serial.print("...And we got an ACK");
@ -69,22 +69,22 @@ void loop() {
if (gotrx) {
gotrx = 0;
noInterrupts();
mrf.mrf_write_short(MRF_BBREG1, 0x04); // RXDECINV - disable receiver
mrf.write_short(MRF_BBREG1, 0x04); // RXDECINV - disable receiver
byte frame_length = mrf.mrf_read_long(0x300); // read start of rxfifo
byte frame_length = mrf.read_long(0x300); // read start of rxfifo
Serial.print("received a packet ");Serial.print(frame_length, DEC);Serial.println(" bytes long");
Serial.println("Packet data:");
for (int i = 1; i <= frame_length; i++) {
tmp = mrf.mrf_read_long(0x300 + i);
tmp = mrf.read_long(0x300 + i);
Serial.print(tmp, HEX);
}
Serial.print("\r\nLQI/RSSI=");
byte lqi = mrf.mrf_read_long(0x300 + frame_length + 1);
byte rssi = mrf.mrf_read_long(0x300 + frame_length + 2);
byte lqi = mrf.read_long(0x300 + frame_length + 1);
byte rssi = mrf.read_long(0x300 + frame_length + 2);
Serial.print(lqi, HEX);
Serial.println(rssi, HEX);
mrf.mrf_write_short(MRF_BBREG1, 0x00); // RXDECINV - enable receiver
mrf.write_short(MRF_BBREG1, 0x00); // RXDECINV - enable receiver
interrupts();
}

View File

@ -19,18 +19,18 @@ Mrf24j::Mrf24j(int pin_reset, int pin_chip_select, int pin_interrupt) {
SPI.begin();
// arguably should not be here...
mrf_reset();
mrf_init();
reset();
init();
}
void Mrf24j::mrf_reset(void) {
void Mrf24j::reset(void) {
digitalWrite(_pin_reset, LOW);
delay(10); // just my gut
digitalWrite(_pin_reset, HIGH);
delay(20); // from manual
}
byte Mrf24j::mrf_read_short(byte address) {
byte Mrf24j::read_short(byte address) {
digitalWrite(_pin_cs, LOW);
// 0 top for short addressing, 0 bottom for read
SPI.transfer(address<<1 & 0b01111110);
@ -39,7 +39,7 @@ byte Mrf24j::mrf_read_short(byte address) {
return ret;
}
byte Mrf24j::mrf_read_long(word address) {
byte Mrf24j::read_long(word address) {
digitalWrite(_pin_cs, LOW);
byte ahigh = address >> 3;
byte alow = address << 5;
@ -51,7 +51,7 @@ byte Mrf24j::mrf_read_long(word address) {
}
void Mrf24j::mrf_write_short(byte address, byte data) {
void Mrf24j::write_short(byte address, byte data) {
digitalWrite(_pin_cs, LOW);
// 0 for top address, 1 bottom for write
SPI.transfer((address<<1 & 0b01111110) | 0x01);
@ -59,7 +59,7 @@ void Mrf24j::mrf_write_short(byte address, byte data) {
digitalWrite(_pin_cs, HIGH);
}
void Mrf24j::mrf_write_long(word address, byte data) {
void Mrf24j::write_long(word address, byte data) {
digitalWrite(_pin_cs, LOW);
byte ahigh = address >> 3;
byte alow = address << 5;
@ -69,103 +69,109 @@ void Mrf24j::mrf_write_long(word address, byte data) {
digitalWrite(_pin_cs, HIGH);
}
word Mrf24j::mrf_pan_read(void) {
byte panh = mrf_read_short(MRF_PANIDH);
return panh << 8 | mrf_read_short(MRF_PANIDL);
word Mrf24j::get_pan(void) {
byte panh = read_short(MRF_PANIDH);
return panh << 8 | read_short(MRF_PANIDL);
}
void Mrf24j::mrf_pan_write(word panid) {
mrf_write_short(MRF_PANIDH, panid >> 8);
mrf_write_short(MRF_PANIDL, panid & 0xff);
void Mrf24j::set_pan(word panid) {
write_short(MRF_PANIDH, panid >> 8);
write_short(MRF_PANIDL, panid & 0xff);
}
void Mrf24j::mrf_address16_write(word address16) {
mrf_write_short(MRF_SADRH, address16 >> 8);
mrf_write_short(MRF_SADRL, address16 & 0xff);
void Mrf24j::address16_write(word address16) {
write_short(MRF_SADRH, address16 >> 8);
write_short(MRF_SADRL, address16 & 0xff);
}
word Mrf24j::mrf_address16_read(void) {
byte a16h = mrf_read_short(MRF_SADRH);
return a16h << 8 | mrf_read_short(MRF_SADRL);
word Mrf24j::address16_read(void) {
byte a16h = read_short(MRF_SADRH);
return a16h << 8 | read_short(MRF_SADRL);
}
/**
* Simple send 16, with acks, not much of anything.. assumes src16 and local pan only.
* @param data
*/
void Mrf24j::mrf_send16(word dest16, byte len, char * data) {
void Mrf24j::send16(word dest16, byte len, char * data) {
int i = 0;
mrf_write_long(i++, 9); // header length
mrf_write_long(i++, 9+2+len); //+2 is because module seems to ignore 2 bytes after the header?!
write_long(i++, 9); // header length
write_long(i++, 9+2+len); //+2 is because module seems to ignore 2 bytes after the header?!
// 0 | pan compression | ack | no security | no data pending | data frame[3 bits]
mrf_write_long(i++, 0b01100001); // first byte of Frame Control
write_long(i++, 0b01100001); // first byte of Frame Control
// 16 bit source, 802.15.4 (2003), 16 bit dest,
mrf_write_long(i++, 0b10001000); // second byte of frame control
mrf_write_long(i++, 1); // sequence number 1
write_long(i++, 0b10001000); // second byte of frame control
write_long(i++, 1); // sequence number 1
word panid = mrf_pan_read();
word panid = get_pan();
mrf_write_long(i++, panid & 0xff); // dest panid
mrf_write_long(i++, panid >> 8);
mrf_write_long(i++, dest16 & 0xff); // dest16 low
mrf_write_long(i++, dest16 >> 8); // dest16 high
write_long(i++, panid & 0xff); // dest panid
write_long(i++, panid >> 8);
write_long(i++, dest16 & 0xff); // dest16 low
write_long(i++, dest16 >> 8); // dest16 high
word src16 = mrf_address16_read();
mrf_write_long(i++, src16 & 0xff); // src16 low
mrf_write_long(i++, src16 >> 8); // src16 high
word src16 = address16_read();
write_long(i++, src16 & 0xff); // src16 low
write_long(i++, src16 >> 8); // src16 high
i+=2; // All testing seems to indicate that the next two bytes are ignored.
for (int q = 0; q < len; q++) {
mrf_write_long(i++, data[q]);
write_long(i++, data[q]);
}
// ack on, and go!
mrf_write_short(MRF_TXNCON, (1<<MRF_TXNACKREQ | 1<<MRF_TXNTRIG));
write_short(MRF_TXNCON, (1<<MRF_TXNACKREQ | 1<<MRF_TXNTRIG));
}
void Mrf24j::mrf_set_interrupts(void) {
void Mrf24j::set_interrupts(void) {
// interrupts for rx and tx normal complete
mrf_write_short(MRF_INTCON, 0b11110110);
write_short(MRF_INTCON, 0b11110110);
}
// Set the channel to 12, 2.41Ghz, xbee channel 0xC
void Mrf24j::mrf_set_channel(void) {
mrf_write_long(MRF_RFCON0, 0x13);
/** use the 802.15.4 channel numbers..
*/
void Mrf24j::set_channel(byte channel) {
write_long(MRF_RFCON0, ((channel - 11) << 4) | 0x03));
}
void Mrf24j::mrf_init(void) {
void Mrf24j::init(void) {
/*
// Seems a bit ridiculous when I use reset pin anyway
mrf_write_short(MRF_SOFTRST, 0x7); // from manual
while (mrf_read_short(MRF_SOFTRST) & 0x7 != 0) {
write_short(MRF_SOFTRST, 0x7); // from manual
while (read_short(MRF_SOFTRST) & 0x7 != 0) {
; // wait for soft reset to finish
}
*/
mrf_write_short(MRF_PACON2, 0x98); // Initialize FIFOEN = 1 and TXONTS = 0x6.
mrf_write_short(MRF_TXSTBL, 0x95); // Initialize RFSTBL = 0x9.
write_short(MRF_PACON2, 0x98); // Initialize FIFOEN = 1 and TXONTS = 0x6.
write_short(MRF_TXSTBL, 0x95); // Initialize RFSTBL = 0x9.
mrf_write_long(MRF_RFCON0, 0x03); // Initialize RFOPT = 0x03.
mrf_write_long(MRF_RFCON1, 0x01); // Initialize VCOOPT = 0x02.
mrf_write_long(MRF_RFCON2, 0x80); // Enable PLL (PLLEN = 1).
mrf_write_long(MRF_RFCON6, 0x90); // Initialize TXFIL = 1 and 20MRECVR = 1.
mrf_write_long(MRF_RFCON7, 0x80); // Initialize SLPCLKSEL = 0x2 (100 kHz Internal oscillator).
mrf_write_long(MRF_RFCON8, 0x10); // Initialize RFVCO = 1.
mrf_write_long(MRF_SLPCON1, 0x21); // Initialize CLKOUTEN = 1 and SLPCLKDIV = 0x01.
write_long(MRF_RFCON0, 0x03); // Initialize RFOPT = 0x03.
write_long(MRF_RFCON1, 0x01); // Initialize VCOOPT = 0x02.
write_long(MRF_RFCON2, 0x80); // Enable PLL (PLLEN = 1).
write_long(MRF_RFCON6, 0x90); // Initialize TXFIL = 1 and 20MRECVR = 1.
write_long(MRF_RFCON7, 0x80); // Initialize SLPCLKSEL = 0x2 (100 kHz Internal oscillator).
write_long(MRF_RFCON8, 0x10); // Initialize RFVCO = 1.
write_long(MRF_SLPCON1, 0x21); // Initialize CLKOUTEN = 1 and SLPCLKDIV = 0x01.
// Configuration for nonbeacon-enabled devices (see Section 3.8 “Beacon-Enabled and
// Nonbeacon-Enabled Networks”):
mrf_write_short(MRF_BBREG2, 0x80); // Set CCA mode to ED
mrf_write_short(MRF_CCAEDTH, 0x60); // Set CCA ED threshold.
mrf_write_short(MRF_BBREG6, 0x40); // Set appended RSSI value to RXFIFO.
mrf_set_interrupts();
mrf_set_channel();
write_short(MRF_BBREG2, 0x80); // Set CCA mode to ED
write_short(MRF_CCAEDTH, 0x60); // Set CCA ED threshold.
write_short(MRF_BBREG6, 0x40); // Set appended RSSI value to RXFIFO.
set_interrupts();
set_channel(12);
// max power is by default.. just leave it...
//Set transmitter power - See “REGISTER 2-62: RF CONTROL 3 REGISTER (ADDRESS: 0x203)”.
mrf_write_short(MRF_RFCTL, 0x04); // Reset RF state machine.
mrf_write_short(MRF_RFCTL, 0x00); // part 2
write_short(MRF_RFCTL, 0x04); // Reset RF state machine.
write_short(MRF_RFCTL, 0x00); // part 2
delay(1); // delay at least 192usec
}
void Mrf24j::set_promiscuous(boolean enabled) {
if (enabled) {
write_short(MRF_RXMCR, 0x01);
} else {
write_short(MRF_RXMCR, 0x00);
}
}

View File

@ -149,27 +149,31 @@ class Mrf24j
public:
Mrf24j(int pin_reset, int pin_chip_select, int pin_interrupt);
void mrf_reset(void);
void mrf_init(void);
void reset(void);
void init(void);
byte mrf_read_short(byte address);
byte mrf_read_long(word address);
byte read_short(byte address);
byte read_long(word address);
void mrf_write_short(byte address, byte data);
void mrf_write_long(word address, byte data);
void write_short(byte address, byte data);
void write_long(word address, byte data);
word mrf_pan_read(void);
void mrf_pan_write(word panid);
word get_pan(void);
void set_pan(word panid);
void mrf_address16_write(word address16);
word mrf_address16_read(void);
void address16_write(word address16);
word address16_read(void);
void mrf_set_interrupts(void);
void set_interrupts(void);
// Set the channel to 12, 2.41Ghz, xbee channel 0xC
void mrf_set_channel(void);
void set_promiscuous(boolean enabled);
void mrf_send16(word dest16, byte len, char * data);
/**
* Set the channel, using 802.15.4 channel numbers (11..26)
*/
void set_channel(byte channel);
void send16(word dest16, byte len, char * data);
private:
int _pin_reset;