| MINI58_BSP V3.01.003 The Board Support Package for Mini58 Series MCU

fmc.c
Go to the documentation of this file.
1/**************************************************************************/
13//* Includes ------------------------------------------------------------------*/
14#include <stdio.h>
15#include "Mini58Series.h"
16
30int32_t g_FMC_i32ErrCode;
31
37 void FMC_Close(void)
38{
39 FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk;
40}
41
42
51int32_t FMC_Erase(uint32_t u32PageAddr)
52{
53 int32_t tout = FMC_TIMEOUT_ERASE;
54
55 FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
56 FMC->ISPADDR = u32PageAddr;
57 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
58
59 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
60 if ((tout <= 0) || (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk))
61 {
62 g_FMC_i32ErrCode = -1;
63 return -1;
64 }
65 return 0;
66}
67
68
74int32_t FMC_GetBootSource (void)
75{
76 if (FMC->ISPCTL & FMC_ISPCTL_BS_Msk)
77 return 1;
78 else
79 return 0;
80}
81
82
86 void FMC_Open(void)
87{
88 FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk;
89}
90
91
100uint32_t FMC_Read(uint32_t u32Addr)
101{
102 int32_t tout = FMC_TIMEOUT_ERASE;
103
104 FMC->ISPCMD = FMC_ISPCMD_READ;
105 FMC->ISPADDR = u32Addr;
106 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
107
108 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
109 if (tout <= 0)
110 {
111 g_FMC_i32ErrCode = -1;
112 return 0xFFFFFFFF;
113 }
114 return FMC->ISPDAT;
115}
116
117
125uint32_t FMC_ReadCID(void)
126{
127 int32_t tout = FMC_TIMEOUT_READ;
128
129 FMC->ISPCMD = FMC_ISPCMD_READ_CID;
130 FMC->ISPADDR = 0x0;
131 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
132 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
133 if (tout <= 0)
134 {
135 g_FMC_i32ErrCode = -1;
136 return 0xFFFFFFFF;
137 }
138 return FMC->ISPDAT;
139}
140
141
149uint32_t FMC_ReadPID(void)
150{
151 int32_t tout = FMC_TIMEOUT_READ;
152
153 FMC->ISPCMD = FMC_ISPCMD_READ_PID;
154 FMC->ISPADDR = 0x04;
155 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
156 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
157 if (tout <= 0)
158 {
159 g_FMC_i32ErrCode = -1;
160 return 0xFFFFFFFF;
161 }
162 return FMC->ISPDAT;
163}
164
165
174uint32_t FMC_ReadUCID(uint32_t u32Index)
175{
176 int32_t tout = FMC_TIMEOUT_READ;
177
178 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
179 FMC->ISPADDR = (0x04 * u32Index) + 0x10;
180 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
181
182 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
183 if (tout <= 0)
184 {
185 g_FMC_i32ErrCode = -1;
186 return 0xFFFFFFFF;
187 }
188 return FMC->ISPDAT;
189}
190
191
200uint32_t FMC_ReadUID(uint32_t u32Index)
201{
202 int32_t tout = FMC_TIMEOUT_READ;
203
204 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
205 FMC->ISPADDR = 0x04 * u32Index;
206 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
207
208 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
209 if (tout <= 0)
210 {
211 g_FMC_i32ErrCode = -1;
212 return 0xFFFFFFFF;
213 }
214 return FMC->ISPDAT;
215}
216
217
222uint32_t FMC_ReadDataFlashBaseAddr(void)
223{
224 return FMC->DFBA;
225}
226
227
235 void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
236{
237 int32_t tout = FMC_TIMEOUT_WRITE;
238
239 FMC->ISPCMD = FMC_ISPCMD_VECMAP;
240 FMC->ISPADDR = u32PageAddr;
241 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
242 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
243 if (tout <= 0)
244 g_FMC_i32ErrCode = -1;
245}
246
247
252uint32_t FMC_GetVectorPageAddr(void)
253{
254 return (FMC->ISPSTS & 0x0FFFFF00ul);
255}
256
257
258
267int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
268{
269 int32_t tout = FMC_TIMEOUT_WRITE;
270
271 FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
272 FMC->ISPADDR = u32Addr;
273 FMC->ISPDAT = u32Data;
274 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
275 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
276 if ((tout <= 0) || (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk))
277 {
278 g_FMC_i32ErrCode = -1;
279 return -1;
280 }
281 return 0;
282}
283
284
294int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
295{
296 if (u32Count < 1)
297 return 0;
298
299 g_FMC_i32ErrCode = 0;
300 u32Config[0] = FMC_Read(FMC_CONFIG_BASE);
301 if (u32Count < 2)
302 return g_FMC_i32ErrCode;
303
304 u32Config[1] = FMC_Read(FMC_CONFIG_BASE+4);
305 return g_FMC_i32ErrCode;
306}
307
308
318int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
319{
320 if (u32Count < 1)
321 return 0;
322
323 g_FMC_i32ErrCode = 0;
324 FMC_ENABLE_CFG_UPDATE();
325 FMC_Erase(FMC_CONFIG_BASE);
326 FMC_Write(FMC_CONFIG_BASE, u32Config[0]);
327
328 if (u32Count < 2)
329 return g_FMC_i32ErrCode;
330
331 FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]);
332 FMC_DISABLE_CFG_UPDATE();
333 return g_FMC_i32ErrCode;
334}
335
336
347int32_t FMC_GetCRC32Sum(uint32_t addr, uint32_t count, uint32_t *chksum)
348{
349 int32_t tout = FMC_TIMEOUT_CHKSUM;
350
351 g_FMC_i32ErrCode = 0;
352 FMC->ISPCMD = FMC_ISPCMD_CAL_CRC32;
353 FMC->ISPADDR = addr;
354 FMC->ISPDAT = count;
355 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
356
357 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
358 if ((tout <= 0) || (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk))
359 {
360 g_FMC_i32ErrCode = -1;
361 return -1;
362 }
363
364 FMC->ISPCMD = FMC_ISPCMD_READ_CRC32;
365 FMC->ISPADDR = addr;
366 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
367
368 tout = FMC_TIMEOUT_CHKSUM;
369 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
370 if ((tout <= 0) || (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk))
371 {
372 g_FMC_i32ErrCode = -1;
373 return -1;
374 }
375 *chksum = FMC->ISPDAT;
376 return 0;
377}
378
379 /* end of group Mini58_FMC_EXPORTED_FUNCTIONS */
381 /* end of group Mini58_FMC_Driver */
383 /* end of group Mini58_Device_Driver */
385
386/*** (C) COPYRIGHT 2022 Nuvoton Technology Corp. ***/
387
388
Mini58Series.h
Mini58 series peripheral access layer header file. This file contains all the peripheral register's d...
FMC_ISPCTL_ISPFF_Msk
#define FMC_ISPCTL_ISPFF_Msk
Definition: Mini58Series.h:1558
FMC_ISPSTS_ISPFF_Msk
#define FMC_ISPSTS_ISPFF_Msk
Definition: Mini58Series.h:1585
FMC_ISPCTL_BS_Msk
#define FMC_ISPCTL_BS_Msk
Definition: Mini58Series.h:1543
FMC_ISPCTL_ISPEN_Msk
#define FMC_ISPCTL_ISPEN_Msk
Definition: Mini58Series.h:1540
FMC_ISPTRG_ISPGO_Msk
#define FMC_ISPTRG_ISPGO_Msk
Definition: Mini58Series.h:1570
FMC_TIMEOUT_ERASE
#define FMC_TIMEOUT_ERASE
Definition: fmc.h:48
FMC_ISPCMD_PROGRAM
#define FMC_ISPCMD_PROGRAM
Definition: fmc.h:60
FMC_CONFIG_BASE
#define FMC_CONFIG_BASE
Definition: fmc.h:40
FMC_TIMEOUT_CHKSUM
#define FMC_TIMEOUT_CHKSUM
Definition: fmc.h:49
FMC_ISPCMD_READ_CID
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:57
FMC_ISPCMD_READ_PID
#define FMC_ISPCMD_READ_PID
Definition: fmc.h:58
FMC_TIMEOUT_READ
#define FMC_TIMEOUT_READ
Definition: fmc.h:46
FMC_ISPCMD_CAL_CRC32
#define FMC_ISPCMD_CAL_CRC32
Definition: fmc.h:62
FMC_ISPCMD_READ
#define FMC_ISPCMD_READ
Definition: fmc.h:55
FMC_ISPCMD_VECMAP
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:63
FMC_ISPCMD_READ_CRC32
#define FMC_ISPCMD_READ_CRC32
Definition: fmc.h:59
FMC_TIMEOUT_WRITE
#define FMC_TIMEOUT_WRITE
Definition: fmc.h:47
FMC_ISPCMD_READ_UID
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:56
FMC_ISPCMD_PAGE_ERASE
#define FMC_ISPCMD_PAGE_ERASE
Definition: fmc.h:61
FMC_Close
void FMC_Close(void)
Disable all FMC functions.
Definition: fmc.c:37
g_FMC_i32ErrCode
int32_t g_FMC_i32ErrCode
Definition: fmc.c:30
FMC_ReadConfig
int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
Read the User Configuration words.
Definition: fmc.c:294
FMC_SetVectorPageAddr
void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:235
FMC_ReadCID
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:125
FMC_GetVectorPageAddr
uint32_t FMC_GetVectorPageAddr(void)
Obtain the current vector page address setting.
Definition: fmc.c:252
FMC_ReadUID
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:200
FMC_GetCRC32Sum
int32_t FMC_GetCRC32Sum(uint32_t addr, uint32_t count, uint32_t *chksum)
Calculate and read the CRC32 checksum of a specified flash area.
Definition: fmc.c:347
FMC_Erase
int32_t FMC_Erase(uint32_t u32PageAddr)
Erase a page. The page size is 512 bytes.
Definition: fmc.c:51
FMC_ReadDataFlashBaseAddr
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:222
FMC_Write
int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Writes a word data to specified flash address.
Definition: fmc.c:267
FMC_DISABLE_CFG_UPDATE
#define FMC_DISABLE_CFG_UPDATE()
Definition: fmc.h:81
FMC_Read
uint32_t FMC_Read(uint32_t u32Addr)
Read a word from specified flash address.
Definition: fmc.c:100
FMC_ENABLE_CFG_UPDATE
#define FMC_ENABLE_CFG_UPDATE()
Definition: fmc.h:87
FMC_ReadPID
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:149
FMC_GetBootSource
int32_t FMC_GetBootSource(void)
get the current boot source
Definition: fmc.c:74
FMC_WriteConfig
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Write User Configuration.
Definition: fmc.c:318
FMC_Open
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:86
FMC_ReadUCID
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:174
FMC
#define FMC
Pointer to FMC register structure.
Definition: Mini58Series.h:11739