| NANO100_BSP V3.05.000 The Board Support Package for Nano100BN Series

fmc.c
Go to the documentation of this file.
1/**************************************************************************/
13//* Includes ------------------------------------------------------------------*/
14#include <stdio.h>
15
16#include "Nano100Series.h"
17
18
32int32_t g_FMC_i32ErrCode;
33
38 void FMC_Close(void)
39{
40 FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk;
41}
42
43
54int32_t FMC_Erase(uint32_t u32PageAddr)
55{
56 int32_t tout = FMC_TIMEOUT_ERASE;
57
58 FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
59 FMC->ISPADR = u32PageAddr;
60 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
61
62 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
63 if ((tout <= 0) || (FMC->ISPCON & FMC_ISPCON_ISPFF_Msk))
64 {
65 g_FMC_i32ErrCode = -1;
66 FMC->ISPCON |= FMC_ISPCON_ISPFF_Msk;
67 return -1;
68 }
69 return 0;
70}
71
72
79int32_t FMC_GetBootSource (void)
80{
81 if (FMC->ISPCON & FMC_ISPCON_BS_Msk)
82 return 1;
83 else
84 return 0;
85}
86
87
92 void FMC_Open(void)
93{
94 FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk;
95}
96
97
104uint32_t FMC_Read(uint32_t u32Addr)
105{
106 int32_t tout = FMC_TIMEOUT_ERASE;
107
108 FMC->ISPCMD = FMC_ISPCMD_READ;
109 FMC->ISPADR = u32Addr;
110 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
111
112 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
113 if (tout <= 0)
114 {
115 g_FMC_i32ErrCode = -1;
116 return 0xFFFFFFFF;
117 }
118 return FMC->ISPDAT;
119}
120
121
129uint32_t FMC_ReadCID(void)
130{
131 int32_t tout = FMC_TIMEOUT_READ;
132
133 FMC->ISPCMD = FMC_ISPCMD_READ_CID;
134 FMC->ISPADR = 0x0;
135 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
136 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
137 if (tout <= 0)
138 {
139 g_FMC_i32ErrCode = -1;
140 return 0xFFFFFFFF;
141 }
142 return FMC->ISPDAT;
143}
144
145
153uint32_t FMC_ReadPID(void)
154{
155 int32_t tout = FMC_TIMEOUT_READ;
156
157 FMC->ISPCMD = FMC_ISPCMD_READ_PID;
158 FMC->ISPADR = 0x04;
159 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
160 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
161 if (tout <= 0)
162 {
163 g_FMC_i32ErrCode = -1;
164 return 0xFFFFFFFF;
165 }
166 return FMC->ISPDAT;
167}
168
169
178uint32_t FMC_ReadUCID(uint32_t u32Index)
179{
180 int32_t tout = FMC_TIMEOUT_READ;
181
182 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
183 FMC->ISPADR = (0x04 * u32Index) + 0x10;
184 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
185
186 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
187 if (tout <= 0)
188 {
189 g_FMC_i32ErrCode = -1;
190 return 0xFFFFFFFF;
191 }
192 return FMC->ISPDAT;
193}
194
195
204uint32_t FMC_ReadUID(uint32_t u32Index)
205{
206 int32_t tout = FMC_TIMEOUT_READ;
207
208 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
209 FMC->ISPADR = 0x04 * u32Index;
210 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
211
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
226uint32_t FMC_ReadDataFlashBaseAddr(void)
227{
228 return FMC->DFBADR;
229}
230
231
239 void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
240{
241 int32_t tout = FMC_TIMEOUT_WRITE;
242
243 FMC->ISPCMD = FMC_ISPCMD_VECMAP;
244 FMC->ISPADR = u32PageAddr;
245 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
246 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
247 if (tout <= 0)
248 g_FMC_i32ErrCode = -1;
249}
250
251
256uint32_t FMC_GetVectorPageAddr(void)
257{
258 return (FMC->ISPSTA & 0x0FFFFF00ul);
259}
260
261
269int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
270{
271 int32_t tout = FMC_TIMEOUT_WRITE;
272
273 FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
274 FMC->ISPADR = u32Addr;
275 FMC->ISPDAT = u32Data;
276 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
277 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
278 if ((tout <= 0) || (FMC->ISPSTA & FMC_ISPSTA_ISPFF_Msk))
279 {
280 g_FMC_i32ErrCode = -1;
281 return -1;
282 }
283 return 0;
284}
285
286
296int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
297{
298 u32Config[0] = FMC_Read(FMC_CONFIG_BASE);
299 if (u32Count < 2)
300 return -1;
301 u32Config[1] = FMC_Read(FMC_CONFIG_BASE+4);
302 return 0;
303}
304
305
315int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
316{
317 FMC_ENABLE_CFG_UPDATE();
318 FMC_Erase(FMC_CONFIG_BASE);
319 FMC_Write(FMC_CONFIG_BASE, u32Config[0]);
320 FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]);
321 FMC_DISABLE_CFG_UPDATE();
322 return 0;
323}
324
325 /* end of group NANO100_FMC_EXPORTED_FUNCTIONS */
327 /* end of group NANO100_FMC_Driver */
329 /* end of group NANO100_Device_Driver */
331
332/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
333
334
Nano100Series.h
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
FMC_ISPCON_ISPFF_Msk
#define FMC_ISPCON_ISPFF_Msk
Definition: Nano100Series.h:1951
FMC_ISPCON_BS_Msk
#define FMC_ISPCON_BS_Msk
Definition: Nano100Series.h:1939
FMC_ISPTRG_ISPGO_Msk
#define FMC_ISPTRG_ISPGO_Msk
Definition: Nano100Series.h:1969
FMC_ISPCON_ISPEN_Msk
#define FMC_ISPCON_ISPEN_Msk
Definition: Nano100Series.h:1936
FMC_ISPSTA_ISPFF_Msk
#define FMC_ISPSTA_ISPFF_Msk
Definition: Nano100Series.h:1981
FMC_TIMEOUT_ERASE
#define FMC_TIMEOUT_ERASE
Definition: fmc.h:49
FMC_ISPCMD_PROGRAM
#define FMC_ISPCMD_PROGRAM
Definition: fmc.h:56
FMC_CONFIG_BASE
#define FMC_CONFIG_BASE
Definition: fmc.h:42
FMC_ISPCMD_READ_CID
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:58
FMC_ISPCMD_READ_PID
#define FMC_ISPCMD_READ_PID
Definition: fmc.h:59
FMC_TIMEOUT_READ
#define FMC_TIMEOUT_READ
Definition: fmc.h:47
FMC_ISPCMD_READ
#define FMC_ISPCMD_READ
Definition: fmc.h:55
FMC_ISPCMD_VECMAP
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:61
FMC_TIMEOUT_WRITE
#define FMC_TIMEOUT_WRITE
Definition: fmc.h:48
FMC_ISPCMD_READ_UID
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:60
FMC_ISPCMD_PAGE_ERASE
#define FMC_ISPCMD_PAGE_ERASE
Definition: fmc.h:57
FMC_Close
void FMC_Close(void)
Disable FMC ISP function.
Definition: fmc.c:38
g_FMC_i32ErrCode
int32_t g_FMC_i32ErrCode
Definition: fmc.c:32
FMC_ReadConfig
int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
Execute ISP command to read User Configuration.
Definition: fmc.c:296
FMC_SetVectorPageAddr
void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:239
FMC_ReadCID
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:129
FMC_GetVectorPageAddr
uint32_t FMC_GetVectorPageAddr(void)
Obtain the current vector page address setting.
Definition: fmc.c:256
FMC_ReadUID
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:204
FMC_Erase
int32_t FMC_Erase(uint32_t u32PageAddr)
Execute ISP command to erase a flash page. The page size is 512 bytes.
Definition: fmc.c:54
FMC_ReadDataFlashBaseAddr
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:226
FMC_Write
int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Execute ISP command to program a word to flash.
Definition: fmc.c:269
FMC_DISABLE_CFG_UPDATE
#define FMC_DISABLE_CFG_UPDATE()
This macro disables User Configuration update function.
Definition: fmc.h:124
FMC_Read
uint32_t FMC_Read(uint32_t u32Addr)
Execute ISP command to read a word from flash.
Definition: fmc.c:104
FMC_ENABLE_CFG_UPDATE
#define FMC_ENABLE_CFG_UPDATE()
This macro enables User Configuration update function.
Definition: fmc.h:116
FMC_ReadPID
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:153
FMC_GetBootSource
int32_t FMC_GetBootSource(void)
Get the current boot source.
Definition: fmc.c:79
FMC_WriteConfig
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Execute ISP command to write User Configuration.
Definition: fmc.c:315
FMC_Open
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:92
FMC_ReadUCID
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:178
FMC
#define FMC
Pointer to FMC register structure.
Definition: Nano100Series.h:11711