Add more wrapper functions to hide more of the raw register accesses

This commit is contained in:
Karl Palsson 2011-03-13 19:49:07 +00:00
parent cb92166dcd
commit effe217111
5 changed files with 35 additions and 12 deletions

View File

@ -34,11 +34,10 @@ void setup() {
volatile uint8_t gotrx; volatile uint8_t gotrx;
volatile uint8_t txok; volatile uint8_t txok;
volatile uint8_t last_interrupt;
void interrupt_routine() { void interrupt_routine() {
// read and clear from the radio // read and clear from the radio
last_interrupt = mrf.read_short(MRF_INTSTAT); byte last_interrupt = mrf.read_short(MRF_INTSTAT);
if (last_interrupt & MRF_I_RXIF) { if (last_interrupt & MRF_I_RXIF) {
gotrx = 1; gotrx = 1;
} }
@ -72,7 +71,7 @@ void loop() {
if (gotrx) { if (gotrx) {
gotrx = 0; gotrx = 0;
noInterrupts(); noInterrupts();
mrf.write_short(MRF_BBREG1, 0x04); // RXDECINV - disable receiver mrf.rx_disable();
byte frame_length = 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.print("received a packet ");Serial.print(frame_length, DEC);Serial.println(" bytes long");
@ -87,7 +86,7 @@ void loop() {
Serial.print(lqi, HEX); Serial.print(lqi, HEX);
Serial.println(rssi, HEX); Serial.println(rssi, HEX);
mrf.write_short(MRF_BBREG1, 0x00); // RXDECINV - enable receiver mrf.rx_enable();
interrupts(); interrupts();
} }

View File

@ -1,5 +1,5 @@
/** /**
* Example code for using a microchip mrf24j40 module in receive mode only * Example code for using a microchip mrf24j40 module to receive only
* packets using plain 802.15.4 * packets using plain 802.15.4
* Requirements: 3 pins for spi, 3 pins for reset, chip select and interrupt * Requirements: 3 pins for spi, 3 pins for reset, chip select and interrupt
* notifications * notifications
@ -21,15 +21,15 @@ void setup() {
mrf.set_pan(0xcafe); mrf.set_pan(0xcafe);
// This is _our_ address // This is _our_ address
mrf.address16_write(0x6001); mrf.address16_write(0x6001);
mrf.set_channel(12);
// uncomment if you want to receive any packet on this channel // uncomment if you want to receive any packet on this channel
// mrf.set_promiscuous(true); // mrf.set_promiscuous(true);
attachInterrupt(0, interrupt_routine, CHANGE); attachInterrupt(0, interrupt_routine, CHANGE);
interrupts();
} }
volatile byte gotrx; volatile uint8_t gotrx;
void interrupt_routine() { void interrupt_routine() {
// read and clear from the radio // read and clear from the radio
@ -41,25 +41,31 @@ void interrupt_routine() {
void loop() { void loop() {
int tmp; int tmp;
interrupts();
if (gotrx) { if (gotrx) {
gotrx = 0; gotrx = 0;
noInterrupts(); noInterrupts();
mrf.write_short(MRF_BBREG1, 0x04); // RXDECINV - disable receiver mrf.rx_disable();
// read start of rxfifo
byte frame_length = mrf.read_long(0x300);
Serial.print("received a packet ");
Serial.print(frame_length, DEC);
Serial.println(" bytes long");
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:"); Serial.println("Packet data:");
for (int i = 1; i <= frame_length; i++) { for (int i = 1; i <= frame_length; i++) {
tmp = mrf.read_long(0x300 + i); tmp = mrf.read_long(0x300 + i);
Serial.print(tmp, HEX); Serial.print(tmp, HEX);
} }
Serial.print("\r\nLQI/RSSI="); Serial.print("\r\nLQI/RSSI=");
byte lqi = mrf.read_long(0x300 + frame_length + 1); byte lqi = mrf.read_long(0x300 + frame_length + 1);
byte rssi = mrf.read_long(0x300 + frame_length + 2); byte rssi = mrf.read_long(0x300 + frame_length + 2);
Serial.print(lqi, HEX); Serial.print(lqi, HEX);
Serial.println(rssi, HEX); Serial.println(rssi, HEX);
mrf.write_short(MRF_BBREG1, 0x00); // RXDECINV - enable receiver mrf.rx_enable();
interrupts(); interrupts();
} }
} }

View File

@ -36,7 +36,7 @@ void interrupt_routine() {
byte last_interrupt = mrf.read_short(MRF_INTSTAT); byte last_interrupt = mrf.read_short(MRF_INTSTAT);
// we don't care about the rx acks, but we need them to be flushed so it can keep receiving // we don't care about the rx acks, but we need them to be flushed so it can keep receiving
if (last_interrupt & MRF_I_RXIF) { if (last_interrupt & MRF_I_RXIF) {
mrf.write_short(MRF_RXFLUSH, 0x01); mrf.rx_flush();
} }
} }

View File

@ -175,3 +175,15 @@ void Mrf24j::set_promiscuous(boolean enabled) {
write_short(MRF_RXMCR, 0x00); write_short(MRF_RXMCR, 0x00);
} }
} }
void Mrf24j::rx_flush(void) {
write_short(MRF_RXFLUSH, 0x01);
}
void Mrf24j::rx_disable(void) {
write_short(MRF_BBREG1, 0x04); // RXDECINV - disable receiver
}
void Mrf24j::rx_enable(void) {
write_short(MRF_BBREG1, 0x00); // RXDECINV - enable receiver
}

View File

@ -173,6 +173,12 @@ void set_promiscuous(boolean enabled);
*/ */
void set_channel(byte channel); void set_channel(byte channel);
void rx_enable(void);
void rx_disable(void);
/** If you want to throw away rx data */
void rx_flush(void);
void send16(word dest16, byte len, char * data); void send16(word dest16, byte len, char * data);
private: private: