8000 mimxrt/tusb: Adds runtime DFU descriptor. · micropython/micropython@6f10103 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f10103

Browse files
committed
mimxrt/tusb: Adds runtime DFU descriptor.
Adds runtime DFU descriptor to firmware. Allows to reset into DFU bootloader. Signed-off-by: Philipp Ebensberger
1 parent 3c9ae5c commit 6f10103

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

ports/mimxrt/modmachine.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "modmachine.h"
3939
#include "fsl_clock.h"
4040
#include "fsl_wdog.h"
41+
#include "mboot/shared/mboot_command.h"
4142

4243
#if MICROPY_PY_MACHINE
4344

@@ -51,6 +52,14 @@ typedef enum {
5152
MP_SOFT_RESET
5253
} reset_reason_t;
5354

55+
__attribute__((section("._bl_command"))) mboot_command_t bl_command;
56+
57+
void reset_boot(void) {
58+
bl_command.magic = BOOT_COMMAND_MAGIC;
59+
bl_command.command = BOOT_COMMAND_RESET_DFU;
60+
NVIC_SystemReset();
61+
}
62+
5463
STATIC mp_obj_t machine_unique_id(void) {
5564
unsigned char id[8];
5665
mp_hal_get_unique_id(id);

ports/mimxrt/tusb_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@
3333
#define CFG_TUD_CDC_RX_BUFSIZE (512)
3434
#define CFG_TUD_CDC_TX_BUFSIZE (512)
3535

36+
#define CFG_TUD_DFU_RUNTIME (1)
37+
3638
#endif // MICROPY_INCLUDED_MIMXRT_TUSB_CONFIG_H

ports/mimxrt/tusb_port.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,33 @@
3939
#define MICROPY_HW_USB_STR_MANUF ("MicroPython")
4040
#endif
4141

42-
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
43-
#define USBD_MAX_POWER_MA (250)
42+
enum
43+
{
44+
ITF_NUM_CDC, // needs 2 interfaces
45+
ITF_NUM_CDC_DUMMY,
46+
ITF_NUM_DFU_RT,
47+
ITF_NUM_TOTAL
48+
};
4449

45-
#define USBD_ITF_CDC (0) // needs 2 interfaces
46-
#define USBD_ITF_MAX (2)
50+
#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_DFU_RT_DESC_LEN)
51+
#define USBD_MAX_POWER_MA (250)
4752

4853
#define USBD_CDC_EP_CMD (0x81)
4954
#define USBD_CDC_EP_OUT (0x02)
5055
#define USBD_CDC_EP_IN (0x82)
5156
#define USBD_CDC_CMD_MAX_SIZE (8)
5257
#define USBD_CDC_IN_OUT_MAX_SIZE (512)
5358

54-
#define USBD_STR_0 (0x00)
55-
#define USBD_STR_MANUF (0x01)
56-
#define USBD_STR_PRODUCT (0x02)
57-
#define USBD_STR_SERIAL (0x03)
58-
#define USBD_STR_CDC (0x04)
59+
enum {
60+
USBD_STR_0,
61+
USBD_STR_MANUF,
62+
USBD_STR_PRODUCT,
63+
USBD_STR_SERIAL,
64+
USBD_STR_CDC,
65+
USBD_STR_DFU,
66+
};
67+
68+
extern void reset_boot(void);
5969

6070
// Note: descriptors returned from callbacks must exist long enough for transfer to complete
6171

@@ -77,18 +87,22 @@ static const tusb_desc_device_t usbd_desc_device = {
7787
};
7888

7989
static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
80-
TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN,
90+
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, USBD_STR_0, USBD_DESC_LEN,
8191
TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, USBD_MAX_POWER_MA),
8292

83-
TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
93+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
8494
USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
95+
96+
// Interface number, string index, attributes, detach timeout, transfer size */
97+
TUD_DFU_RT_DESCRIPTOR(ITF_NUM_DFU_RT, USBD_STR_DFU, 0x0d, 1000, 4096),
8598
};
8699

87100
static const char *const usbd_desc_str[] = {
88101
[USBD_STR_MANUF] = MICROPY_HW_USB_STR_MANUF,
89102
[USBD_STR_PRODUCT] = MICROPY_HW_BOARD_NAME,
90103
[USBD_STR_SERIAL] = "00000000000000000000",
91104
[USBD_STR_CDC] = "Board CDC",
105+
[USBD_STR_DFU] = "Board DFU Runtime"
92106
};
93107

94108
const uint8_t *tud_descriptor_device_cb(void) {
@@ -136,3 +150,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
136150

137151
return desc_str;
138152
}
153+
154+
void tud_dfu_runtime_reboot_to_dfu_cb() {
155+
reset_boot();
156+
}

0 commit comments

Comments
 (0)
0