From e335c74ac63dbd30823fe159b9ef2d984e97d937 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 9 Sep 2018 15:01:28 -0400 Subject: [PATCH] use open-drain capabilities on GPIO; clean up board init; set correct GPIO voltage --- ports/nrf/Makefile | 3 + ports/nrf/boards/feather_nrf52832/board.c | 29 --------- .../boards/feather_nrf52840_express/board.c | 8 +-- ports/nrf/boards/pca10040/board.c | 1 - ports/nrf/boards/pca10056/board.c | 13 ---- ports/nrf/boards/pca10059/board.c | 10 --- ports/nrf/common-hal/digitalio/DigitalInOut.c | 64 ++++++++----------- ports/nrf/common-hal/digitalio/DigitalInOut.h | 2 - ports/nrf/peripherals/nrf/cache.c | 38 +++++++++++ ports/nrf/peripherals/nrf/cache.h | 28 ++++++++ ports/nrf/peripherals/nrf/clocks.c | 35 ++++++++++ ports/nrf/peripherals/nrf/clocks.h | 27 ++++++++ ports/nrf/peripherals/nrf/nrf52832/power.c | 30 +++++++++ ports/nrf/peripherals/nrf/nrf52840/power.c | 40 ++++++++++++ ports/nrf/peripherals/nrf/power.h | 27 ++++++++ ports/nrf/supervisor/port.c | 19 +++++- 16 files changed, 272 insertions(+), 102 deletions(-) create mode 100644 ports/nrf/peripherals/nrf/cache.c create mode 100644 ports/nrf/peripherals/nrf/cache.h create mode 100644 ports/nrf/peripherals/nrf/clocks.c create mode 100644 ports/nrf/peripherals/nrf/clocks.h create mode 100644 ports/nrf/peripherals/nrf/nrf52832/power.c create mode 100644 ports/nrf/peripherals/nrf/nrf52840/power.c create mode 100644 ports/nrf/peripherals/nrf/power.h diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index d3dc23262d..0e9d224be7 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -118,7 +118,10 @@ SRC_C += \ lib/utils/sys_stdio_mphal.c \ nrfx/hal/nrf_nvmc.c \ nrfx/mdk/system_$(MCU_SUB_VARIANT).c \ + peripherals/nrf/cache.c \ + peripherals/nrf/clocks.c \ peripherals/nrf/$(MCU_CHIP)/pins.c \ + peripherals/nrf/$(MCU_CHIP)/power.c \ supervisor/shared/memory.c DRIVERS_SRC_C += $(addprefix modules/,\ diff --git a/ports/nrf/boards/feather_nrf52832/board.c b/ports/nrf/boards/feather_nrf52832/board.c index dde63b5532..29ee2fdcdb 100644 --- a/ports/nrf/boards/feather_nrf52832/board.c +++ b/ports/nrf/boards/feather_nrf52832/board.c @@ -31,41 +31,12 @@ #include "boards/board.h" -// Must match temp register in bootloader -#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0] -uint32_t bootloaderVersion = 0; - void board_init(void) { - // Retrieve bootloader version - bootloaderVersion = BOOTLOADER_VERSION_REGISTER; } -// Check the status of the two buttons on CircuitPlayground Express. If both are -// pressed, then boot into user safe mode. bool board_requests_safe_mode(void) { -// gpio_set_pin_function(PIN_PA14, GPIO_PIN_FUNCTION_OFF); -// gpio_set_pin_direction(PIN_PA14, GPIO_DIRECTION_IN); -// gpio_set_pin_pull_mode(PIN_PA14, GPIO_PULL_DOWN); -// -// gpio_set_pin_function(PIN_PA28, GPIO_PIN_FUNCTION_OFF); -// gpio_set_pin_direction(PIN_PA28, GPIO_DIRECTION_IN); -// gpio_set_pin_pull_mode(PIN_PA28, GPIO_PULL_DOWN); -// bool safe_mode = gpio_get_pin_level(PIN_PA14) && -// gpio_get_pin_level(PIN_PA28); -// reset_pin_number(PIN_PA14); -// reset_pin_number(PIN_PA28); -// return safe_mode; - return false; } void reset_board(void) { -// uint8_t empty[30]; -// memset(empty, 0, 30); -// digitalio_digitalinout_obj_t neopixel_pin; -// common_hal_digitalio_digitalinout_construct(&neopixel_pin, &pin_PB23); -// common_hal_digitalio_digitalinout_switch_to_output(&neopixel_pin, false, -// DRIVE_MODE_PUSH_PULL); -// common_hal_neopixel_write(&neopixel_pin, empty, 30); -// common_hal_digitalio_digitalinout_deinit(&neopixel_pin); } diff --git a/ports/nrf/boards/feather_nrf52840_express/board.c b/ports/nrf/boards/feather_nrf52840_express/board.c index 5ad81c21d7..a6d050fce2 100644 --- a/ports/nrf/boards/feather_nrf52840_express/board.c +++ b/ports/nrf/boards/feather_nrf52840_express/board.c @@ -24,15 +24,11 @@ * THE SOFTWARE. */ -#include -#include - -#include "nrf.h" - #include "boards/board.h" +#include "usb.h" void board_init(void) { - + usb_init(); } bool board_requests_safe_mode(void) { diff --git a/ports/nrf/boards/pca10040/board.c b/ports/nrf/boards/pca10040/board.c index 5ad81c21d7..f1a49a74a7 100644 --- a/ports/nrf/boards/pca10040/board.c +++ b/ports/nrf/boards/pca10040/board.c @@ -32,7 +32,6 @@ #include "boards/board.h" void board_init(void) { - } bool board_requests_safe_mode(void) { diff --git a/ports/nrf/boards/pca10056/board.c b/ports/nrf/boards/pca10056/board.c index 0b667aa9d9..a6d050fce2 100644 --- a/ports/nrf/boards/pca10056/board.c +++ b/ports/nrf/boards/pca10056/board.c @@ -24,18 +24,10 @@ * THE SOFTWARE. */ -#include -#include #include "boards/board.h" -#include "nrfx.h" #include "usb.h" void board_init(void) { - - // Clock - NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); - NRF_CLOCK->TASKS_LFCLKSTART = 1UL; - usb_init(); } @@ -46,8 +38,3 @@ bool board_requests_safe_mode(void) { void reset_board(void) { } - - - - - diff --git a/ports/nrf/boards/pca10059/board.c b/ports/nrf/boards/pca10059/board.c index 0b667aa9d9..cbb4ef3b6d 100644 --- a/ports/nrf/boards/pca10059/board.c +++ b/ports/nrf/boards/pca10059/board.c @@ -31,11 +31,6 @@ #include "usb.h" void board_init(void) { - - // Clock - NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); - NRF_CLOCK->TASKS_LFCLKSTART = 1UL; - usb_init(); } @@ -46,8 +41,3 @@ bool board_requests_safe_mode(void) { void reset_board(void) { } - - - - - diff --git a/ports/nrf/common-hal/digitalio/DigitalInOut.c b/ports/nrf/common-hal/digitalio/DigitalInOut.c index b7a1a5001b..f05cf568f7 100644 --- a/ports/nrf/common-hal/digitalio/DigitalInOut.c +++ b/ports/nrf/common-hal/digitalio/DigitalInOut.c @@ -34,8 +34,6 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { claim_pin(pin); self->pin = pin; - self->output = false; - self->open_drain = false; nrf_gpio_cfg_input(pin->number, NRF_GPIO_PIN_NOPULL); @@ -58,7 +56,6 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self void common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { - self->output = false; nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL); common_hal_digitalio_digitalinout_set_pull(self, pull); } @@ -66,62 +63,54 @@ void common_hal_digitalio_digitalinout_switch_to_input( void common_hal_digitalio_digitalinout_switch_to_output( digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode) { - self->output = true; - self->open_drain = (drive_mode == DRIVE_MODE_OPEN_DRAIN); - - nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL); + common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode); common_hal_digitalio_digitalinout_set_value(self, value); } digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( digitalio_digitalinout_obj_t *self) { - return self->output ? DIRECTION_OUTPUT : DIRECTION_INPUT; + + return (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_INPUT) + ? DIRECTION_INPUT : DIRECTION_OUTPUT; } void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t *self, bool value) { - if (value && self->open_drain) { - nrf_gpio_pin_dir_set(self->pin->number, NRF_GPIO_PIN_DIR_INPUT); - } else { - nrf_gpio_pin_dir_set(self->pin->number, NRF_GPIO_PIN_DIR_OUTPUT); - nrf_gpio_pin_write(self->pin->number, value); - } + nrf_gpio_pin_write(self->pin->number, value); } bool common_hal_digitalio_digitalinout_get_value( digitalio_digitalinout_obj_t *self) { - if (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_INPUT) { - if (self->open_drain) { - return true; - } - - return nrf_gpio_pin_read(self->pin->number); - } - - return nrf_gpio_pin_out_read(self->pin->number); + return (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_INPUT) + ? nrf_gpio_pin_read(self->pin->number) + : nrf_gpio_pin_out_read(self->pin->number); } void common_hal_digitalio_digitalinout_set_drive_mode( digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode) { - const bool value = common_hal_digitalio_digitalinout_get_value(self); - self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; - - // True is implemented differently between modes so reset the value to make - // sure its correct for the new mode. - if (value) { - common_hal_digitalio_digitalinout_set_value(self, value); - } + nrf_gpio_cfg(self->pin->number, + NRF_GPIO_PIN_DIR_OUTPUT, + NRF_GPIO_PIN_INPUT_DISCONNECT, + NRF_GPIO_PIN_NOPULL, + drive_mode == DRIVE_MODE_OPEN_DRAIN ? NRF_GPIO_PIN_H0D1 : NRF_GPIO_PIN_H0H1, + NRF_GPIO_PIN_NOSENSE); } digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( digitalio_digitalinout_obj_t *self) { - if (self->open_drain) { - return DRIVE_MODE_OPEN_DRAIN; - } + uint32_t pin = self->pin->number; + // Changes pin to be a relative pin number in port. + NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&pin); - return DRIVE_MODE_PUSH_PULL; + switch ((reg->PIN_CNF[pin] & GPIO_PIN_CNF_DRIVE_Msk) >> GPIO_PIN_CNF_DRIVE_Pos) { + case NRF_GPIO_PIN_S0D1: + case NRF_GPIO_PIN_H0D1: + return DRIVE_MODE_OPEN_DRAIN; + default: + return DRIVE_MODE_PUSH_PULL; + } } void common_hal_digitalio_digitalinout_set_pull( @@ -151,16 +140,13 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( if (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_OUTPUT) { mp_raise_AttributeError(translate("Cannot get pull while in output mode")); - return PULL_NONE; } - switch (reg->PIN_CNF[pin] & GPIO_PIN_CNF_PULL_Msk) { + switch ((reg->PIN_CNF[pin] & GPIO_PIN_CNF_PULL_Msk) >> GPIO_PIN_CNF_PULL_Pos) { case NRF_GPIO_PIN_PULLUP: return PULL_UP; - case NRF_GPIO_PIN_PULLDOWN: return PULL_DOWN; - default: return PULL_NONE; } diff --git a/ports/nrf/common-hal/digitalio/DigitalInOut.h b/ports/nrf/common-hal/digitalio/DigitalInOut.h index afe8a8b42f..9122ba4a13 100644 --- a/ports/nrf/common-hal/digitalio/DigitalInOut.h +++ b/ports/nrf/common-hal/digitalio/DigitalInOut.h @@ -32,8 +32,6 @@ typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; - bool output; - bool open_drain; } digitalio_digitalinout_obj_t; #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_DIGITALIO_DIGITALINOUT_H diff --git a/ports/nrf/peripherals/nrf/cache.c b/ports/nrf/peripherals/nrf/cache.c new file mode 100644 index 0000000000..02e11c4613 --- /dev/null +++ b/ports/nrf/peripherals/nrf/cache.c @@ -0,0 +1,38 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "nrfx.h" + +// Turn off cache and invalidate all data in it. +void nrf_peripherals_disable_and_clear_cache(void) { + // Disabling cache also invalidates all cache entries. + NRF_NVMC->ICACHECNF &= ~(1 << NVMC_ICACHECNF_CACHEEN_Pos); +} + +// Enable cache +void nrf_peripherals_enable_cache(void) { + NRF_NVMC->ICACHECNF |= 1 << NVMC_ICACHECNF_CACHEEN_Pos; +} diff --git a/ports/nrf/peripherals/nrf/cache.h b/ports/nrf/peripherals/nrf/cache.h new file mode 100644 index 0000000000..d9ba63f3db --- /dev/null +++ b/ports/nrf/peripherals/nrf/cache.h @@ -0,0 +1,28 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +void nrf_peripherals_disable_and_clear_cache(void); +void nrf_peripherals_enable_cache(void); diff --git a/ports/nrf/peripherals/nrf/clocks.c b/ports/nrf/peripherals/nrf/clocks.c new file mode 100644 index 0000000000..811c0161a9 --- /dev/null +++ b/ports/nrf/peripherals/nrf/clocks.c @@ -0,0 +1,35 @@ + +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "nrfx.h" + +void nrf_peripherals_clocks_init(void) { + // Set low-frequency clock source to be crystal. If there's a crystalless board, this will need to be + // generalized. + NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); + NRF_CLOCK->TASKS_LFCLKSTART = 1UL; +} diff --git a/ports/nrf/peripherals/nrf/clocks.h b/ports/nrf/peripherals/nrf/clocks.h new file mode 100644 index 0000000000..e815d849ff --- /dev/null +++ b/ports/nrf/peripherals/nrf/clocks.h @@ -0,0 +1,27 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +void nrf_peripherals_clocks_init(void); diff --git a/ports/nrf/peripherals/nrf/nrf52832/power.c b/ports/nrf/peripherals/nrf/nrf52832/power.c new file mode 100644 index 0000000000..942524f530 --- /dev/null +++ b/ports/nrf/peripherals/nrf/nrf52832/power.c @@ -0,0 +1,30 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "nrfx.h" + +void nrf_peripherals_power_init(void) { +} diff --git a/ports/nrf/peripherals/nrf/nrf52840/power.c b/ports/nrf/peripherals/nrf/nrf52840/power.c new file mode 100644 index 0000000000..9f7a9fa17a --- /dev/null +++ b/ports/nrf/peripherals/nrf/nrf52840/power.c @@ -0,0 +1,40 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "nrfx.h" +#include "nrf_nvmc.h" + +void nrf_peripherals_power_init(void) { + // Set GPIO reference voltage to 3.3V if it isn't already. REGOUT0 will get reset to 0xfffffff + // if flash is erased, which sets the default to 1.8V + // This matters only when "high voltage mode" is enabled, which is true on the PCA10059, + // and might be true on other boards. + if (NRF_UICR->REGOUT0 == 0xffffffff) { + nrf_nvmc_write_word((uint32_t) &NRF_UICR->REGOUT0, UICR_REGOUT0_VOUT_3V3 << UICR_REGOUT0_VOUT_Pos); + // Must reset to make enable change. + NVIC_SystemReset(); + } +} diff --git a/ports/nrf/peripherals/nrf/power.h b/ports/nrf/peripherals/nrf/power.h new file mode 100644 index 0000000000..c3744618ca --- /dev/null +++ b/ports/nrf/peripherals/nrf/power.h @@ -0,0 +1,27 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +void nrf_peripherals_power_init(void); diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index bfde35e62e..e6bb254583 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -28,13 +28,24 @@ #include "supervisor/port.h" #include "boards/board.h" +#include "nrf/cache.h" +#include "nrf/clocks.h" +#include "nrf/power.h" + #include "shared-module/gamepad/__init__.h" #include "common-hal/microcontroller/Pin.h" #include "common-hal/pulseio/PWMOut.h" #include "tick.h" safe_mode_t port_init(void) { - board_init(); + + nrf_peripherals_clocks_init(); + + // If GPIO voltage is set wrong in UICR, this will fix it, and + // will also do a reset to make the change take effect. + nrf_peripherals_power_init(); + + nrf_peripherals_enable_cache(); // Configure millisecond timer initialization. tick_init(); @@ -46,7 +57,12 @@ safe_mode_t port_init(void) { return HARD_CRASH; } #endif +#endif + // Will do usb_init() if chip supports USB. + board_init(); + +#if 0 if (board_requests_safe_mode()) { return USER_SAFE_MODE; } @@ -80,4 +96,3 @@ void HardFault_Handler(void) // (void)bfar; // } } -