Adding DFU Upload support to mk20dx256vlh7
- mk20dx128vlf5 doesn't have enough bootloader space left - Downloads the entire firmware flash (not the bootloader) - Currently no intelligence built-in to determine full size of firmware (tricky, and sometimes not useful)
This commit is contained in:
parent
8efeb60aa0
commit
26915c4d5b
@ -74,7 +74,8 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
|
||||
case USB_CTRL_REQ_DFU_DNLOAD: {
|
||||
void *buf;
|
||||
|
||||
switch (ctx->state) {
|
||||
switch ( ctx->state )
|
||||
{
|
||||
case DFU_STATE_dfuIDLE:
|
||||
ctx->off = 0;
|
||||
break;
|
||||
@ -89,19 +90,24 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
|
||||
* better not allow setup_write to break the protocol.
|
||||
*/
|
||||
ctx->status = ctx->setup_write( ctx->off, req->wLength, &buf );
|
||||
if (ctx->status != DFU_STATUS_OK) {
|
||||
if ( ctx->status != DFU_STATUS_OK )
|
||||
{
|
||||
ctx->state = DFU_STATE_dfuERROR;
|
||||
goto err_have_status;
|
||||
}
|
||||
|
||||
if ( req->wLength > 0 )
|
||||
{
|
||||
usb_ep0_rx( buf, req->wLength, dfu_dnload_complete, ctx );
|
||||
}
|
||||
else
|
||||
{
|
||||
dfu_dnload_complete( NULL, 0, ctx );
|
||||
}
|
||||
goto out_no_status;
|
||||
}
|
||||
case USB_CTRL_REQ_DFU_UPLOAD: {
|
||||
/*
|
||||
#if defined(_mk20dx256vlh7_) // Kiibohd-dfu
|
||||
void *buf;
|
||||
size_t len = 0;
|
||||
|
||||
@ -115,15 +121,23 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Compute the requested offset
|
||||
ctx->off = USB_DFU_TRANSFER_SIZE * req->wValue;
|
||||
|
||||
// Find which sector to read
|
||||
ctx->status = ctx->setup_read( ctx->off, &len, &buf );
|
||||
print("UPLOAD off:");
|
||||
#ifdef FLASH_DEBUG
|
||||
print("UPLOAD req:");
|
||||
printHex( req->wValue );
|
||||
print(" off:");
|
||||
printHex( ctx->off );
|
||||
print(" len:");
|
||||
printHex( len );
|
||||
print(" reqlen: ");
|
||||
printHex( req->wLength );
|
||||
print(" addr:");
|
||||
printHex( (uint32_t)buf );
|
||||
print( NL );
|
||||
#endif
|
||||
|
||||
if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
|
||||
{
|
||||
@ -132,18 +146,30 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
|
||||
}
|
||||
|
||||
// Send bytes to Host
|
||||
if ( len > 0 )
|
||||
// Successfully transferred data to USB
|
||||
if ( usb_ep0_tx( buf, len, len, NULL, NULL ) != -1 )
|
||||
{
|
||||
usb_ep0_rx( buf, len, NULL, NULL );
|
||||
ctx->state = len < req->wLength
|
||||
? DFU_STATE_dfuIDLE
|
||||
: DFU_STATE_dfuUPLOAD_IDLE;
|
||||
fail = 0;
|
||||
}
|
||||
// Problem transferring via USB
|
||||
else
|
||||
{
|
||||
ctx->state = DFU_STATE_dfuIDLE;
|
||||
ctx->state = DFU_STATE_dfuERROR;
|
||||
}
|
||||
#ifdef FLASH_DEBUG
|
||||
print(" state:");
|
||||
printHex( ctx->state );
|
||||
print( NL );
|
||||
#endif
|
||||
|
||||
goto out_no_status;
|
||||
*/
|
||||
return 0;
|
||||
goto out;
|
||||
#else
|
||||
ctx->state = DFU_STATE_dfuERROR;
|
||||
goto out;
|
||||
#endif
|
||||
}
|
||||
case USB_CTRL_REQ_DFU_GETSTATUS: {
|
||||
struct dfu_status_t st;
|
||||
@ -206,7 +232,9 @@ out_no_status:
|
||||
void dfu_init( dfu_setup_read_t setup_read, dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx )
|
||||
{
|
||||
ctx->state = DFU_STATE_dfuIDLE;
|
||||
#if defined(_mk20dx256vlh7_) // Kiibohd-dfu
|
||||
ctx->setup_read = setup_read;
|
||||
#endif
|
||||
ctx->setup_write = setup_write;
|
||||
ctx->finish_write = finish_write;
|
||||
usb_attach_function(&dfu_function, &ctx->header);
|
||||
|
@ -68,7 +68,11 @@ static const struct usb_config_1 usb_config_1 = {
|
||||
},
|
||||
.will_detach = 1,
|
||||
.manifestation_tolerant = 0,
|
||||
#if defined(_mk20dx128vlf5_) // Kiibohd-dfu / McHCK
|
||||
.can_upload = 0,
|
||||
#elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
|
||||
.can_upload = 1,
|
||||
#endif
|
||||
.can_download = 1,
|
||||
.wDetachTimeOut = 0,
|
||||
.wTransferSize = USB_DFU_TRANSFER_SIZE,
|
||||
|
@ -88,19 +88,12 @@ int sector_print( void* buf, size_t sector, size_t chunks )
|
||||
static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
|
||||
{
|
||||
// Calculate starting address from offset
|
||||
*buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
|
||||
*buf = (void*)&_app_rom + off;
|
||||
|
||||
// Calculate length of transfer
|
||||
/*
|
||||
*len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
|
||||
? 0 : USB_DFU_TRANSFER_SIZE;
|
||||
*/
|
||||
|
||||
// Check for error
|
||||
/*
|
||||
if ( *buf > (void*)&_app_rom_end )
|
||||
return (DFU_STATUS_errADDRESS);
|
||||
*/
|
||||
*len = *buf + USB_DFU_TRANSFER_SIZE > (void*)(&_app_rom_end)
|
||||
? (void*)(&_app_rom_end) - *buf + 1
|
||||
: USB_DFU_TRANSFER_SIZE;
|
||||
|
||||
return (DFU_STATUS_OK);
|
||||
}
|
||||
|
Reference in New Issue
Block a user