M480 BSP V3.05.005
The Board Support Package for M480 Series
fmc.h
Go to the documentation of this file.
1/**************************************************************************/
9#ifndef __FMC_H__
10#define __FMC_H__
11
12#ifdef __cplusplus
13extern "C"
14{
15#endif
16
31/*---------------------------------------------------------------------------------------------------------*/
32/* Define Base Address */
33/*---------------------------------------------------------------------------------------------------------*/
34#define FMC_APROM_BASE 0x00000000UL
35#define FMC_APROM_END 0x00080000UL
36#define FMC_APROM_BANK0_END (FMC_APROM_END/2UL)
37#define FMC_LDROM_BASE 0x00100000UL
38#define FMC_LDROM_END 0x00101000UL
39#define FMC_SPROM_BASE 0x00200000UL
40#define FMC_SPROM_END 0x00201000UL
41#define FMC_XOM_BASE 0x00200000UL
42#define FMC_XOMR0_BASE 0x00200000UL
43#define FMC_XOMR1_BASE 0x00200010UL
44#define FMC_XOMR2_BASE 0x00200020UL
45#define FMC_XOMR3_BASE 0x00200030UL
46#define FMC_CONFIG_BASE 0x00300000UL
47#define FMC_USER_CONFIG_0 0x00300000UL
48#define FMC_USER_CONFIG_1 0x00300004UL
49#define FMC_USER_CONFIG_2 0x00300008UL
50#define FMC_KPROM_BASE 0x00301000UL
51#define FMC_OTP_BASE 0x00310000UL
53#define FMC_FLASH_PAGE_SIZE 0x1000UL
54#define FMC_PAGE_ADDR_MASK 0xFFFFF000UL
55#define FMC_MULTI_WORD_PROG_LEN 512
57#define FMC_APROM_SIZE FMC_APROM_END
58#define FMC_BANK_SIZE (FMC_APROM_SIZE/2UL)
59#define FMC_LDROM_SIZE 0x1000UL
60#define FMC_SPROM_SIZE 0x1000UL
61#define FMC_OTP_ENTRY_CNT 256UL
63/*---------------------------------------------------------------------------------------------------------*/
64/* XOM region number constant definitions */
65/*---------------------------------------------------------------------------------------------------------*/
66#define XOMR0 0UL
67#define XOMR1 1UL
68#define XOMR2 2UL
69#define XOMR3 3UL
71/*---------------------------------------------------------------------------------------------------------*/
72/* ISPCTL constant definitions */
73/*---------------------------------------------------------------------------------------------------------*/
74#define IS_BOOT_FROM_LDROM 0x1UL
75#define IS_BOOT_FROM_APROM 0x0UL
77/*---------------------------------------------------------------------------------------------------------*/
78/* ISPCMD constant definitions */
79/*---------------------------------------------------------------------------------------------------------*/
80#define FMC_ISPCMD_READ 0x00UL
81#define FMC_ISPCMD_READ_UID 0x04UL
82#define FMC_ISPCMD_READ_ALL1 0x08UL
83#define FMC_ISPCMD_READ_CID 0x0BUL
84#define FMC_ISPCMD_READ_DID 0x0CUL
85#define FMC_ISPCMD_READ_CKS 0x0DUL
86#define FMC_ISPCMD_PROGRAM 0x21UL
87#define FMC_ISPCMD_PAGE_ERASE 0x22UL
88#define FMC_ISPCMD_BANK_ERASE 0x23UL
89#define FMC_ISPCMD_BLOCK_ERASE 0x25UL
90#define FMC_ISPCMD_PROGRAM_MUL 0x27UL
91#define FMC_ISPCMD_RUN_ALL1 0x28UL
92#define FMC_ISPCMD_RUN_CKS 0x2DUL
93#define FMC_ISPCMD_VECMAP 0x2EUL
94#define FMC_ISPCMD_READ_64 0x40UL
95#define FMC_ISPCMD_PROGRAM_64 0x61UL
97#define READ_ALLONE_YES 0xA11FFFFFUL
98#define READ_ALLONE_NOT 0xA1100000UL
99#define READ_ALLONE_CMD_FAIL 0xFFFFFFFFUL
101#define FMC_TIMEOUT_READ ((SystemCoreClock/10)*2)
102#define FMC_TIMEOUT_WRITE ((SystemCoreClock/10)*2)
103#define FMC_TIMEOUT_ERASE ((SystemCoreClock/10)*4)
104#define FMC_TIMEOUT_CHKSUM (SystemCoreClock*2)
105#define FMC_TIMEOUT_CHKALLONE (SystemCoreClock*2) /* end of group FMC_EXPORTED_CONSTANTS */
108
109
115/*---------------------------------------------------------------------------------------------------------*/
116/* Macros */
117/*---------------------------------------------------------------------------------------------------------*/
118
119#define FMC_SET_APROM_BOOT() (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk)
120#define FMC_SET_LDROM_BOOT() (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk)
121#define FMC_ENABLE_AP_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_APUEN_Msk)
122#define FMC_DISABLE_AP_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk)
123#define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_CFGUEN_Msk)
124#define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk)
125#define FMC_ENABLE_LD_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_LDUEN_Msk)
126#define FMC_DISABLE_LD_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk)
127#define FMC_ENABLE_SP_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_SPUEN_Msk)
128#define FMC_DISABLE_SP_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_SPUEN_Msk)
129#define FMC_DISABLE_ISP() (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk)
130#define FMC_ENABLE_ISP() (FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk)
131#define FMC_GET_FAIL_FLAG() ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL)
132#define FMC_CLR_FAIL_FLAG() (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk) /* end of group FMC_EXPORTED_MACROS */
135
136/*---------------------------------------------------------------------------------------------------------*/
137/* Global variables */
138/*---------------------------------------------------------------------------------------------------------*/
139extern int32_t g_FMC_i32ErrCode;
140
141
146/*---------------------------------------------------------------------------------------------------------*/
147/* inline functions */
148/*---------------------------------------------------------------------------------------------------------*/
149
150__STATIC_INLINE uint32_t FMC_ReadCID(void);
151__STATIC_INLINE uint32_t FMC_ReadPID(void);
152__STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
153__STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
154__STATIC_INLINE int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr);
155__STATIC_INLINE uint32_t FMC_GetVECMAP(void);
156
165__STATIC_INLINE uint32_t FMC_GetVECMAP(void)
166{
167 return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
168}
169
179__STATIC_INLINE uint32_t FMC_ReadCID(void)
180{
181 uint32_t tout = FMC_TIMEOUT_READ;
182
184 FMC->ISPCMD = FMC_ISPCMD_READ_CID; /* Set ISP Command Code */
185 FMC->ISPADDR = 0x0u; /* Must keep 0x0 when read CID */
186 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
187#if ISBEN
188 __ISB();
189#endif /* To make sure ISP/CPU be Synchronized */
190 while (tout-- > 0)
191 {
192 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
193 {
194 if (FMC->ISPDAT != 0xDA)
195 g_FMC_i32ErrCode = -1;
196 return FMC->ISPDAT;
197 }
198 }
199 g_FMC_i32ErrCode = -1;
200 return 0xFFFFFFFF;
201}
202
212__STATIC_INLINE uint32_t FMC_ReadPID(void)
213{
214 uint32_t tout = FMC_TIMEOUT_READ;
215
217 FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */
218 FMC->ISPADDR = 0x04u; /* Must keep 0x4 when read PID */
219 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
220#if ISBEN
221 __ISB();
222#endif /* To make sure ISP/CPU be Synchronized */
223 while (tout-- > 0)
224 {
225 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
226 return FMC->ISPDAT;
227 }
228 g_FMC_i32ErrCode = -1;
229 return 0xFFFFFFFF;
230}
231
241__STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
242{
243 uint32_t tout = FMC_TIMEOUT_READ;
244
246 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
247 FMC->ISPADDR = ((uint32_t)u8Index << 2u);
248 FMC->ISPDAT = 0u;
249 FMC->ISPTRG = 0x1u;
250#if ISBEN
251 __ISB();
252#endif
253 while (tout-- > 0)
254 {
255 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
256 return FMC->ISPDAT;
257 }
258 g_FMC_i32ErrCode = -1;
259 return 0xFFFFFFFF;
260}
261
271__STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
272{
273 uint32_t tout = FMC_TIMEOUT_READ;
274
276 FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */
277 FMC->ISPADDR = (0x04u * u32Index) + 0x10u; /* The UCID is at offset 0x10 with word alignment. */
278 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
279#if ISBEN
280 __ISB();
281#endif /* To make sure ISP/CPU be Synchronized */
282 while (tout-- > 0)
283 {
284 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
285 return FMC->ISPDAT;
286 }
287 g_FMC_i32ErrCode = -1;
288 return 0xFFFFFFFF;
289}
290
304__STATIC_INLINE int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
305{
306 uint32_t tout = FMC_TIMEOUT_WRITE;
307
309 FMC->ISPCMD = FMC_ISPCMD_VECMAP; /* Set ISP Command Code */
310 FMC->ISPADDR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */
311 FMC->ISPTRG = 0x1u; /* Trigger to start ISP procedure */
312#if ISBEN
313 __ISB();
314#endif /* To make sure ISP/CPU be Synchronized */
315 while (tout-- > 0)
316 {
317 if (!FMC->ISPTRG) /* Waiting for ISP Done */
318 return 0;
319 }
320 g_FMC_i32ErrCode = -1;
321 return -1;
322}
323
324
325/*---------------------------------------------------------------------------------------------------------*/
326/* Functions */
327/*---------------------------------------------------------------------------------------------------------*/
328
329extern void FMC_Close(void);
330extern int32_t FMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page);
331extern int32_t FMC_Erase(uint32_t u32PageAddr);
332extern int32_t FMC_Erase_SPROM(void);
333extern int32_t FMC_Erase_Block(uint32_t u32BlockAddr);
334extern int32_t FMC_Erase_Bank(uint32_t u32BankAddr);
335extern int32_t FMC_EraseXOM(uint32_t xom_num);
336extern int32_t FMC_GetXOMState(uint32_t xom_num);
337extern int32_t FMC_GetBootSource(void);
338extern void FMC_Open(void);
339extern uint32_t FMC_Read(uint32_t u32Addr);
340extern int32_t FMC_Read_64(uint32_t u32addr, uint32_t * u32data0, uint32_t * u32data1);
341extern uint32_t FMC_ReadDataFlashBaseAddr(void);
342extern void FMC_SetBootSource(int32_t i32BootSrc);
343extern int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data);
344extern int32_t FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
345extern int32_t FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
346extern int32_t FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
347extern int32_t FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
348extern int32_t FMC_Lock_OTP(uint32_t otp_num);
349extern int32_t FMC_Is_OTP_Locked(uint32_t otp_num);
350extern int32_t FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
351extern int32_t FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count);
352extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
353extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
354extern int32_t FMC_SetSPKey(uint32_t key[3], uint32_t kpmax, uint32_t kemax, const int32_t lock_CONFIG, const int32_t lock_SPROM);
355extern int32_t FMC_CompareSPKey(uint32_t key[3]);
356
357 /* end of group FMC_EXPORTED_FUNCTIONS */
359 /* end of group FMC_Driver */
361 /* end of group Standard_Driver */
363
364#ifdef __cplusplus
365}
366#endif
367
368#endif /* __FMC_H__ */
369
370/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
#define FMC_ISPCMD_READ_DID
Definition: fmc.h:84
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:83
#define FMC_TIMEOUT_READ
Definition: fmc.h:101
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:93
#define FMC_TIMEOUT_WRITE
Definition: fmc.h:102
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:81
int32_t FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word)
Read the 64-bits data from the specified OTP.
Definition: fmc.c:795
void FMC_Close(void)
Disable FMC ISP function.
Definition: fmc.c:35
int32_t FMC_Lock_OTP(uint32_t otp_num)
Lock the specified OTP.
Definition: fmc.c:845
int32_t FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count)
Execute FMC_ISPCMD_READ command to read User Configuration.
Definition: fmc.c:946
int32_t FMC_CompareSPKey(uint32_t key[3])
Execute security key comparison.
Definition: fmc.c:1257
int32_t FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len)
Program Multi-Word data into specified address of flash.
Definition: fmc.c:620
int32_t FMC_Erase_SPROM(void)
Execute FMC_ISPCMD_PAGE_ERASE command to erase SPROM. The page size is 4096 bytes.
Definition: fmc.c:185
int32_t FMC_Erase_Block(uint32_t u32BlockAddr)
Execute FMC_ISPCMD_BLOCK_ERASE command to erase a flash block. The block size is 4 pages.
Definition: fmc.c:223
__STATIC_INLINE int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
Set vector mapping address.
Definition: fmc.h:304
int32_t FMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page)
Config XOM Region.
Definition: fmc.c:56
uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count)
Run CRC32 checksum calculation and get result.
Definition: fmc.c:1039
int32_t FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count)
Execute ISP commands to erase then write User Configuration.
Definition: fmc.c:979
uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count)
Run flash all one verification and get result.
Definition: fmc.c:1091
__STATIC_INLINE uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.h:212
__STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
Read Unique ID.
Definition: fmc.h:241
int32_t FMC_GetXOMState(uint32_t xom_num)
Check the XOM is actived or not.
Definition: fmc.c:374
int32_t FMC_Erase(uint32_t u32PageAddr)
Execute FMC_ISPCMD_PAGE_ERASE command to erase a flash page. The page size is 4096 bytes.
Definition: fmc.c:143
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:500
void FMC_SetBootSource(int32_t i32BootSrc)
Set boot source from LDROM or APROM after next software reset.
Definition: fmc.c:515
int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Execute ISP FMC_ISPCMD_PROGRAM to program a word to flash.
Definition: fmc.c:538
uint32_t FMC_Read(uint32_t u32Addr)
Execute FMC_ISPCMD_READ command to read a word from flash.
Definition: fmc.c:431
int32_t FMC_Is_OTP_Locked(uint32_t otp_num)
Check the OTP is locked or not.
Definition: fmc.c:891
int32_t FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word)
Program a 64-bits data to the specified OTP.
Definition: fmc.c:728
int32_t FMC_GetBootSource(void)
Get the current boot source.
Definition: fmc.c:397
__STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
To read UCID.
Definition: fmc.h:271
int32_t FMC_EraseXOM(uint32_t xom_num)
Execute Erase XOM Region.
Definition: fmc.c:301
int32_t FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1)
Execute ISP FMC_ISPCMD_PROGRAM_64 to program a double-word to flash.
Definition: fmc.c:577
int32_t FMC_Erase_Bank(uint32_t u32BankAddr)
Execute FMC_ISPCMD_BANK_ERASE command to erase a flash block.
Definition: fmc.c:259
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:415
int32_t FMC_Read_64(uint32_t u32addr, uint32_t *u32data0, uint32_t *u32data1)
Execute FMC_ISPCMD_READ_64 command to read a double-word from flash.
Definition: fmc.c:463
__STATIC_INLINE uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.h:179
int32_t FMC_SetSPKey(uint32_t key[3], uint32_t kpmax, uint32_t kemax, const int32_t lock_CONFIG, const int32_t lock_SPROM)
Setup security key.
Definition: fmc.c:1157
__STATIC_INLINE uint32_t FMC_GetVECMAP(void)
Get current vector mapping address.
Definition: fmc.h:165
int32_t g_FMC_i32ErrCode
Definition: fmc.c:28
#define FMC
Definition: M480.h:391
#define FMC_ISPSTS_VECMAP_Msk
Definition: fmc_reg.h:1073
#define FMC_ISPTRG_ISPGO_Msk
Definition: fmc_reg.h:1046