wake up Itsybitsy nRF52840 by internal RTC timer?

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
peterhsi75
 
Posts: 23
Joined: Sat Jan 15, 2022 11:27 pm

wake up Itsybitsy nRF52840 by internal RTC timer?

Post by peterhsi75 »

Does anyone has an example code to wake up Itsybitsy nRF52840 Express board from “system on sleep mode” by internal RTC timers?

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: wake up Itsybitsy nRF52840 by internal RTC timer?

Post by mikeysklar »

Are you look for Arduino example code?

This threads should help get you started although they are not complete.

viewtopic.php?f=24&p=773895
viewtopic.php?f=25&t=188730

Code: Select all

#include "nrf_delay.h" // ADDED: COPIED FROM NRF52 SDK
void buttonHandler() {} // ADDED: STUB

// \brief Main entry point of Arduino application
int main( void )
{
  init();
  initVariant();

#ifdef USE_TINYUSB
  Adafruit_TinyUSB_Core_init();
#endif

#if CFG_DEBUG >= 3
  SEGGER_SYSVIEW_Conf();
#endif

  // BEGIN ADDITION
  pinMode(LED_BUILTIN, OUTPUT);
  nrf_gpio_cfg_sense_input(16, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
  attachInterrupt(digitalPinToInterrupt(16), buttonHandler, CHANGE);
  while(1) {
    digitalWrite(LED_BUILTIN, HIGH);
    nrf_delay_ms(100);
    digitalWrite(LED_BUILTIN, LOW);
    nrf_delay_ms(100);
    __SEV(); // create an event
    __WFE(); // return immediately, clearing all events (including the one created above)
    __WFE(); // wait for the next event
  }
  // END ADDITION (code below here never reached)

  // Create a task for loop()
  xTaskCreate( loop_task, "loop", LOOP_STACK_SZ, NULL, TASK_PRIO_LOW, &_loopHandle);
  // ... rest of the core

User avatar
peterhsi75
 
Posts: 23
Joined: Sat Jan 15, 2022 11:27 pm

Re: wake up Itsybitsy nRF52840 by internal RTC timer?

Post by peterhsi75 »

Thank you very much for your quick response. Your sample sketch seems to rely on a hardware interrupt event. Am I correct?

Yes, I am looking for Arduino code.

I need to wake up a battery powered sensor every hour to take a reading then go back to deep sleep. I do not have access to the board to press a key or other hardware to trigger a hardware interrupt event. It has to rely on an internal timer event to wake up.

I implemented another sketch based on example code of /esp32/deepsleep/timerwakeup.ino for an esp32 board and it worked perfectly. I only need to include two functions:
setup(){
. .
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
. .
}
loop() {
. .
esp_deep_sleep_start();
. .
}

In your example sketch, the 3 functions of _SEV(); _WFE(); _WFE(); is similar to esp_deep_sleep_start() function, right?
But, how to setup a timer event (i.e. define the timer interval - similar to esp_sleep_enable_timer_wakeup() function)?

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: wake up Itsybitsy nRF52840 by internal RTC timer?

Post by mikeysklar »

The second link originally provided was clock based interrupt wakeup. I believe this code was having some issues, but might be worth reviewing.

Code: Select all

 #include <RTClib.h>
#include <Wire.h>
#include <wiring.h>

RTC_DS3231 rtc;
// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 6
//uint32_t pin=6;
uint8_t wake_logic = 0;
volatile byte state = LOW;

void setup()
{

  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  // while(!Serial) delay(10);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC!");Serial.flush(); while (1) delay(10);
  }

  if (rtc.lostPower()) {
    // this will adjust to the date and time at compilation
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  //we don't need the 32K Pin, so disable it
  rtc.disable32K();

  // Making it so, that the alarm will trigger an interrupt
  pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);

  // set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
  // if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
  rtc.clearAlarm(1);
  rtc.clearAlarm(2);

  // stop oscillating signals at SQW Pin
  // otherwise setAlarm1 will fail
  rtc.writeSqwPinMode(DS3231_OFF);

  // turn off alarm 2 (in case it isn't off already)
  // again, this isn't done at reboot, so a previously set alarm could easily go overlooked
  rtc.disableAlarm(2);

  // schedule an alarm 10 seconds in the future
  if (!rtc.setAlarm1(
        rtc.now() + TimeSpan(0, 0, 0, 10),
        DS3231_A1_Second// this mode triggers the alarm when the seconds match. See Doxygen for other options
      )) {
    Serial.println("Error, alarm wasn't set!");
  } else {
    Serial.println("Alarm will happen in 10 seconds!");
  }
   //wake logic is set to 0 in my case
   systemOff(CLOCK_INTERRUPT_PIN, wake_logic);
}
void loop()
{
  // Serial.println("Going To sleep");
  //wake on logic 0;
  digitalWrite(LED_BUILTIN, state);

  Serial.println("Test");
  // print current time
  char date[10] = "hh:mm:ss";
  rtc.now().toString(date);
  Serial.print(date);
  // the value at SQW-Pin (because of pullup 1 means no alarm)
  Serial.print(" SQW: ");
  Serial.print(digitalRead(CLOCK_INTERRUPT_PIN));
  // whether a alarm happened happened
  Serial.print(" Alarm1: ");
  Serial.print(rtc.alarmFired(1));
  // resetting SQW and alarm 1 flag
  // using setAlarm1, the next alarm could now be configurated
  if (rtc.alarmFired(1)) {
    rtc.clearAlarm(1);
    Serial.println("Alarm cleared");
    rtc.disableAlarm(1);
    Serial.println("RECONFIGURING ALARM");
    rtc.setAlarm1(rtc.now() + TimeSpan(0, 0, 0, 10),DS3231_A1_Second);
  }
  delay(2000);
}

void onAlarm()
{
  digitalWrite(LED_BUILTIN, HIGH);
  state = !state;
  Serial.println("Alarm occured ! AWAKE NOW ");
}

User avatar
peterhsi75
 
Posts: 23
Joined: Sat Jan 15, 2022 11:27 pm

Re: wake up Itsybitsy nRF52840 by internal RTC timer?

Post by peterhsi75 »

Thanks again for your sample sketch. This sketch seems to rely on an external DS3232 RTC device. I was hoping to use the built-in RTC counter of nRF52840.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: wake up Itsybitsy nRF52840 by internal RTC timer?

Post by mikeysklar »

I've not seen the internal RTC successfully used with the nRF52840. There is some discussion here about the issues around it and yes another code sample.

viewtopic.php?f=24&t=175815

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”