summaryrefslogtreecommitdiff
path: root/examples/device/dfu/src/main.c
diff options
context:
space:
mode:
authorHiFiPhile <[email protected]>2024-05-09 15:51:53 +0200
committerHiFiPhile <[email protected]>2024-05-09 15:51:53 +0200
commit36ce6fad8ca997804a569fe575584b45a1b5fc79 (patch)
treef79aa084a42438667b5e26b588a6f794c9ad1608 /examples/device/dfu/src/main.c
parentf607a99127cc9e8dfc3f716f977e6c1bfb6f7c2d (diff)
parent74e57499baac36c4cccf76549259905162031e41 (diff)
Merge branch 'master' into vendor_class_zero_length_transfer
Diffstat (limited to 'examples/device/dfu/src/main.c')
-rw-r--r--examples/device/dfu/src/main.c123
1 files changed, 79 insertions, 44 deletions
diff --git a/examples/device/dfu/src/main.c b/examples/device/dfu/src/main.c
index 1c09066a8..81fc0a62c 100644
--- a/examples/device/dfu/src/main.c
+++ b/examples/device/dfu/src/main.c
@@ -26,13 +26,15 @@
/*
* After device is enumerated in dfu mode run the following commands
*
- * To transfer firmware from host to device:
+ * To transfer firmware from host to device (best to test with text file)
*
- * $ dfu-util -D [filename]
+ * $ dfu-util -d cafe -a 0 -D [filename]
+ * $ dfu-util -d cafe -a 1 -D [filename]
*
* To transfer firmware from device to host:
*
- * $ dfu-util -U [filename]
+ * $ dfu-util -d cafe -a 0 -U [filename]
+ * $ dfu-util -d cafe -a 1 -U [filename]
*
*/
@@ -40,25 +42,24 @@
#include <stdio.h>
#include <string.h>
-#include "bsp/board.h"
+#include "bsp/board_api.h"
#include "tusb.h"
-
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
-#ifndef DFU_VERBOSE
-#define DFU_VERBOSE 0
-#endif
+const char* upload_image[2]=
+{
+ "Hello world from TinyUSB DFU! - Partition 0",
+ "Hello world from TinyUSB DFU! - Partition 1"
+};
/* Blink pattern
- * - 1000 ms : device should reboot
* - 250 ms : device not mounted
* - 1000 ms : device mounted
* - 2500 ms : device is suspended
*/
enum {
- BLINK_DFU_MODE = 100,
BLINK_NOT_MOUNTED = 250,
BLINK_MOUNTED = 1000,
BLINK_SUSPENDED = 2500,
@@ -73,15 +74,18 @@ int main(void)
{
board_init();
- tusb_init();
+ // init device stack on configured roothub port
+ tud_init(BOARD_TUD_RHPORT);
+
+ if (board_init_after_tusb) {
+ board_init_after_tusb();
+ }
while (1)
{
tud_task(); // tinyusb device task
led_blinking_task();
}
-
- return 0;
}
//--------------------------------------------------------------------+
@@ -112,61 +116,92 @@ void tud_suspend_cb(bool remote_wakeup_en)
// Invoked when usb bus is resumed
void tud_resume_cb(void)
{
- blink_interval_ms = BLINK_MOUNTED;
-}
-
-// Invoked on DFU_DETACH request to reboot to the bootloader
-void tud_dfu_runtime_reboot_to_dfu_cb(void)
-{
- blink_interval_ms = BLINK_DFU_MODE;
+ blink_interval_ms = tud_mounted() ? BLINK_MOUNTED : BLINK_NOT_MOUNTED;
}
//--------------------------------------------------------------------+
-// Class callbacks
+// DFU callbacks
+// Note: alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
//--------------------------------------------------------------------+
-bool tud_dfu_firmware_valid_check_cb(void)
+
+// Invoked right before tud_dfu_download_cb() (state=DFU_DNBUSY) or tud_dfu_manifest_cb() (state=DFU_MANIFEST)
+// Application return timeout in milliseconds (bwPollTimeout) for the next download/manifest operation.
+// During this period, USB host won't try to communicate with us.
+uint32_t tud_dfu_get_timeout_cb(uint8_t alt, uint8_t state)
{
- printf(" Firmware check\r\n");
- return true;
+ if ( state == DFU_DNBUSY )
+ {
+ // For this example
+ // - Atl0 Flash is fast : 1 ms
+ // - Alt1 EEPROM is slow: 100 ms
+ return (alt == 0) ? 1 : 100;
+ }
+ else if (state == DFU_MANIFEST)
+ {
+ // since we don't buffer entire image and do any flashing in manifest stage
+ return 0;
+ }
+
+ return 0;
}
-void tud_dfu_req_dnload_data_cb(uint16_t wBlockNum, uint8_t* data, uint16_t length)
+// Invoked when received DFU_DNLOAD (wLength>0) following by DFU_GETSTATUS (state=DFU_DNBUSY) requests
+// This callback could be returned before flashing op is complete (async).
+// Once finished flashing, application must call tud_dfu_finish_flashing()
+void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, uint16_t length)
{
- (void) data;
- printf(" Received BlockNum %u of length %u\r\n", wBlockNum, length);
+ (void) alt;
+ (void) block_num;
+
+ //printf("\r\nReceived Alt %u BlockNum %u of length %u\r\n", alt, wBlockNum, length);
-#if DFU_VERBOSE
for(uint16_t i=0; i<length; i++)
{
- printf(" [%u][%u]: %x\r\n", wBlockNum, i, (uint8_t)data[i]);
+ printf("%c", data[i]);
}
-#endif
- tud_dfu_dnload_complete();
+ // flashing op for download complete without error
+ tud_dfu_finish_flashing(DFU_STATUS_OK);
}
-bool tud_dfu_device_data_done_check_cb(void)
+// Invoked when download process is complete, received DFU_DNLOAD (wLength=0) following by DFU_GETSTATUS (state=Manifest)
+// Application can do checksum, or actual flashing if buffered entire image previously.
+// Once finished flashing, application must call tud_dfu_finish_flashing()
+void tud_dfu_manifest_cb(uint8_t alt)
{
- printf(" Host said no more data... Returning true\r\n");
- return true;
-}
+ (void) alt;
+ printf("Download completed, enter manifestation\r\n");
-void tud_dfu_abort_cb(void)
-{
- printf(" Host aborted transfer\r\n");
+ // flashing op for manifest is complete without error
+ // Application can perform checksum, should it fail, use appropriate status such as errVERIFY.
+ tud_dfu_finish_flashing(DFU_STATUS_OK);
}
-#define UPLOAD_SIZE (29)
-const uint8_t upload_test[UPLOAD_SIZE] = "Hello world from TinyUSB DFU!";
-
-uint16_t tud_dfu_req_upload_data_cb(uint16_t block_num, uint8_t* data, uint16_t length)
+// Invoked when received DFU_UPLOAD request
+// Application must populate data with up to length bytes and
+// Return the number of written bytes
+uint16_t tud_dfu_upload_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length)
{
(void) block_num;
(void) length;
- memcpy(data, upload_test, UPLOAD_SIZE);
+ uint16_t const xfer_len = (uint16_t) strlen(upload_image[alt]);
+ memcpy(data, upload_image[alt], xfer_len);
+
+ return xfer_len;
+}
- return UPLOAD_SIZE;
+// Invoked when the Host has terminated a download or upload transfer
+void tud_dfu_abort_cb(uint8_t alt)
+{
+ (void) alt;
+ printf("Host aborted transfer\r\n");
+}
+
+// Invoked when a DFU_DETACH request is received
+void tud_dfu_detach_cb(void)
+{
+ printf("Host detach, we should probably reboot\r\n");
}
//--------------------------------------------------------------------+