Browse Source

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)
blockKey
Jacob Alexander 7 years ago
parent
commit
26915c4d5b
3 changed files with 53 additions and 28 deletions
  1. 45
    17
      Bootloader/dfu.c
  2. 4
    0
      Bootloader/dfu.desc.c
  3. 4
    11
      Bootloader/main.c

+ 45
- 17
Bootloader/dfu.c View File

@@ -69,12 +69,13 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
struct dfu_ctx *ctx = data;
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: {
void *buf;

switch (ctx->state) {
switch ( ctx->state )
{
case DFU_STATE_dfuIDLE:
ctx->off = 0;
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.
* 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) {
ctx->status = ctx->setup_write( ctx->off, req->wLength, &buf );
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);
if ( req->wLength > 0 )
{
usb_ep0_rx( buf, req->wLength, dfu_dnload_complete, ctx );
}
else
dfu_dnload_complete(NULL, 0, ctx);
{
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:");
ctx->status = ctx->setup_read( ctx->off, &len, &buf );
#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);

+ 4
- 0
Bootloader/dfu.desc.c View File

@@ -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,

+ 4
- 11
Bootloader/main.c View File

@@ -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);
}