| NUC472_NUC442_BSP V3.04.000 The Board Support Package for NUC472/NUC442

fmc.c
Go to the documentation of this file.
1/**************************************************************************/
13//* Includes ------------------------------------------------------------------*/
14#include <stdio.h>
15#include "NUC472_442.h"
16
17
31int32_t g_FMC_i32ErrCode;
32
37 void FMC_Close(void)
38{
39 FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk;
40}
41
42
53int32_t FMC_Erase(uint32_t u32PageAddr)
54{
55 int32_t tout;
56
57 FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
58 FMC->ISPADDR = u32PageAddr;
59 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
60
61 tout = FMC_TIMEOUT_ERASE;
62 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
63 if (tout <= 0)
64 {
65 g_FMC_i32ErrCode = -1;
66 return -1;
67 }
68
69 if (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk)
70 {
71 FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk;
72 return -1;
73 }
74 return 0;
75}
76
77
84int32_t FMC_GetBootSource (void)
85{
86 if (FMC->ISPCTL & FMC_ISPCTL_BS_Msk)
87 return 1;
88 else
89 return 0;
90}
91
92
97 void FMC_Open(void)
98{
99 FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk;
100}
101
102
113uint32_t FMC_Read(uint32_t u32Addr)
114{
115 int32_t tout;
116
117 FMC->ISPCMD = FMC_ISPCMD_READ;
118 FMC->ISPADDR = u32Addr;
119 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
120
121 tout = FMC_TIMEOUT_READ;
122 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
123 if (tout <= 0)
124 {
125 g_FMC_i32ErrCode = -1;
126 return 0xFFFFFFFF;
127 }
128 return FMC->ISPDAT;
129}
130
131
143int32_t FMC_Read_64(uint32_t u32Addr, uint32_t *u32Data0, uint32_t *u32Data1)
144{
145 int32_t tout;
146
147 FMC->ISPCMD = FMC_ISPCMD_READ_64;
148 FMC->ISPADDR = u32Addr;
149 FMC->ISPDAT = 0x0;
150 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
151
152 tout = FMC_TIMEOUT_READ;
153 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
154 if (tout <= 0)
155 {
156 g_FMC_i32ErrCode = -1;
157 return -1;
158 }
159
160 *u32Data0 = FMC->MPDAT0;
161 *u32Data1 = FMC->MPDAT1;
162 return 0;
163}
164
165
175uint32_t FMC_ReadCID(void)
176{
177 int32_t tout = FMC_TIMEOUT_READ;
178
179 FMC->ISPCMD = FMC_ISPCMD_READ_CID;
180 FMC->ISPADDR = 0x0;
181 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
182
183 while (tout-- > 0)
184 {
185 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
186 {
187 if (FMC->ISPDAT != 0xDA)
188 g_FMC_i32ErrCode = -1;
189 return FMC->ISPDAT;
190 }
191 }
192 g_FMC_i32ErrCode = -1;
193 return 0xFFFFFFFF;
194}
195
196
206uint32_t FMC_ReadPID(void)
207{
208 int32_t tout = FMC_TIMEOUT_READ;
209
210 FMC->ISPCMD = FMC_ISPCMD_READ_PID;
211 FMC->ISPADDR = 0x04;
212 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
213
214 while (tout-- > 0)
215 {
216 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
217 return FMC->ISPDAT;
218 }
219 g_FMC_i32ErrCode = -1;
220 return 0xFFFFFFFF;
221}
222
223
233uint32_t FMC_ReadUCID(uint32_t u32Index)
234{
235 int32_t tout = FMC_TIMEOUT_READ;
236
237 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
238 FMC->ISPADDR = (0x04 * u32Index) + 0x10;
239 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
240
241 while (tout-- > 0)
242 {
243 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
244 return FMC->ISPDAT;
245 }
246 g_FMC_i32ErrCode = -1;
247 return 0xFFFFFFFF;
248}
249
250
260uint32_t FMC_ReadUID(uint32_t u32Index)
261{
262 int32_t tout = FMC_TIMEOUT_READ;
263
264 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
265 FMC->ISPADDR = 0x04 * u32Index;
266 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
267
268 while (tout-- > 0)
269 {
270 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
271 return FMC->ISPDAT;
272 }
273 g_FMC_i32ErrCode = -1;
274 return 0xFFFFFFFF;
275}
276
277
282uint32_t FMC_ReadDataFlashBaseAddr(void)
283{
284 return FMC->DFBA;
285}
286
287
297int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
298{
299 int32_t tout = FMC_TIMEOUT_WRITE;
300
301 FMC->ISPCMD = FMC_ISPCMD_VECMAP;
302 FMC->ISPADDR = u32PageAddr;
303 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
304
305 while (tout-- > 0)
306 {
307 if (!(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) /* Waiting for ISP Done */
308 return 0;
309 }
310 g_FMC_i32ErrCode = -1;
311 return -1;
312}
313
314
319uint32_t FMC_GetVectorPageAddr(void)
320{
321 return (FMC->ISPSTS & 0x0FFFFF00ul);
322}
323
324
337int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
338{
339 int32_t tout;
340
341 FMC->ISPCMD = FMC_ISPCMD_WRITE;
342 FMC->ISPADDR = u32Addr;
343 FMC->ISPDAT = u32Data;
344 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
345
346 tout = FMC_TIMEOUT_WRITE;
347 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
348 if (tout <= 0)
349 {
350 g_FMC_i32ErrCode = -1;
351 return -1;
352 }
353 if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
354 {
355 FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
356 g_FMC_i32ErrCode = -1;
357 return -1;
358 }
359 return 0;
360}
361
362
374int32_t FMC_Write_64(uint32_t u32Addr, uint32_t u32Data0, uint32_t u32Data1)
375{
376 int32_t tout;
377
378 FMC->ISPCMD = FMC_ISPCMD_WRITE_64;
379 FMC->ISPADDR = u32Addr;
380 FMC->MPDAT0 = u32Data0;
381 FMC->MPDAT1 = u32Data1;
382 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
383
384 tout = FMC_TIMEOUT_WRITE;
385 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
386 if (tout <= 0)
387 {
388 g_FMC_i32ErrCode = -1;
389 return -1;
390 }
391 if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
392 {
393 FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
394 g_FMC_i32ErrCode = -1;
395 return -1;
396 }
397 return 0;
398}
399
400
407uint32_t FMC_CRC8(uint32_t au32Data[], int i32Count)
408{
409 int i32ByteIdx;
410 uint8_t i, u8Cnt, u8InData;
411 uint8_t au8CRC[4] = { 0xff, 0xff, 0xff, 0xff };
412
413 for (i32ByteIdx = 0; i32ByteIdx < 4; i32ByteIdx++)
414 {
415 for (u8Cnt = 0; u8Cnt < i32Count; u8Cnt++)
416 {
417 for (i = 0x80; i != 0; i /= 2)
418 {
419 if ((au8CRC[i32ByteIdx] & 0x80)!=0)
420 {
421 au8CRC[i32ByteIdx] *= 2;
422 au8CRC[i32ByteIdx] ^= 7;
423 }
424 else
425 au8CRC[i32ByteIdx] *= 2;
426
427 u8InData = (au32Data[u8Cnt] >> (i32ByteIdx * 8)) & 0xff;
428
429 if ((u8InData & i) != 0)
430 au8CRC[i32ByteIdx]^=0x7;
431 }
432 }
433 }
434 return (au8CRC[0] | au8CRC[1] << 8 | au8CRC[2] << 16 | au8CRC[3] << 24);
435}
436
437
446int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
447{
448 int i;
449
450 for (i = 0; i < u32Count; i++)
451 {
452 u32Config[i] = FMC_Read(FMC_CONFIG_BASE + i*4);
453 }
454
455 if (FMC->ISPSTS & FMC_ISPSTS_CFGCRCF_Msk)
456 return -1;
457
458 return 0;
459}
460
461
473int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
474{
475 uint32_t i, u32CRC, u32Data;
476
477 if (u32Count != 4)
478 return -1;
479
480 if (FMC_Erase(FMC_CONFIG_BASE) != 0)
481 return -1;
482
483 u32CRC = FMC_CRC8(u32Config, 3);
484
485 for (i = 0; i < 4; i++)
486 {
487 if (FMC_Write(FMC_CONFIG_BASE + i * 4, (i < 3) ? u32Config[i] : u32CRC) != 0)
488 return -1;
489 }
490
491 for (i = 0; i < 4; i++)
492 {
493 u32Data = FMC_Read(FMC_CONFIG_BASE + i * 4);
494
495 if (u32Data != ((i < 3) ? u32Config[i] : u32CRC))
496 return -1;
497 }
498 return 0;
499}
500
501 /* end of group NUC472_442_FMC_EXPORTED_FUNCTIONS */
503 /* end of group NUC472_442_FMC_Driver */
505 /* end of group NUC472_442_Device_Driver */
507
508/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
509
510
NUC472_442.h
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
FMC_ISPSTS_CFGCRCF_Msk
#define FMC_ISPSTS_CFGCRCF_Msk
Definition: NUC472_442.h:14962
FMC_ISPCTL_ISPFF_Msk
#define FMC_ISPCTL_ISPFF_Msk
Definition: NUC472_442.h:14929
FMC_ISPSTS_ISPFF_Msk
#define FMC_ISPSTS_ISPFF_Msk
Definition: NUC472_442.h:14956
FMC_ISPCTL_BS_Msk
#define FMC_ISPCTL_BS_Msk
Definition: NUC472_442.h:14917
FMC_ISPCTL_ISPEN_Msk
#define FMC_ISPCTL_ISPEN_Msk
Definition: NUC472_442.h:14914
FMC_ISPTRG_ISPGO_Msk
#define FMC_ISPTRG_ISPGO_Msk
Definition: NUC472_442.h:14941
FMC_ISPCMD_READ_64
#define FMC_ISPCMD_READ_64
Definition: fmc.h:51
FMC_TIMEOUT_ERASE
#define FMC_TIMEOUT_ERASE
Definition: fmc.h:67
FMC_CONFIG_BASE
#define FMC_CONFIG_BASE
Definition: fmc.h:42
FMC_ISPCMD_READ_CID
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:56
FMC_ISPCMD_READ_PID
#define FMC_ISPCMD_READ_PID
Definition: fmc.h:57
FMC_TIMEOUT_READ
#define FMC_TIMEOUT_READ
Definition: fmc.h:65
FMC_ISPCMD_READ
#define FMC_ISPCMD_READ
Definition: fmc.h:50
FMC_ISPCMD_VECMAP
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:60
FMC_ISPCMD_WRITE
#define FMC_ISPCMD_WRITE
Definition: fmc.h:53
FMC_TIMEOUT_WRITE
#define FMC_TIMEOUT_WRITE
Definition: fmc.h:66
FMC_ISPCMD_WRITE_64
#define FMC_ISPCMD_WRITE_64
Definition: fmc.h:54
FMC_ISPCMD_READ_UID
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:58
FMC_ISPCMD_PAGE_ERASE
#define FMC_ISPCMD_PAGE_ERASE
Definition: fmc.h:55
FMC_Close
void FMC_Close(void)
Disable FMC ISP function.
Definition: fmc.c:37
FMC_Write_64
int32_t FMC_Write_64(uint32_t u32Addr, uint32_t u32Data0, uint32_t u32Data1)
Execute ISP 64-bits write command to program two words to flash.
Definition: fmc.c:374
g_FMC_i32ErrCode
int32_t g_FMC_i32ErrCode
Definition: fmc.c:31
FMC_ReadConfig
int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
Read the User Configuration words.
Definition: fmc.c:446
FMC_ReadCID
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:175
FMC_GetVectorPageAddr
uint32_t FMC_GetVectorPageAddr(void)
Obtain the current vector page address setting.
Definition: fmc.c:319
FMC_ReadUID
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:260
FMC_Erase
int32_t FMC_Erase(uint32_t u32PageAddr)
Erase a page. The page size is 2048 bytes.
Definition: fmc.c:53
FMC_ReadDataFlashBaseAddr
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:282
FMC_Write
int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Execute ISP command to program a word to flash.
Definition: fmc.c:337
FMC_Read
uint32_t FMC_Read(uint32_t u32Addr)
Execute ISP command to read a word from flash.
Definition: fmc.c:113
FMC_ReadPID
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:206
FMC_CRC8
uint32_t FMC_CRC8(uint32_t au32Data[], int i32Count)
CRC8 Calculator.
Definition: fmc.c:407
FMC_GetBootSource
int32_t FMC_GetBootSource(void)
Get the current boot source.
Definition: fmc.c:84
FMC_WriteConfig
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Write User Configuration.
Definition: fmc.c:473
FMC_Read_64
int32_t FMC_Read_64(uint32_t u32Addr, uint32_t *u32Data0, uint32_t *u32Data1)
Execute ISP 64-bits read command to read two words from flash.
Definition: fmc.c:143
FMC_SetVectorPageAddr
int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:297
FMC_Open
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:97
FMC_ReadUCID
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:233
FMC
#define FMC
Definition: NUC472_442.h:28820