8000 nrf/modules/machine: Wake from deep sleep using "sense" Pin argument. · micropython/micropython@821da37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 821da37

Browse files
committed
nrf/modules/machine: Wake from deep sleep using "sense" Pin argument.
Pins are configured as wake sources for deep sleep using an additional port-specific argument "sense" to the machine.Pin constructor. Signed-off-by: Christian Walther <cwalther@gmx.ch>
1 parent 4ba22b1 commit 821da37

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

docs/library/machine.Pin.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Usage Model::
4242
Constructors
4343
------------
4444

45-
.. class:: Pin(id, mode=-1, pull=-1, *, value=None, drive=0, alt=-1)
45+
.. class:: Pin(id, mode=-1, pull=-1, *, value=None, drive=0, alt=-1, sense=-1)
4646

4747
Access the pin peripheral (GPIO pin) associated with the given ``id``. If
4848
additional arguments are given in the constructor then they are used to initialise
@@ -97,6 +97,15 @@ Constructors
9797
one pin alternate function is supported the this argument is not required. Not all
9898
ports implement this argument.
9999

100+
- ``sense`` specifies whether and on what input value the pin contributes to the
101+
*DETECT* signal, which wakes an nRF51/52 system from deep sleep. It can be one of:
102+
103+
- ``Pin.SENSE_DISABLED`` - Do not wake from this pin.
104+
- ``Pin.SENSE_LOW`` - Wake when pin is low.
105+
- ``Pin.SENSE_HIGH`` - Wake when pin is high.
106+
107+
Availability: nrf port.
108+
100109
As specified above, the Pin class allows to set an alternate function for a particular
101110
pin, but it does not specify any further operations on such a pin. Pins configured in
102111
alternate-function mode are usually not used as GPIO but are instead driven by other
@@ -108,7 +117,7 @@ Constructors
108117
Methods
109118
-------
110119

111-
.. method:: Pin.init(mode=-1, pull=-1, *, value=None, drive=0, alt=-1)
120+
.. method:: Pin.init(mode=-1, pull=-1, *, value=None, drive=0, alt=-1, sense=-1)
112121

113122
Re-initialise the pin using the given parameters. Only those arguments that
114123
are specified will be set. The rest of the pin peripheral state will remain
@@ -274,3 +283,13 @@ not all constants are available on all ports.
274283
Pin.IRQ_HIGH_LEVEL
275284

276285
Selects the IRQ trigger type.
286+
287+
.. data:: Pin.SENSE_DISABLED
288+
Pin.SENSE_LOW
289+
Pin.SENSE_HIGH
290+
291+
Selects the *SENSE* configuration of the pin, which determines
292+
whether and on what input value it contributes to the *DETECT*
293+
signal, which wakes the system from deep sleep.
294+
295+
Availability: nrf port.

docs/library/machine.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ Power related functions
166166
nRF microcontrollers are incapable of waking on a timer as all clocks are off in
167167
the deep sleep state.
168168

169+
* Pins are configured as wake sources using the ``sense`` argument to the
170+
`Pin` constructor.
171+
169172
.. function:: wake_reason()
170173

171174
Get the wake reason. See :ref:`constants <machine_constants>` for the possible return values.

ports/nrf/modules/machine/pin.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ static mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
349349
{ MP_QSTR_af, MP_ARG_INT, {.u_int = -1}}, // legacy
350350
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
351351
{ MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
352+
{ MP_QSTR_sense, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
352353
};
353354

354355
// parse args
@@ -374,13 +375,21 @@ static mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
374375
nrf_gpio_pin_input_t input = (mode == NRF_GPIO_PIN_DIR_INPUT) ? NRF_GPIO_PIN_INPUT_CONNECT
375376
: NRF_GPIO_PIN_INPUT_DISCONNECT;
376377

378+
// sense mode (default unmodified)
379+
nrf_gpio_pin_sense_t sense = (nrf_gpio_pin_sense_t)args[5].u_int;
380+
if (sense == (nrf_gpio_pin_sense_t)-1) {
381+
sense = nrf_gpio_pin_sense_get(self->pin);
382+
} else if (sense != NRF_GPIO_PIN_NOSENSE && sense != NRF_GPIO_PIN_SENSE_LOW && sense != NRF_GPIO_PIN_SENSE_HIGH) {
383+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin sense: %d"), sense);
384+
}
385+
377386
if (mode == NRF_GPIO_PIN_DIR_OUTPUT || mode == NRF_GPIO_PIN_DIR_INPUT) {
378387
nrf_gpio_cfg(self->pin,
379388
mode,
380389
input,
381390
pull,
382391
NRF_GPIO_PIN_S0S1,
383-
NRF_GPIO_PIN_NOSENSE);
392+
sense);
384393
} else {
385394
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin mode: %d"), mode);
386395
}
@@ -614,6 +623,10 @@ static const mp_rom_map_elem_t pin_locals_dict_table[] = {
614623
{ MP_ROM_QSTR(MP_QSTR_AF_OD), MP_ROM_INT(GPIO_MODE_AF_OD) },
615624
{ MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) },
616625
*/
626+
{ MP_ROM_QSTR(MP_QSTR_SENSE_DISABLED), MP_ROM_INT(NRF_GPIO_PIN_NOSENSE) },
627+
{ MP_ROM_QSTR(MP_QSTR_SENSE_LOW), MP_ROM_INT(NRF_GPIO_PIN_SENSE_LOW) },
628+
{ MP_ROM_QSTR(MP_QSTR_SENSE_HIGH), MP_ROM_INT(NRF_GPIO_PIN_SENSE_HIGH) },
629+
617630
#include "genhdr/pins_af_const.h"
618631
};
619632

0 commit comments

Comments
 (0)
0