Skip to content

Commit 7680fa3

Browse files
committed
tested pico and arduino interruptConfigure examples
1 parent f31b461 commit 7680fa3

File tree

3 files changed

+83
-65
lines changed

3 files changed

+83
-65
lines changed

examples/InterruptConfigure/InterruptConfigure.ino

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#include "RF24.h"
1919

2020
// We will be using the nRF24L01's IRQ pin for this example
21-
#define IRQ_PIN 2 // this needs to be a digital input capable pin
22-
volatile bool got_interrupt = false; // used to signal processing of interrupt
23-
bool wait_for_event = false; // used to wait for an IRQ event to trigger
21+
#define IRQ_PIN 2 // this needs to be a digital input capable pin
22+
volatile bool got_interrupt = false; // used to signal processing of interrupt
23+
volatile bool wait_for_event = false; // used to wait for an IRQ event to trigger
2424

2525
#define CE_PIN 7
2626
#define CSN_PIN 8
@@ -138,44 +138,10 @@ void setup() {
138138

139139
void loop() {
140140
if (got_interrupt) {
141-
// print IRQ status and all masking flags' states
142-
143-
Serial.println(F("\tIRQ pin is actively LOW")); // show that this function was called
144-
delayMicroseconds(250);
145-
bool tx_ds, tx_df, rx_dr; // declare variables for IRQ masks
146-
radio.whatHappened(tx_ds, tx_df, rx_dr); // get values for IRQ masks
147-
// whatHappened() clears the IRQ masks also. This is required for
148-
// continued TX operations when a transmission fails.
149-
// clearing the IRQ masks resets the IRQ pin to its inactive state (HIGH)
150-
151-
Serial.print(F("\tdata_sent: "));
152-
Serial.print(tx_ds); // print "data sent" mask state
153-
Serial.print(F(", data_fail: "));
154-
Serial.print(tx_df); // print "data fail" mask state
155-
Serial.print(F(", data_ready: "));
156-
Serial.println(rx_dr); // print "data ready" mask state
157-
158-
if (tx_df) // if TX payload failed
159-
radio.flush_tx(); // clear all payloads from the TX FIFO
160-
161-
// print if test passed or failed. Unintentional fails mean the RX node was not listening.
162-
// pl_iterator has already been incremented by now
163-
if (pl_iterator <= 1) {
164-
Serial.print(F(" 'Data Ready' event test "));
165-
Serial.println(rx_dr ? F("passed") : F("failed"));
166-
} else if (pl_iterator == 2) {
167-
Serial.print(F(" 'Data Sent' event test "));
168-
Serial.println(tx_ds ? F("passed") : F("failed"));
169-
} else if (pl_iterator == 4) {
170-
Serial.print(F(" 'Data Fail' event test "));
171-
Serial.println(tx_df ? F("passed") : F("failed"));
172-
}
173-
got_interrupt = false;
174-
wait_for_event = false;
141+
assessInterruptEvent();
175142
}
176-
if (role && !wait_for_event) {
177143

178-
// delay(1); // wait for IRQ pin to fully RISE
144+
if (role && !wait_for_event) {
179145

180146
// This device is a TX node. This if block is only triggered when
181147
// NOT waiting for an IRQ event to happen
@@ -255,25 +221,22 @@ void loop() {
255221
pl_iterator++; // proceed from step 3 to last step (stop at step 4 for readability)
256222
}
257223

258-
} else if (!role) {
259-
// This device is a RX node
260-
261-
if (radio.rxFifoFull()) {
262-
// wait until RX FIFO is full then stop listening
224+
} else if (!role && radio.rxFifoFull()) {
225+
// This device is a RX node:
226+
// wait until RX FIFO is full then stop listening
263227

264-
delay(100); // let ACK payload finish transmitting
265-
radio.stopListening(); // also discards unused ACK payloads
266-
printRxFifo(); // flush the RX FIFO
228+
delay(100); // let ACK payload finish transmitting
229+
radio.stopListening(); // also discards unused ACK payloads
230+
printRxFifo(); // flush the RX FIFO
267231

268-
// Fill the TX FIFO with 3 ACK payloads for the first 3 received
269-
// transmissions on pipe 1.
270-
radio.writeAckPayload(1, &ack_payloads[0], ack_pl_size);
271-
radio.writeAckPayload(1, &ack_payloads[1], ack_pl_size);
272-
radio.writeAckPayload(1, &ack_payloads[2], ack_pl_size);
232+
// Fill the TX FIFO with 3 ACK payloads for the first 3 received
233+
// transmissions on pipe 1.
234+
radio.writeAckPayload(1, &ack_payloads[0], ack_pl_size);
235+
radio.writeAckPayload(1, &ack_payloads[1], ack_pl_size);
236+
radio.writeAckPayload(1, &ack_payloads[2], ack_pl_size);
273237

274-
delay(100); // let TX node finish its role
275-
radio.startListening(); // We're ready to start over. Begin listening.
276-
}
238+
delay(100); // let TX node finish its role
239+
radio.startListening(); // We're ready to start over. Begin listening.
277240

278241
} // role
279242

@@ -319,13 +282,53 @@ void loop() {
319282

320283

321284
/**
322-
* when the IRQ pin goes active LOW, call this fuction print out why
285+
* when the IRQ pin goes active LOW.
286+
* Here we just tell the main loop() to call `assessInterruptEve4nt()`.
323287
*/
324288
void interruptHandler() {
325-
got_interrupt = true;
326-
wait_for_event = false; // ready to continue with loop() operations
327-
} // interruptHandler
289+
got_interrupt = true; // forward event handling back to main loop()
290+
}
328291

292+
/**
293+
* Called when an event has been triggered.
294+
* Here, we want to verify the expected IRQ flag has been asserted.
295+
*/
296+
void assessInterruptEvent() {
297+
// print IRQ status and all masking flags' states
298+
299+
Serial.println(F("\tIRQ pin is actively LOW")); // show that this function was called
300+
delayMicroseconds(250);
301+
bool tx_ds, tx_df, rx_dr; // declare variables for IRQ masks
302+
radio.whatHappened(tx_ds, tx_df, rx_dr); // get values for IRQ masks
303+
// whatHappened() clears the IRQ masks also. This is required for
304+
// continued TX operations when a transmission fails.
305+
// clearing the IRQ masks resets the IRQ pin to its inactive state (HIGH)
306+
307+
Serial.print(F("\tdata_sent: "));
308+
Serial.print(tx_ds); // print "data sent" mask state
309+
Serial.print(F(", data_fail: "));
310+
Serial.print(tx_df); // print "data fail" mask state
311+
Serial.print(F(", data_ready: "));
312+
Serial.println(rx_dr); // print "data ready" mask state
313+
314+
if (tx_df) // if TX payload failed
315+
radio.flush_tx(); // clear all payloads from the TX FIFO
316+
317+
// print if test passed or failed. Unintentional fails mean the RX node was not listening.
318+
// pl_iterator has already been incremented by now
319+
if (pl_iterator <= 1) {
320+
Serial.print(F(" 'Data Ready' event test "));
321+
Serial.println(rx_dr ? F("passed") : F("failed"));
322+
} else if (pl_iterator == 2) {
323+
Serial.print(F(" 'Data Sent' event test "));
324+
Serial.println(tx_ds ? F("passed") : F("failed"));
325+
} else if (pl_iterator == 4) {
326+
Serial.print(F(" 'Data Fail' event test "));
327+
Serial.println(tx_df ? F("passed") : F("failed"));
328+
}
329+
got_interrupt = false; // reset this flag to prevent calling this function from loop()
330+
wait_for_event = false; // ready to continue with loop() operations
331+
}
329332

330333
/**
331334
* Print the entire RX FIFO with one buffer. This will also flush the RX FIFO.

examples_linux/interruptConfigure.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ void ping_n_wait()
289289
}
290290

291291
/**
292-
* when the IRQ pin goes active LOW, call this fuction print out why
292+
* when the IRQ pin goes active LOW.
293+
* Here we just set a flag to unblock ping_n_wait()
293294
*/
294295
void interruptHandler()
295296
{

examples_pico/interruptConfigure.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
// We will be using the nRF24L01's IRQ pin for this example
2323
volatile bool wait_for_event = false; // used to wait for an IRQ event to trigger
24+
volatile bool got_interrupt = false; // used to signal when an IRQ event has been triggered
2425

2526
// instantiate an object for the nRF24L01 transceiver
2627
RF24 radio(CE_PIN, CSN_PIN);
@@ -40,6 +41,7 @@ char ack_payloads[][ack_pl_size + 1] = {"Yak ", "Back", " ACK"};
4041

4142
void interruptHandler(uint gpio, uint32_t events); // prototype to handle IRQ events
4243
void printRxFifo(); // prototype to print RX FIFO with 1 buffer
44+
void assessInterruptEvent(); // prototype to assess IRQ flags triggered
4345

4446
bool setup()
4547
{
@@ -121,7 +123,6 @@ bool setup()
121123
}
122124

123125
// For debugging info
124-
// printf_begin(); // needed only once for printing details
125126
// radio.printDetails(); // (smaller) function that prints raw register values
126127
// radio.printPrettyDetails(); // (larger) function that prints human readable data
127128

@@ -231,6 +232,10 @@ void loop()
231232

232233
} // role
233234

235+
if (got_interrupt) {
236+
assessInterruptEvent();
237+
}
238+
234239
char input = getchar_timeout_us(0); // get char from buffer for user input
235240
if (input != PICO_ERROR_TIMEOUT) {
236241
// change the role via the serial terminal
@@ -239,7 +244,7 @@ void loop()
239244
// Become the TX node
240245
if (!role)
241246
printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n");
242-
else if (role && wait_for_event) // don't interrupt on ongoing test
247+
else if (role && wait_for_event) // don't interrupt an ongoing test
243248
return; // exit now; start next loop()
244249
else
245250
printf("*** RESTARTING IRQ PIN TEST ***\n");
@@ -277,16 +282,24 @@ void loop()
277282
} // loop
278283

279284
/**
280-
* when the IRQ pin goes active LOW, call this fuction print out why
285+
* when the IRQ pin goes active LOW.
286+
* Here we just tell the main loop() to call `assessInterruptEve4nt()`.
281287
*/
282288
void interruptHandler(uint gpio, uint32_t events)
283289
{
284-
285290
if (gpio != IRQ_PIN && !(events | GPIO_IRQ_EDGE_FALL)) {
286291
// the gpio pin and event does not match the configuration we specified
287292
return;
288293
}
294+
got_interrupt = true; // forward event handling back to main loop()
295+
}
289296

297+
/**
298+
* Called when an event has been triggered.
299+
* Here, we want to verify the expected IRQ flag has been asserted.
300+
*/
301+
void assessInterruptEvent()
302+
{
290303
// print IRQ status and all masking flags' states
291304
printf("\tIRQ pin is actively LOW\n"); // show that this function was called
292305
bool tx_ds, tx_df, rx_dr; // declare variables for IRQ masks
@@ -315,8 +328,9 @@ void interruptHandler(uint gpio, uint32_t events)
315328
else if (pl_iterator == 4) {
316329
printf(" 'Data Fail' event test %s\n", tx_df ? "passed" : "failed");
317330
}
331+
got_interrupt = false; // reset this flag to prevent calling this function from loop()
318332
wait_for_event = false; // ready to continue with loop() operations
319-
} // interruptHandler
333+
}
320334

321335
/**
322336
* Print the entire RX FIFO with one buffer. This will also flush the RX FIFO.

0 commit comments

Comments
 (0)