Skip to content

Commit 1358a1b

Browse files
adding sense functions and sleep example
1 parent 7ae3dc4 commit 1358a1b

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

cores/nRF5/wiring_constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ extern "C"{
3030
#define OUTPUT (0x1)
3131
#define INPUT_PULLUP (0x2)
3232
#define INPUT_PULLDOWN (0x3)
33+
#define INPUT_PULLUP_SENSE (0x4)
34+
#define INPUT_PULLDOWN_SENSE (0x5)
3335

3436
#define PI 3.1415926535897932384626433832795
3537
#define HALF_PI 1.5707963267948966192313216916398

cores/nRF5/wiring_digital.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ void pinMode( uint32_t ulPin, uint32_t ulMode )
5555
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
5656
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
5757
break;
58+
59+
case INPUT_PULLUP_SENSE:
60+
// Set pin to input mode with pull-up resistor enabled and sense when Low
61+
port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
62+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
63+
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
64+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
65+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Low << GPIO_PIN_CNF_SENSE_Pos);
66+
break;
5867

5968
case INPUT_PULLDOWN:
6069
// Set pin to input mode with pull-down resistor enabled
@@ -65,6 +74,15 @@ void pinMode( uint32_t ulPin, uint32_t ulMode )
6574
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
6675
break;
6776

77+
case INPUT_PULLDOWN_SENSE:
78+
// Set pin to input mode with pull-down resistor enabled
79+
port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
80+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
81+
| ((uint32_t)GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos)
82+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
83+
| ((uint32_t)GPIO_PIN_CNF_SENSE_High << GPIO_PIN_CNF_SENSE_Pos);
84+
break;
85+
6886
case OUTPUT:
6987
// Set pin to output mode
7088
port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Blink_sleep
3+
Turns on an LED on for one second, then off for one second, until the time to go to sleep runs out.
4+
5+
Based on the blinky arduino example
6+
7+
This example code is in the public domain.
8+
9+
modified 19 Jan 2010
10+
by Pierre Constantineau
11+
12+
*/
13+
14+
#define WAKE_LOW_PIN PIN_A0
15+
#define WAKE_HIGH_PIN PIN_A1
16+
17+
#define SLEEPING_DELAY 30000 // sleep after 30 seconds of blinking
18+
19+
void gotoSleep(unsigned long time)
20+
{
21+
// shutdown when time reaches SLEEPING_DELAY ms
22+
if ((time>SLEEPING_DELAY))
23+
{
24+
// to reduce power consumption when sleeping, turn off all your LEDs (and other power hungry devices)
25+
digitalwrite(LED_BUILTIN, LOW);
26+
27+
// setup your wake-up pins.
28+
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.
29+
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.
30+
31+
// power down nrf52.
32+
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.
33+
}
34+
}
35+
36+
// the setup function runs once when you press reset or power the board
37+
void setup() {
38+
// initialize digital pin LED_BUILTIN as an output.
39+
pinMode(LED_BUILTIN, OUTPUT);
40+
}
41+
42+
// the loop function runs over and over again forever
43+
void loop() {
44+
digitalToggle(LED_BUILTIN); // turn the LED on (HIGH is the voltage level)
45+
gotoSleep(millis()); // call millis() and pass it to the sleep function. On wake-up, millis will start at 0 again.
46+
delay(1000); // wait for a second
47+
}

0 commit comments

Comments
 (0)