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.
12 * Copyright (c) 2018 STMicroelectronics International N.V.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted, provided that the following conditions are met:
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
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.
46 ******************************************************************************
49 /* Includes ------------------------------------------------------------------*/
50 #include "usbd_dfu_if.h"
52 /* USER CODE BEGIN INCLUDE */
54 /* USER CODE END INCLUDE */
56 /* Private typedef -----------------------------------------------------------*/
57 /* Private define ------------------------------------------------------------*/
58 /* Private macro -------------------------------------------------------------*/
60 /* USER CODE BEGIN PV */
61 /* Private variables ---------------------------------------------------------*/
63 /* USER CODE END PV */
65 /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
70 /** @defgroup USBD_DFU
71 * @brief Usb DFU device module.
75 /** @defgroup USBD_DFU_Private_TypesDefinitions
76 * @brief Private types.
80 /* USER CODE BEGIN PRIVATE_TYPES */
82 /* USER CODE END PRIVATE_TYPES */
88 /** @defgroup USBD_DFU_Private_Defines
89 * @brief Private defines.
93 #define FLASH_DESC_STR "@Internal Flash /0x08000000/03*016Ka,01*016Kg,01*064Kg,07*128Kg,04*016Kg,01*064Kg,07*128Kg"
95 /* USER CODE BEGIN PRIVATE_DEFINES */
97 /* USER CODE END PRIVATE_DEFINES */
103 /** @defgroup USBD_DFU_Private_Macros
104 * @brief Private macros.
108 /* USER CODE BEGIN PRIVATE_MACRO */
110 /* USER CODE END PRIVATE_MACRO */
116 /** @defgroup USBD_DFU_Private_Variables
117 * @brief Private variables.
121 /* USER CODE BEGIN PRIVATE_VARIABLES */
123 /* USER CODE END PRIVATE_VARIABLES */
129 /** @defgroup USBD_DFU_Exported_Variables
130 * @brief Public variables.
134 extern USBD_HandleTypeDef hUsbDeviceFS;
136 /* USER CODE BEGIN EXPORTED_VARIABLES */
138 /* USER CODE END EXPORTED_VARIABLES */
144 /** @defgroup USBD_DFU_Private_FunctionPrototypes
145 * @brief Private functions declaration.
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);
156 /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
158 /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
164 #if defined ( __ICCARM__ ) /* IAR Compiler */
165 #pragma data_alignment=4
167 __ALIGN_BEGIN USBD_DFU_MediaTypeDef USBD_DFU_fops_FS __ALIGN_END =
169 (uint8_t*)FLASH_DESC_STR,
178 /* Private functions ---------------------------------------------------------*/
180 * @brief Memory initialization routine.
181 * @retval USBD_OK if operation is successful, MAL_FAIL else.
183 uint16_t MEM_If_Init_FS(void)
185 /* USER CODE BEGIN 0 */
187 /* USER CODE END 0 */
191 * @brief De-Initializes Memory
192 * @retval USBD_OK if operation is successful, MAL_FAIL else
194 uint16_t MEM_If_DeInit_FS(void)
196 /* USER CODE BEGIN 1 */
198 /* USER CODE END 1 */
202 * @brief Erase sector.
203 * @param Add: Address of sector to be erased.
204 * @retval 0 if operation is successful, MAL_FAIL else.
206 uint16_t MEM_If_Erase_FS(uint32_t Add)
208 /* USER CODE BEGIN 2 */
211 /* USER CODE END 2 */
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.
221 uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
223 /* USER CODE BEGIN 3 */
225 /* USER CODE END 3 */
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.
235 uint8_t *MEM_If_Read_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
237 /* Return a valid address to avoid HardFault */
238 /* USER CODE BEGIN 4 */
239 return (uint8_t*)(USBD_OK);
240 /* USER CODE END 4 */
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
250 uint16_t MEM_If_GetStatus_FS(uint32_t Add, uint8_t Cmd, uint8_t *buffer)
252 /* USER CODE BEGIN 5 */
255 case DFU_MEDIA_PROGRAM:
259 case DFU_MEDIA_ERASE:
265 /* USER CODE END 5 */
268 /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
270 /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
280 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/