Added USB Device library sources
[fw/stlink] / example / stm32f4 / STM32_USB_Device_Library / Class / dfu / src / usbd_flash_if.c
1 /**
2   ******************************************************************************
3   * @file    usbd_flash_if.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    22-July-2011
7   * @brief   Specific media access Layer for internal flash.
8   ******************************************************************************
9   * @attention
10   *
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.
17   *
18   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19   ******************************************************************************
20   */ 
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "usbd_flash_if.h"
24 #include "usbd_dfu_mal.h"
25
26 /* Private typedef -----------------------------------------------------------*/
27 /* Private define ------------------------------------------------------------*/
28 /* Private macro -------------------------------------------------------------*/
29
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);
37
38
39 /* Private variables ---------------------------------------------------------*/
40 DFU_MAL_Prop_TypeDef DFU_Flash_cb =
41   {
42     FLASH_IF_STRING,
43     FLASH_If_Init,
44     FLASH_If_DeInit,
45     FLASH_If_Erase,
46     FLASH_If_Write,
47     FLASH_If_Read,
48     FLASH_If_CheckAdd,
49     50, /* Erase Time in ms */
50     50  /* Programming Time in ms */
51   };
52
53 /* Private functions ---------------------------------------------------------*/
54
55 /**
56   * @brief  FLASH_If_Init
57   *         Memory initialization routine.
58   * @param  None
59   * @retval MAL_OK if operation is successeful, MAL_FAIL else.
60   */
61 uint16_t FLASH_If_Init(void)
62 {
63   /* Unlock the internal flash */
64   FLASH_Unlock();
65   
66   return MAL_OK;
67 }
68
69 /**
70   * @brief  FLASH_If_DeInit
71   *         Memory deinitialization routine.
72   * @param  None
73   * @retval MAL_OK if operation is successeful, MAL_FAIL else.
74   */
75 uint16_t FLASH_If_DeInit(void)
76 {
77   /* Lock the internal flash */
78   FLASH_Lock();
79   
80   return MAL_OK;
81 }
82
83 /*******************************************************************************
84 * Function Name  : FLASH_If_Erase
85 * Description    : Erase sector
86 * Input          : None
87 * Output         : None
88 * Return         : None
89 *******************************************************************************/
90 uint16_t FLASH_If_Erase(uint32_t Add)
91 {
92 #ifdef STM32F2XX
93   /* Check which sector has to be erased */
94   if (Add < 0x08004000)
95   {
96     FLASH_EraseSector(FLASH_Sector_0, VoltageRange_3);
97   }
98   else if (Add < 0x08008000)
99   {
100     FLASH_EraseSector(FLASH_Sector_1, VoltageRange_3);
101   }
102   else if (Add < 0x0800C000)
103   {
104     FLASH_EraseSector(FLASH_Sector_2, VoltageRange_3);
105   }
106   else if (Add < 0x08010000)
107   {
108     FLASH_EraseSector(FLASH_Sector_3, VoltageRange_3);
109   }
110   else if (Add < 0x08020000)
111   {
112     FLASH_EraseSector(FLASH_Sector_4, VoltageRange_3);
113   }
114   else if (Add < 0x08040000)
115   {
116     FLASH_EraseSector(FLASH_Sector_5, VoltageRange_3);
117   }
118   else if (Add < 0x08060000)
119   {
120     FLASH_EraseSector(FLASH_Sector_6, VoltageRange_3);
121   }
122   else if (Add < 0x08080000)
123   {
124     FLASH_EraseSector(FLASH_Sector_7, VoltageRange_3);
125   }
126   else if (Add < 0x080A0000)
127   {
128     FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3);
129   }
130   else if (Add < 0x080C0000)
131   {
132     FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3);
133   }
134   else if (Add < 0x080E0000)
135   {
136     FLASH_EraseSector(FLASH_Sector_10, VoltageRange_3);
137   }
138   else if (Add < 0x08100000)
139   {
140     FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);
141   }
142   else
143   {
144     return MAL_FAIL;    
145   }
146 #elif defined(STM32F10X_CL)
147   /* Call the standard Flash erase function */
148   FLASH_ErasePage(Add);  
149 #endif /* STM32F2XX */
150   
151   return MAL_OK;
152 }
153
154 /**
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.
160   */
161 uint16_t FLASH_If_Write(uint32_t Add, uint32_t Len)
162 {
163   uint32_t idx = 0;
164   
165   if  (Len & 0x3) /* Not an aligned data */
166   {
167     for (idx = Len; idx < ((Len & 0xFFFC) + 4); idx++)
168     {
169       MAL_Buffer[idx] = 0xFF;
170     }
171   }
172   
173   /* Data received are Word multiple */
174   for (idx = 0; idx <  Len; idx = idx + 4)
175   {
176     FLASH_ProgramWord(Add, *(uint32_t *)(MAL_Buffer + idx));
177     Add += 4;
178   }
179   return MAL_OK;
180 }
181
182 /**
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.
188   */
189 uint8_t *FLASH_If_Read (uint32_t Add, uint32_t Len)
190 {
191 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
192   uint32_t idx = 0;
193   for (idx = 0; idx < Len; idx += 4)
194   {
195     *(uint32_t*)(MAL_Buffer + idx) = *(uint32_t *)(Add + idx);
196   }
197   return (uint8_t*)(MAL_Buffer);
198 #else  
199   return  (uint8_t *)(Add);
200 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
201 }
202
203 /**
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.
209   */
210 uint16_t FLASH_If_CheckAdd(uint32_t Add)
211 {
212   if ((Add >= FLASH_START_ADD) && (Add < FLASH_END_ADD))
213   {
214     return MAL_OK;
215   }
216   else
217   {
218     return MAL_FAIL;
219   }
220 }
221 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/