| Nano103 BSP V3.01.002 The Board Support Package for Nano103 Series

fmc.c
Go to the documentation of this file.
1 /**************************************************************************/
13 //* Includes ------------------------------------------------------------------*/
14 #include <stdio.h>
15
16 #include "Nano103.h"
17
18
37 void FMC_Close(void)
38 {
39 FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk;
40 }
41
42
51 int32_t FMC_Erase(uint32_t u32PageAddr)
52 {
53 FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
54 FMC->ISPADDR = u32PageAddr;
55 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
56
57 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
58
59 if (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk)
60 {
61 FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk;
62 return -1;
63 }
64 return 0;
65 }
66
73 int32_t FMC_GetBootSource (void)
74 {
75 if (FMC->ISPCTL & FMC_ISPCTL_BS_Msk)
76 return 1;
77 else
78 return 0;
79 }
80
81
86 void FMC_Open(void)
87 {
88 FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk;
89 }
90
91
98 uint32_t FMC_Read(uint32_t u32Addr)
99 {
100 FMC->ISPCMD = FMC_ISPCMD_READ;
101 FMC->ISPADDR = u32Addr;
102 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
103
104 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
105
106 return FMC->ISPDAT;
107 }
108
109
114 uint32_t FMC_ReadCID(void)
115 {
116 FMC->ISPCMD = FMC_ISPCMD_READ_CID;
117 FMC->ISPADDR = 0x0;
118 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
119 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
120 return FMC->ISPDAT;
121 }
122
123
128 uint32_t FMC_ReadPID(void)
129 {
130 FMC->ISPCMD = FMC_ISPCMD_READ_PID;
131 FMC->ISPADDR = 0x04;
132 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
133 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
134 return FMC->ISPDAT;
135 }
136
137
143 uint32_t FMC_ReadUCID(uint32_t u32Index)
144 {
145 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
146 FMC->ISPADDR = (0x04 * u32Index) + 0x10;
147 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
148
149 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
150
151 return FMC->ISPDAT;
152 }
153
154
160 uint32_t FMC_ReadUID(uint32_t u32Index)
161 {
162 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
163 FMC->ISPADDR = 0x04 * u32Index;
164 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
165
166 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
167
168 return FMC->ISPDAT;
169 }
170
171
176 uint32_t FMC_ReadDataFlashBaseAddr(void)
177 {
178 return FMC->DFBA;
179 }
180
181
187 void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
188 {
189 FMC->ISPCMD = FMC_ISPCMD_VECMAP;
190 FMC->ISPADDR = u32PageAddr;
191 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
192 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
193 }
194
195
200 uint32_t FMC_GetVectorPageAddr(void)
201 {
202 return (FMC->ISPSTS & 0x0FFFFF00ul);
203 }
204
205
213 void FMC_Write(uint32_t u32Addr, uint32_t u32Data)
214 {
215 FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
216 FMC->ISPADDR = u32Addr;
217 FMC->ISPDAT = u32Data;
218 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
219 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
220 }
221
222
232 int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
233 {
234 u32Config[0] = FMC_Read(FMC_CONFIG_BASE);
235 if (u32Count < 2)
236 return 0;
237 u32Config[1] = FMC_Read(FMC_CONFIG_BASE+4);
238 return 0;
239 }
240
241
251 int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
252 {
253 FMC_ENABLE_CFG_UPDATE();
254 FMC_Erase(FMC_CONFIG_BASE);
255 FMC_Write(FMC_CONFIG_BASE, u32Config[0]);
256 FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]);
257 FMC_DISABLE_CFG_UPDATE();
258 return 0;
259 }
260
261
272 int32_t FMC_GetChkSum(uint32_t u32Addr, uint32_t u32Count, uint32_t *u32ChkSum)
273 {
274 if ((u32Addr % 512) || (u32Count % 512))
275 return -2;
276
277 *u32ChkSum = 0;
278
279 FMC->ISPCMD = FMC_ISPCMD_RUN_CKS;
280 FMC->ISPADDR = u32Addr;
281 FMC->ISPDAT = u32Count;
282 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
283
284 while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) ;
285
286 if (FMC_GET_FAIL_FLAG())
287 return -1;
288
289 FMC->ISPCMD = FMC_ISPCMD_READ_CKS;
290 FMC->ISPADDR = u32Addr;
291 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
292 while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) ;
293
294 if (FMC_GET_FAIL_FLAG())
295 return -1;
296
297 *u32ChkSum = FMC->ISPDAT;
298 return 0;
299 }
300
301
310 uint32_t FMC_CheckAllOne(uint32_t u32Addr, uint32_t u32Count)
311 {
312 FMC->ISPSTS = 0x80; // clear check alll one bit
313
314 FMC->ISPCMD = FMC_ISPCMD_RUN_ALL1;
315 FMC->ISPADDR = u32Addr;
316 FMC->ISPDAT = u32Count;
317 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
318
319 while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) ;
320
321 if (FMC_GET_FAIL_FLAG())
322 {
323 //printf("FMC_ISPCMD_RUN_ALL1 ISP failed!\n");
324 return READ_ALLONE_CMD_FAIL;
325 }
326
327 do
328 {
329 FMC->ISPCMD = FMC_ISPCMD_READ_ALL1;
330 FMC->ISPADDR = u32Addr;
331 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
332 while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) ;
333 }
334 while (FMC->ISPDAT == 0);
335
336 if (FMC_GET_FAIL_FLAG())
337 {
338 printf("FMC_ISPCMD_READ_ALL1 ISP failed!\n");
339 return READ_ALLONE_CMD_FAIL;
340 }
341
342 if ((FMC->ISPDAT == READ_ALLONE_YES) || (FMC->ISPDAT == READ_ALLONE_NOT))
343 return FMC->ISPDAT;
344
345 return READ_ALLONE_CMD_FAIL;
346 }
347
348
364 int32_t FMC_SKey_Setup(uint32_t key[3], uint32_t kpmax, uint32_t kemax, int lock_CONFIG)
365 {
366 uint32_t lock_ctrl = 0;
367
368 if (FMC->KEYSTS != 0)
369 return -1;
370
371 if (FMC_Erase(FMC_KPROM_BASE))
372 return -2;
373
374 if (FMC_Erase(FMC_KPROM_BASE+FMC_FLASH_PAGE_SIZE))
375 return -2;
376
377 if (!lock_CONFIG)
378 lock_ctrl |= 0x1;
379
380 FMC_Write(FMC_KPROM_BASE, key[0]);
381 FMC_Write(FMC_KPROM_BASE+0x4, key[1]);
382 FMC_Write(FMC_KPROM_BASE+0x8, key[2]);
383 FMC_Write(FMC_KPROM_BASE+0xC, kpmax);
384 FMC_Write(FMC_KPROM_BASE+0x10, kemax);
385 FMC_Write(FMC_KPROM_BASE+0x14, lock_ctrl);
386
387 while (FMC->KEYSTS & FMC_KEYSTS_KEYBUSY_Msk);
388
389 if (!(FMC->KEYSTS & FMC_KEYSTS_KEYLOCK_Msk))
390 {
391 printf("Security key lock failed!\n");
392 return -4;
393 }
394
395 if ((lock_CONFIG && !(FMC->KEYSTS & FMC_KEYSTS_CFGFLAG_Msk)) ||
396 (!lock_CONFIG && (FMC->KEYSTS & FMC_KEYSTS_CFGFLAG_Msk)))
397 {
398 printf("CONFIG lock failed!\n");
399 return -5;
400 }
401
402 if (((FMC->KPCNT & FMC_KPCNT_KPMAX_Msk) >> FMC_KPCNT_KPMAX_Pos) != kpmax)
403 {
404 printf("KPMAX failed!\n");
405 return -7;
406 }
407
408 if (((FMC->KECNT & FMC_KECNT_KEMAX_Msk) >> FMC_KECNT_KEMAX_Pos) != kemax)
409 {
410 printf("KEMAX failed!\n");
411 return -8;
412 }
413 return 0;
414 }
415
416
425 int32_t FMC_SKey_Compare(uint32_t key[3])
426 {
427 if (FMC->KEYSTS & FMC_KEYSTS_FORBID_Msk)
428 {
429 printf("FMC_SKey_Compare - FORBID!\n");
430 return -1;
431 }
432
433 if (!(FMC->KEYSTS & FMC_KEYSTS_KEYLOCK_Msk))
434 {
435 printf("FMC_SKey_Compare - key is not locked!\n");
436 return -3;
437 }
438
439 FMC->KEY0 = key[0];
440 FMC->KEY1 = key[1];
441 FMC->KEY2 = key[2];
442 FMC->KEYTRG = FMC_KEYTRG_KEYGO_Msk | FMC_KEYTRG_TCEN_Msk;
443
444 while (FMC->KEYSTS & FMC_KEYSTS_KEYBUSY_Msk);
445
446 if (!(FMC->KEYSTS & FMC_KEYSTS_KEYMATCH_Msk))
447 {
448 printf("Key mismatched!\n");
449 return -2;
450 }
451
452 if (FMC->KEYSTS & FMC_KEYSTS_KEYLOCK_Msk)
453 {
454 printf("Key matched, but still be locked!\n");
455 return -2;
456 }
457
458 printf("FMC_SKey_Compare - OK.\n");
459 return 0;
460 }
461
462 /* end of group NANO103_FMC_EXPORTED_FUNCTIONS */
464 /* end of group NANO103_FMC_Driver */
466 /* end of group NANO103_Device_Driver */
468
469 /*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/
470
471
FMC_ReadCID
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:114
FMC_FLASH_PAGE_SIZE
#define FMC_FLASH_PAGE_SIZE
Definition: fmc.h:45
FMC_ISPCMD_READ_CID
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:57
FMC_ISPCMD_READ_CKS
#define FMC_ISPCMD_READ_CKS
Definition: fmc.h:61
FMC_ISPCMD_VECMAP
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:64
READ_ALLONE_YES
#define READ_ALLONE_YES
Definition: fmc.h:69
FMC
#define FMC
Pointer to FMC register structure.
Definition: Nano103.h:24898
FMC_KPCNT_KPMAX_Pos
#define FMC_KPCNT_KPMAX_Pos
Definition: Nano103.h:5985
FMC_SKey_Compare
int32_t FMC_SKey_Compare(uint32_t key[3])
Execute security key comparison.
Definition: fmc.c:425
FMC_ReadUCID
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:143
FMC_KECNT_KEMAX_Msk
#define FMC_KECNT_KEMAX_Msk
Definition: Nano103.h:5980
FMC_ISPCMD_RUN_CKS
#define FMC_ISPCMD_RUN_CKS
Definition: fmc.h:60
FMC_Close
void FMC_Close(void)
Disable FMC ISP function.
Definition: fmc.c:37
FMC_KEYSTS_KEYBUSY_Msk
#define FMC_KEYSTS_KEYBUSY_Msk
Definition: Nano103.h:5959
FMC_SKey_Setup
int32_t FMC_SKey_Setup(uint32_t key[3], uint32_t kpmax, uint32_t kemax, int lock_CONFIG)
Setup security key.
Definition: fmc.c:364
FMC_ISPCMD_READ_PID
#define FMC_ISPCMD_READ_PID
Definition: fmc.h:58
FMC_WriteConfig
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Execute ISP command to write User Configuration.
Definition: fmc.c:251
FMC_ISPTRG_ISPGO_Msk
#define FMC_ISPTRG_ISPGO_Msk
Definition: Nano103.h:5914
FMC_CheckAllOne
uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count)
Run flash all one verification and get result.
Definition: fmc.c:310
FMC_SetVectorPageAddr
void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:187
FMC_Read
uint32_t FMC_Read(uint32_t u32Addr)
Execute ISP command to read a word from flash.
Definition: fmc.c:98
FMC_CONFIG_BASE
#define FMC_CONFIG_BASE
Definition: fmc.h:42
FMC_ISPCMD_PROGRAM
#define FMC_ISPCMD_PROGRAM
Definition: fmc.h:55
FMC_ReadConfig
int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
Execute ISP command to read User Configuration.
Definition: fmc.c:232
FMC_KEYSTS_KEYMATCH_Msk
#define FMC_KEYSTS_KEYMATCH_Msk
Definition: Nano103.h:5965
FMC_DISABLE_CFG_UPDATE
#define FMC_DISABLE_CFG_UPDATE()
Definition: fmc.h:90
FMC_ISPCTL_ISPEN_Msk
#define FMC_ISPCTL_ISPEN_Msk
Definition: Nano103.h:5887
FMC_KEYSTS_CFGFLAG_Msk
#define FMC_KEYSTS_CFGFLAG_Msk
Definition: Nano103.h:5974
FMC_Write
void FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Execute ISP command to program a word to flash.
Definition: fmc.c:213
FMC_Open
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:86
FMC_ISPCMD_PAGE_ERASE
#define FMC_ISPCMD_PAGE_ERASE
Definition: fmc.h:56
FMC_ISPCTL_ISPFF_Msk
#define FMC_ISPCTL_ISPFF_Msk
Definition: Nano103.h:5902
FMC_Erase
int32_t FMC_Erase(uint32_t u32PageAddr)
Execute FMC_ISPCMD_PAGE_ERASE command to erase a flash page. The page size is 512 bytes.
Definition: fmc.c:51
FMC_KEYTRG_TCEN_Msk
#define FMC_KEYTRG_TCEN_Msk
Definition: Nano103.h:5956
FMC_GetChkSum
int32_t FMC_GetChkSum(uint32_t u32Addr, uint32_t u32Count, uint32_t *u32ChkSum)
Run CRC32 checksum calculation and get result.
Definition: fmc.c:272
FMC_ENABLE_CFG_UPDATE
#define FMC_ENABLE_CFG_UPDATE()
Definition: fmc.h:89
FMC_KECNT_KEMAX_Pos
#define FMC_KECNT_KEMAX_Pos
Definition: Nano103.h:5979
FMC_GET_FAIL_FLAG
#define FMC_GET_FAIL_FLAG()
Definition: fmc.h:95
READ_ALLONE_NOT
#define READ_ALLONE_NOT
Definition: fmc.h:70
FMC_ReadDataFlashBaseAddr
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:176
FMC_KPCNT_KPMAX_Msk
#define FMC_KPCNT_KPMAX_Msk
Definition: Nano103.h:5986
FMC_ISPSTS_ISPBUSY_Msk
#define FMC_ISPSTS_ISPBUSY_Msk
Definition: Nano103.h:5926
READ_ALLONE_CMD_FAIL
#define READ_ALLONE_CMD_FAIL
Definition: fmc.h:71
Nano103.h
NANO103 peripheral access layer header file. This file contains all the peripheral register's definit...
FMC_ISPCMD_READ_ALL1
#define FMC_ISPCMD_READ_ALL1
Definition: fmc.h:63
FMC_ReadUID
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:160
FMC_GetBootSource
int32_t FMC_GetBootSource(void)
Get the current boot source.
Definition: fmc.c:73
FMC_GetVectorPageAddr
uint32_t FMC_GetVectorPageAddr(void)
Obtain the current vector page address setting.
Definition: fmc.c:200
FMC_ISPCMD_RUN_ALL1
#define FMC_ISPCMD_RUN_ALL1
Definition: fmc.h:62
FMC_KPROM_BASE
#define FMC_KPROM_BASE
Definition: fmc.h:43
FMC_KEYSTS_KEYLOCK_Msk
#define FMC_KEYSTS_KEYLOCK_Msk
Definition: Nano103.h:5962
FMC_KEYTRG_KEYGO_Msk
#define FMC_KEYTRG_KEYGO_Msk
Definition: Nano103.h:5953
FMC_ISPCMD_READ_UID
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:59
FMC_ISPCMD_READ
#define FMC_ISPCMD_READ
Definition: fmc.h:54
FMC_KEYSTS_FORBID_Msk
#define FMC_KEYSTS_FORBID_Msk
Definition: Nano103.h:5968
FMC_ISPCTL_BS_Msk
#define FMC_ISPCTL_BS_Msk
Definition: Nano103.h:5890
FMC_ReadPID
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:128