summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xports/nrf/Makefile3
-rw-r--r--ports/nrf/boards/feather_nrf52832/board.c29
-rw-r--r--ports/nrf/boards/feather_nrf52840_express/board.c8
-rw-r--r--ports/nrf/boards/pca10040/board.c1
-rw-r--r--ports/nrf/boards/pca10056/board.c13
-rw-r--r--ports/nrf/boards/pca10059/board.c10
-rw-r--r--ports/nrf/common-hal/digitalio/DigitalInOut.c62
-rw-r--r--ports/nrf/common-hal/digitalio/DigitalInOut.h2
-rw-r--r--ports/nrf/peripherals/nrf/cache.c38
-rw-r--r--ports/nrf/peripherals/nrf/cache.h28
-rw-r--r--ports/nrf/peripherals/nrf/clocks.c35
-rw-r--r--ports/nrf/peripherals/nrf/clocks.h27
-rw-r--r--ports/nrf/peripherals/nrf/nrf52832/power.c30
-rw-r--r--ports/nrf/peripherals/nrf/nrf52840/power.c40
-rw-r--r--ports/nrf/peripherals/nrf/power.h27
-rw-r--r--ports/nrf/supervisor/port.c19
16 files changed, 271 insertions, 101 deletions
diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile
index d3dc23262..0e9d224be 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 dde63b553..29ee2fdcd 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 5ad81c21d..a6d050fce 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 <string.h>
-#include <stdbool.h>
-
-#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 5ad81c21d..f1a49a74a 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 0b667aa9d..a6d050fce 100644
--- a/ports/nrf/boards/pca10056/board.c
+++ b/ports/nrf/boards/pca10056/board.c
@@ -24,18 +24,10 @@
* THE SOFTWARE.
*/
-#include <string.h>
-#include <stdbool.h>
#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 0b667aa9d..cbb4ef3b6 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 b7a1a5001..f05cf568f 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) {
+ 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);
+
+ 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;
}
-
- 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 afe8a8b42..9122ba4a1 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 000000000..02e11c461
--- /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 000000000..d9ba63f3d
--- /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 000000000..811c0161a
--- /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 000000000..e815d849f
--- /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 000000000..942524f53
--- /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 000000000..9f7a9fa17
--- /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 000000000..c3744618c
--- /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 bfde35e62..e6bb25458 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;
// }
}
-