From 27f5fc681c4039802977b6fe26623360f36af4de Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 28 Nov 2017 22:15:56 +0100 Subject: [PATCH] DFU: Move "Override DFU" (force DFU) code to board-specific section Each board can define its own conditions on which the controller should boot into DFU mode rather than normal application mode. Let's move the "UART loopback jumper" to QMOD specific part. For SIMtrace we have an actual button and can use that in a future patch. --- firmware/apps/dfu/main.c | 29 +++------------ .../libboard/common/include/board_common.h | 1 + firmware/libboard/qmod/source/board_qmod.c | 36 +++++++++++++++++++ 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/firmware/apps/dfu/main.c b/firmware/apps/dfu/main.c index 5f683ce0..01cf1e01 100644 --- a/firmware/apps/dfu/main.c +++ b/firmware/apps/dfu/main.c @@ -105,31 +105,10 @@ int USBDFU_handle_upload(uint8_t altif, unsigned int offset, return req_len; } -static int uart_has_loopback_jumper(void) +/* can be overridden by board specific code, e.g. by pushbutton */ +WEAK int board_override_enter_dfu(void) { - unsigned int i; - const Pin uart_loopback_pins[] = { - {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT}, - {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT} - }; - - /* Configure UART pins as I/O */ - PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins)); - - for (i = 0; i < 10; i++) { - /* Set TxD high; abort if RxD doesn't go high either */ - PIO_Set(&uart_loopback_pins[1]); - if (!PIO_Get(&uart_loopback_pins[0])) - return 0; - /* Set TxD low, abort if RxD doesn't go low either */ - PIO_Clear(&uart_loopback_pins[1]); - if (PIO_Get(&uart_loopback_pins[0])) - return 0; - } - /* if we reached here, RxD always follows TxD and thus a - * loopback jumper has been placed on RxD/TxD, and we will boot - * into DFU unconditionally */ - return 1; + return 0; } /* using this function we can determine if we should enter DFU mode @@ -139,7 +118,7 @@ int USBDFU_OverrideEnterDFU(void) uint32_t *app_part = (uint32_t *)FLASH_ADDR(0); /* If the loopback jumper is set, we enter DFU mode */ - if (uart_has_loopback_jumper()) + if (board_override_enter_dfu()) return 1; /* if the first word of the application partition doesn't look diff --git a/firmware/libboard/common/include/board_common.h b/firmware/libboard/common/include/board_common.h index cfcc49e5..9cd1f89f 100644 --- a/firmware/libboard/common/include/board_common.h +++ b/firmware/libboard/common/include/board_common.h @@ -119,4 +119,5 @@ extern void board_exec_dbg_cmd(int ch); extern void board_main_top(void); +extern int board_override_enter_dfu(void); #endif diff --git a/firmware/libboard/qmod/source/board_qmod.c b/firmware/libboard/qmod/source/board_qmod.c index 49625793..0343895c 100644 --- a/firmware/libboard/qmod/source/board_qmod.c +++ b/firmware/libboard/qmod/source/board_qmod.c @@ -249,3 +249,39 @@ void board_main_top(void) card_present_init(); #endif } + +static int uart_has_loopback_jumper(void) +{ + unsigned int i; + const Pin uart_loopback_pins[] = { + {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT}, + {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT} + }; + + /* Configure UART pins as I/O */ + PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins)); + + for (i = 0; i < 10; i++) { + /* Set TxD high; abort if RxD doesn't go high either */ + PIO_Set(&uart_loopback_pins[1]); + if (!PIO_Get(&uart_loopback_pins[0])) + return 0; + /* Set TxD low, abort if RxD doesn't go low either */ + PIO_Clear(&uart_loopback_pins[1]); + if (PIO_Get(&uart_loopback_pins[0])) + return 0; + } + /* if we reached here, RxD always follows TxD and thus a + * loopback jumper has been placed on RxD/TxD, and we will boot + * into DFU unconditionally */ + return 1; +} + +int board_override_enter_dfu(void) +{ + /* If the loopback jumper is set, we enter DFU mode */ + if (uart_has_loopback_jumper()) + return 1; + + return 0; +}