Skip to content

Commit a443c0e

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 a443c0e

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

ports/mimxrt10xx/boards.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
#include "tusb.h"
4040
#endif
4141

42+
// allow board.h to change the pin configuration for the button
43+
#ifndef BUTTON_PIN_CONFIG
44+
// default to 22k pull up
45+
#define BUTTON_PIN_CONFIG ((1<<16) | (3<<14) | (1<<13) | (1<<12))
46+
#endif
47+
4248
static bool _dfu_mode = false;
4349

4450
// needed by fsl_flexspi_nor_boot
@@ -80,11 +86,27 @@ void board_init(void)
8086
GPIO_PinInit(NEOPIXEL_PORT, NEOPIXEL_PIN, &neopixel_config);
8187
#endif
8288

89+
#if TINYUF2_DFU_BUTTON
90+
// Button
91+
IOMUXC_SetPinMux( BUTTON_PINMUX, 1U);
92+
IOMUXC_SetPinConfig(BUTTON_PINMUX, BUTTON_PIN_CONFIG);
93+
gpio_pin_config_t button_config = { kGPIO_DigitalInput, 0, kGPIO_NoIntmode };
94+
GPIO_PinInit(BUTTON_PORT, BUTTON_PIN, &button_config);
95+
#endif
96+
8397
#if TUF2_LOG
8498
board_uart_init(BOARD_UART_BAUDRATE);
8599
#endif
86100
}
87101

102+
#if TINYUF2_DFU_BUTTON
103+
int board_button_read(void)
104+
{
105+
// active low
106+
return BUTTON_STATE_ACTIVE == GPIO_PinRead(BUTTON_PORT, BUTTON_PIN);
107+
}
108+
#endif
109+
88110
void board_teardown(void)
89111
{
90112
// 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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@
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
4241

42+
// when sensing the button state, wait this long before sampling the pin
43+
#define BUTTON_SETTLE_DELAY 20
44+
4345
#ifndef DBL_TAP_REG
46+
4447
// defined by linker script
4548
extern uint32_t _board_dfu_dbl_tap[];
4649
#define DBL_TAP_REG _board_dfu_dbl_tap[0]
@@ -104,6 +107,24 @@ int main(void)
104107
static bool check_dfu_mode(void)
105108
{
106109
// TODO enable for all port instead of one with double tap
110+
#if TINYUF2_DFU_BUTTON
111+
// always stay in dfu mode if the button is pressed.
112+
// wait for a few milliseconds for the switch pin to reach its pulled value.
113+
_timer_count = 0;
114+
board_timer_start(1);
115+
while(_timer_count < BUTTON_SETTLE_DELAY) {}
116+
board_timer_stop();
117+
if (board_button_read()) {
118+
// force erase app if forced into bootloader mode.
119+
#if TINYUF2_DFU_BUTTON_ERASE
120+
indicator_set(STATE_WRITING_STARTED);
121+
board_flash_erase_app();
122+
indicator_set(STATE_WRITING_FINISHED);
123+
#endif
124+
return true;
125+
}
126+
#endif
127+
107128
#if TINYUF2_DFU_DOUBLE_TAP
108129
// TUF2_LOG1_HEX(&DBL_TAP_REG);
109130

0 commit comments

Comments
 (0)