NANO102/112 BSP V3.03.003
The Board Support Package for Nano102/112 Series
fmc.c
Go to the documentation of this file.
1/**************************************************************************/
13//* Includes ------------------------------------------------------------------*/
14#include <stdio.h>
15
16#include "Nano1X2Series.h"
17
18
33
38void FMC_Close(void)
39{
40 FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk;
41}
42
43
52int32_t FMC_Erase(uint32_t u32PageAddr)
53{
54 int32_t tout = FMC_TIMEOUT_ERASE;
55
56 FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
57 FMC->ISPADR = u32PageAddr;
58 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
59
60 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
61 if ((tout <= 0) || (FMC->ISPCON & FMC_ISPCON_ISPFF_Msk))
62 {
64 FMC->ISPCON |= FMC_ISPCON_ISPFF_Msk;
65 return -1;
66 }
67 return 0;
68}
69
70
77int32_t FMC_GetBootSource (void)
78{
79 if (FMC->ISPCON & FMC_ISPCON_BS_Msk)
80 return 1;
81 else
82 return 0;
83}
84
85
90void FMC_Open(void)
91{
92 FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk;
93}
94
95
102uint32_t FMC_Read(uint32_t u32Addr)
103{
104 int32_t tout = FMC_TIMEOUT_ERASE;
105
106 FMC->ISPCMD = FMC_ISPCMD_READ;
107 FMC->ISPADR = u32Addr;
108 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
109
110 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
111 if (tout <= 0)
112 {
113 g_FMC_i32ErrCode = -1;
114 return 0xFFFFFFFF;
115 }
116 return FMC->ISPDAT;
117}
118
119
127uint32_t FMC_ReadCID(void)
128{
129 int32_t tout = FMC_TIMEOUT_READ;
130
131 FMC->ISPCMD = FMC_ISPCMD_READ_CID;
132 FMC->ISPADR = 0x0;
133 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
134 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
135 if (tout <= 0)
136 {
137 g_FMC_i32ErrCode = -1;
138 return 0xFFFFFFFF;
139 }
140 return FMC->ISPDAT;
141}
142
143
151uint32_t FMC_ReadPID(void)
152{
153 int32_t tout = FMC_TIMEOUT_READ;
154
155 FMC->ISPCMD = FMC_ISPCMD_READ_PID;
156 FMC->ISPADR = 0x04;
157 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
158 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
159 if (tout <= 0)
160 {
161 g_FMC_i32ErrCode = -1;
162 return 0xFFFFFFFF;
163 }
164 return FMC->ISPDAT;
165}
166
167
176uint32_t FMC_ReadUCID(uint32_t u32Index)
177{
178 int32_t tout = FMC_TIMEOUT_READ;
179
180 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
181 FMC->ISPADR = (0x04 * u32Index) + 0x10;
182 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
183
184 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
185 if (tout <= 0)
186 {
187 g_FMC_i32ErrCode = -1;
188 return 0xFFFFFFFF;
189 }
190 return FMC->ISPDAT;
191}
192
193
202uint32_t FMC_ReadUID(uint32_t u32Index)
203{
204 int32_t tout = FMC_TIMEOUT_READ;
205
206 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
207 FMC->ISPADR = 0x04 * u32Index;
208 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
209
210 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
211 if (tout <= 0)
212 {
213 g_FMC_i32ErrCode = -1;
214 return 0xFFFFFFFF;
215 }
216 return FMC->ISPDAT;
217}
218
219
225{
226 return FMC->DFBADR;
227}
228
229
235void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
236{
237 int32_t tout = FMC_TIMEOUT_WRITE;
238
239 FMC->ISPCMD = FMC_ISPCMD_VECMAP;
240 FMC->ISPADR = u32PageAddr;
241 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
242 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
243 if (tout <= 0)
244 g_FMC_i32ErrCode = -1;
245}
246
247
253{
254 return (FMC->ISPSTA & 0x0FFFFF00ul);
255}
256
257
265int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
266{
267 int32_t tout = FMC_TIMEOUT_WRITE;
268
269 FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
270 FMC->ISPADR = u32Addr;
271 FMC->ISPDAT = u32Data;
272 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
273 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) {}
274 if ((tout <= 0) || (FMC->ISPSTA & FMC_ISPSTA_ISPFF_Msk))
275 {
276 g_FMC_i32ErrCode = -1;
277 return -1;
278 }
279 return 0;
280}
281
282
292int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
293{
294 u32Config[0] = FMC_Read(FMC_CONFIG_BASE);
295 if (u32Count < 2)
296 return -1;
297 u32Config[1] = FMC_Read(FMC_CONFIG_BASE+4);
298 return 0;
299}
300
301
311int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
312{
315 FMC_Write(FMC_CONFIG_BASE, u32Config[0]);
316 FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]);
318 return 0;
319}
320
321 /* end of group NANO1X2_FMC_EXPORTED_FUNCTIONS */
323 /* end of group NANO1X2_FMC_Driver */
325 /* end of group NANO1X2_Device_Driver */
327
328/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
329
330
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
#define FMC_ISPCON_ISPFF_Msk
#define FMC_ISPCON_BS_Msk
#define FMC_ISPTRG_ISPGO_Msk
#define FMC_ISPCON_ISPEN_Msk
#define FMC_ISPSTA_ISPFF_Msk
#define FMC_TIMEOUT_ERASE
Definition: fmc.h:51
#define FMC_ISPCMD_PROGRAM
Definition: fmc.h:58
#define FMC_CONFIG_BASE
Definition: fmc.h:44
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:60
#define FMC_ISPCMD_READ_PID
Definition: fmc.h:61
#define FMC_TIMEOUT_READ
Definition: fmc.h:49
#define FMC_ISPCMD_READ
Definition: fmc.h:57
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:63
#define FMC_TIMEOUT_WRITE
Definition: fmc.h:50
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:62
#define FMC_ISPCMD_PAGE_ERASE
Definition: fmc.h:59
void FMC_Close(void)
Disable FMC ISP function.
Definition: fmc.c:38
int32_t g_FMC_i32ErrCode
Definition: fmc.c:32
int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
Execute ISP command to read User Configuration.
Definition: fmc.c:292
void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:235
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:127
uint32_t FMC_GetVectorPageAddr(void)
Obtain the current vector page address setting.
Definition: fmc.c:252
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:202
int32_t FMC_Erase(uint32_t u32PageAddr)
Execute ISP command to erase a flash page. The page size is 512 bytes.
Definition: fmc.c:52
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:224
int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Execute ISP command to program a word to flash.
Definition: fmc.c:265
#define FMC_DISABLE_CFG_UPDATE()
Definition: fmc.h:85
uint32_t FMC_Read(uint32_t u32Addr)
Execute ISP command to read a word from flash.
Definition: fmc.c:102
#define FMC_ENABLE_CFG_UPDATE()
Definition: fmc.h:84
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:151
int32_t FMC_GetBootSource(void)
Get the current boot source.
Definition: fmc.c:77
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Execute ISP command to write User Configuration.
Definition: fmc.c:311
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:90
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:176
#define FMC
Pointer to FMC register structure.