| NANO102/112 BSP V3.04.000 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
32int32_t g_FMC_i32ErrCode;
33
38 void 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 {
63 g_FMC_i32ErrCode = -1;
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
90 void 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
224uint32_t FMC_ReadDataFlashBaseAddr(void)
225{
226 return FMC->DFBADR;
227}
228
229
235 void 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
252uint32_t FMC_GetVectorPageAddr(void)
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{
313 FMC_ENABLE_CFG_UPDATE();
314 FMC_Erase(FMC_CONFIG_BASE);
315 FMC_Write(FMC_CONFIG_BASE, u32Config[0]);
316 FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]);
317 FMC_DISABLE_CFG_UPDATE();
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
Nano1X2Series.h
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
FMC_ISPCON_ISPFF_Msk
#define FMC_ISPCON_ISPFF_Msk
Definition: Nano1X2Series.h:2632
FMC_ISPCON_BS_Msk
#define FMC_ISPCON_BS_Msk
Definition: Nano1X2Series.h:2620
FMC_ISPTRG_ISPGO_Msk
#define FMC_ISPTRG_ISPGO_Msk
Definition: Nano1X2Series.h:2650
FMC_ISPCON_ISPEN_Msk
#define FMC_ISPCON_ISPEN_Msk
Definition: Nano1X2Series.h:2617
FMC_ISPSTA_ISPFF_Msk
#define FMC_ISPSTA_ISPFF_Msk
Definition: Nano1X2Series.h:2665
FMC_TIMEOUT_ERASE
#define FMC_TIMEOUT_ERASE
Definition: fmc.h:51
FMC_ISPCMD_PROGRAM
#define FMC_ISPCMD_PROGRAM
Definition: fmc.h:58
FMC_CONFIG_BASE
#define FMC_CONFIG_BASE
Definition: fmc.h:44
FMC_ISPCMD_READ_CID
#define FMC_ISPCMD_READ_CID
Definition: fmc.h:60
FMC_ISPCMD_READ_PID
#define FMC_ISPCMD_READ_PID
Definition: fmc.h:61
FMC_TIMEOUT_READ
#define FMC_TIMEOUT_READ
Definition: fmc.h:49
FMC_ISPCMD_READ
#define FMC_ISPCMD_READ
Definition: fmc.h:57
FMC_ISPCMD_VECMAP
#define FMC_ISPCMD_VECMAP
Definition: fmc.h:63
FMC_TIMEOUT_WRITE
#define FMC_TIMEOUT_WRITE
Definition: fmc.h:50
FMC_ISPCMD_READ_UID
#define FMC_ISPCMD_READ_UID
Definition: fmc.h:62
FMC_ISPCMD_PAGE_ERASE
#define FMC_ISPCMD_PAGE_ERASE
Definition: fmc.h:59
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:292
FMC_SetVectorPageAddr
void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:235
FMC_ReadCID
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:127
FMC_GetVectorPageAddr
uint32_t FMC_GetVectorPageAddr(void)
Obtain the current vector page address setting.
Definition: fmc.c:252
FMC_ReadUID
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:202
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:52
FMC_ReadDataFlashBaseAddr
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:224
FMC_Write
int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Execute ISP command to program a word to flash.
Definition: fmc.c:265
FMC_DISABLE_CFG_UPDATE
#define FMC_DISABLE_CFG_UPDATE()
Definition: fmc.h:85
FMC_Read
uint32_t FMC_Read(uint32_t u32Addr)
Execute ISP command to read a word from flash.
Definition: fmc.c:102
FMC_ENABLE_CFG_UPDATE
#define FMC_ENABLE_CFG_UPDATE()
Definition: fmc.h:84
FMC_ReadPID
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:151
FMC_GetBootSource
int32_t FMC_GetBootSource(void)
Get the current boot source.
Definition: fmc.c:77
FMC_WriteConfig
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Execute ISP command to write User Configuration.
Definition: fmc.c:311
FMC_Open
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:90
FMC_ReadUCID
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:176
FMC
#define FMC
Pointer to FMC register structure.
Definition: Nano1X2Series.h:10665