2 ******************************************************************************
4 * @author MCD Application Team
7 * @brief Generic media access Layer.
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_dfu_mal.h"
25 #include "usbd_flash_if.h"
27 #ifdef DFU_MAL_SUPPORT_OTP
28 #include "usbd_otp_if.h"
31 #ifdef DFU_MAL_SUPPORT_MEM
32 #include "usbd_mem_if_template.h"
35 /* Private typedef -----------------------------------------------------------*/
36 /* Private define ------------------------------------------------------------*/
37 /* Private macro -------------------------------------------------------------*/
38 /* Private variables ---------------------------------------------------------*/
40 /* Global Memories callback and string descriptors reference tables.
41 To add a new memory, modify the value of MAX_USED_MEDIA in usbd_dfu_mal.h
42 and add the pointer to the callback structure in this table.
43 Then add the pointer to the memory string descriptor in usbd_dfu_StringDesc table.
44 No other operation is required. */
45 DFU_MAL_Prop_TypeDef* tMALTab[MAX_USED_MEDIA] = {
47 #ifdef DFU_MAL_SUPPORT_OTP
50 #ifdef DFU_MAL_SUPPORT_MEM
55 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
56 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
57 #pragma data_alignment=4
59 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
61 __ALIGN_BEGIN const uint8_t* usbd_dfu_StringDesc[MAX_USED_MEDIA] __ALIGN_END = {
63 #ifdef DFU_MAL_SUPPORT_OTP
66 #ifdef DFU_MAL_SUPPORT_MEM
71 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
72 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
73 #pragma data_alignment=4
75 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
76 /* RAM Buffer for Downloaded Data */
77 __ALIGN_BEGIN uint8_t MAL_Buffer[XFERSIZE] __ALIGN_END ;
79 /* Private function prototypes -----------------------------------------------*/
80 static uint8_t MAL_CheckAdd (uint32_t Add);
81 /* Private functions ---------------------------------------------------------*/
85 * Initializes the Media on the STM32
87 * @retval Result of the opeartion (MAL_OK in all cases)
89 uint16_t MAL_Init(void)
93 /* Init all supported memories */
94 for(memIdx = 0; memIdx < MAX_USED_MEDIA; memIdx++)
96 /* If the check addres is positive, exit with the memory index */
97 if (tMALTab[memIdx]->pMAL_Init != NULL)
99 tMALTab[memIdx]->pMAL_Init();
108 * DeInitializes the Media on the STM32
110 * @retval Result of the opeartion (MAL_OK in all cases)
112 uint16_t MAL_DeInit(void)
116 /* Init all supported memories */
117 for(memIdx = 0; memIdx < MAX_USED_MEDIA; memIdx++)
119 /* Check if the command is supported */
120 if (tMALTab[memIdx]->pMAL_DeInit != NULL)
122 tMALTab[memIdx]->pMAL_DeInit();
131 * Erase a sector of memory.
132 * @param Add: Sector address/code
133 * @retval Result of the opeartion: MAL_OK if all operations are OK else MAL_FAIL
135 uint16_t MAL_Erase(uint32_t Add)
137 uint32_t memIdx = MAL_CheckAdd(Add);
139 /* Check if the area is protected */
140 if (DFU_MAL_IS_PROTECTED_AREA(Add))
145 if (memIdx < MAX_USED_MEDIA)
147 /* Check if the command is supported */
148 if (tMALTab[memIdx]->pMAL_Erase != NULL)
150 return tMALTab[memIdx]->pMAL_Erase(Add);
165 * Write sectors of memory.
166 * @param Add: Sector address/code
167 * @param Len: Number of data to be written (in bytes)
168 * @retval Result of the opeartion: MAL_OK if all operations are OK else MAL_FAIL
170 uint16_t MAL_Write (uint32_t Add, uint32_t Len)
172 uint32_t memIdx = MAL_CheckAdd(Add);
174 /* Check if the area is protected */
175 if (DFU_MAL_IS_PROTECTED_AREA(Add))
180 if (memIdx < MAX_USED_MEDIA)
182 /* Check if the command is supported */
183 if (tMALTab[memIdx]->pMAL_Write != NULL)
185 return tMALTab[memIdx]->pMAL_Write(Add, Len);
200 * Read sectors of memory.
201 * @param Add: Sector address/code
202 * @param Len: Number of data to be written (in bytes)
203 * @retval Buffer pointer
205 uint8_t *MAL_Read (uint32_t Add, uint32_t Len)
207 uint32_t memIdx = MAL_CheckAdd(Add);
209 if (memIdx < MAX_USED_MEDIA)
211 /* Check if the command is supported */
212 if (tMALTab[memIdx]->pMAL_Read != NULL)
214 return tMALTab[memIdx]->pMAL_Read(Add, Len);
228 * @brief MAL_GetStatus
229 * Get the status of a given memory.
230 * @param Add: Sector address/code (allow to determine which memory will be addressed)
231 * @param Cmd: 0 for erase and 1 for write
232 * @param buffer: pointer to the buffer where the status data will be stored.
233 * @retval Buffer pointer
235 uint16_t MAL_GetStatus(uint32_t Add , uint8_t Cmd, uint8_t *buffer)
237 uint32_t memIdx = MAL_CheckAdd(Add);
239 if (memIdx < MAX_USED_MEDIA)
243 SET_POLLING_TIMING(tMALTab[memIdx]->EraseTiming);
247 SET_POLLING_TIMING(tMALTab[memIdx]->WriteTiming);
259 * @brief MAL_CheckAdd
260 * Determine which memory should be managed.
261 * @param Add: Sector address/code (allow to determine which memory will be addressed)
262 * @retval Index of the addressed memory.
264 static uint8_t MAL_CheckAdd(uint32_t Add)
268 /* Check with all supported memories */
269 for(memIdx = 0; memIdx < MAX_USED_MEDIA; memIdx++)
271 /* If the check addres is positive, exit with the memory index */
272 if (tMALTab[memIdx]->pMAL_CheckAdd(Add) == MAL_OK)
277 /* If no memory found, return MAX_USED_MEDIA */
278 return (MAX_USED_MEDIA);
281 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/