some cleanups.

This commit is contained in:
jun2sak 2021-02-21 09:57:14 +09:00
parent d659c2ce34
commit 36c59250dd
2 changed files with 107 additions and 102 deletions

View File

@ -51,7 +51,8 @@
#include "nrfx.h"
#include "nrfx_gpiote.h"
#if 0
// XXX
#define DEBUG_LED_PIN (NRF_GPIO_PIN_MAP(1, 11)) // P1_11 = LED
void _debug_led_init(void) {
nrf_gpio_cfg_output(DEBUG_LED_PIN);
@ -59,7 +60,7 @@ void _debug_led_init(void) {
void _debug_led_set(int v) {
nrf_gpio_pin_write(DEBUG_LED_PIN, v);
}
#endif
// Singleton instance of SleepMemory.
const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
@ -84,10 +85,10 @@ STATIC nrf_sleep_source_t _get_wakeup_cause(void) {
return NRF_SLEEP_WAKEUP_TIMER;
}
#if 0
// XXX not implemented yet
if (alarm_touch_touchalarm_woke_us_up()) {
return ESP_SLEEP_WAKEUP_TOUCHPAD;
}
return esp_sleep_get_wakeup_cause();
#endif
if (reset_reason_saved & NRF_POWER_RESETREAS_RESETPIN_MASK) {
return NRF_SLEEP_WAKEUP_RESETPIN;
@ -130,16 +131,16 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
#endif
switch (cause) {
case NRF_SLEEP_WAKEUP_TIMER: {
return alarm_time_timealarm_get_wakeup_alarm(n_alarms, alarms);
return alarm_time_timealarm_get_wakeup_alarm(n_alarms, alarms);
}
case NRF_SLEEP_WAKEUP_TOUCHPAD: {
return mp_const_none;
return mp_const_none;
}
case NRF_SLEEP_WAKEUP_GPIO: {
return alarm_pin_pinalarm_get_wakeup_alarm(n_alarms, alarms);
return alarm_pin_pinalarm_get_wakeup_alarm(n_alarms, alarms);
}
default:
break;
break;
}
return mp_const_none;
}
@ -154,6 +155,7 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t
alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms);
alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms);
#if 0
// XXX not implemented yet
alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms);
#endif
}
@ -170,17 +172,17 @@ STATIC void _idle_until_alarm(void) {
if (alarm_woken_from_sleep()) {
alarm_save_wake_alarm();
#ifdef MY_DEBUGUART
int cause = _get_wakeup_cause();
printf("wakeup(%d)\r\n", cause);
int cause = _get_wakeup_cause();
printf("wakeup(%d)\r\n", cause);
#endif
return;
}
port_idle_until_interrupt();
#ifdef MY_DEBUGUART
if (ct > 0) {
printf("_");
--ct;
}
if (ct > 0) {
printf("_");
--ct;
}
#endif
}
}
@ -201,8 +203,8 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
r_obj = mp_const_none;
}
else {
r_obj = _get_wake_alarm(n_alarms, alarms);
alarm_reset();
r_obj = _get_wake_alarm(n_alarms, alarms);
alarm_reset();
}
return r_obj;
}
@ -213,7 +215,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
void NORETURN alarm_enter_deep_sleep(void) {
alarm_pin_pinalarm_prepare_for_deep_sleep();
//alarm_touch_touchalarm_prepare_for_deep_sleep();
//alarm_touch_touchalarm_prepare_for_deep_sleep(); // XXX
uint8_t sd_enabled;
sd_softdevice_is_enabled(&sd_enabled);

View File

@ -78,8 +78,8 @@ bool common_hal_alarm_pin_pinalarm_get_pull(alarm_pin_pinalarm_obj_t *self) {
static void pinalarm_gpiote_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
++_pinhandler_gpiote_count;
_pinhandler_ev_pin = pin;
++_pinhandler_gpiote_count;
_pinhandler_ev_pin = pin;
}
bool alarm_pin_pinalarm_woke_us_up(void) {
@ -136,93 +136,95 @@ void alarm_pin_pinalarm_reset(void) {
pull_pins = 0;
}
void _setup2(void) {
nrfx_gpiote_in_config_t cfg = {
.sense = NRF_GPIOTE_POLARITY_TOGGLE,
.pull = NRF_GPIO_PIN_PULLUP,
.is_watcher = false,
.hi_accuracy = true,
.skip_gpio_setup = false
};
for(size_t i = 0; i < 64; ++i) {
uint64_t mask = 1ull << i;
#ifdef MY_DEBUGUART
int pull = 0;
int sense = 0;
#endif
if (((high_alarms & mask) == 0) && ((low_alarms & mask) == 0)) {
continue;
static void setup_pin1_for_lightsleep(void) {
if ( nrfx_gpiote_is_init() ) {
nrfx_gpiote_uninit();
}
if (((high_alarms & mask) != 0) && ((low_alarms & mask) == 0)) {
cfg.sense = NRF_GPIOTE_POLARITY_LOTOHI;
cfg.pull = ((pull_pins & mask) != 0) ? NRF_GPIO_PIN_PULLDOWN : NRF_GPIO_PIN_NOPULL;
#ifdef MY_DEBUGUART
pull = -1; sense = 1;
#endif
}
else
if (((high_alarms & mask) == 0) && ((low_alarms & mask) != 0)) {
cfg.sense = NRF_GPIOTE_POLARITY_HITOLO;
cfg.pull = ((pull_pins & mask) != 0) ? NRF_GPIO_PIN_PULLUP : NRF_GPIO_PIN_NOPULL;
#ifdef MY_DEBUGUART
pull = 1; sense = -1;
#endif
}
else {
cfg.sense = NRF_GPIOTE_POLARITY_TOGGLE;
cfg.pull = NRF_GPIO_PIN_NOPULL;
#ifdef MY_DEBUGUART
sense = 9;
#endif
}
nrfx_gpiote_in_init((nrfx_gpiote_pin_t)i, &cfg, pinalarm_gpiote_handler);
nrfx_gpiote_in_event_enable((nrfx_gpiote_pin_t)i, true);
#ifdef MY_DEBUGUART
dbg_printf("pin=%d, sense=%d, pull=%d\r\n", i, sense, pull);
#endif
}
}
nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY);
void _setup_pin1_for_lightsleep(void) {
if ( nrfx_gpiote_is_init() ) {
nrfx_gpiote_uninit();
}
nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY);
_pinhandler_gpiote_count = 0;
_pinhandler_ev_pin = MYGPIOTE_EV_PIN_UNDEF;
_pinhandler_gpiote_count = 0;
_pinhandler_ev_pin = MYGPIOTE_EV_PIN_UNDEF;
_setup2();
}
void _setup_pin1_for_deepsleep(void) {
nrfx_gpiote_in_config_t cfg = {
.sense = NRF_GPIOTE_POLARITY_TOGGLE,
.pull = NRF_GPIO_PIN_PULLUP,
.is_watcher = false,
.hi_accuracy = true,
.skip_gpio_setup = false
};
for(size_t i = 0; i < 64; ++i) {
uint64_t mask = 1ull << i;
int pull = 0;
uint64_t mask = 1ull << i;
#ifdef MY_DEBUGUART
int sense = 0;
int pull = 0;
int sense = 0;
#endif
if (((high_alarms & mask) == 0) && ((low_alarms & mask) == 0)) {
continue;
}
if (((high_alarms & mask) != 0) && ((low_alarms & mask) == 0)) {
pull = ((pull_pins & mask) != 0) ? NRF_GPIO_PIN_PULLDOWN : NRF_GPIO_PIN_NOPULL;
nrf_gpio_cfg_input((uint32_t)i, (nrf_gpio_pin_pull_t)pull);
nrf_gpio_cfg_sense_set((uint32_t)i, NRF_GPIO_PIN_SENSE_HIGH);
if (((high_alarms & mask) == 0) && ((low_alarms & mask) == 0)) {
continue;
}
if (((high_alarms & mask) != 0) && ((low_alarms & mask) == 0)) {
cfg.sense = NRF_GPIOTE_POLARITY_LOTOHI;
cfg.pull = ((pull_pins & mask) != 0) ?
NRF_GPIO_PIN_PULLDOWN : NRF_GPIO_PIN_NOPULL;
#ifdef MY_DEBUGUART
sense = NRF_GPIO_PIN_SENSE_HIGH;
pull = -1; sense = 1;
#endif
}
else
if (((high_alarms & mask) == 0) && ((low_alarms & mask) != 0)) {
pull = ((pull_pins & mask) != 0) ? NRF_GPIO_PIN_PULLUP : NRF_GPIO_PIN_NOPULL;
nrf_gpio_cfg_input((uint32_t)i, (nrf_gpio_pin_pull_t)pull);
nrf_gpio_cfg_sense_set((uint32_t)i, NRF_GPIO_PIN_SENSE_LOW);
}
else
if (((high_alarms & mask) == 0) && ((low_alarms & mask) != 0)) {
cfg.sense = NRF_GPIOTE_POLARITY_HITOLO;
cfg.pull = ((pull_pins & mask) != 0) ?
NRF_GPIO_PIN_PULLUP : NRF_GPIO_PIN_NOPULL;
#ifdef MY_DEBUGUART
sense = NRF_GPIO_PIN_SENSE_LOW;
pull = 1; sense = -1;
#endif
}
}
else {
cfg.sense = NRF_GPIOTE_POLARITY_TOGGLE;
cfg.pull = NRF_GPIO_PIN_NOPULL;
#ifdef MY_DEBUGUART
dbg_printf("pin=%d, sense=%d, pull=%d\r\n", i, sense, pull);
sense = 9;
#endif
}
nrfx_gpiote_in_init((nrfx_gpiote_pin_t)i, &cfg,
pinalarm_gpiote_handler);
nrfx_gpiote_in_event_enable((nrfx_gpiote_pin_t)i, true);
#ifdef MY_DEBUGUART
dbg_printf("pin=%d, sense=%d, pull=%d\r\n", i, sense, pull);
#endif
}
}
static void setup_pin1_for_deepsleep(void) {
for(size_t i = 0; i < 64; ++i) {
uint64_t mask = 1ull << i;
int pull = 0;
#ifdef MY_DEBUGUART
int sense = 0;
#endif
if (((high_alarms & mask) == 0) && ((low_alarms & mask) == 0)) {
continue;
}
if (((high_alarms & mask) != 0) && ((low_alarms & mask) == 0)) {
pull = ((pull_pins & mask) != 0) ?
NRF_GPIO_PIN_PULLDOWN : NRF_GPIO_PIN_NOPULL;
nrf_gpio_cfg_input((uint32_t)i, (nrf_gpio_pin_pull_t)pull);
nrf_gpio_cfg_sense_set((uint32_t)i, NRF_GPIO_PIN_SENSE_HIGH);
#ifdef MY_DEBUGUART
sense = NRF_GPIO_PIN_SENSE_HIGH;
#endif
}
else
if (((high_alarms & mask) == 0) && ((low_alarms & mask) != 0)) {
pull = ((pull_pins & mask) != 0) ?
NRF_GPIO_PIN_PULLUP : NRF_GPIO_PIN_NOPULL;
nrf_gpio_cfg_input((uint32_t)i, (nrf_gpio_pin_pull_t)pull);
nrf_gpio_cfg_sense_set((uint32_t)i, NRF_GPIO_PIN_SENSE_LOW);
#ifdef MY_DEBUGUART
sense = NRF_GPIO_PIN_SENSE_LOW;
#endif
}
#ifdef MY_DEBUGUART
dbg_printf("pin=%d, sense=%d, pull=%d\r\n", i, sense, pull);
#endif
}
#if 0
@ -261,18 +263,19 @@ void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_ob
}
}
if (pin_number != -1) {
if (!deep_sleep) {
_setup_pin1_for_lightsleep();
}
else {
//_setup_pin1_for_deepsleep(pin_number);
}
if (!deep_sleep) {
setup_pin1_for_lightsleep();
}
else {
// we don't setup gpio HW here but do them in
// alarm_pin_pinalarm_prepare_for_deep_sleep() below
}
}
else {
//dbg_printf("alarm_pin_pinalarm_set_alarms() no valid pins\r\n");
//dbg_printf("alarm_pin_pinalarm_set_alarms() no valid pins\r\n");
}
}
void alarm_pin_pinalarm_prepare_for_deep_sleep(void) {
_setup_pin1_for_deepsleep();
setup_pin1_for_deepsleep();
}