2 ******************************************************************************
3 * @file usbd_flash_if.c
4 * @author MCD Application Team
7 * @brief Specific media access Layer for internal flash.
8 ******************************************************************************
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
22 /* Includes ------------------------------------------------------------------*/
23 #include "usbd_flash_if.h"
24 #include "usbd_dfu_mal.h"
26 /* Private typedef -----------------------------------------------------------*/
27 /* Private define ------------------------------------------------------------*/
28 /* Private macro -------------------------------------------------------------*/
30 /* Private function prototypes -----------------------------------------------*/
31 uint16_t FLASH_If_Init(void);
32 uint16_t FLASH_If_Erase (uint32_t Add);
33 uint16_t FLASH_If_Write (uint32_t Add, uint32_t Len);
34 uint8_t *FLASH_If_Read (uint32_t Add, uint32_t Len);
35 uint16_t FLASH_If_DeInit(void);
36 uint16_t FLASH_If_CheckAdd(uint32_t Add);
39 /* Private variables ---------------------------------------------------------*/
40 DFU_MAL_Prop_TypeDef DFU_Flash_cb =
49 50, /* Erase Time in ms */
50 50 /* Programming Time in ms */
53 /* Private functions ---------------------------------------------------------*/
56 * @brief FLASH_If_Init
57 * Memory initialization routine.
59 * @retval MAL_OK if operation is successeful, MAL_FAIL else.
61 uint16_t FLASH_If_Init(void)
63 /* Unlock the internal flash */
70 * @brief FLASH_If_DeInit
71 * Memory deinitialization routine.
73 * @retval MAL_OK if operation is successeful, MAL_FAIL else.
75 uint16_t FLASH_If_DeInit(void)
77 /* Lock the internal flash */
83 /*******************************************************************************
84 * Function Name : FLASH_If_Erase
85 * Description : Erase sector
89 *******************************************************************************/
90 uint16_t FLASH_If_Erase(uint32_t Add)
93 /* Check which sector has to be erased */
96 FLASH_EraseSector(FLASH_Sector_0, VoltageRange_3);
98 else if (Add < 0x08008000)
100 FLASH_EraseSector(FLASH_Sector_1, VoltageRange_3);
102 else if (Add < 0x0800C000)
104 FLASH_EraseSector(FLASH_Sector_2, VoltageRange_3);
106 else if (Add < 0x08010000)
108 FLASH_EraseSector(FLASH_Sector_3, VoltageRange_3);
110 else if (Add < 0x08020000)
112 FLASH_EraseSector(FLASH_Sector_4, VoltageRange_3);
114 else if (Add < 0x08040000)
116 FLASH_EraseSector(FLASH_Sector_5, VoltageRange_3);
118 else if (Add < 0x08060000)
120 FLASH_EraseSector(FLASH_Sector_6, VoltageRange_3);
122 else if (Add < 0x08080000)
124 FLASH_EraseSector(FLASH_Sector_7, VoltageRange_3);
126 else if (Add < 0x080A0000)
128 FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3);
130 else if (Add < 0x080C0000)
132 FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3);
134 else if (Add < 0x080E0000)
136 FLASH_EraseSector(FLASH_Sector_10, VoltageRange_3);
138 else if (Add < 0x08100000)
140 FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);
146 #elif defined(STM32F10X_CL)
147 /* Call the standard Flash erase function */
148 FLASH_ErasePage(Add);
149 #endif /* STM32F2XX */
155 * @brief FLASH_If_Write
156 * Memory write routine.
157 * @param Add: Address to be written to.
158 * @param Len: Number of data to be written (in bytes).
159 * @retval MAL_OK if operation is successeful, MAL_FAIL else.
161 uint16_t FLASH_If_Write(uint32_t Add, uint32_t Len)
165 if (Len & 0x3) /* Not an aligned data */
167 for (idx = Len; idx < ((Len & 0xFFFC) + 4); idx++)
169 MAL_Buffer[idx] = 0xFF;
173 /* Data received are Word multiple */
174 for (idx = 0; idx < Len; idx = idx + 4)
176 FLASH_ProgramWord(Add, *(uint32_t *)(MAL_Buffer + idx));
183 * @brief FLASH_If_Read
184 * Memory read routine.
185 * @param Add: Address to be read from.
186 * @param Len: Number of data to be read (in bytes).
187 * @retval Pointer to the phyisical address where data should be read.
189 uint8_t *FLASH_If_Read (uint32_t Add, uint32_t Len)
191 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
193 for (idx = 0; idx < Len; idx += 4)
195 *(uint32_t*)(MAL_Buffer + idx) = *(uint32_t *)(Add + idx);
197 return (uint8_t*)(MAL_Buffer);
199 return (uint8_t *)(Add);
200 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
204 * @brief FLASH_If_CheckAdd
205 * Check if the address is an allowed address for this memory.
206 * @param Add: Address to be checked.
207 * @param Len: Number of data to be read (in bytes).
208 * @retval MAL_OK if the address is allowed, MAL_FAIL else.
210 uint16_t FLASH_If_CheckAdd(uint32_t Add)
212 if ((Add >= FLASH_START_ADD) && (Add < FLASH_END_ADD))
221 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/