2021-04-05 16:32:58 -04:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 XMOS LIMITED
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tusb_option.h"
|
|
|
|
|
2021-04-12 11:17:01 -04:00
|
|
|
#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_DFU_MODE)
|
2021-04-05 16:32:58 -04:00
|
|
|
|
2021-05-27 17:40:39 +07:00
|
|
|
#include "device/usbd.h"
|
2021-04-05 16:32:58 -04:00
|
|
|
#include "device/usbd_pvt.h"
|
|
|
|
|
2021-05-27 17:40:39 +07:00
|
|
|
#include "dfu_device.h"
|
|
|
|
|
2021-04-05 16:32:58 -04:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// MACRO CONSTANT TYPEDEF
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
2021-07-07 11:55:36 +02:00
|
|
|
#define DFU_INTF_UNUSED 0xFF
|
|
|
|
|
2021-04-05 16:32:58 -04:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// INTERNAL OBJECT & FUNCTION DECLARATION
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
typedef struct TU_ATTR_PACKED
|
|
|
|
{
|
2021-04-22 14:56:52 -04:00
|
|
|
dfu_device_status_t status;
|
|
|
|
dfu_state_t state;
|
2021-07-07 18:02:04 +02:00
|
|
|
uint8_t attrs;
|
2021-04-05 16:32:58 -04:00
|
|
|
bool blk_transfer_in_proc;
|
2021-07-05 17:56:21 +02:00
|
|
|
uint8_t alt;
|
2021-07-08 01:10:02 +02:00
|
|
|
uint16_t block;
|
|
|
|
uint16_t length;
|
2021-04-05 16:32:58 -04:00
|
|
|
CFG_TUSB_MEM_ALIGN uint8_t transfer_buf[CFG_TUD_DFU_TRANSFER_BUFFER_SIZE];
|
2021-04-22 14:56:52 -04:00
|
|
|
} dfu_state_ctx_t;
|
2021-04-05 16:32:58 -04:00
|
|
|
|
|
|
|
// Only a single dfu state is allowed
|
2021-04-22 14:56:52 -04:00
|
|
|
CFG_TUSB_MEM_SECTION static dfu_state_ctx_t _dfu_state_ctx;
|
2021-04-05 16:32:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
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 uint16_t dfu_req_upload(uint8_t rhport, tusb_control_request_t const * request, uint16_t block_num, uint16_t wLength);
|
2021-04-22 17:45:33 -04:00
|
|
|
static void dfu_req_dnload_reply(uint8_t rhport, tusb_control_request_t const * request);
|
2021-04-22 14:56:52 -04:00
|
|
|
static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * request);
|
2021-04-05 16:32:58 -04:00
|
|
|
|
2021-04-22 15:02:50 -04:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Debug
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
#if CFG_TUSB_DEBUG >= 2
|
|
|
|
|
|
|
|
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_GETSTATUS , .data = "GETSTATUS" },
|
|
|
|
{ .key = DFU_REQUEST_CLRSTATUS , .data = "CLRSTATUS" },
|
|
|
|
{ .key = DFU_REQUEST_GETSTATE , .data = "GETSTATE" },
|
|
|
|
{ .key = DFU_REQUEST_ABORT , .data = "ABORT" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static tu_lookup_table_t const _dfu_request_table =
|
|
|
|
{
|
|
|
|
.count = TU_ARRAY_SIZE(_dfu_request_lookup),
|
|
|
|
.items = _dfu_request_lookup
|
|
|
|
};
|
|
|
|
|
|
|
|
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" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static tu_lookup_table_t const _dfu_state_table =
|
|
|
|
{
|
|
|
|
.count = TU_ARRAY_SIZE(_dfu_state_lookup),
|
|
|
|
.items = _dfu_state_lookup
|
|
|
|
};
|
|
|
|
|
|
|
|
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" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static tu_lookup_table_t const _dfu_status_table =
|
|
|
|
{
|
|
|
|
.count = TU_ARRAY_SIZE(_dfu_status_lookup),
|
|
|
|
.items = _dfu_status_lookup
|
|
|
|
};
|
|
|
|
|
|
|
|
#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_state_ctx.state), \
|
|
|
|
tu_lookup_find(&_dfu_status_table, _dfu_state_ctx.status) ); \
|
|
|
|
}
|
2021-04-05 16:32:58 -04:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// USBD Driver API
|
|
|
|
//--------------------------------------------------------------------+
|
2021-04-07 17:05:04 -04:00
|
|
|
void dfu_moded_init(void)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-04-29 16:04:18 -04:00
|
|
|
_dfu_state_ctx.state = DFU_IDLE;
|
2021-04-05 16:32:58 -04:00
|
|
|
_dfu_state_ctx.status = DFU_STATUS_OK;
|
2021-07-07 18:02:04 +02:00
|
|
|
_dfu_state_ctx.attrs = 0;
|
2021-04-05 16:32:58 -04:00
|
|
|
_dfu_state_ctx.blk_transfer_in_proc = false;
|
2021-07-06 18:03:05 +02:00
|
|
|
_dfu_state_ctx.alt = 0;
|
2021-04-05 16:32:58 -04:00
|
|
|
|
|
|
|
dfu_debug_print_context();
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:05:04 -04:00
|
|
|
void dfu_moded_reset(uint8_t rhport)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-05-26 18:16:56 +07:00
|
|
|
(void) rhport;
|
|
|
|
|
2021-04-29 16:04:18 -04:00
|
|
|
_dfu_state_ctx.state = DFU_IDLE;
|
2021-04-05 16:32:58 -04:00
|
|
|
_dfu_state_ctx.status = DFU_STATUS_OK;
|
2021-07-07 18:02:04 +02:00
|
|
|
_dfu_state_ctx.attrs = 0;
|
2021-04-05 16:32:58 -04:00
|
|
|
_dfu_state_ctx.blk_transfer_in_proc = false;
|
2021-07-06 18:03:05 +02:00
|
|
|
_dfu_state_ctx.alt = 0;
|
|
|
|
|
2021-04-05 16:32:58 -04:00
|
|
|
dfu_debug_print_context();
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:05:04 -04:00
|
|
|
uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
|
|
|
(void) rhport;
|
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
//------------- Interface (with Alt) descriptor -------------//
|
|
|
|
uint8_t const itf_num = itf_desc->bInterfaceNumber;
|
|
|
|
uint8_t alt_count = 0;
|
2021-07-07 11:55:36 +02:00
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
uint16_t drv_len = 0;
|
|
|
|
while(itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS && itf_desc->bInterfaceProtocol == DFU_PROTOCOL_DFU)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-07-12 18:48:33 +07:00
|
|
|
TU_ASSERT(max_len > drv_len, 0);
|
2021-07-05 17:56:21 +02:00
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
// Alternate must have the same interface number
|
|
|
|
TU_ASSERT(itf_desc->bInterfaceNumber == itf_num, 0);
|
2021-07-07 11:55:36 +02:00
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
// Alt should increase by one every time
|
|
|
|
TU_ASSERT(itf_desc->bAlternateSetting == alt_count, 0);
|
|
|
|
alt_count++;
|
2021-07-07 11:55:36 +02:00
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
drv_len += tu_desc_len(itf_desc);
|
|
|
|
itf_desc = (tusb_desc_interface_t const *) tu_desc_next(itf_desc);
|
2021-07-05 17:56:21 +02:00
|
|
|
}
|
2021-07-07 11:55:36 +02:00
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
//------------- DFU Functional descriptor -------------//
|
|
|
|
tusb_desc_dfu_functional_t const *func_desc = (tusb_desc_dfu_functional_t const *) itf_desc;
|
|
|
|
TU_ASSERT(tu_desc_type(func_desc) == TUSB_DESC_FUNCTIONAL, 0);
|
|
|
|
drv_len += sizeof(tusb_desc_dfu_functional_t);
|
2021-07-07 18:02:04 +02:00
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
_dfu_state_ctx.attrs = func_desc->bAttributes;
|
2021-07-07 18:02:04 +02:00
|
|
|
|
2021-07-12 18:51:57 +07:00
|
|
|
// CFG_TUD_DFU_TRANSFER_BUFFER_SIZE has to be set to the buffer size used in TUD_DFU_DESCRIPTOR
|
2021-07-12 18:48:33 +07:00
|
|
|
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);
|
2021-07-07 18:02:04 +02:00
|
|
|
|
2021-07-12 18:48:33 +07:00
|
|
|
return drv_len;
|
2021-04-05 16:32:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invoked when a control transfer occurred on an interface of this class
|
|
|
|
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
|
|
|
// return false to stall control endpoint (e.g unsupported request)
|
2021-04-07 17:05:04 -04:00
|
|
|
bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-04-22 17:45:33 -04:00
|
|
|
// nothing to do with DATA stage
|
|
|
|
if ( stage == CONTROL_STAGE_DATA ) return true;
|
2021-04-05 16:32:58 -04:00
|
|
|
|
|
|
|
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
|
|
|
|
|
2021-04-22 17:45:33 -04:00
|
|
|
if(stage == CONTROL_STAGE_SETUP)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-04-22 17:45:33 -04:00
|
|
|
// dfu-util will try to claim the interface with SET_INTERFACE request before sending DFU request
|
2021-07-07 11:55:36 +02:00
|
|
|
if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD && request->bRequest == TUSB_REQ_SET_INTERFACE)
|
2021-04-22 17:45:33 -04:00
|
|
|
{
|
2021-07-07 11:55:36 +02:00
|
|
|
// Switch Alt interface
|
2021-07-05 17:56:21 +02:00
|
|
|
_dfu_state_ctx.alt = request->wValue;
|
2021-07-07 11:55:36 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2021-04-22 17:45:33 -04:00
|
|
|
tud_control_status(rhport, request);
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-05 16:32:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle class request only from here
|
|
|
|
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
|
|
|
|
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
2021-07-07 12:07:10 +02:00
|
|
|
case DFU_REQUEST_DETACH:
|
|
|
|
{
|
|
|
|
tud_control_status(rhport, request);
|
|
|
|
if (tud_dfu_reboot_cb) tud_dfu_reboot_cb();
|
|
|
|
break;
|
|
|
|
}
|
2021-04-05 16:32:58 -04:00
|
|
|
case DFU_REQUEST_DNLOAD:
|
2021-04-22 17:45:33 -04:00
|
|
|
{
|
|
|
|
if ( (stage == CONTROL_STAGE_ACK)
|
2021-07-07 18:02:04 +02:00
|
|
|
&& ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
|
2021-04-22 17:45:33 -04:00
|
|
|
&& (_dfu_state_ctx.state == DFU_DNLOAD_SYNC))
|
|
|
|
{
|
2021-07-08 01:10:02 +02:00
|
|
|
_dfu_state_ctx.block = request->wValue;
|
|
|
|
_dfu_state_ctx.length = request->wLength;
|
2021-04-22 17:45:33 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // fallthrough
|
2021-04-05 16:32:58 -04:00
|
|
|
case DFU_REQUEST_UPLOAD:
|
|
|
|
case DFU_REQUEST_GETSTATUS:
|
|
|
|
case DFU_REQUEST_CLRSTATUS:
|
|
|
|
case DFU_REQUEST_GETSTATE:
|
|
|
|
case DFU_REQUEST_ABORT:
|
|
|
|
{
|
2021-04-22 17:45:33 -04:00
|
|
|
if(stage == CONTROL_STAGE_SETUP)
|
|
|
|
{
|
|
|
|
return dfu_state_machine(rhport, request);
|
|
|
|
}
|
2021-04-05 16:32:58 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
TU_LOG2(" DFU Nonstandard Request: %u\r\n", request->bRequest);
|
2021-04-22 15:59:49 -04:00
|
|
|
return false; // stall unsupported request
|
2021-04-05 16:32:58 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t dfu_req_upload(uint8_t rhport, tusb_control_request_t const * request, uint16_t block_num, uint16_t wLength)
|
|
|
|
{
|
2021-07-05 12:38:15 +07:00
|
|
|
TU_VERIFY( wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, 0);
|
2021-07-05 17:56:21 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-04-29 16:04:18 -04:00
|
|
|
tud_control_xfer(rhport, request, _dfu_state_ctx.transfer_buf, retval);
|
2021-04-05 16:32:58 -04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dfu_req_getstatus_reply(uint8_t rhport, tusb_control_request_t const * request)
|
|
|
|
{
|
|
|
|
dfu_status_req_payload_t resp;
|
|
|
|
|
2021-07-08 00:25:12 +02:00
|
|
|
uint16_t timeout = 0;
|
2021-04-05 16:32:58 -04:00
|
|
|
resp.bStatus = _dfu_state_ctx.status;
|
2021-07-08 00:25:12 +02:00
|
|
|
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;
|
2021-04-05 16:32:58 -04:00
|
|
|
resp.bState = _dfu_state_ctx.state;
|
2021-04-22 16:47:05 -04:00
|
|
|
resp.iString = 0;
|
2021-04-05 16:32:58 -04:00
|
|
|
|
|
|
|
tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_req_payload_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dfu_req_getstate_reply(uint8_t rhport, tusb_control_request_t const * request)
|
|
|
|
{
|
|
|
|
tud_control_xfer(rhport, request, &_dfu_state_ctx.state, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-07-05 12:38:15 +07:00
|
|
|
TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, );
|
2021-04-05 16:32:58 -04:00
|
|
|
// setup for data phase
|
2021-04-29 16:04:18 -04:00
|
|
|
tud_control_xfer(rhport, request, _dfu_state_ctx.transfer_buf, request->wLength);
|
2021-04-05 16:32:58 -04:00
|
|
|
}
|
|
|
|
|
2021-04-22 17:45:33 -04:00
|
|
|
static void dfu_req_dnload_reply(uint8_t rhport, tusb_control_request_t const * request)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-04-22 17:45:33 -04:00
|
|
|
(void) rhport;
|
2021-07-05 12:38:15 +07:00
|
|
|
TU_VERIFY( request->wLength <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, );
|
2021-07-08 01:10:02 +02:00
|
|
|
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);
|
2021-04-05 16:32:58 -04:00
|
|
|
_dfu_state_ctx.blk_transfer_in_proc = false;
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:45:33 -04:00
|
|
|
void tud_dfu_dnload_complete(void)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
|
|
|
if (_dfu_state_ctx.state == DFU_DNBUSY)
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_DNLOAD_SYNC;
|
|
|
|
} else if (_dfu_state_ctx.state == DFU_MANIFEST)
|
|
|
|
{
|
2021-07-07 18:02:04 +02:00
|
|
|
_dfu_state_ctx.state = ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
|
2021-04-05 16:32:58 -04:00
|
|
|
? DFU_MANIFEST_WAIT_RESET : DFU_MANIFEST_SYNC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:56:52 -04:00
|
|
|
static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * request)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
|
|
|
TU_LOG2(" DFU Request: %s\r\n", tu_lookup_find(&_dfu_request_table, request->bRequest));
|
2021-04-22 14:56:52 -04:00
|
|
|
TU_LOG2(" DFU State Machine: %s\r\n", tu_lookup_find(&_dfu_state_table, _dfu_state_ctx.state));
|
2021-04-05 16:32:58 -04:00
|
|
|
|
|
|
|
switch (_dfu_state_ctx.state)
|
|
|
|
{
|
|
|
|
case DFU_IDLE:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
case DFU_REQUEST_DNLOAD:
|
|
|
|
{
|
2021-07-07 18:02:04 +02:00
|
|
|
if( ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
|
2021-04-05 16:32:58 -04:00
|
|
|
&& (request->wLength > 0) )
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_DNLOAD_SYNC;
|
|
|
|
_dfu_state_ctx.blk_transfer_in_proc = true;
|
|
|
|
dfu_req_dnload_setup(rhport, request);
|
|
|
|
} else {
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_UPLOAD:
|
|
|
|
{
|
2021-07-07 18:02:04 +02:00
|
|
|
if( ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_UPLOAD_BITMASK) != 0) )
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_UPLOAD_IDLE;
|
|
|
|
dfu_req_upload(rhport, request, request->wValue, request->wLength);
|
|
|
|
} else {
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATUS:
|
|
|
|
{
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATE:
|
|
|
|
{
|
|
|
|
dfu_req_getstate_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_ABORT:
|
|
|
|
{
|
|
|
|
; // do nothing, but don't stall so continue on
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_DNLOAD_SYNC:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
case DFU_REQUEST_GETSTATUS:
|
|
|
|
{
|
|
|
|
if ( _dfu_state_ctx.blk_transfer_in_proc )
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_DNBUSY;
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
2021-07-08 00:25:12 +02:00
|
|
|
dfu_req_dnload_reply(rhport, request);
|
2021-04-05 16:32:58 -04:00
|
|
|
} else {
|
|
|
|
_dfu_state_ctx.state = DFU_DNLOAD_IDLE;
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATE:
|
|
|
|
{
|
|
|
|
dfu_req_getstate_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_DNBUSY:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_DNLOAD_IDLE:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
case DFU_REQUEST_DNLOAD:
|
|
|
|
{
|
2021-07-07 18:02:04 +02:00
|
|
|
if( ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
|
2021-04-05 16:32:58 -04:00
|
|
|
&& (request->wLength > 0) )
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_DNLOAD_SYNC;
|
|
|
|
_dfu_state_ctx.blk_transfer_in_proc = true;
|
|
|
|
dfu_req_dnload_setup(rhport, request);
|
|
|
|
} else {
|
2021-07-05 17:56:21 +02:00
|
|
|
if ( tud_dfu_device_data_done_check_cb(_dfu_state_ctx.alt) )
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_MANIFEST_SYNC;
|
|
|
|
tud_control_status(rhport, request);
|
|
|
|
} else {
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
return false; // stall
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATUS:
|
|
|
|
{
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATE:
|
|
|
|
{
|
|
|
|
dfu_req_getstate_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_ABORT:
|
|
|
|
{
|
2021-04-22 14:56:52 -04:00
|
|
|
if ( tud_dfu_abort_cb )
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-07-12 18:50:19 +07:00
|
|
|
tud_dfu_abort_cb(_dfu_state_ctx.alt);
|
2021-04-05 16:32:58 -04:00
|
|
|
}
|
|
|
|
_dfu_state_ctx.state = DFU_IDLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_MANIFEST_SYNC:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
case DFU_REQUEST_GETSTATUS:
|
|
|
|
{
|
2021-07-07 18:02:04 +02:00
|
|
|
if ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_MANIFEST;
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
2021-07-08 00:25:12 +02:00
|
|
|
} else
|
|
|
|
{
|
2021-07-05 17:56:21 +02:00
|
|
|
if ( tud_dfu_firmware_valid_check_cb(_dfu_state_ctx.alt) )
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_IDLE;
|
|
|
|
}
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATE:
|
|
|
|
{
|
|
|
|
dfu_req_getstate_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_MANIFEST:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_MANIFEST_WAIT_RESET:
|
|
|
|
{
|
|
|
|
// technically we should never even get here, but we will handle it just in case
|
|
|
|
TU_LOG2(" DFU was in DFU_MANIFEST_WAIT_RESET and got unexpected request: %u\r\n", request->bRequest);
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_UPLOAD_IDLE:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
case DFU_REQUEST_UPLOAD:
|
|
|
|
{
|
|
|
|
if (dfu_req_upload(rhport, request, request->wValue, request->wLength) != request->wLength)
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_IDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATUS:
|
|
|
|
{
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATE:
|
|
|
|
{
|
|
|
|
dfu_req_getstate_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_ABORT:
|
|
|
|
{
|
2021-04-22 14:56:52 -04:00
|
|
|
if (tud_dfu_abort_cb)
|
2021-04-05 16:32:58 -04:00
|
|
|
{
|
2021-07-12 18:50:19 +07:00
|
|
|
tud_dfu_abort_cb(_dfu_state_ctx.alt);
|
2021-04-05 16:32:58 -04:00
|
|
|
}
|
|
|
|
_dfu_state_ctx.state = DFU_IDLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_ERROR:
|
|
|
|
{
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
case DFU_REQUEST_GETSTATUS:
|
|
|
|
{
|
|
|
|
dfu_req_getstatus_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_CLRSTATUS:
|
|
|
|
{
|
|
|
|
_dfu_state_ctx.state = DFU_IDLE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DFU_REQUEST_GETSTATE:
|
|
|
|
{
|
|
|
|
dfu_req_getstate_reply(rhport, request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false; // stall on all other requests
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
_dfu_state_ctx.state = DFU_ERROR;
|
|
|
|
TU_LOG2(" DFU ERROR: Unexpected state\r\nStalling control pipe\r\n");
|
|
|
|
return false; // Unexpected state, stall and change to error
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-05 17:52:33 -04:00
|
|
|
#endif
|