use open-drain capabilities on GPIO; clean up board init; set correct GPIO voltage

This commit is contained in:
Dan Halbert 2018-09-09 15:01:28 -04:00
parent 06a610fd0c
commit e335c74ac6
16 changed files with 272 additions and 102 deletions

View File

@ -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/,\

View File

@ -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);
}

View File

@ -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) {

View File

@ -32,7 +32,6 @@
#include "boards/board.h"
void board_init(void) {
}
bool board_requests_safe_mode(void) {

View File

@ -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) {
}

View File

@ -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) {
}

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -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) {
}

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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;
// }
}