From 53593d1315e589416c78d23cfe7d021d6d1443e8 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 15 May 2018 18:40:49 +0700 Subject: [PATCH] fix #809 define CIRCUITPY_BOOT_OUTPUT_FILE will cause mp_hal_stdout_tx_strn() to invoke before serial_init() is called. Solution is skipped output to serial if it is not inited. --- ports/nrf/boards/feather52/mpconfigboard.mk | 2 +- ports/nrf/hal/hal_uart.c | 4 ++++ ports/nrf/hal/hal_uart.h | 1 + ports/nrf/mpconfigport.h | 2 +- ports/nrf/mphalport.c | 20 +++++++++++++++++--- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/ports/nrf/boards/feather52/mpconfigboard.mk b/ports/nrf/boards/feather52/mpconfigboard.mk index 1e3aa5a249..07aec3f791 100644 --- a/ports/nrf/boards/feather52/mpconfigboard.mk +++ b/ports/nrf/boards/feather52/mpconfigboard.mk @@ -31,7 +31,7 @@ __check_defined = \ .PHONY: dfu-gen dfu-flash boot-flash dfu-gen: - $(NRFUTIL) dfu genpkg --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip + $(NRFUTIL) dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip dfu-flash: @:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0) diff --git a/ports/nrf/hal/hal_uart.c b/ports/nrf/hal/hal_uart.c index e8d4230816..b0a57a2a6e 100644 --- a/ports/nrf/hal/hal_uart.c +++ b/ports/nrf/hal/hal_uart.c @@ -156,6 +156,10 @@ void hal_uart_init(NRF_UART_Type * p_instance, hal_uart_init_t const * p_uart_in NVIC_EnableIRQ(p_uart_init->irq_num); } +bool hal_uart_inited(NRF_UART_Type * p_instance) +{ + return !(p_instance->PSELTXD & (1 << 31)) && !(p_instance->PSELRXD & (1 << 31)); +} void UARTE0_UART0_IRQHandler(void) { diff --git a/ports/nrf/hal/hal_uart.h b/ports/nrf/hal/hal_uart.h index 76c09b335f..47f10cce1b 100644 --- a/ports/nrf/hal/hal_uart.h +++ b/ports/nrf/hal/hal_uart.h @@ -117,6 +117,7 @@ typedef struct typedef void (*uart_complete_cb)(void); void hal_uart_init(NRF_UART_Type * p_instance, hal_uart_init_t const * p_uart_init); +bool hal_uart_inited(NRF_UART_Type * p_instance); hal_uart_error_t hal_uart_char_write(NRF_UART_Type * p_instance, uint8_t ch); diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index a463108b87..ac5741f771 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -330,6 +330,6 @@ extern const struct _mp_obj_module_t ble_module; #include #define MICROPY_PIN_DEFS_PORT_H "pin_defs_nrf5.h" -//#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt" +#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt" #endif diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 957d3535d2..824ce859ae 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -65,9 +65,23 @@ bool mp_hal_stdin_any(void) { } void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { - while(len--) { - hal_uart_char_write(UART_INSTANCE, *str++); - } + +// #ifdef MICROPY_HW_LED_TX +// gpio_toggle_pin_level(MICROPY_HW_LED_TX); +// #endif +// +// #ifdef CIRCUITPY_BOOT_OUTPUT_FILE +// if (boot_output_file != NULL) { +// UINT bytes_written = 0; +// f_write(boot_output_file, str, len, &bytes_written); +// } +// #endif + + if ( hal_uart_inited(UART_INSTANCE) ) { + while(len--) { + hal_uart_char_write(UART_INSTANCE, *str++); + } + } } void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {