diff --git a/ports/mimxrt10xx/boards.c b/ports/mimxrt10xx/boards.c index 350bd717f..6af4861e5 100644 --- a/ports/mimxrt10xx/boards.c +++ b/ports/mimxrt10xx/boards.c @@ -80,11 +80,27 @@ void board_init(void) GPIO_PinInit(NEOPIXEL_PORT, NEOPIXEL_PIN, &neopixel_config); #endif +#ifdef TINYUF2_DFU_BUTTON + // Button + IOMUXC_SetPinMux( BUTTON_PINMUX, 1U); + IOMUXC_SetPinConfig(BUTTON_PINMUX, 0x01B0A0U); + gpio_pin_config_t button_config = { kGPIO_DigitalInput, 0, kGPIO_NoIntmode }; + GPIO_PinInit(BUTTON_PORT, BUTTON_PIN, &button_config); +#endif + #if TUF2_LOG board_uart_init(BOARD_UART_BAUDRATE); #endif } +#ifdef TINYUF2_DFU_BUTTON +int board_button_read(void) +{ + // active low + return BUTTON_STATE_ACTIVE == GPIO_PinRead(BUTTON_PORT, BUTTON_PIN); +} +#endif + void board_teardown(void) { // no GPIO deinit for GPIO: LED, Neopixel, Button diff --git a/src/board_api.h b/src/board_api.h index 5180eca84..6065ca55b 100644 --- a/src/board_api.h +++ b/src/board_api.h @@ -49,6 +49,16 @@ #define TINYUF2_DFU_DOUBLE_TAP 0 #endif +// Force boot to DFU mode when button is pressed +#ifndef TINYUF2_DFU_BUTTON +#define TINYUF2_DFU_BUTTON 0 +// Should holding the DFU button perform an erase as well? +# ifndef TINYUF2_DFU_BUTTON_ERASE +# define TINYUF2_DFU_BUTTON_ERASE 0 +# endif +#endif + + // Use Display to draw DFU image #ifndef TINYUF2_DISPLAY #define TINYUF2_DISPLAY 0 @@ -91,6 +101,11 @@ void board_reset(void); // Write PWM duty value to LED void board_led_write(uint32_t value); +#if TINYUF2_DFU_BUTTON + // Read button. Return true if pressed + int board_button_read(void); +#endif + // Write color to rgb strip void board_rgb_write(uint8_t const rgb[]); diff --git a/src/main.c b/src/main.c index 39b04b672..a002da95f 100644 --- a/src/main.c +++ b/src/main.c @@ -35,7 +35,6 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ -//#define USE_DFU_BUTTON 1 // timeout for double tap detection #define DBL_TAP_DELAY 500 @@ -104,6 +103,18 @@ int main(void) static bool check_dfu_mode(void) { // TODO enable for all port instead of one with double tap +#if TINYUF2_DFU_BUTTON + // always stay in dfu mode if the button is pressed. + if (board_button_read()) { + // force erase app if forced into bootloader mode. +#if TINYUF2_DFU_BUTTON_ERASE + indicator_set(STATE_WRITING_STARTED); + board_flash_erase_app(); + indicator_set(STATE_WRITING_FINISHED); +#endif + return true; + } +#endif #if TINYUF2_DFU_DOUBLE_TAP // TUF2_LOG1_HEX(&DBL_TAP_REG);