summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2021-07-22 11:17:01 +0700
committerGitHub <[email protected]>2021-07-22 11:17:01 +0700
commit1c2bc47c078fb48379bb148840e9b9c88c0fa66f (patch)
tree89b8d2a9947f8ccaf1693df6c2a9d47f310ac3e5 /examples
parente971a4332dc6dc0580e1d71e4fc8866296f8159c (diff)
parent9542fcdbcda23a08fb4d7901bbb01333b6465bfc (diff)
Merge pull request #950 from HiFiPhile/dfu
DFU improvements
Diffstat (limited to 'examples')
-rw-r--r--examples/device/dfu/.skip.MCU_SAMD110
-rw-r--r--examples/device/dfu/src/main.c110
-rw-r--r--examples/device/dfu/src/tusb_config.h7
-rw-r--r--examples/device/dfu/src/usb_descriptors.c14
-rw-r--r--examples/device/midi_test/src/main.c2
5 files changed, 84 insertions, 49 deletions
diff --git a/examples/device/dfu/.skip.MCU_SAMD11 b/examples/device/dfu/.skip.MCU_SAMD11
deleted file mode 100644
index e69de29bb..000000000
--- a/examples/device/dfu/.skip.MCU_SAMD11
+++ /dev/null
diff --git a/examples/device/dfu/src/main.c b/examples/device/dfu/src/main.c
index 1c09066a8..5c8464526 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]
*
*/
@@ -43,22 +45,21 @@
#include "bsp/board.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,
@@ -115,58 +116,89 @@ 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;
-}
-
//--------------------------------------------------------------------+
-// 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 UPLOAD_SIZE;
+ return xfer_len;
+}
+
+// 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");
}
//--------------------------------------------------------------------+
diff --git a/examples/device/dfu/src/tusb_config.h b/examples/device/dfu/src/tusb_config.h
index 90e90b2e4..66e357021 100644
--- a/examples/device/dfu/src/tusb_config.h
+++ b/examples/device/dfu/src/tusb_config.h
@@ -76,12 +76,11 @@
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif
-#define CFG_TUD_DFU_TRANSFER_BUFFER_SIZE 4096
-
//------------- CLASS -------------//
+#define CFG_TUD_DFU 1
-#define CFG_TUD_DFU_RUNTIME 0
-#define CFG_TUD_DFU_MODE 1
+// DFU buffer size, it has to be set to the buffer size used in TUD_DFU_DESCRIPTOR
+#define CFG_TUD_DFU_XFER_BUFSIZE 512
#ifdef __cplusplus
}
diff --git a/examples/device/dfu/src/usb_descriptors.c b/examples/device/dfu/src/usb_descriptors.c
index 8ad64354b..1cfe29c53 100644
--- a/examples/device/dfu/src/usb_descriptors.c
+++ b/examples/device/dfu/src/usb_descriptors.c
@@ -81,23 +81,26 @@ uint8_t const * tud_descriptor_device_cb(void)
// Configuration Descriptor
//--------------------------------------------------------------------+
+// Number of Alternate Interface (each for 1 flash partition)
+#define ALT_COUNT 2
+
enum
{
ITF_NUM_DFU_MODE,
ITF_NUM_TOTAL
};
-#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_DFU_MODE_DESC_LEN)
+#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_DFU_DESC_LEN(ALT_COUNT))
-#define FUNC_ATTRS (DFU_FUNC_ATTR_CAN_UPLOAD_BITMASK | DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK)
+#define FUNC_ATTRS (DFU_ATTR_CAN_UPLOAD | DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_MANIFESTATION_TOLERANT)
uint8_t const desc_configuration[] =
{
// Config number, interface count, string index, total length, attribute, power in mA
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
- // Interface number, string index, attributes, detach timeout, transfer size */
- TUD_DFU_MODE_DESCRIPTOR(ITF_NUM_DFU_MODE, 0, FUNC_ATTRS, 1000, CFG_TUD_DFU_TRANSFER_BUFFER_SIZE),
+ // Interface number, Alternate count, starting string index, attributes, detach timeout, transfer size
+ TUD_DFU_DESCRIPTOR(ITF_NUM_DFU_MODE, ALT_COUNT, 4, FUNC_ATTRS, 1000, CFG_TUD_DFU_XFER_BUFSIZE),
};
// Invoked when received GET CONFIGURATION DESCRIPTOR
@@ -120,7 +123,8 @@ char const* string_desc_arr [] =
"TinyUSB", // 1: Manufacturer
"TinyUSB Device", // 2: Product
"123456", // 3: Serials, should use chip ID
- "TinyUSB DFU", // 4: DFU
+ "FLASH", // 4: DFU Partition 1
+ "EEPROM", // 5: DFU Partition 2
};
static uint16_t _desc_str[32];
diff --git a/examples/device/midi_test/src/main.c b/examples/device/midi_test/src/main.c
index 9e0e82a10..193748477 100644
--- a/examples/device/midi_test/src/main.c
+++ b/examples/device/midi_test/src/main.c
@@ -134,7 +134,7 @@ void midi_task(void)
uint8_t packet[4];
while ( tud_midi_available() ) tud_midi_packet_read(packet);
- // send note every 1000 ms
+ // send note periodically
if (board_millis() - start_ms < 286) return; // not enough time
start_ms += 286;