2017-12-21 07:49:14 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft 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 <stdint.h>
|
|
|
|
#include "supervisor/port.h"
|
|
|
|
#include "boards/board.h"
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
#include "nrfx/hal/nrf_power.h"
|
|
|
|
#include "nrfx/drivers/include/nrfx_power.h"
|
|
|
|
|
2018-09-09 15:01:28 -04:00
|
|
|
#include "nrf/cache.h"
|
|
|
|
#include "nrf/clocks.h"
|
|
|
|
#include "nrf/power.h"
|
2018-10-16 11:05:02 -04:00
|
|
|
#include "nrf/timers.h"
|
2018-09-09 15:01:28 -04:00
|
|
|
|
2018-02-03 11:08:51 -05:00
|
|
|
#include "shared-module/gamepad/__init__.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2018-12-28 22:55:29 -05:00
|
|
|
#include "common-hal/bleio/__init__.h"
|
2018-09-25 15:12:10 -04:00
|
|
|
#include "common-hal/busio/I2C.h"
|
|
|
|
#include "common-hal/busio/SPI.h"
|
2019-02-12 22:34:05 -05:00
|
|
|
#include "common-hal/busio/UART.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "common-hal/pulseio/PWMOut.h"
|
2018-10-16 11:05:02 -04:00
|
|
|
#include "common-hal/pulseio/PulseOut.h"
|
2018-12-18 10:05:17 -05:00
|
|
|
#include "common-hal/pulseio/PulseIn.h"
|
2018-02-03 11:08:51 -05:00
|
|
|
#include "tick.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
static void power_warning_handler(void) {
|
|
|
|
reset_into_safe_mode(BROWNOUT);
|
|
|
|
}
|
2018-09-09 15:01:28 -04:00
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
safe_mode_t port_init(void) {
|
2018-09-09 15:01:28 -04:00
|
|
|
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();
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
nrfx_power_pofwarn_config_t power_failure_config;
|
|
|
|
power_failure_config.handler = power_warning_handler;
|
|
|
|
power_failure_config.thr = NRF_POWER_POFTHR_V27;
|
2018-12-06 19:38:14 -05:00
|
|
|
#if NRF_POWER_HAS_VDDH
|
2018-12-06 17:24:20 -05:00
|
|
|
power_failure_config.thrvddh = NRF_POWER_POFTHRVDDH_V27;
|
2018-12-06 19:38:14 -05:00
|
|
|
#endif
|
2018-12-06 17:24:20 -05:00
|
|
|
nrfx_power_pof_init(&power_failure_config);
|
|
|
|
nrfx_power_pof_enable(&power_failure_config);
|
|
|
|
|
2018-09-09 15:01:28 -04:00
|
|
|
nrf_peripherals_enable_cache();
|
2017-12-21 07:49:14 -05:00
|
|
|
|
|
|
|
// Configure millisecond timer initialization.
|
|
|
|
tick_init();
|
|
|
|
|
2018-09-09 15:01:28 -04:00
|
|
|
// Will do usb_init() if chip supports USB.
|
|
|
|
board_init();
|
2017-12-21 07:49:14 -05:00
|
|
|
|
|
|
|
return NO_SAFE_MODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_port(void) {
|
2018-09-25 15:12:10 -04:00
|
|
|
#ifdef CIRCUITPY_GAMEPAD_TICKS
|
|
|
|
gamepad_reset();
|
|
|
|
#endif
|
2018-02-03 11:08:51 -05:00
|
|
|
|
2018-09-25 15:12:10 -04:00
|
|
|
i2c_reset();
|
|
|
|
spi_reset();
|
2019-02-12 22:34:05 -05:00
|
|
|
uart_reset();
|
2017-12-21 07:49:14 -05:00
|
|
|
pwmout_reset();
|
2018-10-16 11:05:02 -04:00
|
|
|
pulseout_reset();
|
2018-12-18 10:05:17 -05:00
|
|
|
pulsein_reset();
|
2018-10-16 11:05:02 -04:00
|
|
|
timers_reset();
|
2018-09-25 15:12:10 -04:00
|
|
|
|
2018-12-28 22:55:29 -05:00
|
|
|
bleio_reset();
|
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
reset_all_pins();
|
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void reset_to_bootloader(void) {
|
|
|
|
enum { DFU_MAGIC_SERIAL = 0x4e };
|
|
|
|
|
|
|
|
NRF_POWER->GPREGRET = DFU_MAGIC_SERIAL;
|
2018-12-06 17:24:20 -05:00
|
|
|
reset_cpu();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_cpu(void) {
|
2018-10-19 21:46:22 -04:00
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
extern uint32_t _ebss;
|
|
|
|
// Place the word to save just after our BSS section that gets blanked.
|
|
|
|
void port_set_saved_word(uint32_t value) {
|
|
|
|
_ebss = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t port_get_saved_word(void) {
|
|
|
|
return _ebss;
|
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
void HardFault_Handler(void) {
|
|
|
|
reset_into_safe_mode(HARD_CRASH);
|
2018-10-16 11:05:02 -04:00
|
|
|
while (true) {
|
2018-12-06 17:24:20 -05:00
|
|
|
asm("nop;");
|
2018-10-16 11:05:02 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|