Merge branch 'dfu' of https://github.com/HiFiPhile/tinyusb into HiFiPhile-dfu

This commit is contained in:
hathach 2021-07-12 17:50:11 +07:00
commit 10a7e0502a
6 changed files with 184 additions and 56 deletions

View File

@ -115,25 +115,27 @@ void tud_resume_cb(void)
blink_interval_ms = BLINK_MOUNTED; 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 // Class callbacks
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool tud_dfu_firmware_valid_check_cb(void) bool tud_dfu_firmware_valid_check_cb(uint8_t alt)
{ {
(void) alt;
printf(" Firmware check\r\n"); printf(" Firmware check\r\n");
return true; return true;
} }
void tud_dfu_req_dnload_data_cb(uint16_t wBlockNum, uint8_t* data, uint16_t length) uint16_t tud_dfu_set_timeout_cb(uint8_t alt)
{
// For example Alt1 (EEPROM) is slow, add 2000ms timeout
if (alt == 1) return 2000;
return 0;
}
void tud_dfu_req_dnload_data_cb(uint8_t alt, uint16_t wBlockNum, uint8_t* data, uint16_t length)
{ {
(void) data; (void) data;
printf(" Received BlockNum %u of length %u\r\n", wBlockNum, length); printf(" Received Alt %u BlockNum %u of length %u\r\n", alt, wBlockNum, length);
#if DFU_VERBOSE #if DFU_VERBOSE
for(uint16_t i=0; i<length; i++) for(uint16_t i=0; i<length; i++)
@ -145,8 +147,9 @@ void tud_dfu_req_dnload_data_cb(uint16_t wBlockNum, uint8_t* data, uint16_t leng
tud_dfu_dnload_complete(); tud_dfu_dnload_complete();
} }
bool tud_dfu_device_data_done_check_cb(void) bool tud_dfu_device_data_done_check_cb(uint8_t alt)
{ {
(void) alt;
printf(" Host said no more data... Returning true\r\n"); printf(" Host said no more data... Returning true\r\n");
return true; return true;
} }
@ -156,15 +159,16 @@ void tud_dfu_abort_cb(void)
printf(" Host aborted transfer\r\n"); printf(" Host aborted transfer\r\n");
} }
#define UPLOAD_SIZE (29) #define UPLOAD_SIZE 43
const uint8_t upload_test[UPLOAD_SIZE] = "Hello world from TinyUSB DFU!"; const uint8_t upload_test[2][UPLOAD_SIZE] = {"Hello world from TinyUSB DFU! - Partition 0",
"Hello world from TinyUSB DFU! - Partition 1"};
uint16_t tud_dfu_req_upload_data_cb(uint16_t block_num, uint8_t* data, uint16_t length) uint16_t tud_dfu_req_upload_data_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length)
{ {
(void) block_num; (void) block_num;
(void) length; (void) length;
memcpy(data, upload_test, UPLOAD_SIZE); memcpy(data, upload_test[alt], UPLOAD_SIZE);
return UPLOAD_SIZE; return UPLOAD_SIZE;
} }

View File

@ -76,13 +76,16 @@
#define CFG_TUD_ENDPOINT0_SIZE 64 #define CFG_TUD_ENDPOINT0_SIZE 64
#endif #endif
#define CFG_TUD_DFU_TRANSFER_BUFFER_SIZE 4096
//------------- CLASS -------------// //------------- CLASS -------------//
#define CFG_TUD_DFU_RUNTIME 0 #define CFG_TUD_DFU_RUNTIME 0
#define CFG_TUD_DFU_MODE 1 #define CFG_TUD_DFU_MODE 1
// Count of all alt settings, typically it's the partition count (Flash, EEPROM, etc.)
#define CFG_TUD_DFU_ALT_COUNT 2
// DFU buffer size, it has to be set to the buffer size used in TUD_DFU_MODE_DESCRIPTOR
#define CFG_TUD_DFU_TRANSFER_BUFFER_SIZE 4096
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -96,8 +96,8 @@ uint8_t const desc_configuration[] =
// Config number, interface count, string index, total length, attribute, power in mA // 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), 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 */ // Interface number, attributes, detach timeout, transfer size, string index 0, [string index 1 ... string index n]
TUD_DFU_MODE_DESCRIPTOR(ITF_NUM_DFU_MODE, 0, FUNC_ATTRS, 1000, CFG_TUD_DFU_TRANSFER_BUFFER_SIZE), TUD_DFU_MODE_DESCRIPTOR(ITF_NUM_DFU_MODE, FUNC_ATTRS, 1000, CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, 4, 5),
}; };
// Invoked when received GET CONFIGURATION DESCRIPTOR // Invoked when received GET CONFIGURATION DESCRIPTOR
@ -120,7 +120,8 @@ char const* string_desc_arr [] =
"TinyUSB", // 1: Manufacturer "TinyUSB", // 1: Manufacturer
"TinyUSB Device", // 2: Product "TinyUSB Device", // 2: Product
"123456", // 3: Serials, should use chip ID "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]; static uint16_t _desc_str[32];

