1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 // ----- Compiler Includes -----
23 #include <sys/types.h>
27 // ----- Defines -----
29 #define USB_FUNCTION_DFU_IFACE_COUNT 1
32 #ifndef USB_DFU_TRANSFER_SIZE
33 // Sector size is the same as the program flash size
34 #if defined(_mk20dx128vlf5_)
35 #define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE
37 // Sector size is double the program flash size
38 #elif defined(_mk20dx256vlh7_ )
39 #define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE / 2
44 #define USB_FUNCTION_DESC_DFU_DECL \
45 struct dfu_function_desc
47 #define USB_FUNCTION_DFU_IFACE_COUNT 1
48 #define USB_FUNCTION_DFU_RX_EP_COUNT 0
49 #define USB_FUNCTION_DFU_TX_EP_COUNT 0
53 // ----- Enumerations -----
55 enum dfu_dev_subclass {
56 USB_DEV_SUBCLASS_APP_DFU = 0x01
60 USB_DEV_PROTO_DFU_APP = 0x01,
61 USB_DEV_PROTO_DFU_DFU = 0x02
64 enum dfu_ctrl_req_code {
65 USB_CTRL_REQ_DFU_DETACH = 0,
66 USB_CTRL_REQ_DFU_DNLOAD = 1,
67 USB_CTRL_REQ_DFU_UPLOAD = 2,
68 USB_CTRL_REQ_DFU_GETSTATUS = 3,
69 USB_CTRL_REQ_DFU_CLRSTATUS = 4,
70 USB_CTRL_REQ_DFU_GETSTATE = 5,
71 USB_CTRL_REQ_DFU_ABORT = 6
75 DFU_STATUS_async = 0xff,
77 DFU_STATUS_errTARGET = 0x01,
78 DFU_STATUS_errFILE = 0x02,
79 DFU_STATUS_errWRITE = 0x03,
80 DFU_STATUS_errERASE = 0x04,
81 DFU_STATUS_errCHECK_ERASED = 0x05,
82 DFU_STATUS_errPROG = 0x06,
83 DFU_STATUS_errVERIFY = 0x07,
84 DFU_STATUS_errADDRESS = 0x08,
85 DFU_STATUS_errNOTDONE = 0x09,
86 DFU_STATUS_errFIRMWARE = 0x0a,
87 DFU_STATUS_errVENDOR = 0x0b,
88 DFU_STATUS_errUSBR = 0x0c,
89 DFU_STATUS_errPOR = 0x0d,
90 DFU_STATUS_errUNKNOWN = 0x0e,
91 DFU_STATUS_errSTALLEDPKT = 0x0f
95 DFU_STATE_appIDLE = 0,
96 DFU_STATE_appDETACH = 1,
97 DFU_STATE_dfuIDLE = 2,
98 DFU_STATE_dfuDNLOAD_SYNC = 3,
99 DFU_STATE_dfuDNBUSY = 4,
100 DFU_STATE_dfuDNLOAD_IDLE = 5,
101 DFU_STATE_dfuMANIFEST_SYNC = 6,
102 DFU_STATE_dfuMANIFEST = 7,
103 DFU_STATE_dfuMANIFEST_WAIT_RESET = 8,
104 DFU_STATE_dfuUPLOAD_IDLE = 9,
105 DFU_STATE_dfuERROR = 10
110 // ----- Structs -----
112 struct dfu_status_t {
113 enum dfu_status bStatus : 8;
114 uint32_t bwPollTimeout : 24;
115 enum dfu_state bState : 8;
118 CTASSERT_SIZE_BYTE(struct dfu_status_t, 6);
121 typedef enum dfu_status (*dfu_setup_read_t)(size_t off, size_t *len, void **buf);
122 typedef enum dfu_status (*dfu_setup_write_t)(size_t off, size_t len, void **buf);
123 typedef enum dfu_status (*dfu_finish_write_t)(void *, size_t off, size_t len);
124 typedef void (*dfu_detach_t)(void);
127 struct usbd_function_ctx_header header;
128 enum dfu_state state;
129 enum dfu_status status;
130 dfu_setup_read_t setup_read;
131 dfu_setup_write_t setup_write;
132 dfu_finish_write_t finish_write;
138 struct dfu_desc_functional {
140 struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */
143 uint8_t can_download : 1;
144 uint8_t can_upload : 1;
145 uint8_t manifestation_tolerant : 1;
146 uint8_t will_detach : 1;
149 uint8_t bmAttributes;
151 uint16_t wDetachTimeOut;
152 uint16_t wTransferSize;
153 struct usb_bcd_t bcdDFUVersion;
155 CTASSERT_SIZE_BYTE(struct dfu_desc_functional, 9);
157 struct dfu_function_desc {
158 struct usb_desc_iface_t iface;
159 struct dfu_desc_functional dfu;
163 extern const struct usbd_function dfu_function;
164 extern const struct usbd_function dfu_app_function;
168 // ----- Functions -----
170 void dfu_write_done( enum dfu_status, struct dfu_ctx *ctx );
171 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 );
172 void dfu_app_init( dfu_detach_t detachcb );