| NUC029FAE_BSP V3.02.000 The Board Support Package for NUC029FAE MCU

fmc.c
Go to the documentation of this file.
1/**************************************************************************/
13//* Includes ------------------------------------------------------------------*/
14#include <stdio.h>
15#include "NUC029FAE.h"
16
30int32_t g_FMC_i32ErrCode;
31
37 void FMC_SetBootSource (int32_t i32BootSrc)
38{
39 if (i32BootSrc == 1)
40 FMC->ISPCON |= FMC_ISPCON_BS_Msk;
41 else
42 FMC->ISPCON &= ~FMC_ISPCON_BS_Msk;
43}
44
45
49 void FMC_Close(void)
50{
51 FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk;
52}
53
54
58 void FMC_DisableAPUpdate(void)
59{
60 FMC->ISPCON &= ~FMC_ISPCON_APUEN_Msk;
61}
62
63
67 void FMC_DisableConfigUpdate(void)
68{
69 FMC->ISPCON &= ~FMC_ISPCON_CFGUEN_Msk;
70}
71
72
76 void FMC_DisableLDUpdate(void)
77{
78 FMC->ISPCON &= ~FMC_ISPCON_LDUEN_Msk;
79}
80
81
85 void FMC_EnableAPUpdate(void)
86{
87 FMC->ISPCON |= FMC_ISPCON_APUEN_Msk;
88}
89
90
94 void FMC_EnableConfigUpdate(void)
95{
96 FMC->ISPCON |= FMC_ISPCON_CFGUEN_Msk;
97}
98
99
103 void FMC_EnableLDUpdate(void)
104{
105 FMC->ISPCON |= FMC_ISPCON_LDUEN_Msk;
106}
107
108
115int32_t FMC_Erase(uint32_t u32PageAddr)
116{
117 int32_t tout = FMC_TIMEOUT_ERASE;
118
119 FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
120 FMC->ISPADR = u32PageAddr;
121 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
122
123 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
124 if ((tout <= 0) || (FMC->ISPCON & FMC_ISPCON_ISPFF_Msk))
125 {
126 FMC->ISPCON |= FMC_ISPCON_ISPFF_Msk;
127 g_FMC_i32ErrCode = -1;
128 return -1;
129 }
130 return 0;
131}
132
133
139int32_t FMC_GetBootSource (void)
140{
141 if (FMC->ISPCON & FMC_ISPCON_BS_Msk)
142 return 1;
143 else
144 return 0;
145}
146
147
151 void FMC_Open(void)
152{
153 FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk;
154}
155
156
162uint32_t FMC_Read(uint32_t u32Addr)
163{
164 int32_t tout = FMC_TIMEOUT_ERASE;
165
166 FMC->ISPCMD = FMC_ISPCMD_READ;
167 FMC->ISPADR = u32Addr;
168 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
169
170 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
171 if (tout <= 0)
172 {
173 g_FMC_i32ErrCode = -1;
174 return 0xFFFFFFFF;
175 }
176 return FMC->ISPDAT;
177}
178
179
184uint32_t FMC_ReadCID(void)
185{
186 int32_t tout = FMC_TIMEOUT_READ;
187
188 FMC->ISPCMD = FMC_ISPCMD_READ_CID;
189 FMC->ISPADR = 0x0;
190 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
191 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
192 if (tout <= 0)
193 {
194 g_FMC_i32ErrCode = -1;
195 return 0xFFFFFFFF;
196 }
197 return FMC->ISPDAT;
198}
199
200
205uint32_t FMC_ReadPID(void)
206{
207 int32_t tout = FMC_TIMEOUT_READ;
208
209 FMC->ISPCMD = FMC_ISPCMD_READ_PID;
210 FMC->ISPADR = 0x04;
211 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
212 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
213 if (tout <= 0)
214 {
215 g_FMC_i32ErrCode = -1;
216 return 0xFFFFFFFF;
217 }
218 return FMC->ISPDAT;
219}
220
221
227uint32_t FMC_ReadUCID(uint32_t u32Index)
228{
229 int32_t tout = FMC_TIMEOUT_READ;
230
231 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
232 FMC->ISPADR = (0x04 * u32Index) + 0x10;
233 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
234
235 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
236 if (tout <= 0)
237 {
238 g_FMC_i32ErrCode = -1;
239 return 0xFFFFFFFF;
240 }
241 return FMC->ISPDAT;
242}
243
244
250uint32_t FMC_ReadUID(uint32_t u32Index)
251{
252 int32_t tout = FMC_TIMEOUT_READ;
253
254 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
255 FMC->ISPADR = 0x04 * u32Index;
256 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
257
258 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
259 if (tout <= 0)
260 {
261 g_FMC_i32ErrCode = -1;
262 return 0xFFFFFFFF;
263 }
264 return FMC->ISPDAT;
265}
266
267
272uint32_t FMC_ReadDataFlashBaseAddr(void)
273{
274 return FMC->DFBADR;
275}
276
277
282 void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
283{
284 int32_t tout = FMC_TIMEOUT_WRITE;
285
286 FMC->ISPCMD = FMC_ISPCMD_VECMAP;
287 FMC->ISPADR = u32PageAddr;
288 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
289 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
290 if (tout <= 0)
291 g_FMC_i32ErrCode = -1;
292}
293
294
300int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
301{
302 int32_t tout = FMC_TIMEOUT_WRITE;
303
304 FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
305 FMC->ISPADR = u32Addr;
306 FMC->ISPDAT = u32Data;
307 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
308 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {
309 if(tout-- <= 0) {
310 g_FMC_i32ErrCode = -1;
311 return -1;
312 }
313
314 }
315 return 0;
316}
317
318
326int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
327{
328 u32Config[0] = FMC_Read(FMC_CONFIG_BASE);
329 if (u32Count < 2)
330 return 0;
331 u32Config[1] = FMC_Read(FMC_CONFIG_BASE+4);
332 return 0;
333}
334
335
343int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
344{
345 FMC_EnableConfigUpdate();
346 FMC_Erase(FMC_CONFIG_BASE);
347 FMC_Write(FMC_CONFIG_BASE, u32Config[0]);
348 FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]);
349 FMC_DisableConfigUpdate();
350 return 0;
351}
352
353 /* end of group NUC029FAE_FMC_EXPORTED_FUNCTIONS */
355 /* end of group NUC029FAE_FMC_Driver */
357 /* end of group NUC029FAE_Device_Driver */
359
360/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
361
362
NUC029FAE.h
NUC029FAE peripheral access layer header file. This file contains all the peripheral register's defin...
FMC_TIMEOUT_ERASE
#define FMC_TIMEOUT_ERASE
Definition: fmc.h:44
FMC_ISPCMD_PROGRAM
#define FMC_ISPCMD_PROGRAM
Definition: fmc.h:51
FMC_CONFIG_BASE
#define FMC_CONFIG_BASE
Definition: fmc.h:37
FMC_ISPCMD_READ_CID
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:53
FMC_ISPCMD_READ_PID
#define FMC_ISPCMD_READ_PID
Definition: fmc.h:54
FMC_TIMEOUT_READ
#define FMC_TIMEOUT_READ
Definition: fmc.h:42
FMC_ISPCMD_READ
#define FMC_ISPCMD_READ
Definition: fmc.h:50
FMC_ISPCMD_VECMAP
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:56
FMC_TIMEOUT_WRITE
#define FMC_TIMEOUT_WRITE
Definition: fmc.h:43
FMC_ISPCMD_READ_UID
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:55
FMC_ISPCMD_PAGE_ERASE
#define FMC_ISPCMD_PAGE_ERASE
Definition: fmc.h:52
FMC_DisableLDUpdate
void FMC_DisableLDUpdate(void)
Disable LDROM update function.
Definition: fmc.c:76
FMC_Close
void FMC_Close(void)
Disable all FMC functions.
Definition: fmc.c:49
g_FMC_i32ErrCode
int32_t g_FMC_i32ErrCode
Definition: fmc.c:30
FMC_EnableLDUpdate
void FMC_EnableLDUpdate(void)
Enable LDROM update function.
Definition: fmc.c:103
FMC_ReadConfig
int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
Read the User Configuration words.
Definition: fmc.c:326
FMC_SetVectorPageAddr
void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:282
FMC_ReadCID
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:184
FMC_ReadUID
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:250
FMC_EnableAPUpdate
void FMC_EnableAPUpdate(void)
Enable APROM update function.
Definition: fmc.c:85
FMC_Erase
int32_t FMC_Erase(uint32_t u32PageAddr)
Erase a page. The page size is 512 bytes.
Definition: fmc.c:115
FMC_ReadDataFlashBaseAddr
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:272
FMC_SetBootSource
void FMC_SetBootSource(int32_t i32BootSrc)
Set boot source of next software reset.
Definition: fmc.c:37
FMC_Write
int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Writes a word data to specified flash address.
Definition: fmc.c:300
FMC_DisableAPUpdate
void FMC_DisableAPUpdate(void)
Disable APROM update function.
Definition: fmc.c:58
FMC_Read
uint32_t FMC_Read(uint32_t u32Addr)
Read a word from specified flash address.
Definition: fmc.c:162
FMC_ReadPID
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:205
FMC_GetBootSource
int32_t FMC_GetBootSource(void)
get the current boot source
Definition: fmc.c:139
FMC_WriteConfig
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Write User Configuration.
Definition: fmc.c:343
FMC_DisableConfigUpdate
void FMC_DisableConfigUpdate(void)
Disable User Configuration update function.
Definition: fmc.c:67
FMC_Open
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:151
FMC_EnableConfigUpdate
void FMC_EnableConfigUpdate(void)
Enable User Configuration update function.
Definition: fmc.c:94
FMC_ReadUCID
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:227
FMC
#define FMC
Pointer to FMC register structure.
Definition: NUC029FAE.h:3243