Merge pull request #93 from zyp/master
[fw/stlink] / example / stm32f4 / STM32_USB_Device_Library / Class / dfu / src / usbd_otp_if.c
1 /**
2   ******************************************************************************
3   * @file    usbd_otp_if.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    22-July-2011
7   * @brief   Specific media access Layer for OTP (One Time Programming) memory.
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_otp_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 OTP_If_Write (uint32_t Add, uint32_t Len);
32 uint8_t *OTP_If_Read  (uint32_t Add, uint32_t Len);
33 uint16_t OTP_If_DeInit(void);
34 uint16_t OTP_If_CheckAdd(uint32_t Add);
35
36
37 /* Private variables ---------------------------------------------------------*/
38 DFU_MAL_Prop_TypeDef DFU_Otp_cb =
39   {
40     OTP_IF_STRING,
41     NULL, /* Init not supported*/
42     NULL, /* DeInit not supported */
43     NULL, /* Erase not supported */
44     OTP_If_Write,
45     OTP_If_Read,
46     OTP_If_CheckAdd,
47     1,  /* Erase Time in ms */
48     10  /* Programming Time in ms */
49   };
50   
51 /* Private functions ---------------------------------------------------------*/
52
53 /**
54   * @brief  OTP_If_Write
55   *         Memory write routine.
56   * @param  Add: Address to be written to.
57   * @param  Len: Number of data to be written (in bytes).
58   * @retval MAL_OK if operation is successeful, MAL_FAIL else.
59   */
60 uint16_t OTP_If_Write(uint32_t Add, uint32_t Len)
61 {
62   uint32_t idx = 0;
63   
64   if  (Len & 0x3) /* Not an aligned data */
65   {
66     for (idx = Len; idx < ((Len & 0xFFFC) + 4); idx++)
67     {
68       MAL_Buffer[idx] = 0xFF;
69     }
70   }
71   
72   /* Data received are Word multiple */
73   for (idx = 0; idx <  Len; idx = idx + 4)
74   {
75     FLASH_ProgramWord(Add, *(uint32_t *)(MAL_Buffer + idx));
76     Add += 4;
77   }
78   return MAL_OK;
79 }
80
81 /**
82   * @brief  OTP_If_Read
83   *         Memory read routine.
84   * @param  Add: Address to be read from.
85   * @param  Len: Number of data to be read (in bytes).
86   * @retval Pointer to the phyisical address where data should be read.
87   */
88 uint8_t *OTP_If_Read (uint32_t Add, uint32_t Len)
89 {
90 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
91   uint32_t idx = 0;
92   for (idx = 0; idx < Len; idx += 4)
93   {
94     *(uint32_t*)(MAL_Buffer + idx) = *(uint32_t *)(Add + idx);
95   }
96   return (uint8_t*)(MAL_Buffer);
97 #else
98   return  (uint8_t*)(Add);
99 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
100 }
101
102 /**
103   * @brief  OTP_If_CheckAdd
104   *         Check if the address is an allowed address for this memory.
105   * @param  Add: Address to be checked.
106   * @param  Len: Number of data to be read (in bytes).
107   * @retval MAL_OK if the address is allowed, MAL_FAIL else.
108   */
109 uint16_t OTP_If_CheckAdd(uint32_t Add)
110 {
111   if ((Add >= OTP_START_ADD) && (Add < OTP_END_ADD))
112   {
113     return MAL_OK;
114   }
115   else
116   {
117     return MAL_FAIL;
118   }
119 }
120 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/