Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/smasher816/controller int…
Browse files Browse the repository at this point in the history
…o smasher816-master
  • Loading branch information
haata committed Mar 16, 2015
2 parents ed71f7f + 68f357d commit 96e785b
Show file tree
Hide file tree
Showing 46 changed files with 3,306 additions and 3,306 deletions.
6 changes: 3 additions & 3 deletions Bootloader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#|
set( CHIP
"mk20dx128vlf5" # McHCK mk20dx128vlf5
# "mk20dx256vlh7" # Kiibohd-dfu mk20dx256vlh7
# "mk20dx256vlh7" # Kiibohd-dfu mk20dx256vlh7
)


Expand All @@ -34,8 +34,8 @@ set( CHIP
#| Stick with gcc unless you know what you're doing
#| Currently only arm is supported with clang
set( COMPILER
"gcc" # arm-none-eabi-gcc / avr-gcc - Default
# "clang" # arm-none-eabi
"gcc" # arm-none-eabi-gcc / avr-gcc - Default
# "clang" # arm-none-eabi
CACHE STRING "Compiler Type" )


Expand Down
228 changes: 114 additions & 114 deletions Bootloader/dfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,146 +26,146 @@

void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
{
ctx->status = err;
if (ctx->status == DFU_STATUS_OK) {
switch (ctx->state) {
case DFU_STATE_dfuDNBUSY:
ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
break;
default:
break;
}
} else {
ctx->state = DFU_STATE_dfuERROR;
}
ctx->status = err;
if (ctx->status == DFU_STATUS_OK) {
switch (ctx->state) {
case DFU_STATE_dfuDNBUSY:
ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
break;
default:
break;
}
} else {
ctx->state = DFU_STATE_dfuERROR;
}
}

static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
{
struct dfu_ctx *ctx = cbdata;
struct dfu_ctx *ctx = cbdata;

if (len > 0)
ctx->state = DFU_STATE_dfuDNBUSY;
else
ctx->state = DFU_STATE_dfuMANIFEST;
ctx->status = ctx->finish_write(buf, ctx->off, len);
ctx->off += len;
ctx->len = len;
if (len > 0)
ctx->state = DFU_STATE_dfuDNBUSY;
else
ctx->state = DFU_STATE_dfuMANIFEST;
ctx->status = ctx->finish_write(buf, ctx->off, len);
ctx->off += len;
ctx->len = len;

if (ctx->status != DFU_STATUS_async)
dfu_write_done(ctx->status, ctx);
if (ctx->status != DFU_STATUS_async)
dfu_write_done(ctx->status, ctx);

usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
}

static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
{
SOFTWARE_RESET();
SOFTWARE_RESET();
}

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) {
case USB_CTRL_REQ_DFU_DNLOAD: {
void *buf;

switch (ctx->state) {
case DFU_STATE_dfuIDLE:
ctx->off = 0;
break;
case DFU_STATE_dfuDNLOAD_IDLE:
break;
default:
goto err;
}

/**
* 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->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_GETSTATUS: {
struct dfu_status_t st;

st.bState = ctx->state;
st.bStatus = ctx->status;
st.bwPollTimeout = 1000; /* XXX */
/**
* If we're in DFU_STATE_dfuMANIFEST, we just finished
* the download, and we're just about to send our last
* status report. Once the report has been sent, go
* and reset the system to put the new firmware into
* effect.
*/
usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
if (ctx->state == DFU_STATE_dfuMANIFEST) {
usb_handle_control_status_cb(dfu_reset_system);
goto out_no_status;
}
break;
}
case USB_CTRL_REQ_DFU_CLRSTATUS:
ctx->state = DFU_STATE_dfuIDLE;
ctx->status = DFU_STATUS_OK;
break;
case USB_CTRL_REQ_DFU_GETSTATE: {
uint8_t st = ctx->state;
usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
break;
}
case USB_CTRL_REQ_DFU_ABORT:
switch (ctx->state) {
case DFU_STATE_dfuIDLE:
case DFU_STATE_dfuDNLOAD_IDLE:
/* case DFU_STATE_dfuUPLOAD_IDLE: */
ctx->state = DFU_STATE_dfuIDLE;
break;
default:
goto err;
}
break;
/* case USB_CTRL_REQ_DFU_UPLOAD: */
default:
return (0);
}

fail = 0;
goto out;
struct dfu_ctx *ctx = data;
int fail = 1;

switch ((enum dfu_ctrl_req_code)req->bRequest) {
case USB_CTRL_REQ_DFU_DNLOAD: {
void *buf;

switch (ctx->state) {
case DFU_STATE_dfuIDLE:
ctx->off = 0;
break;
case DFU_STATE_dfuDNLOAD_IDLE:
break;
default:
goto err;
}

/**
* 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->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_GETSTATUS: {
struct dfu_status_t st;

st.bState = ctx->state;
st.bStatus = ctx->status;
st.bwPollTimeout = 1000; /* XXX */
/**
* If we're in DFU_STATE_dfuMANIFEST, we just finished
* the download, and we're just about to send our last
* status report. Once the report has been sent, go
* and reset the system to put the new firmware into
* effect.
*/
usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
if (ctx->state == DFU_STATE_dfuMANIFEST) {
usb_handle_control_status_cb(dfu_reset_system);
goto out_no_status;
}
break;
}
case USB_CTRL_REQ_DFU_CLRSTATUS:
ctx->state = DFU_STATE_dfuIDLE;
ctx->status = DFU_STATUS_OK;
break;
case USB_CTRL_REQ_DFU_GETSTATE: {
uint8_t st = ctx->state;
usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
break;
}
case USB_CTRL_REQ_DFU_ABORT:
switch (ctx->state) {
case DFU_STATE_dfuIDLE:
case DFU_STATE_dfuDNLOAD_IDLE:
/* case DFU_STATE_dfuUPLOAD_IDLE: */
ctx->state = DFU_STATE_dfuIDLE;
break;
default:
goto err;
}
break;
/* case USB_CTRL_REQ_DFU_UPLOAD: */
default:
return (0);
}

fail = 0;
goto out;

err:
ctx->status = DFU_STATUS_errSTALLEDPKT;
ctx->status = DFU_STATUS_errSTALLEDPKT;
err_have_status:
ctx->state = DFU_STATE_dfuERROR;
ctx->state = DFU_STATE_dfuERROR;
out:
usb_handle_control_status(fail);
usb_handle_control_status(fail);
out_no_status:
return (1);
return (1);
}

void dfu_init( dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx )
{
ctx->state = DFU_STATE_dfuIDLE;
ctx->setup_write = setup_write;
ctx->finish_write = finish_write;
usb_attach_function(&dfu_function, &ctx->header);
ctx->state = DFU_STATE_dfuIDLE;
ctx->setup_write = setup_write;
ctx->finish_write = finish_write;
usb_attach_function(&dfu_function, &ctx->header);
}

const struct usbd_function dfu_function = {
.control = dfu_handle_control,
.interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
.control = dfu_handle_control,
.interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
};

Loading

0 comments on commit 96e785b

Please sign in to comment.