Skip to content

Commit 7e25c1e

Browse files
committed
Replace target_manifest_app with target_pre_detach
Provide a target-specific hook to run just before detaching/rebooting. The manifested parameter can be used to distinguish between resetting to run existing vs new firmware.
1 parent b3d8255 commit 7e25c1e

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

src/dfu.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ static void dfu_on_detach_complete(usbd_device* usbd_dev, struct usb_setup_data*
101101
(void)usbd_dev;
102102
(void)req;
103103

104+
/* Run the target-specific pre-detach hook before resetting */
105+
target_pre_detach(manifestation_complete);
106+
104107
/* Reset and maybe launch the application */
105108
scb_reset_system();
106109
}
@@ -149,13 +152,17 @@ static void dfu_on_manifest_request(usbd_device* usbd_dev, struct usb_setup_data
149152

150153
if (dfu_manifest_request_callback) {
151154
/* The manifestation callback returns a boolean indicating if it succeeded */
152-
if (dfu_manifest_request_callback()) {
153-
manifestation_complete = true;
154-
dfu_set_state(STATE_DFU_MANIFEST_SYNC);
155-
} else {
156-
dfu_set_status(DFU_STATUS_ERR_FIRMWARE);
157-
return; /* Avoid resetting on error */
158-
}
155+
manifestation_complete = dfu_manifest_request_callback();
156+
} else {
157+
/* Assume manifestation success */
158+
manifestation_complete = true;
159+
}
160+
161+
if (manifestation_complete) {
162+
dfu_set_state(STATE_DFU_MANIFEST_SYNC);
163+
} else {
164+
dfu_set_status(DFU_STATUS_ERR_FIRMWARE);
165+
return; /* Avoid resetting on error */
159166
}
160167

161168
#if DFU_WILL_DETACH
@@ -202,7 +209,6 @@ dfu_control_class_request(usbd_device *usbd_dev,
202209
* the device is manifestation tolerant (DFU_WILL_DETACH == 0) */
203210
if (manifestation_complete) {
204211
/* Only enter idle state after manifestation has completed successfully */
205-
manifestation_complete = false;
206212
dfu_set_state(STATE_DFU_IDLE);
207213
} else {
208214
/* Perform manifestation after download as described in the
@@ -239,6 +245,8 @@ dfu_control_class_request(usbd_device *usbd_dev,
239245
dfu_download_size = req->wLength;
240246
memcpy(dfu_download_buffer, *buf, dfu_download_size);
241247
dfu_set_state(STATE_DFU_DNLOAD_SYNC);
248+
/* Reset manifestation progress on new download */
249+
manifestation_complete = false;
242250
} else {
243251
dfu_set_status(DFU_STATUS_ERR_STALLEDPKT);
244252
usbd_ep_stall_set(usbd_dev, 0x00, 1);

src/dummy.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
void target_get_serial_number(char* dest, size_t max_chars) __attribute__((weak));
2626
void target_log(const char* str) __attribute__((weak));
27-
void target_manifest_app(void) __attribute__((weak));
2827
void target_pre_main(void) __attribute__((weak));
28+
void target_pre_detach(bool manifested) __attribute__((weak));
2929
size_t target_get_timeout(void) __attribute__((weak));
3030

3131
void target_get_serial_number(char* dest, size_t max_chars) {
@@ -39,15 +39,20 @@ void target_log(const char* str) {
3939
(void)str;
4040
}
4141

42-
void target_manifest_app(void) {
43-
scb_reset_system();
44-
}
45-
4642
void target_pre_main(void)
4743
{
4844

4945
}
5046

47+
void target_pre_detach(bool manifested) {
48+
/* This runs just before executing a reboot in response to a USB bus reset
49+
or a detach request.
50+
If new firmware was successfully downloaded, manifested is set to true.
51+
This can be used to set flags or blink LEDs before rebooting.
52+
*/
53+
(void)manifested;
54+
}
55+
5156
size_t target_get_timeout(void)
5257
{
5358
return 100;

src/target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ extern void target_get_serial_number(char* dest, size_t max_chars);
3131
extern size_t target_get_max_firmware_size(void);
3232
extern void target_log(const char* str);
3333
extern void target_relocate_vector_table(void);
34-
extern void target_manifest_app(void);
3534
extern void target_flash_unlock(void);
3635
extern void target_flash_lock(void);
3736
extern bool target_flash_program_array(uint16_t* dest, const uint16_t* data, size_t half_word_count);
3837

3938
extern void target_pre_main(void);
39+
extern void target_pre_detach(bool manifested);
4040
extern size_t target_get_timeout(void);
4141

4242
#endif

0 commit comments

Comments
 (0)