From effe21711146633bfaf6eb9d7255d7d40075fafc Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Sun, 13 Mar 2011 19:49:07 +0000 Subject: [PATCH] Add more wrapper functions to hide more of the raw register accesses --- examples/Basic_TwoWay/Basic_TwoWay.pde | 7 +++---- examples/RX_only/RX_only.pde | 20 +++++++++++++------- examples/TX_only/TX_only.pde | 2 +- mrf24j.cpp | 12 ++++++++++++ mrf24j.h | 6 ++++++ 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/examples/Basic_TwoWay/Basic_TwoWay.pde b/examples/Basic_TwoWay/Basic_TwoWay.pde index 6aa79da..f77dcd7 100644 --- a/examples/Basic_TwoWay/Basic_TwoWay.pde +++ b/examples/Basic_TwoWay/Basic_TwoWay.pde @@ -34,11 +34,10 @@ void setup() { volatile uint8_t gotrx; volatile uint8_t txok; -volatile uint8_t last_interrupt; void interrupt_routine() { // 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) { gotrx = 1; } @@ -72,7 +71,7 @@ void loop() { if (gotrx) { gotrx = 0; noInterrupts(); - mrf.write_short(MRF_BBREG1, 0x04); // RXDECINV - disable receiver + mrf.rx_disable(); 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"); @@ -87,7 +86,7 @@ void loop() { Serial.print(lqi, HEX); Serial.println(rssi, HEX); - mrf.write_short(MRF_BBREG1, 0x00); // RXDECINV - enable receiver + mrf.rx_enable(); interrupts(); } diff --git a/examples/RX_only/RX_only.pde b/examples/RX_only/RX_only.pde index f3a8c7b..07d598c 100644 --- a/examples/RX_only/RX_only.pde +++ b/examples/RX_only/RX_only.pde @@ -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 * Requirements: 3 pins for spi, 3 pins for reset, chip select and interrupt * notifications @@ -21,15 +21,15 @@ void setup() { mrf.set_pan(0xcafe); // This is _our_ address mrf.address16_write(0x6001); + mrf.set_channel(12); // uncomment if you want to receive any packet on this channel // mrf.set_promiscuous(true); attachInterrupt(0, interrupt_routine, CHANGE); - interrupts(); } -volatile byte gotrx; +volatile uint8_t gotrx; void interrupt_routine() { // read and clear from the radio @@ -41,25 +41,31 @@ void interrupt_routine() { void loop() { int tmp; + interrupts(); if (gotrx) { gotrx = 0; 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:"); for (int i = 1; i <= frame_length; i++) { tmp = mrf.read_long(0x300 + i); Serial.print(tmp, HEX); } + Serial.print("\r\nLQI/RSSI="); 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.write_short(MRF_BBREG1, 0x00); // RXDECINV - enable receiver + mrf.rx_enable(); interrupts(); } } diff --git a/examples/TX_only/TX_only.pde b/examples/TX_only/TX_only.pde index ab103fc..89c12a2 100644 --- a/examples/TX_only/TX_only.pde +++ b/examples/TX_only/TX_only.pde @@ -36,7 +36,7 @@ void interrupt_routine() { 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 if (last_interrupt & MRF_I_RXIF) { - mrf.write_short(MRF_RXFLUSH, 0x01); + mrf.rx_flush(); } } diff --git a/mrf24j.cpp b/mrf24j.cpp index ba8bfaf..38f113c 100644 --- a/mrf24j.cpp +++ b/mrf24j.cpp @@ -175,3 +175,15 @@ void Mrf24j::set_promiscuous(boolean enabled) { 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 +} diff --git a/mrf24j.h b/mrf24j.h index 1dbfe20..dc55949 100644 --- a/mrf24j.h +++ b/mrf24j.h @@ -173,6 +173,12 @@ void set_promiscuous(boolean enabled); */ 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); private: