summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhathach <[email protected]>2021-07-12 17:50:11 +0700
committerhathach <[email protected]>2021-07-12 17:50:11 +0700
commit10a7e0502ad0ad1e53899afd547d8ddda12c7afe (patch)
treedac1310f3cebba57e586d151d1ebea68a4f0ca2e /src
parentecb100a62fa99335fc41dca3d7c85a3903a7df65 (diff)
parent2147a31f25e6e848d9ec690d8324a6fa2d1d3127 (diff)
Merge branch 'dfu' of https://github.com/HiFiPhile/tinyusb into HiFiPhile-dfu
Diffstat (limited to 'src')
-rw-r--r--src/class/dfu/dfu_device.c118
-rw-r--r--src/class/dfu/dfu_device.h29
-rw-r--r--src/device/usbd.h47
3 files changed, 157 insertions, 37 deletions
diff --git a/src/class/dfu/dfu_device.c b/src/class/dfu/dfu_device.c
index c696d70a2..6e7ad151b 100644
--- a/src/class/dfu/dfu_device.c
+++ b/src/class/dfu/dfu_device.c
@@ -37,6 +37,8 @@
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
+#define DFU_INTF_UNUSED 0xFF
+
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
@@ -46,6 +48,10 @@ typedef struct TU_ATTR_PACKED
dfu_state_t state;
uint8_t attrs;
bool blk_transfer_in_proc;
+ uint8_t alt;
+ uint8_t intf;
+ uint16_t block;
+ uint16_t length;
CFG_TUSB_MEM_ALIGN uint8_t transfer_buf[CFG_TUD_DFU_TRANSFER_BUFFER_SIZE];
} dfu_state_ctx_t;
@@ -146,6 +152,8 @@ void dfu_moded_init(void)
_dfu_state_ctx.status = DFU_STATUS_OK;
_dfu_state_ctx.attrs = 0;
_dfu_state_ctx.blk_transfer_in_proc = false;
+ _dfu_state_ctx.alt = 0;
+ _dfu_state_ctx.intf = DFU_INTF_UNUSED;
dfu_debug_print_context();
}
@@ -156,32 +164,67 @@ void dfu_moded_reset(uint8_t rhport)
_dfu_state_ctx.state = DFU_IDLE;
_dfu_state_ctx.status = DFU_STATUS_OK;
+ _dfu_state_ctx.attrs = 0;
_dfu_state_ctx.blk_transfer_in_proc = false;
+ _dfu_state_ctx.alt = 0;
+ _dfu_state_ctx.intf = DFU_INTF_UNUSED;
+
dfu_debug_print_context();
}
uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
{
(void) rhport;
- (void) max_len;
- // Ensure this is DFU Mode
- TU_VERIFY((itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS) &&
- (itf_desc->bInterfaceProtocol == DFU_PROTOCOL_DFU), 0);
+ uint16_t const drv_len = sizeof(tusb_desc_interface_t);
+
+ uint8_t const *p_desc = (uint8_t const *)itf_desc;
- uint8_t const * p_desc = tu_desc_next( itf_desc );
- uint16_t drv_len = sizeof(tusb_desc_interface_t);
+ uint16_t total_len = 0;
- if ( TUSB_DESC_FUNCTIONAL == tu_desc_type(p_desc) )
+ uint8_t last_alt = 0;
+
+ while(max_len > drv_len)
{
- tusb_desc_dfu_functional_t const *dfu_desc = (tusb_desc_dfu_functional_t const *)p_desc;
- _dfu_state_ctx.attrs = (uint8_t)dfu_desc->bAttributes;
+ // Ensure this is DFU Mode
+ TU_VERIFY((((tusb_desc_interface_t const *)p_desc)->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS) &&
+ (((tusb_desc_interface_t const *)p_desc)->bInterfaceProtocol == DFU_PROTOCOL_DFU), 0);
+
+ uint8_t const alt = ((tusb_desc_interface_t const *)p_desc)->bAlternateSetting;
+ uint8_t const intf = ((tusb_desc_interface_t const *)p_desc)->bInterfaceNumber;
- drv_len += tu_desc_len(p_desc);
- p_desc = tu_desc_next(p_desc);
+ if (_dfu_state_ctx.intf == DFU_INTF_UNUSED)
+ {
+ _dfu_state_ctx.intf = intf;
+ }
+ else
+ {
+ // Only one DFU interface is supported
+ TU_ASSERT(_dfu_state_ctx.intf == intf, 0);
+ }
+
+ // CFG_TUD_DFU_ATL_MAX should big enough to hold all alt settings
+ TU_ASSERT(alt < CFG_TUD_DFU_ALT_COUNT, 0);
+
+ // Alt should increse by one every time
+ TU_ASSERT(alt == last_alt++, 0);
+
+ p_desc = tu_desc_next(p_desc);
+ max_len -= drv_len;
+ total_len += drv_len;
}
- return drv_len;
+ //------------- DFU descriptor -------------//
+ TU_ASSERT(tu_desc_type(p_desc) == TUSB_DESC_FUNCTIONAL, 0);
+
+ _dfu_state_ctx.attrs = ((tusb_desc_dfu_functional_t const *)p_desc)->bAttributes;
+
+ // CFG_TUD_DFU_TRANSFER_BUFFER_SIZE has to be set to the buffer size used in TUD_DFU_MODE_DESCRIPTOR
+ TU_ASSERT(((tusb_desc_dfu_functional_t const *)p_desc)->wTransferSize <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, 0);
+
+ total_len += sizeof(tusb_desc_dfu_functional_t);
+
+ return total_len;
}
// Invoked when a control transfer occurred on an interface of this class
@@ -197,9 +240,16 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
if(stage == CONTROL_STAGE_SETUP)
{
// dfu-util will try to claim the interface with SET_INTERFACE request before sending DFU request
- if ( TUSB_REQ_TYPE_STANDARD == request->bmRequestType_bit.type &&
- TUSB_REQ_SET_INTERFACE == request->bRequest )
+ if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD && request->bRequest == TUSB_REQ_SET_INTERFACE)
{
+ // Switch Alt interface
+ _dfu_state_ctx.alt = request->wValue;
+
+ // Re-initalise state machine (Necessary ?)
+ _dfu_state_ctx.state = DFU_IDLE;
+ _dfu_state_ctx.status = DFU_STATUS_OK;
+ _dfu_state_ctx.blk_transfer_in_proc = false;
+
tud_control_status(rhport, request);
return true;
}
@@ -210,17 +260,23 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
switch (request->bRequest)
{
+ case DFU_REQUEST_DETACH:
+ {
+ tud_control_status(rhport, request);
+ if (tud_dfu_reboot_cb) tud_dfu_reboot_cb();
+ break;
+ }
case DFU_REQUEST_DNLOAD:
{
if ( (stage == CONTROL_STAGE_ACK)
&& ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
&& (_dfu_state_ctx.state == DFU_DNLOAD_SYNC))
{
- dfu_req_dnload_reply(rhport, request);
+ _dfu_state_ctx.block = request->wValue;
+ _dfu_state_ctx.length = request->wLength;
return true;
}
} // fallthrough
- case DFU_REQUEST_DETACH:
case DFU_REQUEST_UPLOAD:
case DFU_REQUEST_GETSTATUS:
case DFU_REQUEST_CLRSTATUS:
@@ -248,7 +304,11 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
static uint16_t dfu_req_upload(uint8_t rhport, tusb_control_request_t const * request, uint16_t block_num, uint16_t wLength)
{
TU_VERIFY( wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, 0);
- uint16_t retval = tud_dfu_req_upload_data_cb(block_num, (uint8_t *)_dfu_state_ctx.transfer_buf, wLength);
+ uint16_t retval = 0;
+ if (tud_dfu_req_upload_data_cb)
+ {
+ tud_dfu_req_upload_data_cb(_dfu_state_ctx.alt, block_num, (uint8_t *)_dfu_state_ctx.transfer_buf, wLength);
+ }
tud_control_xfer(rhport, request, _dfu_state_ctx.transfer_buf, retval);
return retval;
}
@@ -257,8 +317,16 @@ static void dfu_req_getstatus_reply(uint8_t rhport, tusb_control_request_t const
{
dfu_status_req_payload_t resp;
+ uint16_t timeout = 0;
resp.bStatus = _dfu_state_ctx.status;
- memset((uint8_t *)&resp.bwPollTimeout, 0x00, 3);
+ if(_dfu_state_ctx.state == DFU_DNBUSY && tud_dfu_set_timeout_cb)
+ {
+ timeout = tud_dfu_set_timeout_cb(_dfu_state_ctx.alt);
+
+ }
+ resp.bwPollTimeout[0] = TU_U16_LOW(timeout);
+ resp.bwPollTimeout[1] = TU_U16_HIGH(timeout);
+ resp.bwPollTimeout[2] = 0;
resp.bState = _dfu_state_ctx.state;
resp.iString = 0;
@@ -285,7 +353,7 @@ static void dfu_req_dnload_reply(uint8_t rhport, tusb_control_request_t const *
{
(void) rhport;
TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, );
- tud_dfu_req_dnload_data_cb(request->wValue, (uint8_t *)_dfu_state_ctx.transfer_buf, request->wLength);
+ tud_dfu_req_dnload_data_cb(_dfu_state_ctx.alt,_dfu_state_ctx.block, (uint8_t *)_dfu_state_ctx.transfer_buf, _dfu_state_ctx.length);
_dfu_state_ctx.blk_transfer_in_proc = false;
}
@@ -296,7 +364,7 @@ void tud_dfu_dnload_complete(void)
_dfu_state_ctx.state = DFU_DNLOAD_SYNC;
} else if (_dfu_state_ctx.state == DFU_MANIFEST)
{
- _dfu_state_ctx.state = ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) != 0)
+ _dfu_state_ctx.state = ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
? DFU_MANIFEST_WAIT_RESET : DFU_MANIFEST_SYNC;
}
}
@@ -376,6 +444,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
{
_dfu_state_ctx.state = DFU_DNBUSY;
dfu_req_getstatus_reply(rhport, request);
+ dfu_req_dnload_reply(rhport, request);
} else {
_dfu_state_ctx.state = DFU_DNLOAD_IDLE;
dfu_req_getstatus_reply(rhport, request);
@@ -426,7 +495,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
_dfu_state_ctx.blk_transfer_in_proc = true;
dfu_req_dnload_setup(rhport, request);
} else {
- if ( tud_dfu_device_data_done_check_cb() )
+ if ( tud_dfu_device_data_done_check_cb(_dfu_state_ctx.alt) )
{
_dfu_state_ctx.state = DFU_MANIFEST_SYNC;
tud_control_status(rhport, request);
@@ -476,12 +545,13 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
{
case DFU_REQUEST_GETSTATUS:
{
- if ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) != 0)
+ if ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
{
_dfu_state_ctx.state = DFU_MANIFEST;
dfu_req_getstatus_reply(rhport, request);
- } else {
- if ( tud_dfu_firmware_valid_check_cb() )
+ } else
+ {
+ if ( tud_dfu_firmware_valid_check_cb(_dfu_state_ctx.alt) )
{
_dfu_state_ctx.state = DFU_IDLE;
}
diff --git a/src/class/dfu/dfu_device.h b/src/class/dfu/dfu_device.h
index 9a09a46b1..c09b9a823 100644
--- a/src/class/dfu/dfu_device.h
+++ b/src/class/dfu/dfu_device.h
@@ -33,19 +33,32 @@
extern "C" {
#endif
+//--------------------------------------------------------------------+
+// Class Driver Default Configure & Validation
+//--------------------------------------------------------------------+
+
+#if !defined(CFG_TUD_DFU_TRANSFER_BUFFER_SIZE)
+ #error "CFG_TUD_DFU_TRANSFER_BUFFER_SIZE must be defined, it has to be set to the buffer size used in TUD_DFU_MODE_DESCRIPTOR"
+#endif
//--------------------------------------------------------------------+
// Application Callback API (weak is optional)
//--------------------------------------------------------------------+
-// Invoked during DFU_MANIFEST_SYNC get status request to check if firmware
-// is valid
-bool tud_dfu_firmware_valid_check_cb(void);
+// Invoked during DFU_MANIFEST_SYNC get status request to check if firmware is valid
+// alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
+bool tud_dfu_firmware_valid_check_cb(uint8_t alt);
+
+// Invoked when a DFU_GETSTATUS request is received in DFU_DNBUSY state
+// Used to set the bwPollTimeout value, useful for slow Flash in order to make host wait longer
+// alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
+TU_ATTR_WEAK uint16_t tud_dfu_set_timeout_cb(uint8_t alt);
// Invoked when a DFU_DNLOAD request is received
+// alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
// This callback takes the wBlockNum chunk of length length and provides it
// to the application at the data pointer. This data is only valid for this
// call, so the app must use it not or copy it.
-void tud_dfu_req_dnload_data_cb(uint16_t wBlockNum, uint8_t* data, uint16_t length);
+void tud_dfu_req_dnload_data_cb(uint8_t alt, uint16_t wBlockNum, uint8_t* data, uint16_t length);
// Must be called when the application is done using the last block of data
// provided by tud_dfu_req_dnload_data_cb
@@ -53,19 +66,23 @@ void tud_dfu_dnload_complete(void);
// Invoked during the last DFU_DNLOAD request, signifying that the host believes
// it is done transmitting data.
+// alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
// Return true if the application agrees there is no more data
// Return false if the device disagrees, which will stall the pipe, and the Host
// should initiate a recovery procedure
-bool tud_dfu_device_data_done_check_cb(void);
+bool tud_dfu_device_data_done_check_cb(uint8_t alt);
// Invoked when the Host has terminated a download or upload transfer
TU_ATTR_WEAK void tud_dfu_abort_cb(void);
// Invoked when a DFU_UPLOAD request is received
+// alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
// This callback must populate data with up to length bytes
// Return the number of bytes to write
-uint16_t tud_dfu_req_upload_data_cb(uint16_t block_num, uint8_t* data, uint16_t length);
+TU_ATTR_WEAK uint16_t tud_dfu_req_upload_data_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length);
+// Invoked when a DFU_DETACH request is received
+TU_ATTR_WEAK void tud_dfu_reboot_cb(void);
//--------------------------------------------------------------------+
// Internal Class Driver API
//--------------------------------------------------------------------+
diff --git a/src/device/usbd.h b/src/device/usbd.h
index 45aefe53b..ba415abe8 100644
--- a/src/device/usbd.h
+++ b/src/device/usbd.h
@@ -605,17 +605,50 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
/* Function */ \
9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101)
-// Length of template descriptr: 18 bytes
-#define TUD_DFU_MODE_DESC_LEN (9 + 9)
+// Maximum alternate settings (used for different partitons) supported
+#ifndef CFG_TUD_DFU_ALT_COUNT
+#define CFG_TUD_DFU_ALT_COUNT 1
+#endif
-// DFU runtime descriptor
-// Interface number, string index, attributes, detach timeout, transfer size
-#define TUD_DFU_MODE_DESCRIPTOR(_itfnum, _stridx, _attr, _timeout, _xfer_size) \
- /* Interface */ \
- 9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_DFU, _stridx, \
+// Length of template descriptor: 18 bytes + number of alternatives * 9
+#define TUD_DFU_MODE_DESC_LEN (9 + (CFG_TUD_DFU_ALT_COUNT) * 9)
+
+/* Primary Interface */
+#define TUD_DFU_MODE_FUNC(_attr, _timeout, _xfer_size) \
/* Function */ \
9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101)
+#define TUD_DFU_MODE_ALT(_itfnum, _alt, _stridx) \
+ /* Interface */ \
+ 9, TUSB_DESC_INTERFACE, _itfnum, _alt, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_DFU, _stridx, \
+
+#define _TUD_DFU_FIRST(a, ...) a
+#define _TUD_DFU_REST(a, ...) __VA_ARGS__
+#define _TUD_DFU_COMBINE(...) __VA_ARGS__
+
+#define TUD_DFU_MODE_ALT_1(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 1, _TUD_DFU_FIRST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_2(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 2, _TUD_DFU_FIRST(__VA_ARGS__)) \
+ TUD_DFU_MODE_ALT_1(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_3(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 3, _TUD_DFU_FIRST(__VA_ARGS__)) \
+ TUD_DFU_MODE_ALT_2(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_4(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 4, _TUD_DFU_FIRST(__VA_ARGS__)) \
+ TUD_DFU_MODE_ALT_3(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_5(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 5, _TUD_DFU_FIRST(__VA_ARGS__)) \
+ TUD_DFU_MODE_ALT_4(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_6(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 6, _TUD_DFU_FIRST(__VA_ARGS__)) \
+ TUD_DFU_MODE_ALT_5(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_7(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 7, _TUD_DFU_FIRST(__VA_ARGS__)) \
+ TUD_DFU_MODE_ALT_6(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_8(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 8, _TUD_DFU_FIRST(__VA_ARGS__)) \
+ TUD_DFU_MODE_ALT_7(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+
+#define TUD_DFU_MODE_ALTS(_itfnum, ...) \
+ TU_XSTRCAT(TUD_DFU_MODE_ALT_, CFG_TUD_DFU_ALT_COUNT)(_itfnum, __VA_ARGS__)
+
+// Interface number, attributes, detach timeout, transfer size, string index 0, [string index 1, string index n]
+#define TUD_DFU_MODE_DESCRIPTOR(_itfnum, _attr, _timeout, _xfer_size, _stridx, ...) \
+ TUD_DFU_MODE_ALTS(_itfnum, _TUD_DFU_COMBINE(_stridx, __VA_ARGS__)) \
+ TUD_DFU_MODE_FUNC(_attr, _timeout, _xfer_size)
//------------- CDC-ECM -------------//