GD32F1x0: USB/GD32_USB_Device_Library/Class/dfu/src/usbd_dfu_mal.c Source File

GD32F1x0

usbd_dfu_mal.c
Go to the documentation of this file.
1 
11 /* Includes ------------------------------------------------------------------*/
12 #include "usbd_dfu_mal.h"
13 #include "usbd_flash_if.h"
14 
35 /* The reference tables of global memories callback and string descriptors.
36  To add a new memory, You can do as follows:
37  1. Modify the value of MAX_USED_MEDIA in usbd_dfu_mal.h
38  2. Add the pointer to the callback structure in this table
39  3. Add the pointer to the memory string descriptor in usbd_dfu_StringDesc table
40  No other operation is required.
41  */
42 DFU_MAL_Property_TypeDef* tMALTab[MAX_USED_MEMORY_MEDIA] = {
43  &DFU_Flash_cb
44 };
45 
46 /* The list of memory interface string descriptor pointers. This list
47  can be updated whenever a memory has to be added or removed */
48 const uint8_t* USBD_DFU_StringDesc[MAX_USED_MEMORY_MEDIA] = {
49  FLASH_IF_STRING
50 };
51 
52 /* Memory buffer for downloaded data */
53 uint8_t MAL_Buffer[TRANSFER_SIZE];
54 
59 static uint8_t DFU_MAL_CheckAddr (uint32_t Addr);
60 
70 uint8_t DFU_MAL_Init (void)
71 {
72  uint32_t memIdx = 0;
73 
74  /* Initialize all supported memory medias */
75  for(memIdx = 0; memIdx < MAX_USED_MEMORY_MEDIA; memIdx++)
76  {
77  /* Check if the memory media exists */
78  if (tMALTab[memIdx]->pMAL_Init != NULL)
79  {
80  tMALTab[memIdx]->pMAL_Init();
81  }
82  }
83 
84  return MAL_OK;
85 }
86 
92 uint8_t DFU_MAL_DeInit (void)
93 {
94  uint32_t memIdx = 0;
95 
96  /* Deinitializes all supported memory medias */
97  for(memIdx = 0; memIdx < MAX_USED_MEMORY_MEDIA; memIdx++)
98  {
99  /* Check if the memory media exists */
100  if (tMALTab[memIdx]->pMAL_DeInit != NULL)
101  {
102  tMALTab[memIdx]->pMAL_DeInit();
103  }
104  }
105 
106  return MAL_OK;
107 }
108 
114 uint8_t DFU_MAL_Erase (uint32_t Addr)
115 {
116  uint32_t memIdx = DFU_MAL_CheckAddr(Addr);
117 
118  /* Check if the address is in protected area */
119  if (IS_PROTECTED_AREA(Addr))
120  {
121  return MAL_FAIL;
122  }
123 
124  if (memIdx < MAX_USED_MEMORY_MEDIA)
125  {
126  /* Check if the operation is supported */
127  if (tMALTab[memIdx]->pMAL_Erase != NULL)
128  {
129  return tMALTab[memIdx]->pMAL_Erase(Addr);
130  }
131  else
132  {
133  return MAL_FAIL;
134  }
135  }
136  else
137  {
138  return MAL_FAIL;
139  }
140 }
141 
148 uint8_t DFU_MAL_Write (uint32_t Addr, uint32_t Len)
149 {
150  uint32_t memIdx = DFU_MAL_CheckAddr(Addr);
151 
152  /* Check if the address is in protected area */
153  if (IS_PROTECTED_AREA(Addr))
154  {
155  return MAL_FAIL;
156  }
157 
158  if (memIdx < MAX_USED_MEMORY_MEDIA)
159  {
160  /* Check if the operation is supported */
161  if (tMALTab[memIdx]->pMAL_Write != NULL)
162  {
163  return tMALTab[memIdx]->pMAL_Write(Addr, Len);
164  }
165  else
166  {
167  return MAL_FAIL;
168  }
169  }
170  else
171  {
172  return MAL_FAIL;
173  }
174 }
175 
182 uint8_t* DFU_MAL_Read (uint32_t Addr, uint32_t Len)
183 {
184  uint32_t memIdx = 0;
185 
186  if(Addr != OB_RDPT)
187  {
188  memIdx = DFU_MAL_CheckAddr(Addr);
189  }
190 
191  if (memIdx < MAX_USED_MEMORY_MEDIA)
192  {
193  /* Check if the operation is supported */
194  if (tMALTab[memIdx]->pMAL_Read != NULL)
195  {
196  return tMALTab[memIdx]->pMAL_Read(Addr, Len);
197  }
198  else
199  {
200  return MAL_Buffer;
201  }
202  }
203  else
204  {
205  return MAL_Buffer;
206  }
207 }
208 
216 uint8_t DFU_MAL_GetStatus (uint32_t Addr, uint8_t Cmd, uint8_t *buffer)
217 {
218  uint32_t memIdx = DFU_MAL_CheckAddr(Addr);
219 
220  if (memIdx < MAX_USED_MEMORY_MEDIA)
221  {
222  if (Cmd & 0x01)
223  {
224  SET_POLLING_TIMEOUT(tMALTab[memIdx]->WriteTimeout);
225  }
226  else
227  {
228  SET_POLLING_TIMEOUT(tMALTab[memIdx]->EraseTimeout);
229  }
230 
231  return MAL_OK;
232  }
233  else
234  {
235  return MAL_FAIL;
236  }
237 }
238 
244 static uint8_t DFU_MAL_CheckAddr (uint32_t Addr)
245 {
246  uint32_t memIdx = 0;
247 
248  /* Check with all supported memories */
249  for(memIdx = 0; memIdx < MAX_USED_MEMORY_MEDIA; memIdx++)
250  {
251  /* If the check address is supported, return the memory index */
252  if (tMALTab[memIdx]->pMAL_CheckAdd(Addr) == MAL_OK)
253  {
254  return memIdx;
255  }
256  }
257 
258  /* If there is no memory found, return MAX_USED_MEDIA */
259  return (MAX_USED_MEMORY_MEDIA);
260 }
261 
282 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
uint8_t DFU_MAL_DeInit(void)
Deinitialize the memory media on the GD32.
Definition: usbd_dfu_mal.c:92
uint8_t DFU_MAL_Write(uint32_t Addr, uint32_t Len)
Write data to sectors of memory.
Definition: usbd_dfu_mal.c:148
uint8_t DFU_MAL_GetStatus(uint32_t Addr, uint8_t Cmd, uint8_t *buffer)
Get the status of a given memory and store in buffer.
Definition: usbd_dfu_mal.c:216
uint8_t * DFU_MAL_Read(uint32_t Addr, uint32_t Len)
Read data from sectors of memory.
Definition: usbd_dfu_mal.c:182
uint8_t DFU_MAL_Erase(uint32_t Addr)
Erase a memory sector.
Definition: usbd_dfu_mal.c:114
USB DFU device media access layer header file.
uint8_t DFU_MAL_Init(void)
Initialize the memory media on the GD32.
Definition: usbd_dfu_mal.c:70
Generated by   doxygen 1.8.10