| MINI51DE_BSP V3.02.004 The Board Support Package for Mini51DE Series MCU

fmc.c
Go to the documentation of this file.
1/**************************************************************************/
13//* Includes ------------------------------------------------------------------*/
14#include <stdio.h>
15#include "Mini51Series.h"
16
34 void FMC_Close(void)
35{
36 FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk;
37}
38
39
46int32_t FMC_Erase(uint32_t u32PageAddr)
47{
48 int32_t tout = FMC_TIMEOUT_ERASE;
49
50 FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
51 FMC->ISPADR = u32PageAddr;
52 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
53
54 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) ;
55
56 if ((tout <= 0) || (FMC->ISPCON & FMC_ISPCON_ISPFF_Msk))
57 {
58 FMC->ISPCON |= FMC_ISPCON_ISPFF_Msk;
59 return -1;
60 }
61 return 0;
62}
63
64
70int32_t FMC_GetBootSource (void)
71{
72 if (FMC->ISPCON & FMC_ISPCON_BS_Msk)
73 return 1;
74 else
75 return 0;
76}
77
78
82 void FMC_Open(void)
83{
84 FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk;
85}
86
87
93uint32_t FMC_Read(uint32_t u32Addr)
94{
95 int32_t tout = FMC_TIMEOUT_READ;
96
97 FMC->ISPCMD = FMC_ISPCMD_READ;
98 FMC->ISPADR = u32Addr;
99 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
100
101 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) ;
102 if (tout <= 0)
103 {
104 return 0xFFFFFFFF;
105 }
106
107 return FMC->ISPDAT;
108}
109
110
115uint32_t FMC_ReadCID(void)
116{
117 int32_t tout = FMC_TIMEOUT_READ;
118
119 FMC->ISPCMD = FMC_ISPCMD_READ_CID;
120 FMC->ISPADR = 0x0;
121 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
122 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) ;
123 if (tout <= 0)
124 {
125 return 0xFFFFFFFF;
126 }
127 return FMC->ISPDAT;
128}
129
130
135uint32_t FMC_ReadPID(void)
136{
137 int32_t tout = FMC_TIMEOUT_READ;
138
139 FMC->ISPCMD = FMC_ISPCMD_READ_PID;
140 FMC->ISPADR = 0x04;
141 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
142 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) ;
143 if (tout <= 0)
144 {
145 return 0xFFFFFFFF;
146 }
147 return FMC->ISPDAT;
148}
149
150
156uint32_t FMC_ReadUCID(uint32_t u32Index)
157{
158 int32_t tout = FMC_TIMEOUT_READ;
159
160 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
161 FMC->ISPADR = (0x04 * u32Index) + 0x10;
162 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
163
164 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
165 if (tout <= 0)
166 {
167 return 0xFFFFFFFF;
168 }
169 return FMC->ISPDAT;
170}
171
172
178uint32_t FMC_ReadUID(uint32_t u32Index)
179{
180 int32_t tout = FMC_TIMEOUT_READ;
181
182 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
183 FMC->ISPADR = 0x04 * u32Index;
184 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
185
186 while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ;
187 if (tout <= 0)
188 {
189 return 0xFFFFFFFF;
190 }
191 return FMC->ISPDAT;
192}
193
194
199uint32_t FMC_ReadDataFlashBaseAddr(void)
200{
201 return FMC->DFBADR;
202}
203
204
209uint32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
210{
211 int32_t tout = FMC_TIMEOUT_WRITE;
212
213 FMC->ISPCMD = FMC_ISPCMD_VECMAP;
214 FMC->ISPADR = u32PageAddr;
215 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
216 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) ;
217 if (tout <= 0)
218 {
219 return 0xFFFFFFFF;
220 }
221 return 0;
222}
223
224
229uint32_t FMC_GetVectorPageAddr(void)
230{
231 return (FMC->ISPSTA & 0x0FFFFF00ul);
232}
233
234
240uint32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
241{
242 int32_t tout = FMC_TIMEOUT_WRITE;
243
244 FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
245 FMC->ISPADR = u32Addr;
246 FMC->ISPDAT = u32Data;
247 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
248 while ((tout-- > 0) && (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)) ;
249 if (tout <= 0)
250 {
251 return 0xFFFFFFFF;
252 }
253 return 0;
254}
255
256
264int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
265{
266 u32Config[0] = FMC_Read(FMC_CONFIG_BASE);
267 if (u32Count < 2)
268 return 0;
269 u32Config[1] = FMC_Read(FMC_CONFIG_BASE+4);
270 return 0;
271}
272
273
281int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
282{
283 FMC_ENABLE_CFG_UPDATE();
284 FMC_Erase(FMC_CONFIG_BASE);
285 FMC_Write(FMC_CONFIG_BASE, u32Config[0]);
286 FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]);
287 FMC_DISABLE_CFG_UPDATE();
288 return 0;
289}
290
291 /* end of group MINI51_FMC_EXPORTED_FUNCTIONS */
293 /* end of group MINI51_FMC_Driver */
295 /* end of group MINI51_Device_Driver */
297
298/*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/
299
300
Mini51Series.h
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
FMC_ISPCON_ISPFF_Msk
#define FMC_ISPCON_ISPFF_Msk
Definition: Mini51Series.h:1159
FMC_ISPCON_BS_Msk
#define FMC_ISPCON_BS_Msk
Definition: Mini51Series.h:1147
FMC_ISPTRG_ISPGO_Msk
#define FMC_ISPTRG_ISPGO_Msk
Definition: Mini51Series.h:1171
FMC_ISPCON_ISPEN_Msk
#define FMC_ISPCON_ISPEN_Msk
Definition: Mini51Series.h:1144
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_Close
void FMC_Close(void)
Disable all FMC functions.
Definition: fmc.c:34
FMC_ReadConfig
int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count)
Read the User Configuration words.
Definition: fmc.c:264
FMC_ReadCID
uint32_t FMC_ReadCID(void)
Read company ID.
Definition: fmc.c:115
FMC_GetVectorPageAddr
uint32_t FMC_GetVectorPageAddr(void)
Obtain the current vector page address setting.
Definition: fmc.c:229
FMC_ReadUID
uint32_t FMC_ReadUID(uint32_t u32Index)
This function reads one of the three UID.
Definition: fmc.c:178
FMC_Erase
int32_t FMC_Erase(uint32_t u32PageAddr)
Erase a page. The page size is 512 bytes.
Definition: fmc.c:46
FMC_ReadDataFlashBaseAddr
uint32_t FMC_ReadDataFlashBaseAddr(void)
Get the base address of Data Flash if enabled.
Definition: fmc.c:199
FMC_SetVectorPageAddr
uint32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
This function will force re-map assigned flash page to CPU address 0x0.
Definition: fmc.c:209
FMC_DISABLE_CFG_UPDATE
#define FMC_DISABLE_CFG_UPDATE()
Definition: fmc.h:68
FMC_Read
uint32_t FMC_Read(uint32_t u32Addr)
Read a word from specified flash address.
Definition: fmc.c:93
FMC_ENABLE_CFG_UPDATE
#define FMC_ENABLE_CFG_UPDATE()
Definition: fmc.h:73
FMC_ReadPID
uint32_t FMC_ReadPID(void)
Read product ID.
Definition: fmc.c:135
FMC_Write
uint32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data)
Writes a word data to specified flash address.
Definition: fmc.c:240
FMC_GetBootSource
int32_t FMC_GetBootSource(void)
get the current boot source
Definition: fmc.c:70
FMC_WriteConfig
int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count)
Write User Configuration.
Definition: fmc.c:281
FMC_Open
void FMC_Open(void)
Enable FMC ISP function.
Definition: fmc.c:82
FMC_ReadUCID
uint32_t FMC_ReadUCID(uint32_t u32Index)
This function reads one of the four UCID.
Definition: fmc.c:156
FMC
#define FMC
Pointer to FMC register structure.
Definition: Mini51Series.h:5745