1 /**
2   ******************************************************************************
3   * @file    usbd_dfu.h
4   * @author  MCD Application Team
5   * @version V2.4.2
6   * @date    11-December-2015
7   * @brief   Header file for the usbd_dfu.c file.
8   ******************************************************************************
9   * @attention
10   *
11   * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
12   *
13   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14   * You may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at:
16   *
17   *        http://www.st.com/software_license_agreement_liberty_v2
18   *
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   *
25   ******************************************************************************
26   */
27 
28 /* Define to prevent recursive inclusion -------------------------------------*/
29 #ifndef __USB_DFU_H
30 #define __USB_DFU_H
31 
32 #ifdef __cplusplus
33  extern "C" {
34 #endif
35 
36 /* Includes ------------------------------------------------------------------*/
37 #include  "usbd_ioreq.h"
38 
39 /** @addtogroup STM32_USB_DEVICE_LIBRARY
40   * @{
41   */
42 
43 /** @defgroup USBD_DFU
44   * @brief This file is the Header file for usbd_dfu.c
45   * @{
46   */
47 
48 
49 /** @defgroup USBD_DFU_Exported_Defines
50   * @{
51   */
52 
53 #define USB_DFU_CONFIG_DESC_SIZ        (18 + (9 * USBD_DFU_MAX_ITF_NUM))
54 #define USB_DFU_DESC_SIZ               9
55 
56 #define DFU_DESCRIPTOR_TYPE            0x21
57 
58 
59 /**************************************************/
60 /* DFU Requests  DFU states                       */
61 /**************************************************/
62 #define APP_STATE_IDLE                 0
63 #define APP_STATE_DETACH               1
64 #define DFU_STATE_IDLE                 2
65 #define DFU_STATE_DNLOAD_SYNC          3
66 #define DFU_STATE_DNLOAD_BUSY          4
67 #define DFU_STATE_DNLOAD_IDLE          5
68 #define DFU_STATE_MANIFEST_SYNC        6
69 #define DFU_STATE_MANIFEST             7
70 #define DFU_STATE_MANIFEST_WAIT_RESET  8
71 #define DFU_STATE_UPLOAD_IDLE          9
72 #define DFU_STATE_ERROR                10
73 
74 /**************************************************/
75 /* DFU errors                                     */
76 /**************************************************/
77 #define DFU_ERROR_NONE                 0x00
78 #define DFU_ERROR_TARGET               0x01
79 #define DFU_ERROR_FILE                 0x02
80 #define DFU_ERROR_WRITE                0x03
81 #define DFU_ERROR_ERASE                0x04
82 #define DFU_ERROR_CHECK_ERASED         0x05
83 #define DFU_ERROR_PROG                 0x06
84 #define DFU_ERROR_VERIFY               0x07
85 #define DFU_ERROR_ADDRESS              0x08
86 #define DFU_ERROR_NOTDONE              0x09
87 #define DFU_ERROR_FIRMWARE             0x0A
88 #define DFU_ERROR_VENDOR               0x0B
89 #define DFU_ERROR_USB                  0x0C
90 #define DFU_ERROR_POR                  0x0D
91 #define DFU_ERROR_UNKNOWN              0x0E
92 #define DFU_ERROR_STALLEDPKT           0x0F
93 
94 /**************************************************/
95 /* DFU Manifestation State                        */
96 /**************************************************/
97 #define DFU_MANIFEST_COMPLETE          0x00
98 #define DFU_MANIFEST_IN_PROGRESS       0x01
99 
100 
101 /**************************************************/
102 /* Special Commands  with Download Request        */
103 /**************************************************/
104 #define DFU_CMD_GETCOMMANDS            0x00
105 #define DFU_CMD_SETADDRESSPOINTER      0x21
106 #define DFU_CMD_ERASE                  0x41
107 
108 #define DFU_MEDIA_ERASE                0x00
109 #define DFU_MEDIA_PROGRAM              0x01
110 
111 /**************************************************/
112 /* Other defines                                  */
113 /**************************************************/
114 /* Bit Detach capable = bit 3 in bmAttributes field */
115 #define DFU_DETACH_MASK                (uint8_t)(1 << 4)
116 #define DFU_STATUS_DEPTH               (6)
117 
118 typedef enum
119 {
120   DFU_DETACH = 0,
121   DFU_DNLOAD ,
122   DFU_UPLOAD,
123   DFU_GETSTATUS,
124   DFU_CLRSTATUS,
125   DFU_GETSTATE,
126   DFU_ABORT
127 } DFU_RequestTypeDef;
128 
129 typedef  void (*pFunction)(void);
130 
131 
132 /**********  Descriptor of DFU interface 0 Alternate setting n ****************/
133 #define USBD_DFU_IF_DESC(n)            0x09,   /* bLength: Interface Descriptor size */ \
134                                       USB_DESC_TYPE_INTERFACE,   /* bDescriptorType */ \
135                                       0x00,   /* bInterfaceNumber: Number of Interface */ \
136                                       (n),      /* bAlternateSetting: Alternate setting */ \
137                                       0x00,   /* bNumEndpoints*/ \
138                                       0xFE,   /* bInterfaceClass: Application Specific Class Code */ \
139                                       0x01,   /* bInterfaceSubClass : Device Firmware Upgrade Code */ \
140                                       0x02,   /* nInterfaceProtocol: DFU mode protocol */ \
141                                       USBD_IDX_INTERFACE_STR + (n) + 1 /* iInterface: Index of string descriptor */ \
142 
143 #define TRANSFER_SIZE_BYTES(size)      ((uint8_t)(size)), /* XFERSIZEB0 */\
144                                        ((uint8_t)(size >> 8)) /* XFERSIZEB1 */
145 
146 #define IS_PROTECTED_AREA(add)         (uint8_t)(((add >= 0x08000000) && (add < (APP_DEFAULT_ADD)))? 1:0)
147 
148 /**
149   * @}
150   */
151 
152 
153 /** @defgroup USBD_CORE_Exported_TypesDefinitions
154   * @{
155   */
156 
157 typedef struct
158 {
159   union
160   {
161     uint32_t d32[USBD_DFU_XFER_SIZE/4];
162     uint8_t  d8[USBD_DFU_XFER_SIZE];
163   }buffer;
164 
165   uint8_t              dev_state;
166   uint8_t              dev_status[DFU_STATUS_DEPTH];
167   uint8_t              manif_state;
168 
169   uint32_t             wblock_num;
170   uint32_t             wlength;
171   uint32_t             data_ptr;
172   __IO uint32_t        alt_setting;
173 
174 }
175 USBD_DFU_HandleTypeDef;
176 
177 
178 typedef struct
179 {
180   const uint8_t* pStrDesc;
181   uint16_t (* Init)     (void);
182   uint16_t (* DeInit)   (void);
183   uint16_t (* Erase)    (uint32_t Add);
184   uint16_t (* Write)    (uint8_t *src, uint8_t *dest, uint32_t Len);
185   uint8_t* (* Read)     (uint8_t *src, uint8_t *dest, uint32_t Len);
186   uint16_t (* GetStatus)(uint32_t Add, uint8_t cmd, uint8_t *buff);
187 }
188 USBD_DFU_MediaTypeDef;
189 /**
190   * @}
191   */
192 
193 
194 
195 /** @defgroup USBD_CORE_Exported_Macros
196   * @{
197   */
198 
199 /**
200   * @}
201   */
202 
203 /** @defgroup USBD_CORE_Exported_Variables
204   * @{
205   */
206 
207 extern USBD_ClassTypeDef  USBD_DFU;
208 #define USBD_DFU_CLASS    &USBD_DFU
209 /**
210   * @}
211   */
212 
213 /** @defgroup USB_CORE_Exported_Functions
214   * @{
215   */
216 uint8_t  USBD_DFU_RegisterMedia    (USBD_HandleTypeDef   *pdev,
217                                     USBD_DFU_MediaTypeDef *fops);
218 /**
219   * @}
220   */
221 
222 #ifdef __cplusplus
223 }
224 #endif
225 
226 #endif  /* __USB_DFU_H */
227 /**
228   * @}
229   */
230 
231 /**
232   * @}
233   */
234 
235 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
236