From 1358a1bc73cb3f99391cd20fbdc52eaf6bc5f5ff Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 19 Jan 2020 17:08:34 -0600 Subject: [PATCH] adding sense functions and sleep example --- cores/nRF5/wiring_constants.h | 2 + cores/nRF5/wiring_digital.c | 18 +++++++ .../Hardware/blink_sleep/blink_sleep.ino | 47 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 libraries/Bluefruit52Lib/examples/Hardware/blink_sleep/blink_sleep.ino diff --git a/cores/nRF5/wiring_constants.h b/cores/nRF5/wiring_constants.h index 2cbda4ba..5bc30ba5 100644 --- a/cores/nRF5/wiring_constants.h +++ b/cores/nRF5/wiring_constants.h @@ -30,6 +30,8 @@ extern "C"{ #define OUTPUT (0x1) #define INPUT_PULLUP (0x2) #define INPUT_PULLDOWN (0x3) +#define INPUT_PULLUP_SENSE (0x4) +#define INPUT_PULLDOWN_SENSE (0x5) #define PI 3.1415926535897932384626433832795 #define HALF_PI 1.5707963267948966192313216916398 diff --git a/cores/nRF5/wiring_digital.c b/cores/nRF5/wiring_digital.c index ba435c40..d70b4a74 100644 --- a/cores/nRF5/wiring_digital.c +++ b/cores/nRF5/wiring_digital.c @@ -55,6 +55,15 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); break; + + case INPUT_PULLUP_SENSE: + // Set pin to input mode with pull-up resistor enabled and sense when Low + port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos); + break; case INPUT_PULLDOWN: // Set pin to input mode with pull-down resistor enabled @@ -65,6 +74,15 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); break; + case INPUT_PULLDOWN_SENSE: + // Set pin to input mode with pull-down resistor enabled + port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos); + break; + case OUTPUT: // Set pin to output mode port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) diff --git a/libraries/Bluefruit52Lib/examples/Hardware/blink_sleep/blink_sleep.ino b/libraries/Bluefruit52Lib/examples/Hardware/blink_sleep/blink_sleep.ino new file mode 100644 index 00000000..27e22631 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Hardware/blink_sleep/blink_sleep.ino @@ -0,0 +1,47 @@ +/* + Blink_sleep + Turns on an LED on for one second, then off for one second, until the time to go to sleep runs out. + + Based on the blinky arduino example + + This example code is in the public domain. + + modified 19 Jan 2010 + by Pierre Constantineau + +*/ + +#define WAKE_LOW_PIN PIN_A0 +#define WAKE_HIGH_PIN PIN_A1 + +#define SLEEPING_DELAY 30000 // sleep after 30 seconds of blinking + +void gotoSleep(unsigned long time) +{ + // shutdown when time reaches SLEEPING_DELAY ms + if ((time>SLEEPING_DELAY)) + { + // to reduce power consumption when sleeping, turn off all your LEDs (and other power hungry devices) + digitalwrite(LED_BUILTIN, LOW); + + // setup your wake-up pins. + pinModeSense(WAKE_LOW_PIN, INPUT_PULLUP_SENSE); // this pin (WAKE_LOW_PIN) is pulled up and wakes up the feather when externally connected to ground. + pinModeSense(WAKE_HIGH_PIN, INPUT_PULLDOWN_SENSE); // this pin (WAKE_HIGH_PIN) is pulled down and wakes up the feather when externally connected to 3.3v. + + // power down nrf52. + sd_power_system_off(); // this function puts the whole nRF52 to deep sleep (no Bluetooth). If no sense pins are setup (or other hardware interrupts), the nrf52 will not wake up. + } +} + +// the setup function runs once when you press reset or power the board +void setup() { + // initialize digital pin LED_BUILTIN as an output. + pinMode(LED_BUILTIN, OUTPUT); +} + +// the loop function runs over and over again forever +void loop() { + digitalToggle(LED_BUILTIN); // turn the LED on (HIGH is the voltage level) + gotoSleep(millis()); // call millis() and pass it to the sleep function. On wake-up, millis will start at 0 again. + delay(1000); // wait for a second +}