1 /*!
2     \file  dfu_core.h
3     \brief the header file of USB DFU device class core functions
4 
5     \version 2020-08-04, V1.1.0, firmware for GD32VF103
6 */
7 
8 /*
9     Copyright (c) 2020, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef __DFU_CORE_H
36 #define __DFU_CORE_H
37 
38 #include "usbd_enum.h"
39 
40 /* DFU class code */
41 #define USB_DFU_CLASS                 0xFEU
42 
43 /* DFU subclass code */
44 #define USB_DFU_SUBCLASS_UPGRADE      0x01U
45 
46 /* DFU protocl code */
47 #define USB_DFU_PROTOCL_RUNTIME       0x01U
48 #define USB_DFU_PROTOCL_DFU           0x02U
49 
50 /* manifestation state */
51 #define MANIFEST_COMPLETE             0x00U
52 #define MANIFEST_IN_PROGRESS          0x01U
53 
54 /* DFU attributes code */
55 #define USB_DFU_CAN_DOWNLOAD          0x01U
56 #define USB_DFU_CAN_UPLOAD            0x02U
57 #define USB_DFU_MANIFEST_TOLERANT     0x04U
58 #define USB_DFU_WILL_DETACH           0x08U
59 
60 /* special commands with download request */
61 #define GET_COMMANDS                  0x00U
62 #define SET_ADDRESS_POINTER           0x21U
63 #define ERASE                         0x41U
64 
65 /* memory operation command */
66 #define CMD_ERASE                     0U
67 #define CMD_WRITE                     1U
68 
69 #define _BYTE1(x)                     (uint8_t)((x) & 0xFFU)                /*!< addressing cycle 1st byte */
70 #define _BYTE2(x)                     (uint8_t)(((x) & 0xFF00U) >> 8U)      /*!< addressing cycle 2nd byte */
71 #define _BYTE3(x)                     (uint8_t)(((x) & 0xFF0000U) >> 16U)   /*!< addressing cycle 3rd byte */
72 
73 #define FLASH_ERASE_TIMEOUT           60U
74 #define FLASH_WRITE_TIMEOUT           80U
75 
76 /* bit detach capable = bit 3 in bmAttributes field */
77 #define DFU_DETACH_MASK               (uint8_t)(0x10U)
78 
79 #define USB_SERIAL_STR_LEN            0x06U
80 
81 #define USB_DFU_CONFIG_DESC_SIZE      27U
82 
83 #define DFU_DESC_TYPE                 0x21U
84 
85 /* DFU device state defines */
86 typedef enum {
87     STATE_APP_IDLE = 0x00U,
88     STATE_APP_DETACH,
89     STATE_DFU_IDLE,
90     STATE_DFU_DNLOAD_SYNC,
91     STATE_DFU_DNBUSY,
92     STATE_DFU_DNLOAD_IDLE,
93     STATE_DFU_MANIFEST_SYNC,
94     STATE_DFU_MANIFEST,
95     STATE_DFU_MANIFEST_WAIT_RESET,
96     STATE_DFU_UPLOAD_IDLE,
97     STATE_DFU_ERROR
98 } dfu_state;
99 
100 /* DFU device status defines */
101 typedef enum {
102     STATUS_OK = 0x00U,
103     STATUS_ERR_TARGET,
104     STATUS_ERR_FILE,
105     STATUS_ERR_WRITE,
106     STATUS_ERR_ERASE,
107     STATUS_ERR_CHECK_ERASED,
108     STATUS_ERR_PROG,
109     STATUS_ERR_VERIFY,
110     STATUS_ERR_ADDRESS,
111     STATUS_ERR_NOTDONE,
112     STATUS_ERR_FIRMWARE,
113     STATUS_ERR_VENDOR,
114     STATUS_ERR_USBR,
115     STATUS_ERR_POR,
116     STATUS_ERR_UNKNOWN,
117     STATUS_ERR_STALLEDPKT
118 } dfu_status;
119 
120 /* DFU class-specific requests */
121 typedef enum {
122     DFU_DETACH = 0U,
123     DFU_DNLOAD,
124     DFU_UPLOAD,
125     DFU_GETSTATUS,
126     DFU_CLRSTATUS,
127     DFU_GETSTATE,
128     DFU_ABORT,
129     DFU_REQ_MAX
130 } dfu_requests;
131 
132 #pragma pack(1)
133 
134 /* USB dfu function descriptor structure */
135 typedef struct
136 {
137     usb_desc_header header;               /*!< descriptor header, including type and size */
138     uint8_t bmAttributes;                 /*!< DFU attributes */
139     uint16_t wDetachTimeOut;              /*!< time, in milliseconds, that the device will wait after receipt of the DFU_DETACH request. If */
140     uint16_t wTransferSize;               /*!< maximum number of bytes that the device can accept per control-write transaction */
141     uint16_t bcdDFUVersion;               /*!< numeric expression identifying the version of the DFU Specification release. */
142 } usb_desc_dfu_func;
143 
144 #pragma pack()
145 
146 /* USB configuration descriptor structure */
147 typedef struct
148 {
149     usb_desc_config           config;
150     usb_desc_itf              dfu_itf;
151     usb_desc_dfu_func         dfu_func;
152 } usb_dfu_desc_config_set;
153 
154 typedef struct
155 {
156     uint8_t bStatus;
157     uint8_t bwPollTimeout0;
158     uint8_t bwPollTimeout1;
159     uint8_t bwPollTimeout2;
160     uint8_t bState;
161     uint8_t iString;
162 
163     uint8_t manifest_state;
164     uint32_t data_len;
165     uint16_t block_num;
166     uint32_t base_addr;
167 
168     uint8_t buf[TRANSFER_SIZE];
169 } usbd_dfu_handler;
170 
171 typedef  void  (*app_func) (void);
172 
173 extern usb_desc dfu_desc;
174 extern usb_class_core dfu_class;
175 
176 #endif  /* DFU_CORE_H */
177