Skip to content

Commit a611636

Browse files
committed
Add capability to force stay in DFU (and erase app) with button if
TINYUF2_DFU_BUTTON is set to 1. This adds a check to see if a button is pressed. If the board button is pressed, stay if dfu mode. If TINYUF2_DFU_BUTTON_ERASE is also set to 1, then the app is erased.
1 parent c6093a5 commit a611636

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

ports/mimxrt10xx/boards.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,27 @@ void board_init(void)
8080
GPIO_PinInit(NEOPIXEL_PORT, NEOPIXEL_PIN, &neopixel_config);
8181
#endif
8282

83+
#ifdef TINYUF2_DFU_BUTTON
84+
// Button
85+
IOMUXC_SetPinMux( BUTTON_PINMUX, 1U);
86+
IOMUXC_SetPinConfig(BUTTON_PINMUX, 0x01B0A0U);
87+
gpio_pin_config_t button_config = { kGPIO_DigitalInput, 0, kGPIO_NoIntmode };
88+
GPIO_PinInit(BUTTON_PORT, BUTTON_PIN, &button_config);
89+
#endif
90+
8391
#if TUF2_LOG
8492
board_uart_init(BOARD_UART_BAUDRATE);
8593
#endif
8694
}
8795

96+
#ifdef TINYUF2_DFU_BUTTON
97+
int board_button_read(void)
98+
{
99+
// active low
100+
return BUTTON_STATE_ACTIVE == GPIO_PinRead(BUTTON_PORT, BUTTON_PIN);
101+
}
102+
#endif
103+
88104
void board_teardown(void)
89105
{
90106
// no GPIO deinit for GPIO: LED, Neopixel, Button

src/board_api.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
#define TINYUF2_DFU_DOUBLE_TAP 0
5050
#endif
5151

52+
// Force boot to DFU mode when button is pressed
53+
#ifndef TINYUF2_DFU_BUTTON
54+
#define TINYUF2_DFU_BUTTON 0
55+
// Should holding the DFU button perform an erase as well?
56+
# ifndef TINYUF2_DFU_BUTTON_ERASE
57+
# define TINYUF2_DFU_BUTTON_ERASE 0
58+
# endif
59+
#endif
60+
61+
5262
// Use Display to draw DFU image
5363
#ifndef TINYUF2_DISPLAY
5464
#define TINYUF2_DISPLAY 0
@@ -91,6 +101,11 @@ void board_reset(void);
91101
// Write PWM duty value to LED
92102
void board_led_write(uint32_t value);
93103

104+
#if TINYUF2_DFU_BUTTON
105+
// Read button. Return true if pressed
106+
int board_button_read(void);
107+
#endif
108+
94109
// Write color to rgb strip
95110
void board_rgb_write(uint8_t const rgb[]);
96111

src/main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
//--------------------------------------------------------------------+
3636
// MACRO CONSTANT TYPEDEF PROTYPES
3737
//--------------------------------------------------------------------+
38-
//#define USE_DFU_BUTTON 1
3938

4039
// timeout for double tap detection
4140
#define DBL_TAP_DELAY 500
@@ -104,6 +103,18 @@ int main(void)
104103
static bool check_dfu_mode(void)
105104
{
106105
// TODO enable for all port instead of one with double tap
106+
#if TINYUF2_DFU_BUTTON
107+
// always stay in dfu mode if the button is pressed.
108+
if (board_button_read()) {
109+
// force erase app if forced into bootloader mode.
110+
#if TINYUF2_DFU_BUTTON_ERASE
111+
indicator_set(STATE_WRITING_STARTED);
112+
board_flash_erase_app();
113+
indicator_set(STATE_WRITING_FINISHED);
114+
#endif
115+
return true;
116+
}
117+
#endif
107118
#if TINYUF2_DFU_DOUBLE_TAP
108119
// TUF2_LOG1_HEX(&DBL_TAP_REG);
109120

0 commit comments

Comments
 (0)