I'm trying to make a device which has Bluetooth LE connectivity that will be battery powered.
So far I've used the Adafruit nRF8001 library which has had some attention from Paul in the past correcting the AVR specificity. I've managed to get the nRF8001 module working with the basic echo demo, and it works perfectly.
However, whenever I try to sleep using Duffs Snooze library (https://github.com/duff2013/Snooze) the module stops working; i.e. doesn't process the next event.
I've tried emulating sleep, by using a `while` loop waiting for a LOW signal on the RDY line from the nRF8001 and that works fine. But when I replace this with the `Snooze.sleep(config)` line, everything falls apart.
I believe I've narrowed it down to the `toggle_eimsk` function inside `hal_aci_tl.cpp`. This seems to disable the interrupts for the Snooze library as well, preventing them from waking the Teensy from sleep.
Thinking that the interrupts might be clashing with each other, I paired pin 0 with pin 2 (nRF8001 RDY) so that I could attach a separate interrupt handler to pin 0. When using my `while` block, the pin levels toggle correctly and everything is good. When I use the Snooze library, the RDY line stays LOW. If I remove the pairing between pin 0 and 2, pin 0 will no longer wake the Teensy 3 from sleep even if I manually ground it. If I attach another wakeup pin, and call `pollACI()`, it also stops working.
I changed all calls to `Serial` to `Serial3` so that I could see what was going on when the Teensy was asleep (Duffs library severs the USB connection).
My C knowledge is pretty limited, so I normally end up grabbing libraries and trying to use them together, but this has gotten the better of me
Is there any way to get these 2 libraries to work together?
EDIT: Should've added source code.
I modified the `pollACI` function to return true if there is nothing to do and false otherwise
So far I've used the Adafruit nRF8001 library which has had some attention from Paul in the past correcting the AVR specificity. I've managed to get the nRF8001 module working with the basic echo demo, and it works perfectly.
However, whenever I try to sleep using Duffs Snooze library (https://github.com/duff2013/Snooze) the module stops working; i.e. doesn't process the next event.
I've tried emulating sleep, by using a `while` loop waiting for a LOW signal on the RDY line from the nRF8001 and that works fine. But when I replace this with the `Snooze.sleep(config)` line, everything falls apart.
I believe I've narrowed it down to the `toggle_eimsk` function inside `hal_aci_tl.cpp`. This seems to disable the interrupts for the Snooze library as well, preventing them from waking the Teensy from sleep.
Thinking that the interrupts might be clashing with each other, I paired pin 0 with pin 2 (nRF8001 RDY) so that I could attach a separate interrupt handler to pin 0. When using my `while` block, the pin levels toggle correctly and everything is good. When I use the Snooze library, the RDY line stays LOW. If I remove the pairing between pin 0 and 2, pin 0 will no longer wake the Teensy 3 from sleep even if I manually ground it. If I attach another wakeup pin, and call `pollACI()`, it also stops working.
I changed all calls to `Serial` to `Serial3` so that I could see what was going on when the Teensy was asleep (Duffs library severs the USB connection).
My C knowledge is pretty limited, so I normally end up grabbing libraries and trying to use them together, but this has gotten the better of me
Is there any way to get these 2 libraries to work together?
EDIT: Should've added source code.
I modified the `pollACI` function to return true if there is nothing to do and false otherwise
Code:
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include <Snooze.h>
// nRF8001 pins
#define NRF_RQN 23
#define NRF_RDY 2
#define NRF_RST 6
#define NRF_ACT 3
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(NRF_RQN, NRF_RDY, NRF_RST);
SnoozeBlock config;
aci_evt_opcode_t status;
#define SNOOZE_ENABLED
bool canSleep = false;
#ifndef SNOOZE_ENABLED
volatile bool canContinue = false;
#endif
void setup(void) {
Serial3.begin(115200);
delay(2000);
Serial3.println("STARTING");
// Make sure we start with the radio fully configured
BTLEserial.begin();
while (status != ACI_EVT_DEVICE_STARTED) {
status = BTLEserial.getState();
BTLEserial.pollACI();
}
#ifdef SNOOZE_ENABLED
config.pinMode(14, INPUT, FALLING);
config.pinMode(0, INPUT_PULLUP, FALLING);
#else
attachInterrupt(0, paired_interrupt, LOW);
#endif
// LED
pinMode(16, OUTPUT);
}
#ifndef SNOOZE_ENABLED
void paired_interrupt() {
canContinue = true;
detachInterrupt(0);
}
#endif
void loop() {
canSleep = BTLEserial.pollACI();
//status = BTLEserial.getState();
// Only sleep if the RQN line is HIGH
// if (digitalRead(0) == HIGH){// && status == ACI_EVT_DEVICE_STARTED) {
if (canSleep) {
Serial3.print("SLEEP\t");
Serial3.println(millis());
Serial3.flush();
#ifdef SNOOZE_ENABLED
Snooze.sleep(config);
#else
while (!canContinue) {}
canContinue = false;
attachInterrupt(0, paired_interrupt, LOW);
#endif
canSleep = false;
Serial3.println("WAKE");
}
delay(10);
}
Last edited: