diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index 6e19e7645e..d3c31b51a6 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -77,6 +77,82 @@ static void power_warning_handler(void) { reset_into_safe_mode(BROWNOUT); } +#ifdef MY_DEBUGUART +#include +#include +#include "nrfx.h" +#include "nrf_uart.h" +#include "nrfx_uart.h" +const nrfx_uarte_t _dbg_uart_inst = NRFX_UARTE_INSTANCE(1); +static int _dbg_uart_initialized = 0; +#define DBG_PBUF_LEN 80 +static char _dbg_pbuf[DBG_PBUF_LEN+1]; + +void _debug_uart_init(void) { + //if (_dbg_uart_initialized) return; + nrfx_uarte_config_t config = { + .pseltxd = 26, + .pselrxd = 15, + .pselcts = NRF_UARTE_PSEL_DISCONNECTED, + .pselrts = NRF_UARTE_PSEL_DISCONNECTED, + .p_context = NULL, + .baudrate = NRF_UART_BAUDRATE_115200, + .interrupt_priority = 7, + .hal_cfg = { + .hwfc = NRF_UARTE_HWFC_DISABLED, + .parity = NRF_UARTE_PARITY_EXCLUDED + } + }; + nrfx_uarte_init(&_dbg_uart_inst, &config, NULL); + // drive config + nrf_gpio_cfg(config.pseltxd, + NRF_GPIO_PIN_DIR_OUTPUT, + NRF_GPIO_PIN_INPUT_DISCONNECT, + NRF_GPIO_PIN_PULLUP, // orig=NOPULL + NRF_GPIO_PIN_H0H1, // orig=S0S1 + NRF_GPIO_PIN_NOSENSE); + _dbg_uart_initialized = 1; + return; +} + +void _debug_print_substr(const char* text, uint32_t length) { + char* data = (char*)text; + int siz; + while(length != 0) { + if (length <= DBG_PBUF_LEN) { + siz = length; + } + else { + siz = DBG_PBUF_LEN; + } + memcpy(_dbg_pbuf, data, siz); + _dbg_pbuf[siz] = 0; + nrfx_uarte_tx(&_dbg_uart_inst, (uint8_t const*)_dbg_pbuf, siz); + data += siz; + length -= siz; + } +} + +void _debug_uart_deinit(void) { + nrfx_uarte_uninit(&_dbg_uart_inst); +} + +int dbg_printf(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int ret = vprintf(fmt, ap); + va_end(ap); + return ret; +} + +extern void _debug_led_init(void); +extern void _debug_led_set(int v); +#else /*!MY_DEBUGUART*/ +int dbg_printf(const char *fmt, ...) { + return 0; +} +#endif + const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(2); const nrfx_rtc_config_t rtc_config = { @@ -149,7 +225,6 @@ void tick_init(void) { } } - safe_mode_t port_init(void) { nrf_peripherals_clocks_init(); @@ -201,7 +276,6 @@ safe_mode_t port_init(void) { return NO_SAFE_MODE; } - void reset_port(void) { #ifdef CIRCUITPY_GAMEPAD_TICKS gamepad_reset(); diff --git a/supervisor/serial.h b/supervisor/serial.h index 6cd8530b36..457ee5336a 100644 --- a/supervisor/serial.h +++ b/supervisor/serial.h @@ -48,6 +48,7 @@ char serial_read(void); bool serial_bytes_available(void); bool serial_connected(void); +// XXX used in nrf52-sleep debug int dbg_printf(const char *fmt, ...)__attribute__((format (printf, 1, 2))); #endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H diff --git a/supervisor/shared/serial.c b/supervisor/shared/serial.c index b5d4ef919c..d2fde39a64 100644 --- a/supervisor/shared/serial.c +++ b/supervisor/shared/serial.c @@ -37,82 +37,11 @@ #include "tusb.h" #ifdef MY_DEBUGUART -#include -#include -#include "nrfx.h" -#include "nrf_uart.h" -#include "nrfx_uart.h" -const nrfx_uarte_t _dbg_uart_inst = NRFX_UARTE_INSTANCE(1); -static int _dbg_uart_initialized = 0; -#define DBG_PBUF_LEN 80 -static char _dbg_pbuf[DBG_PBUF_LEN+1]; - -void _debug_uart_init(void) { - //if (_dbg_uart_initialized) return; - nrfx_uarte_config_t config = { - .pseltxd = 26, - .pselrxd = 15, - .pselcts = NRF_UARTE_PSEL_DISCONNECTED, - .pselrts = NRF_UARTE_PSEL_DISCONNECTED, - .p_context = NULL, - .baudrate = NRF_UART_BAUDRATE_115200, - .interrupt_priority = 7, - .hal_cfg = { - .hwfc = NRF_UARTE_HWFC_DISABLED, - .parity = NRF_UARTE_PARITY_EXCLUDED - } - }; - nrfx_uarte_init(&_dbg_uart_inst, &config, NULL); - // drive config - nrf_gpio_cfg(config.pseltxd, - NRF_GPIO_PIN_DIR_OUTPUT, - NRF_GPIO_PIN_INPUT_DISCONNECT, - NRF_GPIO_PIN_PULLUP, // orig=NOPULL - NRF_GPIO_PIN_H0H1, // orig=S0S1 - NRF_GPIO_PIN_NOSENSE); - _dbg_uart_initialized = 1; - return; -} - -void _debug_print_substr(const char* text, uint32_t length) { - char* data = (char*)text; - int siz; - while(length != 0) { - if (length <= DBG_PBUF_LEN) { - siz = length; - } - else { - siz = DBG_PBUF_LEN; - } - memcpy(_dbg_pbuf, data, siz); - _dbg_pbuf[siz] = 0; - nrfx_uarte_tx(&_dbg_uart_inst, (uint8_t const*)_dbg_pbuf, siz); - data += siz; - length -= siz; - } -} - -void _debug_uart_deinit(void) { - nrfx_uarte_uninit(&_dbg_uart_inst); -} - -int dbg_printf(const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - int ret = vprintf(fmt, ap); - va_end(ap); - return ret; -} - -extern void _debug_led_init(void); -extern void _debug_led_set(int v); -#else /*!MY_DEBUGUART*/ -int dbg_printf(const char *fmt, ...) { - return 0; -} +// XXX these functions are in nrf/supervisor/port.c +extern void _debug_uart_init(void); +extern void _debug_print_substr(const char* text, uint32_t length); #endif - /* * Note: DEBUG_UART currently only works on STM32, * enabling on another platform will cause a crash. @@ -143,7 +72,6 @@ void serial_early_init(void) { #ifdef MY_DEBUGUART _debug_uart_init(); - _debug_led_init(); #endif }