]> mj.ucw.cz Git - home-hw.git/blob - Src/usbd_dfu_if.c
Experiments with USBD
[home-hw.git] / Src / usbd_dfu_if.c
1 /**
2   ******************************************************************************
3   * @file           : usbd_dfu_if.c
4   * @brief          : Usb device for Download Firmware Update.
5   ******************************************************************************
6   * This notice applies to any and all portions of this file
7   * that are not between comment pairs USER CODE BEGIN and
8   * USER CODE END. Other portions of this file, whether 
9   * inserted by the user or by software development tools
10   * are owned by their respective copyright owners.
11   *
12   * Copyright (c) 2018 STMicroelectronics International N.V. 
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or without 
16   * modification, are permitted, provided that the following conditions are met:
17   *
18   * 1. Redistribution of source code must retain the above copyright notice, 
19   *    this list of conditions and the following disclaimer.
20   * 2. Redistributions in binary form must reproduce the above copyright notice,
21   *    this list of conditions and the following disclaimer in the documentation
22   *    and/or other materials provided with the distribution.
23   * 3. Neither the name of STMicroelectronics nor the names of other 
24   *    contributors to this software may be used to endorse or promote products 
25   *    derived from this software without specific written permission.
26   * 4. This software, including modifications and/or derivative works of this 
27   *    software, must execute solely and exclusively on microcontroller or
28   *    microprocessor devices manufactured by or for STMicroelectronics.
29   * 5. Redistribution and use of this software other than as permitted under 
30   *    this license is void and will automatically terminate your rights under 
31   *    this license. 
32   *
33   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
34   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
35   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
36   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
37   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
38   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
41   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
42   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
43   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
44   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   *
46   ******************************************************************************
47   */
48
49 /* Includes ------------------------------------------------------------------*/
50 #include "usbd_dfu_if.h"
51
52 /* USER CODE BEGIN INCLUDE */
53
54 /* USER CODE END INCLUDE */
55
56 /* Private typedef -----------------------------------------------------------*/
57 /* Private define ------------------------------------------------------------*/
58 /* Private macro -------------------------------------------------------------*/
59
60 /* USER CODE BEGIN PV */
61 /* Private variables ---------------------------------------------------------*/
62
63 /* USER CODE END PV */
64
65 /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
66   * @brief Usb device.
67   * @{
68   */
69
70 /** @defgroup USBD_DFU
71   * @brief Usb DFU device module.
72   * @{
73   */
74
75 /** @defgroup USBD_DFU_Private_TypesDefinitions
76   * @brief Private types.
77   * @{
78   */
79
80 /* USER CODE BEGIN PRIVATE_TYPES */
81
82 /* USER CODE END PRIVATE_TYPES */
83
84 /**
85   * @}
86   */
87
88 /** @defgroup USBD_DFU_Private_Defines
89   * @brief Private defines.
90   * @{
91   */
92
93 #define FLASH_DESC_STR      "@Internal Flash   /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg,04*016Kg,01*064Kg,07*128Kg"
94
95 /* USER CODE BEGIN PRIVATE_DEFINES */
96
97 /* USER CODE END PRIVATE_DEFINES */
98
99 /**
100   * @}
101   */
102
103 /** @defgroup USBD_DFU_Private_Macros
104   * @brief Private macros.
105   * @{
106   */
107
108 /* USER CODE BEGIN PRIVATE_MACRO */
109
110 /* USER CODE END PRIVATE_MACRO */
111
112 /**
113   * @}
114   */
115
116 /** @defgroup USBD_DFU_Private_Variables
117   * @brief Private variables.
118   * @{
119   */
120
121 /* USER CODE BEGIN PRIVATE_VARIABLES */
122
123 /* USER CODE END PRIVATE_VARIABLES */
124
125 /**
126   * @}
127   */
128
129 /** @defgroup USBD_DFU_Exported_Variables
130   * @brief Public variables.
131   * @{
132   */
133
134 extern USBD_HandleTypeDef hUsbDeviceFS;
135
136 /* USER CODE BEGIN EXPORTED_VARIABLES */
137
138 /* USER CODE END EXPORTED_VARIABLES */
139
140 /**
141   * @}
142   */
143
144 /** @defgroup USBD_DFU_Private_FunctionPrototypes
145   * @brief Private functions declaration.
146   * @{
147   */
148
149 static uint16_t MEM_If_Init_FS(void);
150 static uint16_t MEM_If_Erase_FS(uint32_t Add);
151 static uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len);
152 static uint8_t *MEM_If_Read_FS(uint8_t *src, uint8_t *dest, uint32_t Len);
153 static uint16_t MEM_If_DeInit_FS(void);
154 static uint16_t MEM_If_GetStatus_FS(uint32_t Add, uint8_t Cmd, uint8_t *buffer);
155
156 /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
157
158 /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
159
160 /**
161   * @}
162   */
163
164 #if defined ( __ICCARM__ ) /* IAR Compiler */
165   #pragma data_alignment=4
166 #endif
167 __ALIGN_BEGIN USBD_DFU_MediaTypeDef USBD_DFU_fops_FS __ALIGN_END =
168 {
169    (uint8_t*)FLASH_DESC_STR,
170     MEM_If_Init_FS,
171     MEM_If_DeInit_FS,
172     MEM_If_Erase_FS,
173     MEM_If_Write_FS,
174     MEM_If_Read_FS,
175     MEM_If_GetStatus_FS
176 };
177
178 /* Private functions ---------------------------------------------------------*/
179 /**
180   * @brief  Memory initialization routine.
181   * @retval USBD_OK if operation is successful, MAL_FAIL else.
182   */
183 uint16_t MEM_If_Init_FS(void)
184 {
185   /* USER CODE BEGIN 0 */
186   return (USBD_OK);
187   /* USER CODE END 0 */
188 }
189
190 /**
191   * @brief  De-Initializes Memory
192   * @retval USBD_OK if operation is successful, MAL_FAIL else
193   */
194 uint16_t MEM_If_DeInit_FS(void)
195 {
196   /* USER CODE BEGIN 1 */
197   return (USBD_OK);
198   /* USER CODE END 1 */
199 }
200
201 /**
202   * @brief  Erase sector.
203   * @param  Add: Address of sector to be erased.
204   * @retval 0 if operation is successful, MAL_FAIL else.
205   */
206 uint16_t MEM_If_Erase_FS(uint32_t Add)
207 {
208   /* USER CODE BEGIN 2 */
209
210   return (USBD_OK);
211   /* USER CODE END 2 */
212 }
213
214 /**
215   * @brief  Memory write routine.
216   * @param  src: Pointer to the source buffer. Address to be written to.
217   * @param  dest: Pointer to the destination buffer.
218   * @param  Len: Number of data to be written (in bytes).
219   * @retval USBD_OK if operation is successful, MAL_FAIL else.
220   */
221 uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
222 {
223   /* USER CODE BEGIN 3 */
224   return (USBD_OK);
225   /* USER CODE END 3 */
226 }
227
228 /**
229   * @brief  Memory read routine.
230   * @param  src: Pointer to the source buffer. Address to be written to.
231   * @param  dest: Pointer to the destination buffer.
232   * @param  Len: Number of data to be read (in bytes).
233   * @retval Pointer to the physical address where data should be read.
234   */
235 uint8_t *MEM_If_Read_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
236 {
237   /* Return a valid address to avoid HardFault */
238   /* USER CODE BEGIN 4 */
239   return (uint8_t*)(USBD_OK);
240   /* USER CODE END 4 */
241 }
242
243 /**
244   * @brief  Get status routine
245   * @param  Add: Address to be read from
246   * @param  Cmd: Number of data to be read (in bytes)
247   * @param  buffer: used for returning the time necessary for a program or an erase operation
248   * @retval USBD_OK if operation is successful
249   */
250 uint16_t MEM_If_GetStatus_FS(uint32_t Add, uint8_t Cmd, uint8_t *buffer)
251 {
252   /* USER CODE BEGIN 5 */
253   switch (Cmd)
254   {
255     case DFU_MEDIA_PROGRAM:
256
257     break;
258
259     case DFU_MEDIA_ERASE:
260     default:
261
262     break;
263   }
264   return (USBD_OK);
265   /* USER CODE END 5 */
266 }
267
268 /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
269
270 /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
271
272 /**
273   * @}
274   */
275
276 /**
277   * @}
278   */
279
280 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/