Skip to content

Commit e8f3249

Browse files
committed
Add capability to force stay in DFU and erase app with button
This adds a check to see if a button is pressed. If the board button is pressed, stay if dfu mode. Behavior is currently: * If button is pressed at power up, enter DFU mode regardless of whether app is valid. * If button is held longer than a timeout (Default 5 seconds), the app is erased. * If button is released within the timeout period, the app is booted, if it's valid.
1 parent 08c47b9 commit e8f3249

File tree

5 files changed

+233
-48
lines changed

5 files changed

+233
-48
lines changed

ports/mimxrt10xx/boards.c

Lines changed: 34 additions & 3 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) | (4<<3))
46+
#endif
47+
4248
static bool _dfu_mode = false;
4349

4450
// needed by fsl_flexspi_nor_boot
@@ -80,11 +86,34 @@ 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+
#ifndef BUILD_NO_TINYUSB
96+
timer_set_ticks(0);
97+
timer_start(1);
98+
while(timer_uptime() < TINYUF2_BUTTON_SETTLE_DELAY);
99+
timer_stop();
100+
timer_set_ticks(0);
101+
#endif
102+
#endif
103+
83104
#if TUF2_LOG
84105
board_uart_init(BOARD_UART_BAUDRATE);
85106
#endif
86107
}
87108

109+
#if TINYUF2_DFU_BUTTON
110+
uint32_t board_button_read(void)
111+
{
112+
uint32_t pressed = !!(BUTTON_STATE_ACTIVE == GPIO_PinRead(BUTTON_PORT, BUTTON_PIN));
113+
return pressed;
114+
}
115+
#endif
116+
88117
void board_teardown(void)
89118
{
90119
// no GPIO deinit for GPIO: LED, Neopixel, Button
@@ -241,8 +270,10 @@ void board_app_jump(void)
241270

242271
void board_timer_start(uint32_t ms)
243272
{
244-
// due to highspeed SystemCoreClock = 600 mhz, max interval of 24 bit systick is only 27 ms
245-
const uint32_t tick = (SystemCoreClock/1000) * ms;
273+
uint32_t tick = (SystemCoreClock/1000) * ms;
274+
if (tick > SysTick_LOAD_RELOAD_Msk)
275+
// if requesting too long an interval, just pick the longest we can support.
276+
tick = SysTick_LOAD_RELOAD_Msk;
246277
SysTick_Config( tick );
247278
}
248279

@@ -253,7 +284,7 @@ void board_timer_stop(void)
253284

254285
void SysTick_Handler(void)
255286
{
256-
board_timer_handler();
287+
board_timer_handler();
257288
}
258289

259290
//--------------------------------------------------------------------+

ports/mimxrt10xx/boards/imxrt1010_evk/board.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
//--------------------------------------------------------------------+
7070

7171
// SW8 button
72+
#define TINYUF2_DFU_BUTTON 1
73+
#define TINYUF2_BUTTON_SETTLE_DELAY 10
7274
#define BUTTON_PINMUX IOMUXC_GPIO_SD_05_GPIO2_IO05
7375
#define BUTTON_PORT GPIO2
7476
#define BUTTON_PIN 5

ports/mimxrt10xx/boards/imxrt1010_evk/board.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MCU = MIMXRT1011
2-
CFLAGS += -DCPU_MIMXRT1011DAE5A
2+
CFLAGS += -DCPU_MIMXRT1011DAE5A -Wno-unused-parameter
33

44
# For flash-jlink target
55
JLINK_DEVICE = MIMXRT1011DAE5A

src/board_api.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,36 @@ void board_reset(void);
9898
// Write PWM duty value to LED
9999
void board_led_write(uint32_t value);
100100

101+
#ifndef TINYUF2_DFU_BUTTON
102+
#define TINYUF2_DFU_BUTTON 0
103+
#endif
104+
105+
#if TINYUF2_DFU_BUTTON
106+
// Read button. Return a bitmask of which buttons are pressed.
107+
uint32_t board_button_read(void);
108+
109+
// Hold the button for this many milliseconds to erase the app.
110+
#ifndef TINYUF2_DFU_BUTTON_ERASE_TIMEOUT
111+
#define TINYUF2_DFU_BUTTON_ERASE_TIMEOUT (5000)
112+
#endif
113+
114+
// This is a bitmask used to check which button will cause a stay-in-dfu-mode
115+
// default to use any button
116+
// I'm somewhat unsure of how to deal with multiple buttons?
117+
// perhaps instead of (or in addition to) board_button_read, there can be a
118+
// weak function called board_button_read_force_dfu() or something like that
119+
// and that will leave the logic of when to enter the forced DFU mode up to
120+
// the individual boards.
121+
#ifndef TINYUF2_DFU_BUTTON_FORCE_MASK
122+
#define TINYUF2_DFU_BUTTON_FORCE_MASK 0xFFFFFFFF
123+
#endif
124+
#ifndef TINYUF2_BUTTON_SETTLE_DELAY
125+
// default to no settling time for button startup. This will
126+
// if more settling time is needed, add it in the board.h file
127+
#define TINYUF2_BUTTON_SETTLE_DELAY 0
128+
#endif
129+
#endif
130+
101131
// Write color to rgb strip
102132
void board_rgb_write(uint8_t const rgb[]);
103133

@@ -251,10 +281,28 @@ enum {
251281
STATE_USB_UNPLUGGED, ///< STATE_USB_UNPLUGGED
252282
STATE_WRITING_STARTED, ///< STATE_WRITING_STARTED
253283
STATE_WRITING_FINISHED, ///< STATE_WRITING_FINISHED
284+
STATE_BUTTON_STAY_IN_DFU, ///< STATE_BUTTON_STAY_IN_DFU
285+
STATE_UNUSED, ///< STATE_UNUSED
254286
};
255287

288+
// Set the state of the app, including the indicators
256289
void indicator_set(uint32_t state);
257290

291+
// Get the app state
292+
uint32_t indicator_get(void);
293+
294+
// Start a timer with a tick of interval_ms
295+
void timer_start(uint32_t interval_ms);
296+
297+
// top the timer
298+
void timer_stop(void);
299+
300+
// Set the current tick count
301+
void timer_set_ticks(uint32_t ticks);
302+
303+
// get uptime in ms. Currently, this isn't counted when the timer is stopped.
304+
uint32_t timer_uptime(void);
305+
258306
static inline void rgb_brightness(uint8_t out[3], uint8_t const in[3], uint8_t brightness)
259307
{
260308
for(uint32_t i=0; i<3; i++ )

0 commit comments

Comments
 (0)