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
@ -69,12 +69,13 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
|
|||||||
struct dfu_ctx *ctx = data;
|
struct dfu_ctx *ctx = data;
|
||||||
int fail = 1;
|
int fail = 1;
|
||||||
|
|
||||||
switch ((enum dfu_ctrl_req_code)req->bRequest)
|
switch ( (enum dfu_ctrl_req_code)req->bRequest )
|
||||||
{
|
{
|
||||||
case USB_CTRL_REQ_DFU_DNLOAD: {
|
case USB_CTRL_REQ_DFU_DNLOAD: {
|
||||||
void *buf;
|
void *buf;
|
||||||
|
|
||||||
switch (ctx->state) {
|
switch ( ctx->state )
|
||||||
|
{
|
||||||
case DFU_STATE_dfuIDLE:
|
case DFU_STATE_dfuIDLE:
|
||||||
ctx->off = 0;
|
ctx->off = 0;
|
||||||
break;
|
break;
|
||||||
@ -88,20 +89,25 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
|
|||||||
* XXX we are not allowed to STALL here, and we need to eat all transferred data.
|
* XXX we are not allowed to STALL here, and we need to eat all transferred data.
|
||||||
* better not allow setup_write to break the protocol.
|
* better not allow setup_write to break the protocol.
|
||||||
*/
|
*/
|
||||||
ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
|
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;
|
ctx->state = DFU_STATE_dfuERROR;
|
||||||
goto err_have_status;
|
goto err_have_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req->wLength > 0)
|
if ( req->wLength > 0 )
|
||||||
usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
|
{
|
||||||
|
usb_ep0_rx( buf, req->wLength, dfu_dnload_complete, ctx );
|
||||||
|
}
|
||||||
else
|
else
|
||||||
dfu_dnload_complete(NULL, 0, ctx);
|
{
|
||||||
|
dfu_dnload_complete( NULL, 0, ctx );
|
||||||
|
}
|
||||||
goto out_no_status;
|
goto out_no_status;
|
||||||
}
|
}
|
||||||
case USB_CTRL_REQ_DFU_UPLOAD: {
|
case USB_CTRL_REQ_DFU_UPLOAD: {
|
||||||
/*
|
#if defined(_mk20dx256vlh7_) // Kiibohd-dfu
|
||||||
void *buf;
|
void *buf;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
|
|
||||||
@ -115,15 +121,23 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute the requested offset
|
||||||
|
ctx->off = USB_DFU_TRANSFER_SIZE * req->wValue;
|
||||||
|
|
||||||
// Find which sector to read
|
// Find which sector to read
|
||||||
ctx->status = ctx->setup_read(ctx->off, &len, &buf);
|
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 );
|
printHex( ctx->off );
|
||||||
print(" len:");
|
print(" len:");
|
||||||
printHex( len );
|
printHex( len );
|
||||||
|
print(" reqlen: ");
|
||||||
|
printHex( req->wLength );
|
||||||
print(" addr:");
|
print(" addr:");
|
||||||
printHex( (uint32_t)buf );
|
printHex( (uint32_t)buf );
|
||||||
print( NL );
|
#endif
|
||||||
|
|
||||||
if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
|
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
|
// 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
|
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;
|
goto out;
|
||||||
*/
|
#else
|
||||||
return 0;
|
ctx->state = DFU_STATE_dfuERROR;
|
||||||
|
goto out;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
case USB_CTRL_REQ_DFU_GETSTATUS: {
|
case USB_CTRL_REQ_DFU_GETSTATUS: {
|
||||||
struct dfu_status_t st;
|
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 )
|
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;
|
ctx->state = DFU_STATE_dfuIDLE;
|
||||||
|
#if defined(_mk20dx256vlh7_) // Kiibohd-dfu
|
||||||
ctx->setup_read = setup_read;
|
ctx->setup_read = setup_read;
|
||||||
|
#endif
|
||||||
ctx->setup_write = setup_write;
|
ctx->setup_write = setup_write;
|
||||||
ctx->finish_write = finish_write;
|
ctx->finish_write = finish_write;
|
||||||
usb_attach_function(&dfu_function, &ctx->header);
|
usb_attach_function(&dfu_function, &ctx->header);
|
||||||
|
@ -68,7 +68,11 @@ static const struct usb_config_1 usb_config_1 = {
|
|||||||
},
|
},
|
||||||
.will_detach = 1,
|
.will_detach = 1,
|
||||||
.manifestation_tolerant = 0,
|
.manifestation_tolerant = 0,
|
||||||
|
#if defined(_mk20dx128vlf5_) // Kiibohd-dfu / McHCK
|
||||||
.can_upload = 0,
|
.can_upload = 0,
|
||||||
|
#elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
|
||||||
|
.can_upload = 1,
|
||||||
|
#endif
|
||||||
.can_download = 1,
|
.can_download = 1,
|
||||||
.wDetachTimeOut = 0,
|
.wDetachTimeOut = 0,
|
||||||
.wTransferSize = USB_DFU_TRANSFER_SIZE,
|
.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 )
|
static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
|
||||||
{
|
{
|
||||||
// Calculate starting address from offset
|
// Calculate starting address from offset
|
||||||
*buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
|
*buf = (void*)&_app_rom + off;
|
||||||
|
|
||||||
// Calculate length of transfer
|
// Calculate length of transfer
|
||||||
/*
|
*len = *buf + USB_DFU_TRANSFER_SIZE > (void*)(&_app_rom_end)
|
||||||
*len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
|
? (void*)(&_app_rom_end) - *buf + 1
|
||||||
? 0 : USB_DFU_TRANSFER_SIZE;
|
: USB_DFU_TRANSFER_SIZE;
|
||||||
*/
|
|
||||||
|
|
||||||
// Check for error
|
|
||||||
/*
|
|
||||||
if ( *buf > (void*)&_app_rom_end )
|
|
||||||
return (DFU_STATUS_errADDRESS);
|
|
||||||
*/
|
|
||||||
|
|
||||||
return (DFU_STATUS_OK);
|
return (DFU_STATUS_OK);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user