summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhathach <[email protected]>2021-07-15 20:47:50 +0700
committerhathach <[email protected]>2021-07-15 20:47:50 +0700
commitdaca9e520bce0b4eb5882c5692986ec5c513f319 (patch)
treec8ce4a1c2004dccd0ca9edf21b70f3cf34b5554b /src
parentb4fde90b5518c4e92750af2e27eae598267ad8cf (diff)
wrap up DFU update
Diffstat (limited to 'src')
-rw-r--r--src/class/dfu/dfu.h47
-rw-r--r--src/class/dfu/dfu_device.c393
-rw-r--r--src/class/dfu/dfu_device.h62
-rw-r--r--src/class/dfu/dfu_rt_device.c6
-rw-r--r--src/common/tusb_types.h2
-rw-r--r--src/device/usbd.c2
-rw-r--r--src/tusb_option.h4
7 files changed, 304 insertions, 212 deletions
diff --git a/src/class/dfu/dfu.h b/src/class/dfu/dfu.h
index 88f2f3529..114c827b8 100644
--- a/src/class/dfu/dfu.h
+++ b/src/class/dfu/dfu.h
@@ -36,6 +36,7 @@
//--------------------------------------------------------------------+
// Common Definitions
//--------------------------------------------------------------------+
+
// DFU Protocol
typedef enum
{
@@ -77,28 +78,28 @@ typedef enum {
// DFU Status
typedef enum {
- DFU_STATUS_OK = 0x00,
- DFU_STATUS_ERRTARGET = 0x01,
- DFU_STATUS_ERRFILE = 0x02,
- DFU_STATUS_ERRWRITE = 0x03,
- DFU_STATUS_ERRERASE = 0x04,
- DFU_STATUS_ERRCHECK_ERASED = 0x05,
- DFU_STATUS_ERRPROG = 0x06,
- DFU_STATUS_ERRVERIFY = 0x07,
- DFU_STATUS_ERRADDRESS = 0x08,
- DFU_STATUS_ERRNOTDONE = 0x09,
- DFU_STATUS_ERRFIRMWARE = 0x0A,
- DFU_STATUS_ERRVENDOR = 0x0B,
- DFU_STATUS_ERRUSBR = 0x0C,
- DFU_STATUS_ERRPOR = 0x0D,
- DFU_STATUS_ERRUNKNOWN = 0x0E,
- DFU_STATUS_ERRSTALLEDPKT = 0x0F,
-} dfu_device_status_t;
+ DFU_STATUS_OK = 0x00,
+ DFU_STATUS_ERR_TARGET = 0x01,
+ DFU_STATUS_ERR_FILE = 0x02,
+ DFU_STATUS_ERR_WRITE = 0x03,
+ DFU_STATUS_ERR_ERASE = 0x04,
+ DFU_STATUS_ERR_CHECK_ERASED = 0x05,
+ DFU_STATUS_ERR_PROG = 0x06,
+ DFU_STATUS_ERR_VERIFY = 0x07,
+ DFU_STATUS_ERR_ADDRESS = 0x08,
+ DFU_STATUS_ERR_NOTDONE = 0x09,
+ DFU_STATUS_ERR_FIRMWARE = 0x0A,
+ DFU_STATUS_ERR_VENDOR = 0x0B,
+ DFU_STATUS_ERR_USBR = 0x0C,
+ DFU_STATUS_ERR_POR = 0x0D,
+ DFU_STATUS_ERR_UNKNOWN = 0x0E,
+ DFU_STATUS_ERR_STALLEDPKT = 0x0F,
+} dfu_status_t;
-#define DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK (1u << 0)
-#define DFU_FUNC_ATTR_CAN_UPLOAD_BITMASK (1u << 1)
-#define DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK (1u << 2)
-#define DFU_FUNC_ATTR_WILL_DETACH_BITMASK (1u << 3)
+#define DFU_ATTR_CAN_DOWNLOAD (1u << 0)
+#define DFU_ATTR_CAN_UPLOAD (1u << 1)
+#define DFU_ATTR_MANIFESTATION_TOLERANT (1u << 2)
+#define DFU_ATTR_WILL_DETACH (1u << 3)
// DFU Status Request Payload
typedef struct TU_ATTR_PACKED
@@ -107,9 +108,9 @@ typedef struct TU_ATTR_PACKED
uint8_t bwPollTimeout[3];
uint8_t bState;
uint8_t iString;
-} dfu_status_req_payload_t;
+} dfu_status_response_t;
-TU_VERIFY_STATIC( sizeof(dfu_status_req_payload_t) == 6, "size is not correct");
+TU_VERIFY_STATIC( sizeof(dfu_status_response_t) == 6, "size is not correct");
#ifdef __cplusplus
}
diff --git a/src/class/dfu/dfu_device.c b/src/class/dfu/dfu_device.c
index 686d9b023..3aa90f5c5 100644
--- a/src/class/dfu/dfu_device.c
+++ b/src/class/dfu/dfu_device.c
@@ -42,32 +42,33 @@
//--------------------------------------------------------------------+
typedef struct
{
- dfu_device_status_t status;
- dfu_state_t state;
- uint8_t attrs;
- bool blk_transfer_in_proc;
- uint8_t alt_num;
- uint16_t block;
- uint16_t length;
- CFG_TUSB_MEM_ALIGN uint8_t transfer_buf[CFG_TUD_DFU_TRANSFER_BUFFER_SIZE];
+ uint8_t attrs;
+ uint8_t alt;
+
+ dfu_state_t state;
+ dfu_status_t status;
+
+ bool flashing_in_progress;
+ uint16_t block;
+ uint16_t length;
+
+ CFG_TUSB_MEM_ALIGN uint8_t transfer_buf[CFG_TUD_DFU_TRANSFER_BUFSIZE];
} dfu_state_ctx_t;
// Only a single dfu state is allowed
CFG_TUSB_MEM_SECTION static dfu_state_ctx_t _dfu_ctx;
-
-static void dfu_req_dnload_setup(uint8_t rhport, tusb_control_request_t const * request);
-static void dfu_req_getstatus_reply(uint8_t rhport, tusb_control_request_t const * request);
-static void dfu_req_dnload_reply(uint8_t rhport, tusb_control_request_t const * request);
-static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * request);
-
static void reset_state(void)
{
_dfu_ctx.state = DFU_IDLE;
_dfu_ctx.status = DFU_STATUS_OK;
- _dfu_ctx.blk_transfer_in_proc = false;
+ _dfu_ctx.flashing_in_progress = false;
}
+static bool reply_getstatus(uint8_t rhport, tusb_control_request_t const * request, dfu_state_t state, dfu_status_t status, uint32_t timeout);
+static bool process_download_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
+static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
+
//--------------------------------------------------------------------+
// Debug
//--------------------------------------------------------------------+
@@ -75,13 +76,13 @@ static void reset_state(void)
static tu_lookup_entry_t const _dfu_request_lookup[] =
{
- { .key = DFU_REQUEST_DETACH , .data = "DETACH" },
- { .key = DFU_REQUEST_DNLOAD , .data = "DNLOAD" },
- { .key = DFU_REQUEST_UPLOAD , .data = "UPLOAD" },
+ { .key = DFU_REQUEST_DETACH , .data = "DETACH" },
+ { .key = DFU_REQUEST_DNLOAD , .data = "DNLOAD" },
+ { .key = DFU_REQUEST_UPLOAD , .data = "UPLOAD" },
{ .key = DFU_REQUEST_GETSTATUS , .data = "GETSTATUS" },
{ .key = DFU_REQUEST_CLRSTATUS , .data = "CLRSTATUS" },
- { .key = DFU_REQUEST_GETSTATE , .data = "GETSTATE" },
- { .key = DFU_REQUEST_ABORT , .data = "ABORT" },
+ { .key = DFU_REQUEST_GETSTATE , .data = "GETSTATE" },
+ { .key = DFU_REQUEST_ABORT , .data = "ABORT" },
};
static tu_lookup_table_t const _dfu_request_table =
@@ -92,17 +93,17 @@ static tu_lookup_table_t const _dfu_request_table =
static tu_lookup_entry_t const _dfu_state_lookup[] =
{
- { .key = APP_IDLE , .data = "APP_IDLE" },
- { .key = APP_DETACH , .data = "APP_DETACH" },
- { .key = DFU_IDLE , .data = "DFU_IDLE" },
- { .key = DFU_DNLOAD_SYNC , .data = "DFU_DNLOAD_SYNC" },
- { .key = DFU_DNBUSY , .data = "DFU_DNBUSY" },
- { .key = DFU_DNLOAD_IDLE , .data = "DFU_DNLOAD_IDLE" },
- { .key = DFU_MANIFEST_SYNC , .data = "DFU_MANIFEST_SYNC" },
- { .key = DFU_MANIFEST , .data = "DFU_MANIFEST" },
- { .key = DFU_MANIFEST_WAIT_RESET , .data = "DFU_MANIFEST_WAIT_RESET" },
- { .key = DFU_UPLOAD_IDLE , .data = "DFU_UPLOAD_IDLE" },
- { .key = DFU_ERROR , .data = "DFU_ERROR" },
+ { .key = APP_IDLE , .data = "APP_IDLE" },
+ { .key = APP_DETACH , .data = "APP_DETACH" },
+ { .key = DFU_IDLE , .data = "IDLE" },
+ { .key = DFU_DNLOAD_SYNC , .data = "DNLOAD_SYNC" },
+ { .key = DFU_DNBUSY , .data = "DNBUSY" },
+ { .key = DFU_DNLOAD_IDLE , .data = "DNLOAD_IDLE" },
+ { .key = DFU_MANIFEST_SYNC , .data = "MANIFEST_SYNC" },
+ { .key = DFU_MANIFEST , .data = "MANIFEST" },
+ { .key = DFU_MANIFEST_WAIT_RESET , .data = "MANIFEST_WAIT_RESET" },
+ { .key = DFU_UPLOAD_IDLE , .data = "UPLOAD_IDLE" },
+ { .key = DFU_ERROR , .data = "ERROR" },
};
static tu_lookup_table_t const _dfu_state_table =
@@ -113,22 +114,22 @@ static tu_lookup_table_t const _dfu_state_table =
static tu_lookup_entry_t const _dfu_status_lookup[] =
{
- { .key = DFU_STATUS_OK , .data = "OK" },
- { .key = DFU_STATUS_ERRTARGET , .data = "errTARGET" },
- { .key = DFU_STATUS_ERRFILE , .data = "errFILE" },
- { .key = DFU_STATUS_ERRWRITE , .data = "errWRITE" },
- { .key = DFU_STATUS_ERRERASE , .data = "errERASE" },
- { .key = DFU_STATUS_ERRCHECK_ERASED , .data = "errCHECK_ERASED" },
- { .key = DFU_STATUS_ERRPROG , .data = "errPROG" },
- { .key = DFU_STATUS_ERRVERIFY , .data = "errVERIFY" },
- { .key = DFU_STATUS_ERRADDRESS , .data = "errADDRESS" },
- { .key = DFU_STATUS_ERRNOTDONE , .data = "errNOTDONE" },
- { .key = DFU_STATUS_ERRFIRMWARE , .data = "errFIRMWARE" },
- { .key = DFU_STATUS_ERRVENDOR , .data = "errVENDOR" },
- { .key = DFU_STATUS_ERRUSBR , .data = "errUSBR" },
- { .key = DFU_STATUS_ERRPOR , .data = "errPOR" },
- { .key = DFU_STATUS_ERRUNKNOWN , .data = "errUNKNOWN" },
- { .key = DFU_STATUS_ERRSTALLEDPKT , .data = "errSTALLEDPKT" },
+ { .key = DFU_STATUS_OK , .data = "OK" },
+ { .key = DFU_STATUS_ERR_TARGET , .data = "errTARGET" },
+ { .key = DFU_STATUS_ERR_FILE , .data = "errFILE" },
+ { .key = DFU_STATUS_ERR_WRITE , .data = "errWRITE" },
+ { .key = DFU_STATUS_ERR_ERASE , .data = "errERASE" },
+ { .key = DFU_STATUS_ERR_CHECK_ERASED , .data = "errCHECK_ERASED" },
+ { .key = DFU_STATUS_ERR_PROG , .data = "errPROG" },
+ { .key = DFU_STATUS_ERR_VERIFY , .data = "errVERIFY" },
+ { .key = DFU_STATUS_ERR_ADDRESS , .data = "errADDRESS" },
+ { .key = DFU_STATUS_ERR_NOTDONE , .data = "errNOTDONE" },
+ { .key = DFU_STATUS_ERR_FIRMWARE , .data = "errFIRMWARE" },
+ { .key = DFU_STATUS_ERR_VENDOR , .data = "errVENDOR" },
+ { .key = DFU_STATUS_ERR_USBR , .data = "errUSBR" },
+ { .key = DFU_STATUS_ERR_POR , .data = "errPOR" },
+ { .key = DFU_STATUS_ERR_UNKNOWN , .data = "errUNKNOWN" },
+ { .key = DFU_STATUS_ERR_STALLEDPKT , .data = "errSTALLEDPKT" },
};
static tu_lookup_table_t const _dfu_status_table =
@@ -139,38 +140,22 @@ static tu_lookup_table_t const _dfu_status_table =
#endif
-#define dfu_debug_print_context() \
-{ \
- TU_LOG2(" DFU at State: %s\r\n Status: %s\r\n", \
- tu_lookup_find(&_dfu_state_table, _dfu_ctx.state), \
- tu_lookup_find(&_dfu_status_table, _dfu_ctx.status) ); \
-}
-
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
-void dfu_moded_init(void)
-{
- _dfu_ctx.state = DFU_IDLE;
- _dfu_ctx.status = DFU_STATUS_OK;
- _dfu_ctx.attrs = 0;
- _dfu_ctx.blk_transfer_in_proc = false;
- _dfu_ctx.alt_num = 0;
-
- dfu_debug_print_context();
-}
-
void dfu_moded_reset(uint8_t rhport)
{
(void) rhport;
- _dfu_ctx.state = DFU_IDLE;
- _dfu_ctx.status = DFU_STATUS_OK;
_dfu_ctx.attrs = 0;
- _dfu_ctx.blk_transfer_in_proc = false;
- _dfu_ctx.alt_num = 0;
+ _dfu_ctx.alt = 0;
+
+ reset_state();
+}
- dfu_debug_print_context();
+void dfu_moded_init(void)
+{
+ dfu_moded_reset(0);
}
uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
@@ -204,9 +189,9 @@ uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
_dfu_ctx.attrs = func_desc->bAttributes;
- // CFG_TUD_DFU_TRANSFER_BUFFER_SIZE has to be set to the buffer size used in TUD_DFU_DESCRIPTOR
+ // CFG_TUD_DFU_TRANSFER_BUFSIZE has to be set to the buffer size used in TUD_DFU_DESCRIPTOR
uint16_t const transfer_size = tu_le16toh( tu_unaligned_read16(&func_desc->wTransferSize) );
- TU_ASSERT(transfer_size <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, drv_len);
+ TU_ASSERT(transfer_size <= CFG_TUD_DFU_TRANSFER_BUFSIZE, drv_len);
return drv_len;
}
@@ -216,11 +201,10 @@ uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
// return false to stall control endpoint (e.g unsupported request)
bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
{
- // nothing to do with DATA stage
- if ( stage == CONTROL_STAGE_DATA ) return true;
-
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
+ TU_LOG2(" DFU State : %s, Status: %s\r\n", tu_lookup_find(&_dfu_state_table, _dfu_ctx.state), tu_lookup_find(&_dfu_status_table, _dfu_ctx.status));
+
if ( request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD )
{
// Standard request include GET/SET_INTERFACE
@@ -229,8 +213,8 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
case TUSB_REQ_SET_INTERFACE:
if ( stage == CONTROL_STAGE_SETUP )
{
- // Switch Alt interface and Re-initalize state machine
- _dfu_ctx.alt_num = (uint8_t) request->wValue;
+ // Switch Alt interface and reset state machine
+ _dfu_ctx.alt = (uint8_t) request->wValue;
reset_state();
return tud_control_status(rhport, request);
}
@@ -239,7 +223,7 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
case TUSB_REQ_GET_INTERFACE:
if(stage == CONTROL_STAGE_SETUP)
{
- return tud_control_xfer(rhport, request, &_dfu_ctx.alt_num, 1);
+ return tud_control_xfer(rhport, request, &_dfu_ctx.alt, 1);
}
break;
@@ -249,6 +233,8 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
}
else if ( request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS )
{
+ TU_LOG2(" DFU Request: %s\r\n", tu_lookup_find(&_dfu_request_table, request->bRequest));
+
// Class request
switch ( request->bRequest )
{
@@ -259,73 +245,97 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
}
else if ( stage == CONTROL_STAGE_ACK )
{
- if (tud_dfu_detach_cb) tud_dfu_detach_cb();
+ if ( tud_dfu_detach_cb ) tud_dfu_detach_cb();
}
break;
- case DFU_REQUEST_ABORT:
+ case DFU_REQUEST_CLRSTATUS:
if ( stage == CONTROL_STAGE_SETUP )
{
- if (tud_dfu_abort_cb) tud_dfu_abort_cb(_dfu_ctx.alt_num);
-
reset_state();
tud_control_status(rhport, request);
}
break;
- case DFU_REQUEST_CLRSTATUS:
+ case DFU_REQUEST_GETSTATE:
if ( stage == CONTROL_STAGE_SETUP )
{
- reset_state();
- tud_control_status(rhport, request);
+ tud_control_xfer(rhport, request, &_dfu_ctx.state, 1);
}
break;
- case DFU_REQUEST_GETSTATE:
+ case DFU_REQUEST_ABORT:
if ( stage == CONTROL_STAGE_SETUP )
{
- tud_control_xfer(rhport, request, &_dfu_ctx.state, 1);
+ reset_state();
+ tud_control_status(rhport, request);
+ }
+ else if ( stage == CONTROL_STAGE_ACK )
+ {
+ if ( tud_dfu_abort_cb ) tud_dfu_abort_cb(_dfu_ctx.alt);
}
break;
case DFU_REQUEST_UPLOAD:
if ( stage == CONTROL_STAGE_SETUP )
{
- TU_VERIFY(_dfu_ctx.attrs & DFU_FUNC_ATTR_CAN_UPLOAD_BITMASK);
+ TU_VERIFY(_dfu_ctx.attrs & DFU_ATTR_CAN_UPLOAD);
TU_VERIFY(tud_dfu_upload_cb);
- TU_VERIFY(request->wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE);
+ TU_VERIFY(request->wLength <= CFG_TUD_DFU_TRANSFER_BUFSIZE);
- uint16_t const xfer_len = tud_dfu_upload_cb(_dfu_ctx.alt_num, request->wValue, _dfu_ctx.transfer_buf, request->wLength);
+ uint16_t const xfer_len = tud_dfu_upload_cb(_dfu_ctx.alt, request->wValue, _dfu_ctx.transfer_buf, request->wLength);
- tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, xfer_len);
+ return tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, xfer_len);
}
break;
case DFU_REQUEST_DNLOAD:
- {
- if ( (stage == CONTROL_STAGE_ACK)
- && ((_dfu_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
- && (_dfu_ctx.state == DFU_DNLOAD_SYNC))
+ if ( stage == CONTROL_STAGE_SETUP )
{
- _dfu_ctx.block = request->wValue;
+ TU_VERIFY(_dfu_ctx.attrs & DFU_ATTR_CAN_DOWNLOAD);
+ TU_VERIFY(_dfu_ctx.state == DFU_IDLE || _dfu_ctx.state == DFU_DNLOAD_IDLE);
+ TU_VERIFY(request->wLength <= CFG_TUD_DFU_TRANSFER_BUFSIZE);
+
+ // set to true for both download and manifest
+ _dfu_ctx.flashing_in_progress = true;
+
+ // save block and length for flashing
+ _dfu_ctx.block = request->wValue;
_dfu_ctx.length = request->wLength;
- return true;
+
+ if ( request->wLength )
+ {
+ // Download with payload -> transition to DOWNLOAD SYNC
+ _dfu_ctx.state = DFU_DNLOAD_SYNC;
+ return tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, request->wLength);
+ }
+ else
+ {
+ // Download is complete -> transition to MANIFEST SYNC
+ _dfu_ctx.state = DFU_MANIFEST_SYNC;
+ return tud_control_status(rhport, request);
+ }
}
- }
- // fallthrough
- case DFU_REQUEST_GETSTATUS:
+ break;
- {
- if(stage == CONTROL_STAGE_SETUP)
+ case DFU_REQUEST_GETSTATUS:
+ switch ( _dfu_ctx.state )
{
- return dfu_state_machine(rhport, request);
+ case DFU_DNLOAD_SYNC:
+ return process_download_get_status(rhport, stage, request);
+ break;
+
+ case DFU_MANIFEST_SYNC:
+ return process_manifest_get_status(rhport, stage, request);
+ break;
+
+ default:
+ if ( stage == CONTROL_STAGE_SETUP ) return reply_getstatus(rhport, request, _dfu_ctx.state, _dfu_ctx.status, 0);
+ break;
}
- }
break;
- default:
- return false; // stall unsupported request
- break;
+ default: return false; // stall unsupported request
}
}else
{
@@ -335,32 +345,130 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
return true;
}
-static void dfu_req_getstatus_reply(uint8_t rhport, tusb_control_request_t const * request)
+void tud_dfu_finish_flashing(uint8_t status)
+{
+ _dfu_ctx.flashing_in_progress = false;
+
+ if ( status == DFU_STATUS_OK )
+ {
+ if (_dfu_ctx.state == DFU_DNBUSY)
+ {
+ _dfu_ctx.state = DFU_DNLOAD_SYNC;
+ }
+ else if (_dfu_ctx.state == DFU_MANIFEST)
+ {
+ _dfu_ctx.state = (_dfu_ctx.attrs & DFU_ATTR_MANIFESTATION_TOLERANT)
+ ? DFU_MANIFEST_SYNC : DFU_MANIFEST_WAIT_RESET;
+ }
+ }
+ else
+ {
+ // failed while flashing, move to dfuError
+ _dfu_ctx.state = DFU_ERROR;
+ _dfu_ctx.status = status;
+ }
+}
+
+static bool process_download_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
+{
+ if ( stage == CONTROL_STAGE_SETUP )
+ {
+ // only transition to next state on CONTROL_STAGE_ACK
+ dfu_state_t next_state;
+ uint32_t timeout;
+
+ if ( _dfu_ctx.flashing_in_progress )
+ {
+ next_state = DFU_DNBUSY;
+ timeout = tud_dfu_get_timeout_cb(_dfu_ctx.alt, (uint8_t) next_state);
+ }
+ else
+ {
+ next_state = DFU_DNLOAD_IDLE;
+ timeout = 0;
+ }
+
+ return reply_getstatus(rhport, request, next_state, _dfu_ctx.status, timeout);
+ }
+ else if ( stage == CONTROL_STAGE_ACK )
+ {
+ if ( _dfu_ctx.flashing_in_progress )
+ {
+ _dfu_ctx.state = DFU_DNBUSY;
+ tud_dfu_download_cb(_dfu_ctx.alt, _dfu_ctx.block, _dfu_ctx.transfer_buf, _dfu_ctx.length);
+ }else
+ {
+ _dfu_ctx.state = DFU_DNLOAD_IDLE;
+ }
+ }
+
+ return true;
+}
+
+static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
{
- uint32_t timeout = 0;
- if ( tud_dfu_get_status_cb )
+ if ( stage == CONTROL_STAGE_SETUP )
{
- timeout = tud_dfu_get_status_cb(_dfu_ctx.alt_num, _dfu_ctx.state);
+ // only transition to next state on CONTROL_STAGE_ACK
+ dfu_state_t next_state;
+ uint32_t timeout;
+
+ if ( _dfu_ctx.flashing_in_progress )
+ {
+ next_state = DFU_MANIFEST;
+ timeout = tud_dfu_get_timeout_cb(_dfu_ctx.alt, next_state);
+ }
+ else
+ {
+ next_state = DFU_IDLE;
+ timeout = 0;
+ }
+
+ return reply_getstatus(rhport, request, next_state, _dfu_ctx.status, timeout);
+ }
+ else if ( stage == CONTROL_STAGE_ACK )
+ {
+ if ( _dfu_ctx.flashing_in_progress )
+ {
+ _dfu_ctx.state = DFU_MANIFEST;
+ tud_dfu_manifest_cb(_dfu_ctx.alt);
+ }
+ else
+ {
+ _dfu_ctx.state = DFU_IDLE;
+ }
}
- dfu_status_req_payload_t resp;
- resp.bStatus = _dfu_ctx.status;
+ return true;
+}
+
+static bool reply_getstatus(uint8_t rhport, tusb_control_request_t const * request, dfu_state_t state, dfu_status_t status, uint32_t timeout)
+{
+ dfu_status_response_t resp;
+ resp.bStatus = (uint8_t) status;
resp.bwPollTimeout[0] = TU_U32_BYTE0(timeout);
resp.bwPollTimeout[1] = TU_U32_BYTE1(timeout);
resp.bwPollTimeout[2] = TU_U32_BYTE2(timeout);
- resp.bState = _dfu_ctx.state;
- resp.iString = 0;
+ resp.bState = (uint8_t) state;
+ resp.iString = 0;
- tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_req_payload_t));
+ return tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_response_t));
}
+
+#if 0
+
+static void dfu_req_dnload_setup(uint8_t rhport, tusb_control_request_t const * request);
+static void dfu_req_dnload_reply(uint8_t rhport, tusb_control_request_t const * request);
+static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * request);
+
static void dfu_req_dnload_setup(uint8_t rhport, tusb_control_request_t const * request)
{
// TODO: add "zero" copy mode so the buffer we read into can be provided by the user
// if they wish, there still will be the internal control buffer copy to this buffer
// but this mode would provide zero copy from the class driver to the application
- TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, );
+ TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFSIZE, );
// setup for data phase
tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, request->wLength);
}
@@ -368,21 +476,9 @@ static void dfu_req_dnload_setup(uint8_t rhport, tusb_control_request_t const *
static void dfu_req_dnload_reply(uint8_t rhport, tusb_control_request_t const * request)
{
(void) rhport;
- TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, );
- tud_dfu_download_cb(_dfu_ctx.alt_num,_dfu_ctx.block, (uint8_t *)_dfu_ctx.transfer_buf, _dfu_ctx.length);
- _dfu_ctx.blk_transfer_in_proc = false;
-}
-
-void tud_dfu_download_complete(void)
-{
- if (_dfu_ctx.state == DFU_DNBUSY)
- {
- _dfu_ctx.state = DFU_DNLOAD_SYNC;
- } else if (_dfu_ctx.state == DFU_MANIFEST)
- {
- _dfu_ctx.state = ((_dfu_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
- ? DFU_MANIFEST_WAIT_RESET : DFU_MANIFEST_SYNC;
- }
+ TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFSIZE, );
+ tud_dfu_download_cb(_dfu_ctx.alt,_dfu_ctx.block, (uint8_t *)_dfu_ctx.transfer_buf, _dfu_ctx.length);
+ _dfu_ctx.flashing_in_progress = false;
}
static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * request)
@@ -398,11 +494,11 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
{
case DFU_REQUEST_DNLOAD:
{
- if( ((_dfu_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
+ if( ((_dfu_ctx.attrs & DFU_ATTR_CAN_DOWNLOAD) != 0)
&& (request->wLength > 0) )
{
_dfu_ctx.state = DFU_DNLOAD_SYNC;
- _dfu_ctx.blk_transfer_in_proc = true;
+ _dfu_ctx.flashing_in_progress = true;
dfu_req_dnload_setup(rhport, request);
} else {
_dfu_ctx.state = DFU_ERROR;
@@ -411,7 +507,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
break;
case DFU_REQUEST_GETSTATUS:
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
break;
default:
@@ -428,14 +524,14 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
{
case DFU_REQUEST_GETSTATUS:
{
- if ( _dfu_ctx.blk_transfer_in_proc )
+ if ( _dfu_ctx.flashing_in_progress )
{
_dfu_ctx.state = DFU_DNBUSY;
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
dfu_req_dnload_reply(rhport, request);
} else {
_dfu_ctx.state = DFU_DNLOAD_IDLE;
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
}
}
break;
@@ -466,14 +562,14 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
{
case DFU_REQUEST_DNLOAD:
{
- if( ((_dfu_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
+ if( ((_dfu_ctx.attrs & DFU_ATTR_CAN_DOWNLOAD) != 0)
&& (request->wLength > 0) )
{
_dfu_ctx.state = DFU_DNLOAD_SYNC;
- _dfu_ctx.blk_transfer_in_proc = true;
+ _dfu_ctx.flashing_in_progress = true;
dfu_req_dnload_setup(rhport, request);
} else {
- if ( tud_dfu_device_data_done_check_cb(_dfu_ctx.alt_num) )
+ if ( tud_dfu_download_complete_cb(_dfu_ctx.alt) )
{
_dfu_ctx.state = DFU_MANIFEST_SYNC;
tud_control_status(rhport, request);
@@ -486,7 +582,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
break;
case DFU_REQUEST_GETSTATUS:
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
break;
default:
@@ -503,17 +599,17 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
{
case DFU_REQUEST_GETSTATUS:
{
- if ((_dfu_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
+ if ((_dfu_ctx.attrs & DFU_ATTR_MANIFESTATION_TOLERANT) == 0)
{
_dfu_ctx.state = DFU_MANIFEST;
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
} else
{
- if ( tud_dfu_firmware_valid_check_cb(_dfu_ctx.alt_num) )
+ if ( tud_dfu_manifest_cb(_dfu_ctx.alt) )
{
_dfu_ctx.state = DFU_IDLE;
}
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
}
}
break;
@@ -556,7 +652,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
switch (request->bRequest)
{
case DFU_REQUEST_GETSTATUS:
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
break;
default:
@@ -571,7 +667,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
switch (request->bRequest)
{
case DFU_REQUEST_GETSTATUS:
- dfu_req_getstatus_reply(rhport, request);
+ reply_getstatus(rhport, request);
break;
default:
@@ -590,5 +686,6 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
return true;
}
+#endif
#endif
diff --git a/src/class/dfu/dfu_device.h b/src/class/dfu/dfu_device.h
index d5d464f3b..2d398aada 100644
--- a/src/class/dfu/dfu_device.h
+++ b/src/class/dfu/dfu_device.h
@@ -37,53 +37,51 @@
// 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_DESCRIPTOR"
+#if !defined(CFG_TUD_DFU_TRANSFER_BUFSIZE)
+ #error "CFG_TUD_DFU_TRANSFER_BUFSIZE must be defined, it has to be set to the buffer size used in TUD_DFU_DESCRIPTOR"
#endif
//--------------------------------------------------------------------+
-// Application Callback API (weak is optional)
+// Application API
//--------------------------------------------------------------------+
-// 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
-// Return the bwPollTimeout value for host's response, 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 uint32_t tud_dfu_get_status_cb(uint8_t alt, uint8_t state);
+// Must be called when the application is done with flashing started by
+// tud_dfu_download_cb() and tud_dfu_manifest_cb().
+// status is DFU_STATUS_OK if successful, any other error status will cause state to enter dfuError
+void tud_dfu_finish_flashing(uint8_t status);
-// 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_download_cb(uint8_t alt, uint16_t wBlockNum, uint8_t* data, uint16_t length);
+//--------------------------------------------------------------------+
+// Application Callback API (weak is optional)
+//--------------------------------------------------------------------+
-// Must be called when the application is done using the last block of data
-// provided by tud_dfu_download_cb
-void tud_dfu_download_complete(void);
+// Note: alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc.
-// 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(uint8_t alt);
+// 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);
-// Invoked when the Host has terminated a download or upload transfer
-TU_ATTR_WEAK void tud_dfu_abort_cb(uint8_t alt);
+// 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);
+
+// 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);
-// 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
+// Invoked when received DFU_UPLOAD request
+// Application must populate data with up to length bytes and
+// Return the number of written bytes
TU_ATTR_WEAK uint16_t tud_dfu_upload_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_detach_cb(void);
+// Invoked when the Host has terminated a download or upload transfer
+TU_ATTR_WEAK void tud_dfu_abort_cb(uint8_t alt);
+
//--------------------------------------------------------------------+
// Internal Class Driver API
//--------------------------------------------------------------------+
diff --git a/src/class/dfu/dfu_rt_device.c b/src/class/dfu/dfu_rt_device.c
index 07e6f30f3..afee2aa1f 100644
--- a/src/class/dfu/dfu_rt_device.c
+++ b/src/class/dfu/dfu_rt_device.c
@@ -108,10 +108,10 @@ bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
case DFU_REQUEST_GETSTATUS:
{
TU_LOG2(" DFU RT Request: GETSTATUS\r\n");
- dfu_status_req_payload_t resp;
+ dfu_status_response_t resp;
// Status = OK, Poll timeout is ignored during RT, State = APP_IDLE, IString = 0
- memset(&resp, 0x00, sizeof(dfu_status_req_payload_t));
- tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_req_payload_t));
+ memset(&resp, 0x00, sizeof(dfu_status_response_t));
+ tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_response_t));
}
break;
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h
index fc1035e2c..eab67ebd5 100644
--- a/src/common/tusb_types.h
+++ b/src/common/tusb_types.h
@@ -449,7 +449,7 @@ typedef struct TU_ATTR_PACKED
/*------------------------------------------------------------------*/
/* Types
*------------------------------------------------------------------*/
-typedef struct TU_ATTR_PACKED TU_ATTR_ALIGNED(4) {
+typedef struct TU_ATTR_PACKED {
union {
struct TU_ATTR_PACKED {
uint8_t recipient : 5; ///< Recipient type tusb_request_recipient_t.
diff --git a/src/device/usbd.c b/src/device/usbd.c
index af4fd58c4..cf9af783d 100644
--- a/src/device/usbd.c
+++ b/src/device/usbd.c
@@ -190,7 +190,7 @@ static usbd_class_driver_t const _usbd_driver[] =
#if CFG_TUD_DFU_MODE
{
- DRIVER_NAME("DFU-MODE")
+ DRIVER_NAME("DFU")
.init = dfu_moded_init,
.reset = dfu_moded_reset,
.open = dfu_moded_open,
diff --git a/src/tusb_option.h b/src/tusb_option.h
index bf7591dcf..4351d9486 100644
--- a/src/tusb_option.h
+++ b/src/tusb_option.h
@@ -253,10 +253,6 @@
#define CFG_TUD_DFU_MODE 0
#endif
-#ifndef CFG_TUD_DFU_TRANSFER_BUFFER_SIZE
- #define CFG_TUD_DFU_TRANSFER_BUFFER_SIZE 64
-#endif
-
#ifndef CFG_TUD_NET
#define CFG_TUD_NET 0
#endif