View File

@ -37,6 +37,8 @@
// MACRO CONSTANT TYPEDEF // MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define DFU_INTF_UNUSED 0xFF
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION // INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -46,6 +48,10 @@ typedef struct TU_ATTR_PACKED
dfu_state_t state; dfu_state_t state;
uint8_t attrs; uint8_t attrs;
bool blk_transfer_in_proc; 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]; CFG_TUSB_MEM_ALIGN uint8_t transfer_buf[CFG_TUD_DFU_TRANSFER_BUFFER_SIZE];
} dfu_state_ctx_t; } dfu_state_ctx_t;
@ -146,6 +152,8 @@ void dfu_moded_init(void)
_dfu_state_ctx.status = DFU_STATUS_OK; _dfu_state_ctx.status = DFU_STATUS_OK;
_dfu_state_ctx.attrs = 0; _dfu_state_ctx.attrs = 0;
_dfu_state_ctx.blk_transfer_in_proc = false; _dfu_state_ctx.blk_transfer_in_proc = false;
_dfu_state_ctx.alt = 0;
_dfu_state_ctx.intf = DFU_INTF_UNUSED;
dfu_debug_print_context(); dfu_debug_print_context();
} }
@ -156,32 +164,67 @@ void dfu_moded_reset(uint8_t rhport)
_dfu_state_ctx.state = DFU_IDLE; _dfu_state_ctx.state = DFU_IDLE;
_dfu_state_ctx.status = DFU_STATUS_OK; _dfu_state_ctx.status = DFU_STATUS_OK;
_dfu_state_ctx.attrs = 0;
_dfu_state_ctx.blk_transfer_in_proc = false; _dfu_state_ctx.blk_transfer_in_proc = false;
_dfu_state_ctx.alt = 0;
_dfu_state_ctx.intf = DFU_INTF_UNUSED;
dfu_debug_print_context(); dfu_debug_print_context();
} }
uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
{ {
(void) rhport; (void) rhport;
(void) max_len;
// Ensure this is DFU Mode uint16_t const drv_len = sizeof(tusb_desc_interface_t);
TU_VERIFY((itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS) &&
(itf_desc->bInterfaceProtocol == DFU_PROTOCOL_DFU), 0);
uint8_t const * p_desc = tu_desc_next( itf_desc ); uint8_t const *p_desc = (uint8_t const *)itf_desc;
uint16_t drv_len = sizeof(tusb_desc_interface_t);
if ( TUSB_DESC_FUNCTIONAL == tu_desc_type(p_desc) ) uint16_t total_len = 0;
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; // Ensure this is DFU Mode
_dfu_state_ctx.attrs = (uint8_t)dfu_desc->bAttributes; 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);
drv_len += tu_desc_len(p_desc); uint8_t const alt = ((tusb_desc_interface_t const *)p_desc)->bAlternateSetting;
p_desc = tu_desc_next(p_desc); uint8_t const intf = ((tusb_desc_interface_t const *)p_desc)->bInterfaceNumber;
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 // 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) if(stage == CONTROL_STAGE_SETUP)
{ {
// dfu-util will try to claim the interface with SET_INTERFACE request before sending DFU request // 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 && if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD && request->bRequest == TUSB_REQ_SET_INTERFACE)
TUSB_REQ_SET_INTERFACE == request->bRequest )
{ {
// 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); tud_control_status(rhport, request);
return true; return true;
} }
@ -210,17 +260,23 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
switch (request->bRequest) 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: case DFU_REQUEST_DNLOAD:
{ {
if ( (stage == CONTROL_STAGE_ACK) if ( (stage == CONTROL_STAGE_ACK)
&& ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0) && ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
&& (_dfu_state_ctx.state == DFU_DNLOAD_SYNC)) && (_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; return true;
} }
} // fallthrough } // fallthrough
case DFU_REQUEST_DETACH:
case DFU_REQUEST_UPLOAD: case DFU_REQUEST_UPLOAD:
case DFU_REQUEST_GETSTATUS: case DFU_REQUEST_GETSTATUS:
case DFU_REQUEST_CLRSTATUS: 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) 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); 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); tud_control_xfer(rhport, request, _dfu_state_ctx.transfer_buf, retval);
return 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; dfu_status_req_payload_t resp;
uint16_t timeout = 0;
resp.bStatus = _dfu_state_ctx.status; 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.bState = _dfu_state_ctx.state;
resp.iString = 0; resp.iString = 0;
@ -285,7 +353,7 @@ static void dfu_req_dnload_reply(uint8_t rhport, tusb_control_request_t const *
{ {
(void) rhport; (void) rhport;
TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, ); 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; _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; _dfu_state_ctx.state = DFU_DNLOAD_SYNC;
} else if (_dfu_state_ctx.state == DFU_MANIFEST) } 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; ? 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_state_ctx.state = DFU_DNBUSY;
dfu_req_getstatus_reply(rhport, request); dfu_req_getstatus_reply(rhport, request);
dfu_req_dnload_reply(rhport, request);
} else { } else {
_dfu_state_ctx.state = DFU_DNLOAD_IDLE; _dfu_state_ctx.state = DFU_DNLOAD_IDLE;
dfu_req_getstatus_reply(rhport, request); 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_state_ctx.blk_transfer_in_proc = true;
dfu_req_dnload_setup(rhport, request); dfu_req_dnload_setup(rhport, request);
} else { } 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; _dfu_state_ctx.state = DFU_MANIFEST_SYNC;
tud_control_status(rhport, request); 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: 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_state_ctx.state = DFU_MANIFEST;
dfu_req_getstatus_reply(rhport, request); dfu_req_getstatus_reply(rhport, request);
} else { } else
if ( tud_dfu_firmware_valid_check_cb() ) {
if ( tud_dfu_firmware_valid_check_cb(_dfu_state_ctx.alt) )
{ {
_dfu_state_ctx.state = DFU_IDLE; _dfu_state_ctx.state = DFU_IDLE;
} }

View File

@ -33,19 +33,32 @@
extern "C" { extern "C" {
#endif #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) // Application Callback API (weak is optional)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Invoked during DFU_MANIFEST_SYNC get status request to check if firmware // Invoked during DFU_MANIFEST_SYNC get status request to check if firmware is valid
// 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(void); 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 // 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 // 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 // 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. // 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 // Must be called when the application is done using the last block of data
// provided by tud_dfu_req_dnload_data_cb // 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 // Invoked during the last DFU_DNLOAD request, signifying that the host believes
// it is done transmitting data. // 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 true if the application agrees there is no more data
// Return false if the device disagrees, which will stall the pipe, and the Host // Return false if the device disagrees, which will stall the pipe, and the Host
// should initiate a recovery procedure // 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 // Invoked when the Host has terminated a download or upload transfer
TU_ATTR_WEAK void tud_dfu_abort_cb(void); TU_ATTR_WEAK void tud_dfu_abort_cb(void);
// Invoked when a DFU_UPLOAD request is received // 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 // This callback must populate data with up to length bytes
// Return the number of bytes to write // 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 // Internal Class Driver API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -605,17 +605,50 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
/* Function */ \ /* Function */ \
9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101) 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 // Maximum alternate settings (used for different partitons) supported
#define TUD_DFU_MODE_DESC_LEN (9 + 9) #ifndef CFG_TUD_DFU_ALT_COUNT
#define CFG_TUD_DFU_ALT_COUNT 1
#endif
// DFU runtime descriptor // Length of template descriptor: 18 bytes + number of alternatives * 9
// Interface number, string index, attributes, detach timeout, transfer size #define TUD_DFU_MODE_DESC_LEN (9 + (CFG_TUD_DFU_ALT_COUNT) * 9)
#define TUD_DFU_MODE_DESCRIPTOR(_itfnum, _stridx, _attr, _timeout, _xfer_size) \
/* Interface */ \ /* Primary Interface */
9, TUSB_DESC_INTERFACE, _itfnum, 0, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_DFU, _stridx, \ #define TUD_DFU_MODE_FUNC(_attr, _timeout, _xfer_size) \
/* Function */ \ /* Function */ \
9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101) 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 -------------// //------------- CDC-ECM -------------//