GD32F1x0: USB/GD32_USB_Device_Library/Class/IAP/src/usbd_flash_if.c Source File

GD32F1x0

usbd_flash_if.c
1 
11 /* Includes ------------------------------------------------------------------*/
12 #include "usbd_flash_if.h"
13 
33 static uint8_t Flash_If_Init (void);
34 static uint8_t Flash_If_DeInit (void);
35 static uint8_t Flash_If_Erase (uint32_t Addr, uint32_t Page_Count);
36 static uint8_t Flash_If_Write (uint8_t Data[], uint32_t Addr, uint32_t Len);
37 static uint8_t* Flash_If_Read (uint32_t Addr, uint32_t Len);
38 static uint8_t Flash_If_CheckAddr (uint32_t Addr);
39 
47 IAP_MAL_Property_TypeDef IAP_Flash_cb =
48 {
49  FLASH_IF_STRING,
50  Flash_If_Init,
51  Flash_If_DeInit,
52  Flash_If_Erase,
53  Flash_If_Write,
54  Flash_If_Read,
55  Flash_If_CheckAddr,
56  60, /* Flash erase timeout in ms */
57  80 /* Flash programming timeout in ms (80us * RAM Buffer size (1024 Bytes) */
58 };
59 
69 static uint8_t Flash_If_Init (void)
70 {
71  /* Unlock the internal flash */
72  FMC_Unlock();
73 
74  return MAL_OK;
75 }
76 
82 static uint8_t Flash_If_DeInit (void)
83 {
84  /* Lock the internal flash */
85  FMC_Lock();
86 
87  return MAL_OK;
88 }
89 
95 static uint8_t Flash_If_Erase (uint32_t Addr, uint32_t PageCount)
96 {
97  int i;
98 
99  for(i = 0; i < PageCount; i ++)
100  {
101  /* Call the standard flash erase-page function */
102  FMC_ErasePage(Addr);
103 
104  Addr += PAGE_SIZE;
105  }
106 
107  return MAL_OK;
108 }
109 
116 static uint8_t Flash_If_Write (uint8_t Data[], uint32_t Addr, uint32_t Len)
117 {
118  uint32_t idx = 0;
119 
120  FMC_Unlock();
121 
122  if (Len & 0x03) /* Not an aligned data */
123  {
124  for (idx = Len; idx < ((Len & 0xFFFC) + 4); idx++)
125  {
126  Data[idx] = 0xFF;
127  }
128  }
129 
130  /* Data received are word multiple */
131  for (idx = 0; idx < Len; idx += 4)
132  {
133  FMC_ProgramWord(Addr, *(uint32_t *)(Data + idx));
134  Addr += 4;
135  }
136 
137  return MAL_OK;
138 }
139 
146 static uint8_t* Flash_If_Read (uint32_t Addr, uint32_t Len)
147 {
148  return (uint8_t *)(Addr);
149 }
150 
156 static uint8_t Flash_If_CheckAddr (uint32_t Addr)
157 {
158  if ((Addr >= FLASH_START_ADDR) && (Addr < FLASH_END_ADDR))
159  {
160  return MAL_OK;
161  }
162  else
163  {
164  return MAL_FAIL;
165  }
166 }
167 
188 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
Generated by   doxygen 1.8.10