# HG changeset patch # User Jacob Alexander # Date 1434159115 25200 # Node ID f7b14e25ca5b2d36dcacf1a9f25e65b9139fbe91 # Parent b2c8581307bc44482c2f82113929d7b4a6ae1df1# Parent 05a09eda53fb020e8dbbc43efedf3b23d08f8768 Merge pull request #27 from smasher816/wakeup-devel Inital Remote Wakeup Support diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/CMakeLists.txt --- a/Bootloader/CMakeLists.txt Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/CMakeLists.txt Fri Jun 12 18:31:55 2015 -0700 @@ -21,7 +21,7 @@ #| set( CHIP "mk20dx128vlf5" # McHCK mk20dx128vlf5 -# "mk20dx256vlh7" # Kiibohd-dfu mk20dx256vlh7 +# "mk20dx256vlh7" # Kiibohd-dfu mk20dx256vlh7 ) @@ -34,8 +34,8 @@ #| 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" ) diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/dfu.c --- a/Bootloader/dfu.c Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/dfu.c Fri Jun 12 18:31:55 2015 -0700 @@ -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; + 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 ((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; - } + 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; - } + /** + * 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; + 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); - } + 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; + 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, }; diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/dfu.h --- a/Bootloader/dfu.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/dfu.h Fri Jun 12 18:31:55 2015 -0700 @@ -30,104 +30,104 @@ #ifndef USB_DFU_TRANSFER_SIZE -#define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE +#define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE #endif #define USB_FUNCTION_DESC_DFU_DECL \ - struct dfu_function_desc + struct dfu_function_desc -#define USB_FUNCTION_DFU_IFACE_COUNT 1 -#define USB_FUNCTION_DFU_RX_EP_COUNT 0 -#define USB_FUNCTION_DFU_TX_EP_COUNT 0 +#define USB_FUNCTION_DFU_IFACE_COUNT 1 +#define USB_FUNCTION_DFU_RX_EP_COUNT 0 +#define USB_FUNCTION_DFU_TX_EP_COUNT 0 // ----- Macros ----- #define USB_FUNCTION_DESC_DFU(state...) \ - { \ - .iface = { \ - .bLength = sizeof(struct usb_desc_iface_t), \ - .bDescriptorType = USB_DESC_IFACE, \ - .bInterfaceNumber = USB_FUNCTION_IFACE(0, state), \ - .bAlternateSetting = 0, \ - .bNumEndpoints = 0, \ - .bInterfaceClass = USB_DEV_CLASS_APP, \ - .bInterfaceSubClass = USB_DEV_SUBCLASS_APP_DFU, \ - .bInterfaceProtocol = USB_DEV_PROTO_DFU_DFU, \ - .iInterface = 0, \ - }, \ - .dfu = { \ - .bLength = sizeof(struct dfu_desc_functional), \ - .bDescriptorType = { \ - .id = 0x1, \ - .type_type = USB_DESC_TYPE_CLASS \ - }, \ - .will_detach = 1, \ - .manifestation_tolerant = 0, \ - .can_upload = 0, \ - .can_download = 1, \ - .wDetachTimeOut = 0, \ - .wTransferSize = USB_DFU_TRANSFER_SIZE, \ - .bcdDFUVersion = { .maj = 1, .min = 1 } \ - } \ - } + { \ + .iface = { \ + .bLength = sizeof(struct usb_desc_iface_t), \ + .bDescriptorType = USB_DESC_IFACE, \ + .bInterfaceNumber = USB_FUNCTION_IFACE(0, state), \ + .bAlternateSetting = 0, \ + .bNumEndpoints = 0, \ + .bInterfaceClass = USB_DEV_CLASS_APP, \ + .bInterfaceSubClass = USB_DEV_SUBCLASS_APP_DFU, \ + .bInterfaceProtocol = USB_DEV_PROTO_DFU_DFU, \ + .iInterface = 0, \ + }, \ + .dfu = { \ + .bLength = sizeof(struct dfu_desc_functional), \ + .bDescriptorType = { \ + .id = 0x1, \ + .type_type = USB_DESC_TYPE_CLASS \ + }, \ + .will_detach = 1, \ + .manifestation_tolerant = 0, \ + .can_upload = 0, \ + .can_download = 1, \ + .wDetachTimeOut = 0, \ + .wTransferSize = USB_DFU_TRANSFER_SIZE, \ + .bcdDFUVersion = { .maj = 1, .min = 1 } \ + } \ + } // ----- Enumerations ----- enum dfu_dev_subclass { - USB_DEV_SUBCLASS_APP_DFU = 0x01 + USB_DEV_SUBCLASS_APP_DFU = 0x01 }; enum dfu_dev_proto { - USB_DEV_PROTO_DFU_APP = 0x01, - USB_DEV_PROTO_DFU_DFU = 0x02 + USB_DEV_PROTO_DFU_APP = 0x01, + USB_DEV_PROTO_DFU_DFU = 0x02 }; enum dfu_ctrl_req_code { - USB_CTRL_REQ_DFU_DETACH = 0, - USB_CTRL_REQ_DFU_DNLOAD = 1, - USB_CTRL_REQ_DFU_UPLOAD = 2, - USB_CTRL_REQ_DFU_GETSTATUS = 3, - USB_CTRL_REQ_DFU_CLRSTATUS = 4, - USB_CTRL_REQ_DFU_GETSTATE = 5, - USB_CTRL_REQ_DFU_ABORT = 6 + USB_CTRL_REQ_DFU_DETACH = 0, + USB_CTRL_REQ_DFU_DNLOAD = 1, + USB_CTRL_REQ_DFU_UPLOAD = 2, + USB_CTRL_REQ_DFU_GETSTATUS = 3, + USB_CTRL_REQ_DFU_CLRSTATUS = 4, + USB_CTRL_REQ_DFU_GETSTATE = 5, + USB_CTRL_REQ_DFU_ABORT = 6 }; enum dfu_status { - DFU_STATUS_async = 0xff, - DFU_STATUS_OK = 0x00, - DFU_STATUS_errTARGET = 0x01, - DFU_STATUS_errFILE = 0x02, - DFU_STATUS_errWRITE = 0x03, - DFU_STATUS_errERASE = 0x04, - DFU_STATUS_errCHECK_ERASED = 0x05, - DFU_STATUS_errPROG = 0x06, - DFU_STATUS_errVERIFY = 0x07, - DFU_STATUS_errADDRESS = 0x08, - DFU_STATUS_errNOTDONE = 0x09, - DFU_STATUS_errFIRMWARE = 0x0a, - DFU_STATUS_errVENDOR = 0x0b, - DFU_STATUS_errUSBR = 0x0c, - DFU_STATUS_errPOR = 0x0d, - DFU_STATUS_errUNKNOWN = 0x0e, - DFU_STATUS_errSTALLEDPKT = 0x0f + DFU_STATUS_async = 0xff, + DFU_STATUS_OK = 0x00, + DFU_STATUS_errTARGET = 0x01, + DFU_STATUS_errFILE = 0x02, + DFU_STATUS_errWRITE = 0x03, + DFU_STATUS_errERASE = 0x04, + DFU_STATUS_errCHECK_ERASED = 0x05, + DFU_STATUS_errPROG = 0x06, + DFU_STATUS_errVERIFY = 0x07, + DFU_STATUS_errADDRESS = 0x08, + DFU_STATUS_errNOTDONE = 0x09, + DFU_STATUS_errFIRMWARE = 0x0a, + DFU_STATUS_errVENDOR = 0x0b, + DFU_STATUS_errUSBR = 0x0c, + DFU_STATUS_errPOR = 0x0d, + DFU_STATUS_errUNKNOWN = 0x0e, + DFU_STATUS_errSTALLEDPKT = 0x0f }; enum dfu_state { - DFU_STATE_appIDLE = 0, - DFU_STATE_appDETACH = 1, - DFU_STATE_dfuIDLE = 2, - DFU_STATE_dfuDNLOAD_SYNC = 3, - DFU_STATE_dfuDNBUSY = 4, - DFU_STATE_dfuDNLOAD_IDLE = 5, - DFU_STATE_dfuMANIFEST_SYNC = 6, - DFU_STATE_dfuMANIFEST = 7, - DFU_STATE_dfuMANIFEST_WAIT_RESET = 8, - DFU_STATE_dfuUPLOAD_IDLE = 9, - DFU_STATE_dfuERROR = 10 + DFU_STATE_appIDLE = 0, + DFU_STATE_appDETACH = 1, + DFU_STATE_dfuIDLE = 2, + DFU_STATE_dfuDNLOAD_SYNC = 3, + DFU_STATE_dfuDNBUSY = 4, + DFU_STATE_dfuDNLOAD_IDLE = 5, + DFU_STATE_dfuMANIFEST_SYNC = 6, + DFU_STATE_dfuMANIFEST = 7, + DFU_STATE_dfuMANIFEST_WAIT_RESET = 8, + DFU_STATE_dfuUPLOAD_IDLE = 9, + DFU_STATE_dfuERROR = 10 }; @@ -135,10 +135,10 @@ // ----- Structs ----- struct dfu_status_t { - enum dfu_status bStatus : 8; - uint32_t bwPollTimeout : 24; - enum dfu_state bState : 8; - uint8_t iString; + enum dfu_status bStatus : 8; + uint32_t bwPollTimeout : 24; + enum dfu_state bState : 8; + uint8_t iString; } __packed; CTASSERT_SIZE_BYTE(struct dfu_status_t, 6); @@ -148,38 +148,38 @@ typedef void (*dfu_detach_t)(void); struct dfu_ctx { - struct usbd_function_ctx_header header; - enum dfu_state state; - enum dfu_status status; - dfu_setup_write_t setup_write; - dfu_finish_write_t finish_write; - size_t off; - size_t len; + struct usbd_function_ctx_header header; + enum dfu_state state; + enum dfu_status status; + dfu_setup_write_t setup_write; + dfu_finish_write_t finish_write; + size_t off; + size_t len; }; struct dfu_desc_functional { - uint8_t bLength; - struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */ - union { - struct { - uint8_t can_download : 1; - uint8_t can_upload : 1; - uint8_t manifestation_tolerant : 1; - uint8_t will_detach : 1; - uint8_t _rsvd0 : 4; - }; - uint8_t bmAttributes; - }; - uint16_t wDetachTimeOut; - uint16_t wTransferSize; - struct usb_bcd_t bcdDFUVersion; + uint8_t bLength; + struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */ + union { + struct { + uint8_t can_download : 1; + uint8_t can_upload : 1; + uint8_t manifestation_tolerant : 1; + uint8_t will_detach : 1; + uint8_t _rsvd0 : 4; + }; + uint8_t bmAttributes; + }; + uint16_t wDetachTimeOut; + uint16_t wTransferSize; + struct usb_bcd_t bcdDFUVersion; } __packed; CTASSERT_SIZE_BYTE(struct dfu_desc_functional, 9); struct dfu_function_desc { - struct usb_desc_iface_t iface; - struct dfu_desc_functional dfu; + struct usb_desc_iface_t iface; + struct dfu_desc_functional dfu; }; diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/flash.c --- a/Bootloader/flash.c Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/flash.c Fri Jun 12 18:31:55 2015 -0700 @@ -33,60 +33,60 @@ __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call)) int ftfl_submit_cmd(void) { - FTFL.fstat.raw = ((struct FTFL_FSTAT_t){ - .ccif = 1, - .rdcolerr = 1, - .accerr = 1, - .fpviol = 1 - }).raw; - struct FTFL_FSTAT_t stat; - while (!(stat = FTFL.fstat).ccif) - /* NOTHING */; /* XXX maybe WFI? */ - return (!!stat.mgstat0); + FTFL.fstat.raw = ((struct FTFL_FSTAT_t){ + .ccif = 1, + .rdcolerr = 1, + .accerr = 1, + .fpviol = 1 + }).raw; + struct FTFL_FSTAT_t stat; + while (!(stat = FTFL.fstat).ccif) + /* NOTHING */; /* XXX maybe WFI? */ + return (!!stat.mgstat0); } int flash_prepare_flashing(void) { - /* switch to FlexRAM */ - if (!FTFL.fcnfg.ramrdy) { - FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM; - FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM; - return (ftfl_submit_cmd()); - } - return (0); + /* switch to FlexRAM */ + if (!FTFL.fcnfg.ramrdy) { + FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM; + FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM; + return (ftfl_submit_cmd()); + } + return (0); } int flash_erase_sector(uintptr_t addr) { - if (addr < (uintptr_t)&_app_rom && - flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420) - return (-1); - FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR; - FTFL.fccob.erase.addr = addr; - return (ftfl_submit_cmd()); + if (addr < (uintptr_t)&_app_rom && + flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420) + return (-1); + FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR; + FTFL.fccob.erase.addr = addr; + return (ftfl_submit_cmd()); } int flash_program_section(uintptr_t addr, size_t num_words) { - FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION; - FTFL.fccob.program_section.addr = addr; - FTFL.fccob.program_section.num_words = num_words; - return (ftfl_submit_cmd()); + FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION; + FTFL.fccob.program_section.addr = addr; + FTFL.fccob.program_section.num_words = num_words; + return (ftfl_submit_cmd()); } int flash_program_sector(uintptr_t addr, size_t len) { - return (len != FLASH_SECTOR_SIZE || - (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || - flash_erase_sector(addr) || - flash_program_section(addr, FLASH_SECTOR_SIZE/4)); + return (len != FLASH_SECTOR_SIZE || + (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || + flash_erase_sector(addr) || + flash_program_section(addr, FLASH_SECTOR_SIZE/4)); } void *flash_get_staging_area(uintptr_t addr, size_t len) { - if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 || - len != FLASH_SECTOR_SIZE) - return (NULL); - return (FlexRAM); + if ((addr & (FLASH_SECTOR_SIZE - 1)) != 0 || + len != FLASH_SECTOR_SIZE) + return (NULL); + return (FlexRAM); } diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/ftfl.h --- a/Bootloader/ftfl.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/ftfl.h Fri Jun 12 18:31:55 2015 -0700 @@ -27,60 +27,60 @@ // ----- Structs ----- struct FTFL_FSTAT_t { - UNION_STRUCT_START(8); - uint8_t mgstat0 : 1; - uint8_t _rsvd0 : 3; - uint8_t fpviol : 1; - uint8_t accerr : 1; - uint8_t rdcolerr : 1; - uint8_t ccif : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t mgstat0 : 1; + uint8_t _rsvd0 : 3; + uint8_t fpviol : 1; + uint8_t accerr : 1; + uint8_t rdcolerr : 1; + uint8_t ccif : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct FTFL_FSTAT_t, 8); struct FTFL_FCNFG_t { - UNION_STRUCT_START(8); - uint8_t eeerdy : 1; - uint8_t ramrdy : 1; - uint8_t pflsh : 1; - uint8_t _rsvd0 : 1; - uint8_t erssusp : 1; - uint8_t ersareq : 1; - uint8_t rdcollie : 1; - uint8_t ccie : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t eeerdy : 1; + uint8_t ramrdy : 1; + uint8_t pflsh : 1; + uint8_t _rsvd0 : 1; + uint8_t erssusp : 1; + uint8_t ersareq : 1; + uint8_t rdcollie : 1; + uint8_t ccie : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct FTFL_FCNFG_t, 8); struct FTFL_FSEC_t { - UNION_STRUCT_START(8); - enum { - FTFL_FSEC_SEC_UNSECURE = 2, - FTFL_FSEC_SEC_SECURE = 3 - } sec : 2; - enum { - FTFL_FSEC_FSLACC_DENY = 1, - FTFL_FSEC_FSLACC_GRANT = 3 - } fslacc : 2; - enum { - FTFL_FSEC_MEEN_DISABLE = 2, - FTFL_FSEC_MEEN_ENABLE = 3 - } meen : 2; - enum { - FTFL_FSEC_KEYEN_DISABLE = 1, - FTFL_FSEC_KEYEN_ENABLE = 2 - } keyen : 2; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + enum { + FTFL_FSEC_SEC_UNSECURE = 2, + FTFL_FSEC_SEC_SECURE = 3 + } sec : 2; + enum { + FTFL_FSEC_FSLACC_DENY = 1, + FTFL_FSEC_FSLACC_GRANT = 3 + } fslacc : 2; + enum { + FTFL_FSEC_MEEN_DISABLE = 2, + FTFL_FSEC_MEEN_ENABLE = 3 + } meen : 2; + enum { + FTFL_FSEC_KEYEN_DISABLE = 1, + FTFL_FSEC_KEYEN_ENABLE = 2 + } keyen : 2; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct FTFL_FSEC_t, 8); struct FTFL_FOPT_t { - UNION_STRUCT_START(8); - uint8_t lpboot : 1; - uint8_t ezport_dis : 1; - uint8_t nmi_dis : 1; - uint8_t _rsvd0 : 5; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t lpboot : 1; + uint8_t ezport_dis : 1; + uint8_t nmi_dis : 1; + uint8_t _rsvd0 : 5; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct FTFL_FOPT_t, 8); @@ -90,151 +90,151 @@ * some that is little endian. */ union FTFL_FCCOB_t { - struct ftfl_generic { - uint32_t addr : 24; - enum FTFL_FCMD { - FTFL_FCMD_READ_1s_BLOCK = 0x00, - FTFL_FCMD_READ_1s_SECTION = 0x01, - FTFL_FCMD_PROGRAM_CHECK = 0x02, - FTFL_FCMD_READ_RESOURCE = 0x03, - FTFL_FCMD_PROGRAM_LONGWORD = 0x06, - FTFL_FCMD_ERASE_BLOCK = 0x08, - FTFL_FCMD_ERASE_SECTOR = 0x09, - FTFL_FCMD_PROGRAM_SECTION = 0x0b, - FTFL_FCMD_READ_1s_ALL_BLOCKS = 0x40, - FTFL_FCMD_READ_ONCE = 0x41, - FTFL_FCMD_PROGRAM_ONCE = 0x43, - FTFL_FCMD_ERASE_ALL_BLOCKS = 0x44, - FTFL_FCMD_VERIFY_KEY = 0x45, - FTFL_FCMD_PROGRAM_PARTITION = 0x80, - FTFL_FCMD_SET_FLEXRAM = 0x81 - } fcmd : 8; - uint8_t data_be[8]; - } generic; - struct { - uint32_t addr : 24; - enum FTFL_FCMD fcmd : 8; - uint8_t _rsvd0[3]; - enum FTFL_MARGIN_CHOICE { - FTFL_MARGIN_NORMAL = 0x00, - FTFL_MARGIN_USER = 0x01, - FTFL_MARGIN_FACTORY = 0x02 - } margin : 8; - } read_1s_block; - struct ftfl_data_num_words { - uint32_t addr : 24; - enum FTFL_FCMD fcmd : 8; - uint8_t _rsvd0; - enum FTFL_MARGIN_CHOICE margin : 8; - uint16_t num_words; - } read_1s_section; - struct { - uint32_t addr : 24; - enum FTFL_FCMD fcmd : 8; - uint8_t _rsvd0[3]; - enum FTFL_MARGIN_CHOICE margin : 8; - uint8_t data_be[4]; - } program_check; - struct { - uint32_t addr : 24; - enum FTFL_FCMD fcmd : 8; - uint32_t data; - uint8_t _rsvd0[3]; - enum FTFL_RESOURCE_SELECT { - FTFL_RESOURCE_IFR = 0x00, - FTFL_RESOURCE_VERSION = 0x01 - } resource_select : 8; - } read_resource; - struct { - uint32_t addr : 24; - enum FTFL_FCMD fcmd : 8; - uint8_t data_be[4]; - } program_longword; - struct { - uint32_t addr : 24; - enum FTFL_FCMD fcmd : 8; - } erase; - struct ftfl_data_num_words program_section; - struct { - uint8_t _rsvd0[2]; - enum FTFL_MARGIN_CHOICE margin : 8; - enum FTFL_FCMD fcmd : 8; - } read_1s_all_blocks; - struct ftfl_cmd_once { - uint8_t _rsvd0[2]; - uint8_t idx; - enum FTFL_FCMD fcmd : 8; - uint8_t data_be[4]; - } read_once; - struct ftfl_cmd_once program_once; - struct { - uint8_t _rsvd0[3]; - enum FTFL_FCMD fcmd : 8; - } erase_all; - struct { - uint8_t _rsvd0[3]; - enum FTFL_FCMD fcmd : 8; - uint8_t key_be[8]; - } verify_key; - struct { - uint8_t _rsvd0[3]; - enum FTFL_FCMD fcmd : 8; - uint8_t _rsvd1[2]; + struct ftfl_generic { + uint32_t addr : 24; + enum FTFL_FCMD { + FTFL_FCMD_READ_1s_BLOCK = 0x00, + FTFL_FCMD_READ_1s_SECTION = 0x01, + FTFL_FCMD_PROGRAM_CHECK = 0x02, + FTFL_FCMD_READ_RESOURCE = 0x03, + FTFL_FCMD_PROGRAM_LONGWORD = 0x06, + FTFL_FCMD_ERASE_BLOCK = 0x08, + FTFL_FCMD_ERASE_SECTOR = 0x09, + FTFL_FCMD_PROGRAM_SECTION = 0x0b, + FTFL_FCMD_READ_1s_ALL_BLOCKS = 0x40, + FTFL_FCMD_READ_ONCE = 0x41, + FTFL_FCMD_PROGRAM_ONCE = 0x43, + FTFL_FCMD_ERASE_ALL_BLOCKS = 0x44, + FTFL_FCMD_VERIFY_KEY = 0x45, + FTFL_FCMD_PROGRAM_PARTITION = 0x80, + FTFL_FCMD_SET_FLEXRAM = 0x81 + } fcmd : 8; + uint8_t data_be[8]; + } generic; + struct { + uint32_t addr : 24; + enum FTFL_FCMD fcmd : 8; + uint8_t _rsvd0[3]; + enum FTFL_MARGIN_CHOICE { + FTFL_MARGIN_NORMAL = 0x00, + FTFL_MARGIN_USER = 0x01, + FTFL_MARGIN_FACTORY = 0x02 + } margin : 8; + } read_1s_block; + struct ftfl_data_num_words { + uint32_t addr : 24; + enum FTFL_FCMD fcmd : 8; + uint8_t _rsvd0; + enum FTFL_MARGIN_CHOICE margin : 8; + uint16_t num_words; + } read_1s_section; + struct { + uint32_t addr : 24; + enum FTFL_FCMD fcmd : 8; + uint8_t _rsvd0[3]; + enum FTFL_MARGIN_CHOICE margin : 8; + uint8_t data_be[4]; + } program_check; + struct { + uint32_t addr : 24; + enum FTFL_FCMD fcmd : 8; + uint32_t data; + uint8_t _rsvd0[3]; + enum FTFL_RESOURCE_SELECT { + FTFL_RESOURCE_IFR = 0x00, + FTFL_RESOURCE_VERSION = 0x01 + } resource_select : 8; + } read_resource; + struct { + uint32_t addr : 24; + enum FTFL_FCMD fcmd : 8; + uint8_t data_be[4]; + } program_longword; + struct { + uint32_t addr : 24; + enum FTFL_FCMD fcmd : 8; + } erase; + struct ftfl_data_num_words program_section; + struct { + uint8_t _rsvd0[2]; + enum FTFL_MARGIN_CHOICE margin : 8; + enum FTFL_FCMD fcmd : 8; + } read_1s_all_blocks; + struct ftfl_cmd_once { + uint8_t _rsvd0[2]; + uint8_t idx; + enum FTFL_FCMD fcmd : 8; + uint8_t data_be[4]; + } read_once; + struct ftfl_cmd_once program_once; + struct { + uint8_t _rsvd0[3]; + enum FTFL_FCMD fcmd : 8; + } erase_all; + struct { + uint8_t _rsvd0[3]; + enum FTFL_FCMD fcmd : 8; + uint8_t key_be[8]; + } verify_key; + struct { + uint8_t _rsvd0[3]; + enum FTFL_FCMD fcmd : 8; + uint8_t _rsvd1[2]; - /* the following enum is analogous to enum - * SIM_FLEXNVM_PARTITION in sim.h, but this one is padded - * with four 1-bits to make an 8-bit value. - */ + /* the following enum is analogous to enum + * SIM_FLEXNVM_PARTITION in sim.h, but this one is padded + * with four 1-bits to make an 8-bit value. + */ - enum FTFL_FLEXNVM_PARTITION { - FTFL_FLEXNVM_DATA_32_EEPROM_0 = 0xF0, - FTFL_FLEXNVM_DATA_24_EEPROM_8 = 0xF1, - FTFL_FLEXNVM_DATA_16_EEPROM_16 = 0xF2, - FTFL_FLEXNVM_DATA_8_EEPROM_24 = 0xF9, - FTFL_FLEXNVM_DATA_0_EEPROM_32 = 0xF3 - } flexnvm_partition : 8; - enum FTFL_EEPROM_SIZE { - FTFL_EEPROM_SIZE_0 = 0x3f, - FTFL_EEPROM_SIZE_32 = 0x39, - FTFL_EEPROM_SIZE_64 = 0x38, - FTFL_EEPROM_SIZE_128 = 0x37, - FTFL_EEPROM_SIZE_256 = 0x36, - FTFL_EEPROM_SIZE_512 = 0x35, - FTFL_EEPROM_SIZE_1024 = 0x34, - FTFL_EEPROM_SIZE_2048 = 0x33 - } eeprom_size : 8; - } program_partition; - struct { - uint8_t _rsvd0[2]; - enum FTFL_FLEXRAM_FUNCTION { - FTFL_FLEXRAM_EEPROM = 0x00, - FTFL_FLEXRAM_RAM = 0xff - } flexram_function : 8; - enum FTFL_FCMD fcmd : 8; - } set_flexram; + enum FTFL_FLEXNVM_PARTITION { + FTFL_FLEXNVM_DATA_32_EEPROM_0 = 0xF0, + FTFL_FLEXNVM_DATA_24_EEPROM_8 = 0xF1, + FTFL_FLEXNVM_DATA_16_EEPROM_16 = 0xF2, + FTFL_FLEXNVM_DATA_8_EEPROM_24 = 0xF9, + FTFL_FLEXNVM_DATA_0_EEPROM_32 = 0xF3 + } flexnvm_partition : 8; + enum FTFL_EEPROM_SIZE { + FTFL_EEPROM_SIZE_0 = 0x3f, + FTFL_EEPROM_SIZE_32 = 0x39, + FTFL_EEPROM_SIZE_64 = 0x38, + FTFL_EEPROM_SIZE_128 = 0x37, + FTFL_EEPROM_SIZE_256 = 0x36, + FTFL_EEPROM_SIZE_512 = 0x35, + FTFL_EEPROM_SIZE_1024 = 0x34, + FTFL_EEPROM_SIZE_2048 = 0x33 + } eeprom_size : 8; + } program_partition; + struct { + uint8_t _rsvd0[2]; + enum FTFL_FLEXRAM_FUNCTION { + FTFL_FLEXRAM_EEPROM = 0x00, + FTFL_FLEXRAM_RAM = 0xff + } flexram_function : 8; + enum FTFL_FCMD fcmd : 8; + } set_flexram; }; CTASSERT_SIZE_BYTE(union FTFL_FCCOB_t, 12); struct FTFL_t { - struct FTFL_FSTAT_t fstat; - struct FTFL_FCNFG_t fcnfg; - struct FTFL_FSEC_t fsec; - struct FTFL_FOPT_t fopt; - union FTFL_FCCOB_t fccob; - uint8_t fprot_be[4]; - uint8_t feprot; - uint8_t fdprot; + struct FTFL_FSTAT_t fstat; + struct FTFL_FCNFG_t fcnfg; + struct FTFL_FSEC_t fsec; + struct FTFL_FOPT_t fopt; + union FTFL_FCCOB_t fccob; + uint8_t fprot_be[4]; + uint8_t feprot; + uint8_t fdprot; }; CTASSERT_SIZE_BYTE(struct FTFL_t, 0x18); /* Flash Configuration Field, see Sub-Family Reference Manual, section 28.3.1 */ struct FTFL_CONFIG_t { - uint8_t key[8]; - uint8_t fprot[4]; - struct FTFL_FSEC_t fsec; - struct FTFL_FOPT_t fopt; - uint8_t feprot; - uint8_t fdprot; + uint8_t key[8]; + uint8_t fprot[4]; + struct FTFL_FSEC_t fsec; + struct FTFL_FOPT_t fopt; + uint8_t feprot; + uint8_t fdprot; }; CTASSERT_SIZE_BYTE(struct FTFL_CONFIG_t, 16); diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/kinetis.c --- a/Bootloader/kinetis.c Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/kinetis.c Fri Jun 12 18:31:55 2015 -0700 @@ -55,272 +55,272 @@ static struct USB_BD_t * usb_get_bd(struct usbd_ep_pipe_state_t *s) { - return (&bdt[(s->ep_num << 2) | (s->ep_dir << 1) | s->pingpong]); + return (&bdt[(s->ep_num << 2) | (s->ep_dir << 1) | s->pingpong]); } static struct USB_BD_t * usb_get_bd_stat(struct USB_STAT_t *stat) { - return (((void *)(uintptr_t)bdt + (stat->raw << 1))); + return (((void *)(uintptr_t)bdt + (stat->raw << 1))); } void *usb_get_xfer_data(struct usb_xfer_info *i) { - return (usb_get_bd_stat(i)->addr); + return (usb_get_bd_stat(i)->addr); } enum usb_tok_pid usb_get_xfer_pid(struct usb_xfer_info *i) { - return (usb_get_bd_stat(i)->tok_pid); + return (usb_get_bd_stat(i)->tok_pid); } int usb_get_xfer_ep(struct usb_xfer_info *i) { - return (i->ep); + return (i->ep); } enum usb_ep_dir usb_get_xfer_dir(struct usb_xfer_info *i) { - return (i->dir); + return (i->dir); } void usb_enable_xfers(void) { - USB0.ctl.raw = ((struct USB_CTL_t){ - .txd_suspend = 0, - .usben = 1 - }).raw; + USB0.ctl.raw = ((struct USB_CTL_t){ + .txd_suspend = 0, + .usben = 1 + }).raw; } void usb_set_addr(int addr) { - USB0.addr.raw = addr; + USB0.addr.raw = addr; } void usb_pipe_stall(struct usbd_ep_pipe_state_t *s) { - volatile struct USB_BD_t *bd = usb_get_bd(s); - bd->raw = ((struct USB_BD_BITS_t){ - .stall = 1, - .own = 1 - }).raw; + volatile struct USB_BD_t *bd = usb_get_bd(s); + bd->raw = ((struct USB_BD_BITS_t){ + .stall = 1, + .own = 1 + }).raw; } void usb_pipe_unstall(struct usbd_ep_pipe_state_t *s) { - volatile struct USB_BD_t *bd = usb_get_bd(s); - struct USB_BD_BITS_t state = { .raw = bd->raw }; + volatile struct USB_BD_t *bd = usb_get_bd(s); + struct USB_BD_BITS_t state = { .raw = bd->raw }; - if (state.own && state.stall) - bd->raw = 0; + if (state.own && state.stall) + bd->raw = 0; } void usb_pipe_enable(struct usbd_ep_pipe_state_t *s) { - USB0.endpt[s->ep_num].raw |= ((struct USB_ENDPT_t){ - .eptxen = s->ep_dir == USB_EP_TX, - .eprxen = s->ep_dir == USB_EP_RX, - .ephshk = 1, /* XXX ISO */ - .epctldis = s->ep_num != 0 - }).raw; + USB0.endpt[s->ep_num].raw |= ((struct USB_ENDPT_t){ + .eptxen = s->ep_dir == USB_EP_TX, + .eprxen = s->ep_dir == USB_EP_RX, + .ephshk = 1, /* XXX ISO */ + .epctldis = s->ep_num != 0 + }).raw; } void usb_pipe_disable(struct usbd_ep_pipe_state_t *s) { - USB0.endpt[s->ep_num].raw &= ~((struct USB_ENDPT_t){ - .eptxen = s->ep_dir == USB_EP_TX, - .eprxen = s->ep_dir == USB_EP_RX, - .epctldis = 1 - }).raw; + USB0.endpt[s->ep_num].raw &= ~((struct USB_ENDPT_t){ + .eptxen = s->ep_dir == USB_EP_TX, + .eprxen = s->ep_dir == USB_EP_RX, + .epctldis = 1 + }).raw; } size_t usb_ep_get_transfer_size(struct usbd_ep_pipe_state_t *s) { - struct USB_BD_t *bd = usb_get_bd(s); - return (bd->bc); + struct USB_BD_t *bd = usb_get_bd(s); + return (bd->bc); } void usb_queue_next(struct usbd_ep_pipe_state_t *s, void *addr, size_t len) { - volatile struct USB_BD_t *bd = usb_get_bd(s); + volatile struct USB_BD_t *bd = usb_get_bd(s); - bd->addr = addr; - /* damn you bitfield problems */ - bd->raw = ((struct USB_BD_BITS_t){ - .dts = 1, - .own = 1, - .data01 = s->data01, - .bc = len, - }).raw; + bd->addr = addr; + /* damn you bitfield problems */ + bd->raw = ((struct USB_BD_BITS_t){ + .dts = 1, + .own = 1, + .data01 = s->data01, + .bc = len, + }).raw; } static void usb_reset(void) { - /* reset pingpong state */ - /* For some obscure reason, we need to use or here. */ - USB0.ctl.raw |= ((struct USB_CTL_t){ - .txd_suspend = 1, - .oddrst = 1, - }).raw; + /* reset pingpong state */ + /* For some obscure reason, we need to use or here. */ + USB0.ctl.raw |= ((struct USB_CTL_t){ + .txd_suspend = 1, + .oddrst = 1, + }).raw; - /* clear all interrupt bits - not sure if needed */ - USB0.istat.raw = 0xff; - USB0.errstat.raw = 0xff; - USB0.otgistat.raw = 0xff; + /* clear all interrupt bits - not sure if needed */ + USB0.istat.raw = 0xff; + USB0.errstat.raw = 0xff; + USB0.otgistat.raw = 0xff; - /* zap also BDT pingpong & queued transactions */ - memset(bdt, 0, sizeof(bdt)); - USB0.addr.raw = 0; + /* zap also BDT pingpong & queued transactions */ + memset(bdt, 0, sizeof(bdt)); + USB0.addr.raw = 0; - usb_restart(); + usb_restart(); - USB0.ctl.raw = ((struct USB_CTL_t){ - .txd_suspend = 0, - .usben = 1 - }).raw; + USB0.ctl.raw = ((struct USB_CTL_t){ + .txd_suspend = 0, + .usben = 1 + }).raw; - /* we're only interested in reset and transfers */ - USB0.inten.raw = ((struct USB_ISTAT_t){ - .tokdne = 1, - .usbrst = 1, - .stall = 1, - .sleep = 1, - }).raw; + /* we're only interested in reset and transfers */ + USB0.inten.raw = ((struct USB_ISTAT_t){ + .tokdne = 1, + .usbrst = 1, + .stall = 1, + .sleep = 1, + }).raw; - USB0.usbtrc0.usbresmen = 0; - USB0.usbctrl.susp = 0; + USB0.usbtrc0.usbresmen = 0; + USB0.usbctrl.susp = 0; } void usb_enable(void) { - SIM.sopt2.usbsrc = 1; /* usb from mcg */ - SIM.scgc4.usbotg = 1; /* enable usb clock */ + SIM.sopt2.usbsrc = 1; /* usb from mcg */ + SIM.scgc4.usbotg = 1; /* enable usb clock */ - /* reset module - not sure if needed */ - USB0.usbtrc0.raw = ((struct USB_USBTRC0_t){ - .usbreset = 1, - .usbresmen = 1 - }).raw; - while (USB0.usbtrc0.usbreset) - /* NOTHING */; + /* reset module - not sure if needed */ + USB0.usbtrc0.raw = ((struct USB_USBTRC0_t){ + .usbreset = 1, + .usbresmen = 1 + }).raw; + while (USB0.usbtrc0.usbreset) + /* NOTHING */; - USB0.bdtpage1 = (uintptr_t)bdt >> 8; - USB0.bdtpage2 = (uintptr_t)bdt >> 16; - USB0.bdtpage3 = (uintptr_t)bdt >> 24; + USB0.bdtpage1 = (uintptr_t)bdt >> 8; + USB0.bdtpage2 = (uintptr_t)bdt >> 16; + USB0.bdtpage3 = (uintptr_t)bdt >> 24; - USB0.control.raw = ((struct USB_CONTROL_t){ - .dppullupnonotg = 1 /* enable pullup */ - }).raw; + USB0.control.raw = ((struct USB_CONTROL_t){ + .dppullupnonotg = 1 /* enable pullup */ + }).raw; - USB0.usbctrl.raw = 0; /* resume peripheral & disable pulldowns */ - usb_reset(); /* this will start usb processing */ + USB0.usbctrl.raw = 0; /* resume peripheral & disable pulldowns */ + usb_reset(); /* this will start usb processing */ - /* really only one thing we want */ - USB0.inten.raw = ((struct USB_ISTAT_t){ - .usbrst = 1, - }).raw; + /* really only one thing we want */ + USB0.inten.raw = ((struct USB_ISTAT_t){ + .usbrst = 1, + }).raw; - /** - * Suspend transceiver now - we'll wake up at reset again. - */ + /** + * Suspend transceiver now - we'll wake up at reset again. + */ // TODO - Possible removal - USB0.usbctrl.susp = 1; - USB0.usbtrc0.usbresmen = 1; + USB0.usbctrl.susp = 1; + USB0.usbtrc0.usbresmen = 1; } void USB0_Handler(void) { - struct USB_ISTAT_t stat = {.raw = USB0.istat.raw }; + struct USB_ISTAT_t stat = {.raw = USB0.istat.raw }; - if (stat.usbrst) { - usb_reset(); - return; - } - if (stat.stall) { - /* XXX need more work for non-0 ep */ - volatile struct USB_BD_t *bd = usb_get_bd(&usb.ep_state[0].rx); - if (bd->stall) - usb_setup_control(); - } - if (stat.tokdne) { - struct usb_xfer_info stat = USB0.stat; - usb_handle_transaction(&stat); - } - if (stat.sleep) { - USB0.inten.sleep = 0; - USB0.inten.resume = 1; - USB0.usbctrl.susp = 1; - USB0.usbtrc0.usbresmen = 1; + if (stat.usbrst) { + usb_reset(); + return; + } + if (stat.stall) { + /* XXX need more work for non-0 ep */ + volatile struct USB_BD_t *bd = usb_get_bd(&usb.ep_state[0].rx); + if (bd->stall) + usb_setup_control(); + } + if (stat.tokdne) { + struct usb_xfer_info stat = USB0.stat; + usb_handle_transaction(&stat); + } + if (stat.sleep) { + USB0.inten.sleep = 0; + USB0.inten.resume = 1; + USB0.usbctrl.susp = 1; + USB0.usbtrc0.usbresmen = 1; - /** - * Clear interrupts now so that we can detect a fresh - * resume later on. - */ - USB0.istat.raw = stat.raw; + /** + * Clear interrupts now so that we can detect a fresh + * resume later on. + */ + USB0.istat.raw = stat.raw; - const struct usbd_config *c = usb_get_config_data(-1); - if (c && c->suspend) - c->suspend(); - } - /** - * XXX it is unclear whether we will receive a synchronous - * resume interrupt if we were in sleep. This code assumes we - * do. - */ - if (stat.resume || USB0.usbtrc0.usb_resume_int) { - USB0.inten.resume = 0; - USB0.inten.sleep = 1; - USB0.usbtrc0.usbresmen = 0; - USB0.usbctrl.susp = 0; + const struct usbd_config *c = usb_get_config_data(-1); + if (c && c->suspend) + c->suspend(); + } + /** + * XXX it is unclear whether we will receive a synchronous + * resume interrupt if we were in sleep. This code assumes we + * do. + */ + if (stat.resume || USB0.usbtrc0.usb_resume_int) { + USB0.inten.resume = 0; + USB0.inten.sleep = 1; + USB0.usbtrc0.usbresmen = 0; + USB0.usbctrl.susp = 0; - const struct usbd_config *c = usb_get_config_data(-1); - if (c && c->resume) - c->resume(); + const struct usbd_config *c = usb_get_config_data(-1); + if (c && c->resume) + c->resume(); - stat.resume = 1; /* always clear bit */ - } - USB0.istat.raw = stat.raw; + stat.resume = 1; /* always clear bit */ + } + USB0.istat.raw = stat.raw; } void usb_poll(void) { - USB0_Handler(); + USB0_Handler(); } int usb_tx_serialno(size_t reqlen) { - struct usb_desc_string_t *d; - const size_t nregs = 3; - /** - * actually 4, but UIDH is 0xffffffff. Also our output buffer - * is only 64 bytes, and 128 bit + desc header exceeds this by - * 2 bytes. - */ - const size_t len = nregs * 4 * 2 * 2 + sizeof(*d); + struct usb_desc_string_t *d; + const size_t nregs = 3; + /** + * actually 4, but UIDH is 0xffffffff. Also our output buffer + * is only 64 bytes, and 128 bit + desc header exceeds this by + * 2 bytes. + */ + const size_t len = nregs * 4 * 2 * 2 + sizeof(*d); - d = usb_ep0_tx_inplace_prepare(len); + d = usb_ep0_tx_inplace_prepare(len); - if (d == NULL) - return (-1); + if (d == NULL) + return (-1); - d->bLength = len; - d->bDescriptorType = USB_DESC_STRING; + d->bLength = len; + d->bDescriptorType = USB_DESC_STRING; - size_t bufpos = 0; - for (size_t reg = 0; reg < nregs; ++reg) { - /* registers run MSW first */ - uint32_t val = (&SIM.uidmh)[reg]; + size_t bufpos = 0; + for (size_t reg = 0; reg < nregs; ++reg) { + /* registers run MSW first */ + uint32_t val = (&SIM.uidmh)[reg]; - for (size_t bits = 32; bits > 0; bits -= 4, val <<= 4) { - int nibble = val >> 28; + for (size_t bits = 32; bits > 0; bits -= 4, val <<= 4) { + int nibble = val >> 28; - if (nibble > 9) - nibble += 'a' - '9' - 1; - ((char16_t *)d->bString)[bufpos++] = nibble + '0'; - } - } - usb_ep0_tx(d, len, reqlen, NULL, NULL); - return (0); + if (nibble > 9) + nibble += 'a' - '9' - 1; + ((char16_t *)d->bString)[bufpos++] = nibble + '0'; + } + } + usb_ep0_tx(d, len, reqlen, NULL, NULL); + return (0); } diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/main.c --- a/Bootloader/main.c Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/main.c Fri Jun 12 18:31:55 2015 -0700 @@ -36,38 +36,38 @@ static enum dfu_status setup_write(size_t off, size_t len, void **buf) { - static int last = 0; + static int last = 0; - if (len > sizeof(staging)) - return (DFU_STATUS_errADDRESS); + if (len > sizeof(staging)) + return (DFU_STATUS_errADDRESS); - // We only allow the last write to be less than one sector size. - if (off == 0) - last = 0; - if (last && len != 0) - return (DFU_STATUS_errADDRESS); - if (len != FLASH_SECTOR_SIZE) { - last = 1; - memset(staging, 0xff, sizeof(staging)); - } + // We only allow the last write to be less than one sector size. + if (off == 0) + last = 0; + if (last && len != 0) + return (DFU_STATUS_errADDRESS); + if (len != FLASH_SECTOR_SIZE) { + last = 1; + memset(staging, 0xff, sizeof(staging)); + } - *buf = staging; - return (DFU_STATUS_OK); + *buf = staging; + return (DFU_STATUS_OK); } static enum dfu_status finish_write( void *buf, size_t off, size_t len ) { - void *target; - if (len == 0) - return (DFU_STATUS_OK); + void *target; + if (len == 0) + return (DFU_STATUS_OK); - target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE); - if (!target) - return (DFU_STATUS_errADDRESS); - memcpy(target, buf, len); - if (flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) != 0) - return (DFU_STATUS_errADDRESS); - return (DFU_STATUS_OK); + target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE); + if (!target) + return (DFU_STATUS_errADDRESS); + memcpy(target, buf, len); + if (flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) != 0) + return (DFU_STATUS_errADDRESS); + return (DFU_STATUS_OK); } @@ -75,7 +75,7 @@ void init_usb_bootloader( int config ) { - dfu_init(setup_write, finish_write, &dfu_ctx); + dfu_init(setup_write, finish_write, &dfu_ctx); } void main() @@ -98,12 +98,12 @@ #endif - flash_prepare_flashing(); + flash_prepare_flashing(); - usb_init( &dfu_device ); - for (;;) + usb_init( &dfu_device ); + for (;;) { - usb_poll(); - } + usb_poll(); + } } diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/mchck-cdefs.h --- a/Bootloader/mchck-cdefs.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/mchck-cdefs.h Fri Jun 12 18:31:55 2015 -0700 @@ -42,15 +42,15 @@ #define CTASSERT_SIZE_BIT(t, s) CTASSERT(sizeof(t) * 8 == (s)) #define UNION_STRUCT_START(size) \ - union { \ - _CONCAT(_CONCAT(uint, size), _t) raw; \ - struct { \ - /* just to swallow the following semicolon */ \ - struct _CONCAT(_CONCAT(__dummy_, __COUNTER__), _t) {} + union { \ + _CONCAT(_CONCAT(uint, size), _t) raw; \ + struct { \ + /* just to swallow the following semicolon */ \ + struct _CONCAT(_CONCAT(__dummy_, __COUNTER__), _t) {} #define UNION_STRUCT_END \ - }; /* struct */ \ - }; /* union */ + }; /* struct */ \ + }; /* union */ /** @@ -58,25 +58,25 @@ * */ #define __PP_NARG(...) \ - __PP_NARG_(__0, ## __VA_ARGS__, __PP_RSEQ_N()) + __PP_NARG_(__0, ## __VA_ARGS__, __PP_RSEQ_N()) #define __PP_NARG_(...) \ - __PP_ARG_N(__VA_ARGS__) + __PP_ARG_N(__VA_ARGS__) #define __PP_ARG_N( \ - _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \ - _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \ - _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \ - _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \ - _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \ - _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \ - _61,_62,_63,N,...) N + _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \ + _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \ + _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \ + _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \ + _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \ + _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \ + _61,_62,_63,N,...) N #define __PP_RSEQ_N() \ - 62,61,60, \ - 59,58,57,56,55,54,53,52,51,50, \ - 49,48,47,46,45,44,43,42,41,40, \ - 39,38,37,36,35,34,33,32,31,30, \ - 29,28,27,26,25,24,23,22,21,20, \ - 19,18,17,16,15,14,13,12,11,10, \ - 9,8,7,6,5,4,3,2,1,0 + 62,61,60, \ + 59,58,57,56,55,54,53,52,51,50, \ + 49,48,47,46,45,44,43,42,41,40, \ + 39,38,37,36,35,34,33,32,31,30, \ + 29,28,27,26,25,24,23,22,21,20, \ + 19,18,17,16,15,14,13,12,11,10, \ + 9,8,7,6,5,4,3,2,1,0 /** * From @@ -126,13 +126,13 @@ #define __REPEAT_INNER(...) __OBSTRUCT(__REPEAT_INDIRECT) () (__VA_ARGS__) #define __REPEAT_INDIRECT() __REPEAT_ #define __REPEAT_(iter, itermacro, macro, a, ...) \ - __OBSTRUCT(macro)(iter, a) \ - __WHEN(__PP_NARG(__VA_ARGS__)) \ - ( \ - __OBSTRUCT(__REPEAT_INDIRECT) () ( \ - itermacro(iter, a), itermacro, macro, __VA_ARGS__ \ - ) \ - ) + __OBSTRUCT(macro)(iter, a) \ + __WHEN(__PP_NARG(__VA_ARGS__)) \ + ( \ + __OBSTRUCT(__REPEAT_INDIRECT) () ( \ + itermacro(iter, a), itermacro, macro, __VA_ARGS__ \ + ) \ + ) #endif diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/sim.h --- a/Bootloader/sim.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/sim.h Fri Jun 12 18:31:55 2015 -0700 @@ -27,302 +27,302 @@ // ----- Structs ----- struct SIM_t { - struct SIM_SOPT1_t { - UNION_STRUCT_START(32); - uint32_t _rsvd0 : 12; - enum { - SIM_RAMSIZE_8KB = 1, - SIM_RAMSIZE_16KB = 3 - } ramsize : 4; - uint32_t _rsvd1 : 2; - enum { - SIM_OSC32KSEL_SYSTEM = 0, - SIM_OSC32KSEL_RTC = 2, - SIM_OSC32KSEL_LPO = 3 - } osc32ksel : 2; - uint32_t _rsvd2 : 9; - uint32_t usbvstby : 1; - uint32_t usbsstby : 1; - uint32_t usbregen : 1; - UNION_STRUCT_END; - } sopt1; - struct SIM_SOPT1CFG_t { - UNION_STRUCT_START(32); - uint32_t _rsvd0 : 24; - uint32_t urwe : 1; - uint32_t uvswe : 1; - uint32_t usswe : 1; - uint32_t _rsvd1 : 5; - UNION_STRUCT_END; - } sopt1cfg; - uint32_t _pad0[(0x1004 - 0x8) / 4]; - struct SIM_SOPT2_t { - UNION_STRUCT_START(32); - uint32_t _rsvd0 : 4; - enum { - SIM_RTCCLKOUTSEL_1HZ = 0, - SIM_RTCCLKOUTSEL_32KHZ = 1 - } rtcclkoutsel : 1; - enum { - SIM_CLKOUTSEL_FLASH = 2, - SIM_CLKOUTSEL_LPO = 3, - SIM_CLKOUTSEL_MCG = 4, - SIM_CLKOUTSEL_RTC = 5, - SIM_CLKOUTSEL_OSC = 6 - } clkoutsel : 3; - uint32_t _rsvd1 : 3; - enum { - SIM_PTD7PAD_SINGLE = 0, - SIM_PTD7PAD_DOUBLE = 1 - } ptd7pad : 1; - enum { - SIM_TRACECLKSEL_MCG = 0, - SIM_TRACECLKSEL_CORE = 1 - } traceclksel : 1; - uint32_t _rsvd2 : 3; - enum { - SIM_PLLFLLSEL_FLL = 0, - SIM_PLLFLLSEL_PLL = 1 - } pllfllsel : 1; - uint32_t _rsvd3 : 1; - enum { - SIM_USBSRC_EXTERNAL = 0, - SIM_USBSRC_MCG = 1 - } usbsrc : 1; - uint32_t _rsvd4 : 13; - UNION_STRUCT_END; - } sopt2; - uint32_t _pad1; - struct SIM_SOPT4_t { - UNION_STRUCT_START(32); - enum sim_ftmflt { - SIM_FTMFLT_FTM = 0, - SIM_FTMFLT_CMP = 1 - } ftm0flt0 : 1; - enum sim_ftmflt ftm0flt1 : 1; - uint32_t _rsvd0 : 2; - enum sim_ftmflt ftm1flt0 : 1; - uint32_t _rsvd1 : 13; - enum { - SIM_FTMCHSRC_FTM = 0, - SIM_FTMCHSRC_CMP0 = 1, - SIM_FTMCHSRC_CMP1 = 2, - SIM_FTMCHSRC_USBSOF = 3 - } ftm1ch0src : 2; - uint32_t _rsvd2 : 4; - enum sim_ftmclksel { - SIM_FTMCLKSEL_CLK0 = 0, - SIM_FTMCLKSEL_CLK1 = 1 - } ftm0clksel : 1; - enum sim_ftmclksel ftm1clksel : 1; - uint32_t _rsvd3 : 2; - enum { - SIM_FTMTRGSRC_HSCMP0 = 0, - SIM_FTMTRGSRC_FTM1 = 1 - } ftm0trg0src : 1; - uint32_t _rsvd4 : 3; - UNION_STRUCT_END; - } sopt4; - struct SIM_SOPT5_t { - UNION_STRUCT_START(32); - enum sim_uarttxsrc { - SIM_UARTTXSRC_UART = 0, - SIM_UARTTXSRC_FTM = 1 - } uart0txsrc : 1; - uint32_t _rsvd0 : 1; - enum sim_uartrxsrc { - SIM_UARTRXSRC_UART = 0, - SIM_UARTRXSRC_CMP0 = 1, - SIM_UARTRXSRC_CMP1 = 2 - } uart0rxsrc : 2; - enum sim_uarttxsrc uart1txsrc : 1; - uint32_t _rsvd1 : 1; - enum sim_uartrxsrc uart1rxsrc : 2; - uint32_t _rsvd2 : 24; - UNION_STRUCT_END; - } sopt5; - uint32_t _pad2; - struct SIM_SOPT7_t { - UNION_STRUCT_START(32); - enum { - SIM_ADCTRGSEL_PDB = 0, - SIM_ADCTRGSEL_HSCMP0 = 1, - SIM_ADCTRGSEL_HSCMP1 = 2, - SIM_ADCTRGSEL_PIT0 = 4, - SIM_ADCTRGSEL_PIT1 = 5, - SIM_ADCTRGSEL_PIT2 = 6, - SIM_ADCTRGSEL_PIT3 = 7, - SIM_ADCTRGSEL_FTM0 = 8, - SIM_ADCTRGSEL_FTM1 = 9, - SIM_ADCTRGSEL_RTCALARM = 12, - SIM_ADCTRGSEL_RTCSECS = 13, - SIM_ADCTRGSEL_LPTIMER = 14 - } adc0trgsel : 4; - enum { - SIM_ADCPRETRGSEL_A = 0, - SIM_ADCPRETRGSEL_B = 1 - } adc0pretrgsel : 1; - uint32_t _rsvd0 : 2; - enum { - SIM_ADCALTTRGEN_PDB = 0, - SIM_ADCALTTRGEN_ALT = 1 - } adc0alttrgen : 1; - uint32_t _rsvd1 : 24; - UNION_STRUCT_END; - } sopt7; - uint32_t _pad3[(0x1024 - 0x101c) / 4]; - struct SIM_SDID_t { - UNION_STRUCT_START(32); - enum { - SIM_PINID_32 = 2, - SIM_PINID_48 = 4, - SIM_PINID_64 = 5 - } pinid : 4; - enum { - SIM_FAMID_K10 = 0, - SIM_FAMID_K20 = 1 - } famid : 3; - uint32_t _rsvd1 : 5; - uint32_t revid : 4; - uint32_t _rsvd2 : 16; - UNION_STRUCT_END; - } sdid; - uint32_t _pad4[(0x1034 - 0x1028) / 4]; - struct SIM_SCGC4_t { - UNION_STRUCT_START(32); - uint32_t _rsvd0 : 1; - uint32_t ewm : 1; - uint32_t cmt : 1; - uint32_t _rsvd1 : 3; - uint32_t i2c0 : 1; - uint32_t _rsvd2 : 3; - uint32_t uart0 : 1; - uint32_t uart1 : 1; - uint32_t uart2 : 1; - uint32_t _rsvd3 : 5; - uint32_t usbotg : 1; - uint32_t cmp : 1; - uint32_t vref : 1; - uint32_t _rsvd4 : 11; - UNION_STRUCT_END; - } scgc4; - struct SIM_SCGC5_t { - UNION_STRUCT_START(32); - uint32_t lptimer : 1; - uint32_t _rsvd0 : 4; - uint32_t tsi : 1; - uint32_t _rsvd1 : 3; - uint32_t porta : 1; - uint32_t portb : 1; - uint32_t portc : 1; - uint32_t portd : 1; - uint32_t porte : 1; - uint32_t _rsvd2 : 18; - UNION_STRUCT_END; - } scgc5; - struct SIM_SCGC6_t { - UNION_STRUCT_START(32); - uint32_t ftfl : 1; - uint32_t dmamux : 1; - uint32_t _rsvd0 : 10; - uint32_t spi0 : 1; - uint32_t _rsvd1 : 2; - uint32_t i2s : 1; - uint32_t _rsvd2 : 2; - uint32_t crc : 1; - uint32_t _rsvd3 : 2; - uint32_t usbdcd : 1; - uint32_t pdb : 1; - uint32_t pit : 1; - uint32_t ftm0 : 1; - uint32_t ftm1 : 1; - uint32_t _rsvd4 : 1; - uint32_t adc0 : 1; - uint32_t _rsvd5 : 1; - uint32_t rtc : 1; - uint32_t _rsvd6 : 2; - UNION_STRUCT_END; - } scgc6; - struct SIM_SCGC7_t { - UNION_STRUCT_START(32); - uint32_t _rsvd0 : 1; - uint32_t dma : 1; - uint32_t _rsvd1 : 30; - UNION_STRUCT_END; - } scgc7; - struct SIM_CLKDIV1_t { - UNION_STRUCT_START(32); - uint32_t _rsvd0 : 16; - uint32_t outdiv4 : 4; - uint32_t _rsvd1 : 4; - uint32_t outdiv2 : 4; - uint32_t outdiv1 : 4; - UNION_STRUCT_END; - } clkdiv1; - struct SIM_CLKDIV2_t { - UNION_STRUCT_START(32); - uint32_t usbfrac : 1; - uint32_t usbdiv : 3; - uint32_t _rsvd0 : 28; - UNION_STRUCT_END; - } clkdiv2; - struct SIM_FCFG1_t { - UNION_STRUCT_START(32); - uint32_t flashdis : 1; - uint32_t flashdoze : 1; - uint32_t _rsvd0 : 6; + struct SIM_SOPT1_t { + UNION_STRUCT_START(32); + uint32_t _rsvd0 : 12; + enum { + SIM_RAMSIZE_8KB = 1, + SIM_RAMSIZE_16KB = 3 + } ramsize : 4; + uint32_t _rsvd1 : 2; + enum { + SIM_OSC32KSEL_SYSTEM = 0, + SIM_OSC32KSEL_RTC = 2, + SIM_OSC32KSEL_LPO = 3 + } osc32ksel : 2; + uint32_t _rsvd2 : 9; + uint32_t usbvstby : 1; + uint32_t usbsstby : 1; + uint32_t usbregen : 1; + UNION_STRUCT_END; + } sopt1; + struct SIM_SOPT1CFG_t { + UNION_STRUCT_START(32); + uint32_t _rsvd0 : 24; + uint32_t urwe : 1; + uint32_t uvswe : 1; + uint32_t usswe : 1; + uint32_t _rsvd1 : 5; + UNION_STRUCT_END; + } sopt1cfg; + uint32_t _pad0[(0x1004 - 0x8) / 4]; + struct SIM_SOPT2_t { + UNION_STRUCT_START(32); + uint32_t _rsvd0 : 4; + enum { + SIM_RTCCLKOUTSEL_1HZ = 0, + SIM_RTCCLKOUTSEL_32KHZ = 1 + } rtcclkoutsel : 1; + enum { + SIM_CLKOUTSEL_FLASH = 2, + SIM_CLKOUTSEL_LPO = 3, + SIM_CLKOUTSEL_MCG = 4, + SIM_CLKOUTSEL_RTC = 5, + SIM_CLKOUTSEL_OSC = 6 + } clkoutsel : 3; + uint32_t _rsvd1 : 3; + enum { + SIM_PTD7PAD_SINGLE = 0, + SIM_PTD7PAD_DOUBLE = 1 + } ptd7pad : 1; + enum { + SIM_TRACECLKSEL_MCG = 0, + SIM_TRACECLKSEL_CORE = 1 + } traceclksel : 1; + uint32_t _rsvd2 : 3; + enum { + SIM_PLLFLLSEL_FLL = 0, + SIM_PLLFLLSEL_PLL = 1 + } pllfllsel : 1; + uint32_t _rsvd3 : 1; + enum { + SIM_USBSRC_EXTERNAL = 0, + SIM_USBSRC_MCG = 1 + } usbsrc : 1; + uint32_t _rsvd4 : 13; + UNION_STRUCT_END; + } sopt2; + uint32_t _pad1; + struct SIM_SOPT4_t { + UNION_STRUCT_START(32); + enum sim_ftmflt { + SIM_FTMFLT_FTM = 0, + SIM_FTMFLT_CMP = 1 + } ftm0flt0 : 1; + enum sim_ftmflt ftm0flt1 : 1; + uint32_t _rsvd0 : 2; + enum sim_ftmflt ftm1flt0 : 1; + uint32_t _rsvd1 : 13; + enum { + SIM_FTMCHSRC_FTM = 0, + SIM_FTMCHSRC_CMP0 = 1, + SIM_FTMCHSRC_CMP1 = 2, + SIM_FTMCHSRC_USBSOF = 3 + } ftm1ch0src : 2; + uint32_t _rsvd2 : 4; + enum sim_ftmclksel { + SIM_FTMCLKSEL_CLK0 = 0, + SIM_FTMCLKSEL_CLK1 = 1 + } ftm0clksel : 1; + enum sim_ftmclksel ftm1clksel : 1; + uint32_t _rsvd3 : 2; + enum { + SIM_FTMTRGSRC_HSCMP0 = 0, + SIM_FTMTRGSRC_FTM1 = 1 + } ftm0trg0src : 1; + uint32_t _rsvd4 : 3; + UNION_STRUCT_END; + } sopt4; + struct SIM_SOPT5_t { + UNION_STRUCT_START(32); + enum sim_uarttxsrc { + SIM_UARTTXSRC_UART = 0, + SIM_UARTTXSRC_FTM = 1 + } uart0txsrc : 1; + uint32_t _rsvd0 : 1; + enum sim_uartrxsrc { + SIM_UARTRXSRC_UART = 0, + SIM_UARTRXSRC_CMP0 = 1, + SIM_UARTRXSRC_CMP1 = 2 + } uart0rxsrc : 2; + enum sim_uarttxsrc uart1txsrc : 1; + uint32_t _rsvd1 : 1; + enum sim_uartrxsrc uart1rxsrc : 2; + uint32_t _rsvd2 : 24; + UNION_STRUCT_END; + } sopt5; + uint32_t _pad2; + struct SIM_SOPT7_t { + UNION_STRUCT_START(32); + enum { + SIM_ADCTRGSEL_PDB = 0, + SIM_ADCTRGSEL_HSCMP0 = 1, + SIM_ADCTRGSEL_HSCMP1 = 2, + SIM_ADCTRGSEL_PIT0 = 4, + SIM_ADCTRGSEL_PIT1 = 5, + SIM_ADCTRGSEL_PIT2 = 6, + SIM_ADCTRGSEL_PIT3 = 7, + SIM_ADCTRGSEL_FTM0 = 8, + SIM_ADCTRGSEL_FTM1 = 9, + SIM_ADCTRGSEL_RTCALARM = 12, + SIM_ADCTRGSEL_RTCSECS = 13, + SIM_ADCTRGSEL_LPTIMER = 14 + } adc0trgsel : 4; + enum { + SIM_ADCPRETRGSEL_A = 0, + SIM_ADCPRETRGSEL_B = 1 + } adc0pretrgsel : 1; + uint32_t _rsvd0 : 2; + enum { + SIM_ADCALTTRGEN_PDB = 0, + SIM_ADCALTTRGEN_ALT = 1 + } adc0alttrgen : 1; + uint32_t _rsvd1 : 24; + UNION_STRUCT_END; + } sopt7; + uint32_t _pad3[(0x1024 - 0x101c) / 4]; + struct SIM_SDID_t { + UNION_STRUCT_START(32); + enum { + SIM_PINID_32 = 2, + SIM_PINID_48 = 4, + SIM_PINID_64 = 5 + } pinid : 4; + enum { + SIM_FAMID_K10 = 0, + SIM_FAMID_K20 = 1 + } famid : 3; + uint32_t _rsvd1 : 5; + uint32_t revid : 4; + uint32_t _rsvd2 : 16; + UNION_STRUCT_END; + } sdid; + uint32_t _pad4[(0x1034 - 0x1028) / 4]; + struct SIM_SCGC4_t { + UNION_STRUCT_START(32); + uint32_t _rsvd0 : 1; + uint32_t ewm : 1; + uint32_t cmt : 1; + uint32_t _rsvd1 : 3; + uint32_t i2c0 : 1; + uint32_t _rsvd2 : 3; + uint32_t uart0 : 1; + uint32_t uart1 : 1; + uint32_t uart2 : 1; + uint32_t _rsvd3 : 5; + uint32_t usbotg : 1; + uint32_t cmp : 1; + uint32_t vref : 1; + uint32_t _rsvd4 : 11; + UNION_STRUCT_END; + } scgc4; + struct SIM_SCGC5_t { + UNION_STRUCT_START(32); + uint32_t lptimer : 1; + uint32_t _rsvd0 : 4; + uint32_t tsi : 1; + uint32_t _rsvd1 : 3; + uint32_t porta : 1; + uint32_t portb : 1; + uint32_t portc : 1; + uint32_t portd : 1; + uint32_t porte : 1; + uint32_t _rsvd2 : 18; + UNION_STRUCT_END; + } scgc5; + struct SIM_SCGC6_t { + UNION_STRUCT_START(32); + uint32_t ftfl : 1; + uint32_t dmamux : 1; + uint32_t _rsvd0 : 10; + uint32_t spi0 : 1; + uint32_t _rsvd1 : 2; + uint32_t i2s : 1; + uint32_t _rsvd2 : 2; + uint32_t crc : 1; + uint32_t _rsvd3 : 2; + uint32_t usbdcd : 1; + uint32_t pdb : 1; + uint32_t pit : 1; + uint32_t ftm0 : 1; + uint32_t ftm1 : 1; + uint32_t _rsvd4 : 1; + uint32_t adc0 : 1; + uint32_t _rsvd5 : 1; + uint32_t rtc : 1; + uint32_t _rsvd6 : 2; + UNION_STRUCT_END; + } scgc6; + struct SIM_SCGC7_t { + UNION_STRUCT_START(32); + uint32_t _rsvd0 : 1; + uint32_t dma : 1; + uint32_t _rsvd1 : 30; + UNION_STRUCT_END; + } scgc7; + struct SIM_CLKDIV1_t { + UNION_STRUCT_START(32); + uint32_t _rsvd0 : 16; + uint32_t outdiv4 : 4; + uint32_t _rsvd1 : 4; + uint32_t outdiv2 : 4; + uint32_t outdiv1 : 4; + UNION_STRUCT_END; + } clkdiv1; + struct SIM_CLKDIV2_t { + UNION_STRUCT_START(32); + uint32_t usbfrac : 1; + uint32_t usbdiv : 3; + uint32_t _rsvd0 : 28; + UNION_STRUCT_END; + } clkdiv2; + struct SIM_FCFG1_t { + UNION_STRUCT_START(32); + uint32_t flashdis : 1; + uint32_t flashdoze : 1; + uint32_t _rsvd0 : 6; - /* the following enum is analogous to enum - * FTFL_FLEXNVM_PARTITION in ftfl.h, but that one is padded - * with four 1-bits to make an 8-bit value. - */ - enum SIM_FLEXNVM_PARTITION { - SIM_FLEXNVM_DATA_32_EEPROM_0 = 0x0, - SIM_FLEXNVM_DATA_24_EEPROM_8 = 0x1, - SIM_FLEXNVM_DATA_16_EEPROM_16 = 0x2, - SIM_FLEXNVM_DATA_8_EEPROM_24 = 0x9, - SIM_FLEXNVM_DATA_0_EEPROM_32 = 0x3 - } depart : 4; + /* the following enum is analogous to enum + * FTFL_FLEXNVM_PARTITION in ftfl.h, but that one is padded + * with four 1-bits to make an 8-bit value. + */ + enum SIM_FLEXNVM_PARTITION { + SIM_FLEXNVM_DATA_32_EEPROM_0 = 0x0, + SIM_FLEXNVM_DATA_24_EEPROM_8 = 0x1, + SIM_FLEXNVM_DATA_16_EEPROM_16 = 0x2, + SIM_FLEXNVM_DATA_8_EEPROM_24 = 0x9, + SIM_FLEXNVM_DATA_0_EEPROM_32 = 0x3 + } depart : 4; - uint32_t _rsvd1 : 4; - enum { - SIM_EESIZE_2KB = 3, - SIM_EESIZE_1KB = 4, - SIM_EESIZE_512B = 5, - SIM_EESIZE_256B = 6, - SIM_EESIZE_128B = 7, - SIM_EESIZE_64B = 8, - SIM_EESIZE_32B = 9, - SIM_EESIZE_0B = 15 - } eesize : 4; - uint32_t _rsvd2 : 4; - enum { - SIM_PFSIZE_32KB = 3, - SIM_PFSIZE_64KB = 5, - SIM_PFSIZE_128KB = 7 - } pfsize : 4; - enum { - SIM_NVMSIZE_0KB = 0, - SIM_NVMSIZE_32KB = 3 - } nvmsize : 4; - UNION_STRUCT_END; - } fcfg1; - struct SIM_FCFG2_t { - UNION_STRUCT_START(32); - uint32_t _rsvd0 : 16; - uint32_t maxaddr1 : 7; - enum { - SIM_PFLSH_FLEXNVM = 0, - SIM_PFLSH_PROGRAM = 1 - } pflsh : 1; - uint32_t maxaddr0 : 7; - uint32_t _rsvd1 : 1; - UNION_STRUCT_END; - } fcfg2; - uint32_t uidh; - uint32_t uidmh; - uint32_t uidml; - uint32_t uidl; + uint32_t _rsvd1 : 4; + enum { + SIM_EESIZE_2KB = 3, + SIM_EESIZE_1KB = 4, + SIM_EESIZE_512B = 5, + SIM_EESIZE_256B = 6, + SIM_EESIZE_128B = 7, + SIM_EESIZE_64B = 8, + SIM_EESIZE_32B = 9, + SIM_EESIZE_0B = 15 + } eesize : 4; + uint32_t _rsvd2 : 4; + enum { + SIM_PFSIZE_32KB = 3, + SIM_PFSIZE_64KB = 5, + SIM_PFSIZE_128KB = 7 + } pfsize : 4; + enum { + SIM_NVMSIZE_0KB = 0, + SIM_NVMSIZE_32KB = 3 + } nvmsize : 4; + UNION_STRUCT_END; + } fcfg1; + struct SIM_FCFG2_t { + UNION_STRUCT_START(32); + uint32_t _rsvd0 : 16; + uint32_t maxaddr1 : 7; + enum { + SIM_PFLSH_FLEXNVM = 0, + SIM_PFLSH_PROGRAM = 1 + } pflsh : 1; + uint32_t maxaddr0 : 7; + uint32_t _rsvd1 : 1; + UNION_STRUCT_END; + } fcfg2; + uint32_t uidh; + uint32_t uidmh; + uint32_t uidml; + uint32_t uidl; }; CTASSERT_SIZE_BYTE(struct SIM_t, 0x1064); diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/usb.c --- a/Bootloader/usb.c Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/usb.c Fri Jun 12 18:31:55 2015 -0700 @@ -171,10 +171,10 @@ */ size_t nextlen = s->transfer_size; - if (nextlen > s->ep_maxsize) - nextlen = s->ep_maxsize; + if (nextlen > s->ep_maxsize) + nextlen = s->ep_maxsize; - void *addr = s->data_buf + s->pos; + void *addr = s->data_buf + s->pos; usb_queue_next(s, addr, nextlen); return (1); diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/usb.h --- a/Bootloader/usb.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/usb.h Fri Jun 12 18:31:55 2015 -0700 @@ -37,113 +37,113 @@ #define USB_CTRL_REQ_TYPE_SHIFT 1 #define USB_CTRL_REQ_RECP_SHIFT 3 #define USB_CTRL_REQ_CODE_SHIFT 8 -#define USB_CTRL_REQ(req_inout, req_type, req_code) \ - (uint16_t) \ - ((USB_CTRL_REQ_##req_inout << USB_CTRL_REQ_DIR_SHIFT) \ - |(USB_CTRL_REQ_##req_type << USB_CTRL_REQ_TYPE_SHIFT) \ +#define USB_CTRL_REQ(req_inout, req_type, req_code) \ + (uint16_t) \ + ((USB_CTRL_REQ_##req_inout << USB_CTRL_REQ_DIR_SHIFT) \ + |(USB_CTRL_REQ_##req_type << USB_CTRL_REQ_TYPE_SHIFT) \ |(USB_CTRL_REQ_##req_code << USB_CTRL_REQ_CODE_SHIFT)) // ----- Macros ----- -#define USB_DESC_STRING(s) \ - (const void *)&(const struct { \ - struct usb_desc_string_t dsc; \ - char16_t str[sizeof(s) / 2 - 1]; \ - }) {{ \ - .bLength = sizeof(struct usb_desc_string_t) + \ - sizeof(s) - 2, \ - .bDescriptorType = USB_DESC_STRING, \ - }, \ - s \ +#define USB_DESC_STRING(s) \ + (const void *)&(const struct { \ + struct usb_desc_string_t dsc; \ + char16_t str[sizeof(s) / 2 - 1]; \ + }) {{ \ + .bLength = sizeof(struct usb_desc_string_t) + \ + sizeof(s) - 2, \ + .bDescriptorType = USB_DESC_STRING, \ + }, \ + s \ } #define USB_DESC_STRING_LANG_ENUS USB_DESC_STRING(u"\x0409") #define USB_DESC_STRING_SERIALNO ((const void *)1) -#define USB_FUNCTION_IFACE(iface, iface_off, tx_ep_off, rx_ep_off) \ +#define USB_FUNCTION_IFACE(iface, iface_off, tx_ep_off, rx_ep_off) \ ((iface_off) + (iface)) -#define USB_FUNCTION_TX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \ +#define USB_FUNCTION_TX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \ ((tx_ep_off) + (ep)) -#define USB_FUNCTION_RX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \ +#define USB_FUNCTION_RX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \ ((rx_ep_off) + (ep)) #define USB__INCREMENT(i, _0) (i + 1) -#define USB__COUNT_IFACE_EP(i, e) \ +#define USB__COUNT_IFACE_EP(i, e) \ __DEFER(USB__COUNT_IFACE_EP_)(__EXPAND i, e) #define USB__COUNT_IFACE_EP_(iface, tx_ep, rx_ep, func) \ - (iface + USB_FUNCTION_ ## func ## _IFACE_COUNT, \ - tx_ep + USB_FUNCTION_ ## func ## _TX_EP_COUNT, \ + (iface + USB_FUNCTION_ ## func ## _IFACE_COUNT, \ + tx_ep + USB_FUNCTION_ ## func ## _TX_EP_COUNT, \ rx_ep + USB_FUNCTION_ ## func ## _RX_EP_COUNT) -#define USB__GET_FUNCTION_IFACE_COUNT(iter, func) \ +#define USB__GET_FUNCTION_IFACE_COUNT(iter, func) \ USB_FUNCTION_ ## func ## _IFACE_COUNT + -#define USB__DEFINE_FUNCTION_DESC(iter, func) \ +#define USB__DEFINE_FUNCTION_DESC(iter, func) \ USB_FUNCTION_DESC_ ## func ## _DECL __CAT(__usb_func_desc, __COUNTER__); -#define USB__INIT_FUNCTION_DESC(iter, func) \ +#define USB__INIT_FUNCTION_DESC(iter, func) \ USB_FUNCTION_DESC_ ## func iter, -#define USB__DEFINE_CONFIG_DESC(confignum, name, ...) \ - &((const struct name { \ - struct usb_desc_config_t config; \ +#define USB__DEFINE_CONFIG_DESC(confignum, name, ...) \ + &((const struct name { \ + struct usb_desc_config_t config; \ __REPEAT_INNER(, __EAT, USB__DEFINE_FUNCTION_DESC, __VA_ARGS__) \ - }){ \ - .config = { \ - .bLength = sizeof(struct usb_desc_config_t), \ - .bDescriptorType = USB_DESC_CONFIG, \ - .wTotalLength = sizeof(struct name), \ + }){ \ + .config = { \ + .bLength = sizeof(struct usb_desc_config_t), \ + .bDescriptorType = USB_DESC_CONFIG, \ + .wTotalLength = sizeof(struct name), \ .bNumInterfaces = __REPEAT_INNER(, __EAT, USB__GET_FUNCTION_IFACE_COUNT, __VA_ARGS__) 0, \ - .bConfigurationValue = confignum, \ - .iConfiguration = 0, \ - .one = 1, \ - .bMaxPower = 50 \ - }, \ + .bConfigurationValue = confignum, \ + .iConfiguration = 0, \ + .one = 1, \ + .bMaxPower = 50 \ + }, \ __REPEAT_INNER((0, 0, 0), USB__COUNT_IFACE_EP, USB__INIT_FUNCTION_DESC, __VA_ARGS__) \ }).config -#define USB__DEFINE_CONFIG(iter, args) \ +#define USB__DEFINE_CONFIG(iter, args) \ __DEFER(USB__DEFINE_CONFIG_)(iter, __EXPAND args) -#define USB__DEFINE_CONFIG_(confignum, initfun, ...) \ - &(const struct usbd_config){ \ - .init = initfun, \ - .desc = USB__DEFINE_CONFIG_DESC( \ - confignum, \ - __CAT(__usb_desc, __COUNTER__), \ - __VA_ARGS__) \ +#define USB__DEFINE_CONFIG_(confignum, initfun, ...) \ + &(const struct usbd_config){ \ + .init = initfun, \ + .desc = USB__DEFINE_CONFIG_DESC( \ + confignum, \ + __CAT(__usb_desc, __COUNTER__), \ + __VA_ARGS__) \ }, -#define USB_INIT_DEVICE(vid, pid, manuf, product, ...) \ - { \ - .dev_desc = &(const struct usb_desc_dev_t){ \ - .bLength = sizeof(struct usb_desc_dev_t), \ - .bDescriptorType = USB_DESC_DEV, \ - .bcdUSB = { .maj = 2 }, \ - .bDeviceClass = USB_DEV_CLASS_SEE_IFACE, \ - .bDeviceSubClass = USB_DEV_SUBCLASS_SEE_IFACE, \ - .bDeviceProtocol = USB_DEV_PROTO_SEE_IFACE, \ - .bMaxPacketSize0 = EP0_BUFSIZE, \ - .idVendor = vid, \ - .idProduct = pid, \ - .bcdDevice = { .raw = 0 }, \ - .iManufacturer = 1, \ - .iProduct = 2, \ - .iSerialNumber = 3, \ - .bNumConfigurations = __PP_NARG(__VA_ARGS__), \ - }, \ +#define USB_INIT_DEVICE(vid, pid, manuf, product, ...) \ + { \ + .dev_desc = &(const struct usb_desc_dev_t){ \ + .bLength = sizeof(struct usb_desc_dev_t), \ + .bDescriptorType = USB_DESC_DEV, \ + .bcdUSB = { .maj = 2 }, \ + .bDeviceClass = USB_DEV_CLASS_SEE_IFACE, \ + .bDeviceSubClass = USB_DEV_SUBCLASS_SEE_IFACE, \ + .bDeviceProtocol = USB_DEV_PROTO_SEE_IFACE, \ + .bMaxPacketSize0 = EP0_BUFSIZE, \ + .idVendor = vid, \ + .idProduct = pid, \ + .bcdDevice = { .raw = 0 }, \ + .iManufacturer = 1, \ + .iProduct = 2, \ + .iSerialNumber = 3, \ + .bNumConfigurations = __PP_NARG(__VA_ARGS__), \ + }, \ .string_descs = (const struct usb_desc_string_t * const []){ \ - USB_DESC_STRING_LANG_ENUS, \ - USB_DESC_STRING(manuf), \ - USB_DESC_STRING(product), \ - USB_DESC_STRING_SERIALNO, \ - NULL \ - }, \ - .configs = { \ + USB_DESC_STRING_LANG_ENUS, \ + USB_DESC_STRING(manuf), \ + USB_DESC_STRING(product), \ + USB_DESC_STRING_SERIALNO, \ + NULL \ + }, \ + .configs = { \ __REPEAT(1, USB__INCREMENT, USB__DEFINE_CONFIG, __VA_ARGS__) \ - NULL \ - } \ + NULL \ + } \ } @@ -220,7 +220,7 @@ struct usb_desc_dev_t { uint8_t bLength; enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_DEV */ - struct usb_bcd_t bcdUSB; /* = 0x0200 */ + struct usb_bcd_t bcdUSB; /* = 0x0200 */ enum usb_dev_class bDeviceClass : 8; enum usb_dev_subclass bDeviceSubClass : 8; enum usb_dev_proto bDeviceProtocol : 8; @@ -290,7 +290,7 @@ struct usb_desc_config_t { uint8_t bLength; enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_CONFIG */ - uint16_t wTotalLength; /* size of config, iface, ep */ + uint16_t wTotalLength; /* size of config, iface, ep */ uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; @@ -300,7 +300,7 @@ uint8_t self_powered : 1; uint8_t one : 1; /* = 1 for historical reasons */ }; - uint8_t bMaxPower; /* units of 2mA */ + uint8_t bMaxPower; /* units of 2mA */ } __packed; CTASSERT_SIZE_BYTE(struct usb_desc_config_t, 9); diff -r 05a09eda53fb -r f7b14e25ca5b Bootloader/usbotg.h --- a/Bootloader/usbotg.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Bootloader/usbotg.h Fri Jun 12 18:31:55 2015 -0700 @@ -29,271 +29,271 @@ */ struct USB_ADDINFO_t { - UNION_STRUCT_START(8); - uint8_t iehost : 1; - uint8_t _rsvd0 : 2; - uint8_t irqnum : 5; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t iehost : 1; + uint8_t _rsvd0 : 2; + uint8_t irqnum : 5; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_ADDINFO_t, 8); struct USB_OTGSTAT_t { - UNION_STRUCT_START(8); - uint8_t avbus : 1; - uint8_t _rsvd0 : 1; - uint8_t b_sess : 1; - uint8_t sessvld : 1; - uint8_t _rsvd1 : 1; - uint8_t line_state : 1; - uint8_t onemsec : 1; - uint8_t idchg : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t avbus : 1; + uint8_t _rsvd0 : 1; + uint8_t b_sess : 1; + uint8_t sessvld : 1; + uint8_t _rsvd1 : 1; + uint8_t line_state : 1; + uint8_t onemsec : 1; + uint8_t idchg : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_OTGSTAT_t, 8); struct USB_OTGCTL_t { - UNION_STRUCT_START(8); - uint8_t _rsvd0 : 2; - uint8_t otgen : 1; - uint8_t _rsvd1 : 1; - uint8_t dmlow : 1; - uint8_t dplow : 1; - uint8_t _rsvd2 : 1; - uint8_t dphigh : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t _rsvd0 : 2; + uint8_t otgen : 1; + uint8_t _rsvd1 : 1; + uint8_t dmlow : 1; + uint8_t dplow : 1; + uint8_t _rsvd2 : 1; + uint8_t dphigh : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_OTGCTL_t, 8); struct USB_ISTAT_t { - UNION_STRUCT_START(8); - uint8_t usbrst : 1; - uint8_t error : 1; - uint8_t softok : 1; - uint8_t tokdne : 1; - uint8_t sleep : 1; - uint8_t resume : 1; - uint8_t attach : 1; - uint8_t stall : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t usbrst : 1; + uint8_t error : 1; + uint8_t softok : 1; + uint8_t tokdne : 1; + uint8_t sleep : 1; + uint8_t resume : 1; + uint8_t attach : 1; + uint8_t stall : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_ISTAT_t, 8); struct USB_ERRSTAT_t { - UNION_STRUCT_START(8); - uint8_t piderr : 1; - uint8_t crc5eof : 1; - uint8_t crc16 : 1; - uint8_t dfn8 : 1; - uint8_t btoerr : 1; - uint8_t dmaerr : 1; - uint8_t _rsvd0 : 1; - uint8_t btserr : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t piderr : 1; + uint8_t crc5eof : 1; + uint8_t crc16 : 1; + uint8_t dfn8 : 1; + uint8_t btoerr : 1; + uint8_t dmaerr : 1; + uint8_t _rsvd0 : 1; + uint8_t btserr : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_ERRSTAT_t, 8); struct USB_STAT_t { - UNION_STRUCT_START(8); - uint8_t _rsvd0 : 2; - enum usb_ep_pingpong pingpong : 1; - enum usb_ep_dir dir : 1; - uint8_t ep : 4; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t _rsvd0 : 2; + enum usb_ep_pingpong pingpong : 1; + enum usb_ep_dir dir : 1; + uint8_t ep : 4; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_STAT_t, 8); struct USB_CTL_t { - union { - struct /* common */ { - uint8_t _rsvd1 : 1; - uint8_t oddrst : 1; - uint8_t resume : 1; - uint8_t _rsvd2 : 3; - uint8_t se0 : 1; - uint8_t jstate : 1; - }; - struct /* host */ { - uint8_t sofen : 1; - uint8_t _rsvd3 : 2; - uint8_t hostmodeen : 1; - uint8_t reset : 1; - uint8_t token_busy : 1; - uint8_t _rsvd4 : 2; - }; - struct /* device */ { - uint8_t usben : 1; - uint8_t _rsvd5 : 4; - uint8_t txd_suspend : 1; - uint8_t _rsvd6 : 2; - }; - uint8_t raw; - }; + union { + struct /* common */ { + uint8_t _rsvd1 : 1; + uint8_t oddrst : 1; + uint8_t resume : 1; + uint8_t _rsvd2 : 3; + uint8_t se0 : 1; + uint8_t jstate : 1; + }; + struct /* host */ { + uint8_t sofen : 1; + uint8_t _rsvd3 : 2; + uint8_t hostmodeen : 1; + uint8_t reset : 1; + uint8_t token_busy : 1; + uint8_t _rsvd4 : 2; + }; + struct /* device */ { + uint8_t usben : 1; + uint8_t _rsvd5 : 4; + uint8_t txd_suspend : 1; + uint8_t _rsvd6 : 2; + }; + uint8_t raw; + }; }; CTASSERT_SIZE_BIT(struct USB_CTL_t, 8); struct USB_ADDR_t { - UNION_STRUCT_START(8); - uint8_t addr : 7; - uint8_t lsen : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t addr : 7; + uint8_t lsen : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_ADDR_t, 8); struct USB_TOKEN_t { - UNION_STRUCT_START(8); - uint8_t endpt : 4; - enum usb_tok_pid pid : 4; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t endpt : 4; + enum usb_tok_pid pid : 4; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_TOKEN_t, 8); struct USB_ENDPT_t { - UNION_STRUCT_START(8); - uint8_t ephshk : 1; - uint8_t epstall : 1; - uint8_t eptxen : 1; - uint8_t eprxen : 1; - uint8_t epctldis : 1; - uint8_t _rsvd0 : 1; - uint8_t retrydis : 1; - uint8_t hostwohub : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t ephshk : 1; + uint8_t epstall : 1; + uint8_t eptxen : 1; + uint8_t eprxen : 1; + uint8_t epctldis : 1; + uint8_t _rsvd0 : 1; + uint8_t retrydis : 1; + uint8_t hostwohub : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_ENDPT_t, 8); struct USB_USBCTRL_t { - UNION_STRUCT_START(8); - uint8_t _rsvd0 : 6; - uint8_t pde : 1; - uint8_t susp : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t _rsvd0 : 6; + uint8_t pde : 1; + uint8_t susp : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_USBCTRL_t, 8); struct USB_OBSERVE_t { - UNION_STRUCT_START(8); - uint8_t _rsvd0 : 4; - uint8_t dmpd : 1; - uint8_t _rsvd1 : 1; - uint8_t dppd : 1; - uint8_t dppu : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t _rsvd0 : 4; + uint8_t dmpd : 1; + uint8_t _rsvd1 : 1; + uint8_t dppd : 1; + uint8_t dppu : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_OBSERVE_t, 8); struct USB_CONTROL_t { - UNION_STRUCT_START(8); - uint8_t _rsvd0 : 4; - uint8_t dppullupnonotg : 1; - uint8_t _rsvd1 : 3; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t _rsvd0 : 4; + uint8_t dppullupnonotg : 1; + uint8_t _rsvd1 : 3; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_CONTROL_t, 8); struct USB_USBTRC0_t { - UNION_STRUCT_START(8); - uint8_t usb_resume_int : 1; - uint8_t sync_det : 1; - uint8_t _rsvd0 : 3; - uint8_t usbresmen : 1; - uint8_t _rsvd1 : 1; - uint8_t usbreset : 1; - UNION_STRUCT_END; + UNION_STRUCT_START(8); + uint8_t usb_resume_int : 1; + uint8_t sync_det : 1; + uint8_t _rsvd0 : 3; + uint8_t usbresmen : 1; + uint8_t _rsvd1 : 1; + uint8_t usbreset : 1; + UNION_STRUCT_END; }; CTASSERT_SIZE_BIT(struct USB_USBTRC0_t, 8); struct USB_t { - uint8_t perid; - uint8_t _pad0[3]; - uint8_t idcomp; - uint8_t _pad1[3]; - uint8_t rev; - uint8_t _pad2[3]; - struct USB_ADDINFO_t addinfo; - uint8_t _pad3[3]; - struct USB_OTGSTAT_t otgistat; - uint8_t _pad4[3]; - struct USB_OTGSTAT_t otgicr; - uint8_t _pad5[3]; - struct USB_OTGSTAT_t otgstat; - uint8_t _pad6[3]; - struct USB_OTGCTL_t otgctl; - uint8_t _pad7[3]; - uint8_t _pad8[0x80 - 0x20]; - struct USB_ISTAT_t istat; - uint8_t _pad9[3]; - struct USB_ISTAT_t inten; - uint8_t _pad10[3]; - struct USB_ERRSTAT_t errstat; - uint8_t _pad11[3]; - struct USB_ERRSTAT_t erren; - uint8_t _pad12[3]; - struct USB_STAT_t stat; - uint8_t _pad13[3]; - struct USB_CTL_t ctl; - uint8_t _pad14[3]; - struct USB_ADDR_t addr; - uint8_t _pad15[3]; - uint8_t bdtpage1; - uint8_t _pad16[3]; - uint8_t frmnuml; - uint8_t _pad17[3]; - struct { - uint8_t frmnumh : 3; - uint8_t _rsvd0 : 5; - }; - uint8_t _pad18[3]; - struct USB_TOKEN_t token; - uint8_t _pad19[3]; - uint8_t softhld; - uint8_t _pad20[3]; - uint8_t bdtpage2; - uint8_t _pad21[3]; - uint8_t bdtpage3; - uint8_t _pad22[3]; - uint8_t _pad23[0xc0 - 0xb8]; - struct { - struct USB_ENDPT_t; - uint8_t _pad24[3]; - } endpt[16]; - struct USB_USBCTRL_t usbctrl; - uint8_t _pad25[3]; - struct USB_OBSERVE_t observe; - uint8_t _pad26[3]; - struct USB_CONTROL_t control; - uint8_t _pad27[3]; - struct USB_USBTRC0_t usbtrc0; - uint8_t _pad28[3]; - uint8_t _pad29[4]; - uint8_t usbfrmadjust; - uint8_t _pad30[3]; + uint8_t perid; + uint8_t _pad0[3]; + uint8_t idcomp; + uint8_t _pad1[3]; + uint8_t rev; + uint8_t _pad2[3]; + struct USB_ADDINFO_t addinfo; + uint8_t _pad3[3]; + struct USB_OTGSTAT_t otgistat; + uint8_t _pad4[3]; + struct USB_OTGSTAT_t otgicr; + uint8_t _pad5[3]; + struct USB_OTGSTAT_t otgstat; + uint8_t _pad6[3]; + struct USB_OTGCTL_t otgctl; + uint8_t _pad7[3]; + uint8_t _pad8[0x80 - 0x20]; + struct USB_ISTAT_t istat; + uint8_t _pad9[3]; + struct USB_ISTAT_t inten; + uint8_t _pad10[3]; + struct USB_ERRSTAT_t errstat; + uint8_t _pad11[3]; + struct USB_ERRSTAT_t erren; + uint8_t _pad12[3]; + struct USB_STAT_t stat; + uint8_t _pad13[3]; + struct USB_CTL_t ctl; + uint8_t _pad14[3]; + struct USB_ADDR_t addr; + uint8_t _pad15[3]; + uint8_t bdtpage1; + uint8_t _pad16[3]; + uint8_t frmnuml; + uint8_t _pad17[3]; + struct { + uint8_t frmnumh : 3; + uint8_t _rsvd0 : 5; + }; + uint8_t _pad18[3]; + struct USB_TOKEN_t token; + uint8_t _pad19[3]; + uint8_t softhld; + uint8_t _pad20[3]; + uint8_t bdtpage2; + uint8_t _pad21[3]; + uint8_t bdtpage3; + uint8_t _pad22[3]; + uint8_t _pad23[0xc0 - 0xb8]; + struct { + struct USB_ENDPT_t; + uint8_t _pad24[3]; + } endpt[16]; + struct USB_USBCTRL_t usbctrl; + uint8_t _pad25[3]; + struct USB_OBSERVE_t observe; + uint8_t _pad26[3]; + struct USB_CONTROL_t control; + uint8_t _pad27[3]; + struct USB_USBTRC0_t usbtrc0; + uint8_t _pad28[3]; + uint8_t _pad29[4]; + uint8_t usbfrmadjust; + uint8_t _pad30[3]; }; CTASSERT_SIZE_BYTE(struct USB_t, 0x118); struct USB_BD_t { - struct USB_BD_BITS_t { - union { - struct { - uint32_t _rsvd0 : 2; - uint32_t stall : 1; - uint32_t dts : 1; - uint32_t ninc : 1; - uint32_t keep : 1; - enum usb_data01 data01 : 1; - uint32_t own : 1; - uint32_t _rsvd1 : 8; - uint32_t bc : 10; - uint32_t _rsvd2 : 6; - }; - struct /* processor */ { - uint32_t _rsvd5 : 2; - enum usb_tok_pid tok_pid : 4; - uint32_t _rsvd6 : 26; - }; - uint32_t raw; - }; - }; - void *addr; + struct USB_BD_BITS_t { + union { + struct { + uint32_t _rsvd0 : 2; + uint32_t stall : 1; + uint32_t dts : 1; + uint32_t ninc : 1; + uint32_t keep : 1; + enum usb_data01 data01 : 1; + uint32_t own : 1; + uint32_t _rsvd1 : 8; + uint32_t bc : 10; + uint32_t _rsvd2 : 6; + }; + struct /* processor */ { + uint32_t _rsvd5 : 2; + enum usb_tok_pid tok_pid : 4; + uint32_t _rsvd6 : 26; + }; + uint32_t raw; + }; + }; + void *addr; }; CTASSERT_SIZE_BYTE(struct USB_BD_t, 8); diff -r 05a09eda53fb -r f7b14e25ca5b CMakeLists.txt --- a/CMakeLists.txt Sun Mar 08 22:35:55 2015 -0700 +++ b/CMakeLists.txt Fri Jun 12 18:31:55 2015 -0700 @@ -17,14 +17,14 @@ #| You _MUST_ clean the build directory if you change this value #| set( CHIP -# "at90usb162" # Teensy 1.0 (avr) -# "atmega32u4" # Teensy 2.0 (avr) -# "at90usb646" # Teensy++ 1.0 (avr) -# "at90usb1286" # Teensy++ 2.0 (avr) -# "mk20dx128" # Teensy 3.0 (arm) +# "at90usb162" # Teensy 1.0 (avr) +# "atmega32u4" # Teensy 2.0 (avr) +# "at90usb646" # Teensy++ 1.0 (avr) +# "at90usb1286" # Teensy++ 2.0 (avr) +# "mk20dx128" # Teensy 3.0 (arm) "mk20dx128vlf5" # McHCK mk20dx128vlf5 -# "mk20dx256" # Teensy 3.1 (arm) -# "mk20dx256vlh7" # Kiibohd-dfu mk20dx256vlh7 +# "mk20dx256" # Teensy 3.1 (arm) +# "mk20dx256vlh7" # Kiibohd-dfu mk20dx256vlh7 CACHE STRING "Microcontroller Chip" ) @@ -37,8 +37,8 @@ #| 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" ) diff -r 05a09eda53fb -r f7b14e25ca5b Debug/cli/cli.c --- a/Debug/cli/cli.c Sun Mar 08 22:35:55 2015 -0700 +++ b/Debug/cli/cli.c Fri Jun 12 18:31:55 2015 -0700 @@ -35,6 +35,7 @@ // ----- Variables ----- // Basic command dictionary +CLIDict_Entry( clear, "Clear the screen."); CLIDict_Entry( cliDebug, "Enables/Disables hex output of the most recent cli input." ); CLIDict_Entry( help, "You're looking at it :P" ); CLIDict_Entry( led, "Enables/Disables indicator LED. Try a couple times just in case the LED is in an odd state.\r\n\t\t\033[33mWarning\033[0m: May adversely affect some modules..." ); @@ -44,6 +45,7 @@ CLIDict_Entry( version, "Version information about this firmware." ); CLIDict_Def( basicCLIDict, "General Commands" ) = { + CLIDict_Item( clear ), CLIDict_Item( cliDebug ), CLIDict_Item( help ), CLIDict_Item( led ), @@ -70,6 +72,11 @@ // Reset the Line Buffer CLILineBufferCurrent = 0; + // History starts empty + CLIHistoryHead = 0; + CLIHistoryCurrent = 0; + CLIHistoryTail = 0; + // Set prompt prompt(); @@ -152,6 +159,22 @@ // Process the current line buffer CLI_commandLookup(); + // Add the command to the history + CLI_saveHistory( CLILineBuffer ); + + // Keep the array circular, discarding the older entries + if ( CLIHistoryTail < CLIHistoryHead ) + CLIHistoryHead = ( CLIHistoryHead + 1 ) % CLIMaxHistorySize; + CLIHistoryTail++; + if ( CLIHistoryTail == CLIMaxHistorySize ) + { + CLIHistoryTail = 0; + CLIHistoryHead = 1; + } + + CLIHistoryCurrent = CLIHistoryTail; // 'Up' starts at the last item + CLI_saveHistory( NULL ); // delete the old temp buffer + // Reset the buffer CLILineBufferCurrent = 0; @@ -173,9 +196,38 @@ // Doesn't look like it will happen *that* often, so not handling it for now -HaaTa return; - case 0x1B: // Esc - // Check for escape sequence - // TODO + case 0x1B: // Esc / Escape codes + // Check for other escape sequence + + // \e[ is an escape code in vt100 compatable terminals + if ( CLILineBufferCurrent >= prev_buf_pos + 3 + && CLILineBuffer[ prev_buf_pos ] == 0x1B + && CLILineBuffer[ prev_buf_pos + 1] == 0x5B ) + { + // Arrow Keys: A (0x41) = Up, B (0x42) = Down, C (0x43) = Right, D (0x44) = Left + + if ( CLILineBuffer[ prev_buf_pos + 2 ] == 0x41 ) // Hist prev + { + if ( CLIHistoryCurrent == CLIHistoryTail ) + { + // Is first time pressing arrow. Save the current buffer + CLILineBuffer[ prev_buf_pos ] = '\0'; + CLI_saveHistory( CLILineBuffer ); + } + + // Grab the previus item from the history if there is one + if ( RING_PREV( CLIHistoryCurrent ) != RING_PREV( CLIHistoryHead ) ) + CLIHistoryCurrent = RING_PREV( CLIHistoryCurrent ); + CLI_retreiveHistory( CLIHistoryCurrent ); + } + if ( CLILineBuffer[ prev_buf_pos + 2 ] == 0x42 ) // Hist next + { + // Grab the next item from the history if it exists + if ( RING_NEXT( CLIHistoryCurrent ) != RING_NEXT( CLIHistoryTail ) ) + CLIHistoryCurrent = RING_NEXT( CLIHistoryCurrent ); + CLI_retreiveHistory( CLIHistoryCurrent ); + } + } return; case 0x08: @@ -346,10 +398,63 @@ } } +inline int CLI_wrap( int kX, int const kLowerBound, int const kUpperBound ) +{ + int range_size = kUpperBound - kLowerBound + 1; + + if ( kX < kLowerBound ) + kX += range_size * ((kLowerBound - kX) / range_size + 1); + + return kLowerBound + (kX - kLowerBound) % range_size; +} + +inline void CLI_saveHistory( char *buff ) +{ + if ( buff == NULL ) + { + //clear the item + CLIHistoryBuffer[ CLIHistoryTail ][ 0 ] = '\0'; + return; + } + + // Copy the line to the history + int i; + for (i = 0; i < CLILineBufferCurrent; i++) + { + CLIHistoryBuffer[ CLIHistoryTail ][ i ] = CLILineBuffer[ i ]; + } +} + +void CLI_retreiveHistory( int index ) +{ + char *histMatch = CLIHistoryBuffer[ index ]; + + // Reset the buffer + CLILineBufferCurrent = 0; + + // Reprint the prompt (automatically clears the line) + prompt(); + + // Display the command + dPrint( histMatch ); + + // There are no index counts, so just copy the whole string to the input buffe + CLILineBufferCurrent = 0; + while ( *histMatch != '\0' ) + { + CLILineBuffer[ CLILineBufferCurrent++ ] = *histMatch++; + } +} + // ----- CLI Command Functions ----- +void cliFunc_clear( char* args) +{ + print("\033[2J\033[H\r"); // Erases the whole screen +} + void cliFunc_cliDebug( char* args ) { // Toggle Hex Debug Mode diff -r 05a09eda53fb -r f7b14e25ca5b Debug/cli/cli.h --- a/Debug/cli/cli.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Debug/cli/cli.h Fri Jun 12 18:31:55 2015 -0700 @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 by Jacob Alexander +/* Copyright (C) 2014-2015 by Jacob Alexander * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,9 +34,9 @@ // ----- Defines ----- #define CLILineBufferMaxSize 100 -#define CLIMaxDictionaries 5 +#define CLIMaxDictionaries 10 #define CLIEntryTabAlign 13 - +#define CLIMaxHistorySize 10 // ----- Macros ----- @@ -67,6 +67,8 @@ const char name##CLIDict_DescEntry[] = description; #endif +#define RING_PREV(i) CLI_wrap(i - 1, 0, CLIMaxHistorySize - 1) +#define RING_NEXT(i) CLI_wrap(i + 1, 0, CLIMaxHistorySize - 1) // ----- Structs ----- @@ -90,6 +92,13 @@ char* CLIDictNames[CLIMaxDictionaries]; uint8_t CLIDictionariesUsed; +// History +char CLIHistoryBuffer[CLIMaxHistorySize][CLILineBufferMaxSize]; +uint8_t CLIHistoryHead; +uint8_t CLIHistoryTail; +int8_t CLIHistoryCurrent; + +// Debug uint8_t CLILEDState; uint8_t CLIHexDebugMode; @@ -102,12 +111,16 @@ void CLI_registerDictionary( const CLIDictItem *cmdDict, const char* dictName ); void CLI_argumentIsolation( char* string, char** first, char** second ); +int CLI_wrap( int x, int low, int high ); void CLI_commandLookup(); void CLI_tabCompletion(); +void CLI_saveHistory( char *buff ); +void CLI_retreiveHistory( int index ); // CLI Command Functions void cliFunc_arch ( char* args ); void cliFunc_chip ( char* args ); +void cliFunc_clear ( char* args ); void cliFunc_cliDebug( char* args ); void cliFunc_device ( char* args ); void cliFunc_help ( char* args ); diff -r 05a09eda53fb -r f7b14e25ca5b Debug/print/print.h --- a/Debug/print/print.h Sun Mar 08 22:35:55 2015 -0700 +++ b/Debug/print/print.h Fri Jun 12 18:31:55 2015 -0700 @@ -54,11 +54,11 @@ // Special Msg Constructs (Uses VT100 tags) #define dPrintMsg(colour_code_str,msg,...) \ - printstrs("\033[", colour_code_str, "m", msg, "\033[0m - ", __VA_ARGS__, NL, "\0\0\0") + printstrs("\033[", colour_code_str, "m", msg, "\033[0m - ", __VA_ARGS__, NL, "\0\0\0") #define printMsgNL(colour_code_str,msg,str) \ - print("\033[" colour_code_str "m" msg "\033[0m - " str NL) + print("\033[" colour_code_str "m" msg "\033[0m - " str NL) #define printMsg(colour_code_str,msg,str) \ - print("\033[" colour_code_str "m" msg "\033[0m - " str) + print("\033[" colour_code_str "m" msg "\033[0m - " str) // Info Messages #define info_dPrint(...) dPrintMsg ("1;32", "INFO", __VA_ARGS__) // Info Msg diff -r 05a09eda53fb -r f7b14e25ca5b Keyboards/README.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Keyboards/README.markdown Fri Jun 12 18:31:55 2015 -0700 @@ -0,0 +1,37 @@ +Keyboard Compiler Scripts +========================= + +Scripts for major keyboards designed using the Kiibohd firmware. +Please refer to `