blob: bd422f2973eb5fb92a2cdf1f7cdb774f2cbbe62f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/**************************************************************************//**
* @file sys.c
* @version V3.00
* @brief M460 series SYS driver source file
*
* @copyright SPDX-License-Identifier: Apache-2.0
* @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "NuMicro.h"
#ifdef __cplusplus
extern "C"
{
#endif
/** @addtogroup Standard_Driver Standard Driver
@{
*/
/** @addtogroup SYS_Driver SYS Driver
@{
*/
int32_t g_SYS_i32ErrCode = 0; /*!< SYS global error code */
/** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions
@{
*/
/**
* @brief Clear reset source
* @param[in] u32Src is system reset source. Including :
* - \ref SYS_RSTSTS_CPULKRF_Msk
* - \ref SYS_RSTSTS_CPURF_Msk
* - \ref SYS_RSTSTS_HRESETRF_Msk
* - \ref SYS_RSTSTS_MCURF_Msk
* - \ref SYS_RSTSTS_BODRF_Msk
* - \ref SYS_RSTSTS_LVRF_Msk
* - \ref SYS_RSTSTS_WDTRF_Msk
* - \ref SYS_RSTSTS_PINRF_Msk
* - \ref SYS_RSTSTS_PORF_Msk
* @return None
* @details This function clear the selected system reset source.
*/
void SYS_ClearResetSrc(uint32_t u32Src)
{
SYS->RSTSTS = u32Src;
}
/**
* @brief Get Brown-out detector output status
* @param None
* @retval 0 System voltage is higher than BODVL setting or BODEN is 0.
* @retval 1 System voltage is lower than BODVL setting.
* @details This function get Brown-out detector output status.
*/
uint32_t SYS_GetBODStatus(void)
{
return ((SYS->BODCTL & SYS_BODCTL_BODOUT_Msk) >> SYS_BODCTL_BODOUT_Pos);
}
/**
* @brief Get reset status register value
* @param None
* @return Reset source
* @details This function get the system reset status register value.
*/
uint32_t SYS_GetResetSrc(void)
{
return (SYS->RSTSTS);
}
/**
* @brief Check if register is locked nor not
* @param None
* @retval 0 Write-protection function is disabled.
* 1 Write-protection function is enabled.
* @details This function check register write-protection bit setting.
*/
uint32_t SYS_IsRegLocked(void)
{
return SYS->REGLCTL & 1UL ? 0UL : 1UL;
}
/**
* @brief Get product ID
* @param None
* @return Product ID
* @details This function get product ID.
*/
uint32_t SYS_ReadPDID(void)
{
return SYS->PDID;
}
/**
* @brief Reset chip with chip reset
* @param None
* @return None
* @details This function reset chip with chip reset.
* The register write-protection function should be disabled before using this function.
*/
void SYS_ResetChip(void)
{
SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk;
}
/**
* @brief Reset chip with CPU reset
* @param None
* @return None
* @details This function reset CPU with CPU reset.
* The register write-protection function should be disabled before using this function.
*/
void SYS_ResetCPU(void)
{
SYS->IPRST0 |= SYS_IPRST0_CPURST_Msk;
}
/**
* @brief Reset selected module
* @param[in] u32ModuleIndex is module index. Including :
* - \ref PDMA0_RST
* - \ref PDMA1_RST
* - \ref EBI_RST
* - \ref EMAC0_RST
* - \ref SDH0_RST
* - \ref SDH1_RST
* - \ref CRC_RST
* - \ref CCAP_RST
* - \ref HSUSBD_RST
* - \ref HBI_RST
* - \ref CRPT_RST
* - \ref KS_RST
* - \ref SPIM_RST
* - \ref HSUSBH_RST
* - \ref CANFD0_RST
* - \ref CANFD1_RST
* - \ref CANFD2_RST
* - \ref CANFD3_RST
* - \ref GPIO_RST
* - \ref TMR0_RST
* - \ref TMR1_RST
* - \ref TMR2_RST
* - \ref TMR3_RST
* - \ref ACMP01_RST
* - \ref ACMP23_RST
* - \ref I2C0_RST
* - \ref I2C1_RST
* - \ref I2C2_RST
* - \ref I2C3_RST
* - \ref I2C4_RST
* - \ref QSPI0_RST
* - \ref QSPI1_RST
* - \ref SPI0_RST
* - \ref SPI1_RST
* - \ref SPI2_RST
* - \ref SPI3_RST
* - \ref SPI4_RST
* - \ref SPI5_RST
* - \ref SPI6_RST
* - \ref SPI7_RST
* - \ref SPI8_RST
* - \ref SPI9_RST
* - \ref SPI10_RST
* - \ref UART0_RST
* - \ref UART1_RST
* - \ref UART2_RST
* - \ref UART3_RST
* - \ref UART4_RST
* - \ref UART5_RST
* - \ref UART6_RST
* - \ref UART7_RST
* - \ref UART8_RST
* - \ref UART9_RST
* - \ref OTG_RST
* - \ref USBD_RST
* - \ref EADC0_RST
* - \ref EADC1_RST
* - \ref EADC2_RST
* - \ref I2S0_RST
* - \ref I2S1_RST
* - \ref HSOTG_RST
* - \ref TRNG_RST
* - \ref SC0_RST
* - \ref SC1_RST
* - \ref SC2_RST
* - \ref USCI0_RST
* - \ref PSIO_RST
* - \ref DAC_RST
* - \ref EPWM0_RST
* - \ref EPWM1_RST
* - \ref BPWM0_RST
* - \ref BPWM1_RST
* - \ref EQEI0_RST
* - \ref EQEI1_RST
* - \ref EQEI2_RST
* - \ref EQEI3_RST
* - \ref ECAP0_RST
* - \ref ECAP1_RST
* - \ref ECAP2_RST
* - \ref ECAP3_RST
* - \ref EADC1_RST
* - \ref KPI_RST
* @return None
* @details This function reset selected module.
* The register write-protection function should be disabled before using this function.
*/
void SYS_ResetModule(uint32_t u32ModuleIndex)
{
uint32_t u32tmpVal = 0UL, u32tmpAddr = 0UL;
/* Generate reset signal to the corresponding module */
u32tmpVal = (1UL << (u32ModuleIndex & 0x00ffffffUL));
u32tmpAddr = (uint32_t)&SYS->IPRST0 + ((u32ModuleIndex >> 24UL));
*(volatile uint32_t *)u32tmpAddr |= u32tmpVal;
/* Release corresponding module from reset state */
u32tmpVal = ~(1UL << (u32ModuleIndex & 0x00ffffffUL));
*(volatile uint32_t *)u32tmpAddr &= u32tmpVal;
}
/**
* @brief Enable and configure Brown-out detector function
* @param[in] i32Mode is reset or interrupt mode. Including :
* - \ref SYS_BODCTL_BOD_RST_EN
* - \ref SYS_BODCTL_BOD_INTERRUPT_EN
* @param[in] u32BODLevel is Brown-out voltage level. Including :
* - \ref SYS_BODCTL_BODVL_3_0V
* - \ref SYS_BODCTL_BODVL_2_8V
* - \ref SYS_BODCTL_BODVL_2_6V
* - \ref SYS_BODCTL_BODVL_2_4V
* - \ref SYS_BODCTL_BODVL_2_2V
* - \ref SYS_BODCTL_BODVL_2_0V
* - \ref SYS_BODCTL_BODVL_1_8V
* - \ref SYS_BODCTL_BODVL_1_6V
* @return None
* @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level.
* The register write-protection function should be disabled before using this function.
*/
void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel)
{
/* Enable Brown-out Detector function */
/* Enable Brown-out interrupt or reset function */
/* Select Brown-out Detector threshold voltage */
SYS->BODCTL = (SYS->BODCTL & ~(SYS_BODCTL_BODRSTEN_Msk | SYS_BODCTL_BODVL_Msk)) |
((uint32_t)i32Mode) | (u32BODLevel) | (SYS_BODCTL_BODEN_Msk);
}
/**
* @brief Disable Brown-out detector function
* @param None
* @return None
* @details This function disable Brown-out detector function.
* The register write-protection function should be disabled before using this function.
*/
void SYS_DisableBOD(void)
{
SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk;
}
/**
* @brief Set Power Level
* @param[in] u32PowerLevel is power level setting. Including :
* - \ref SYS_PLCTL_PLSEL_PL0 : Supports system clock up to 200MHz.
* - \ref SYS_PLCTL_PLSEL_PL1 : Supports system clock up to 180MHz.
* @return None
* @details This function select power level.
* The register write-protection function should be disabled before using this function.
* @note This function sets g_SYS_i32ErrCode to SYS_TIMEOUT_ERR if waiting SYS time-out.
*/
void SYS_SetPowerLevel(uint32_t u32PowerLevel)
{
uint32_t u32TimeOutCount = 0;
g_SYS_i32ErrCode = 0;
/* Wait for power level change busy flag is cleared */
u32TimeOutCount = SystemCoreClock; /* 1 second time-out */
while (SYS->PLSTS & SYS_PLSTS_PLCBUSY_Msk)
{
if (--u32TimeOutCount == 0)
{
g_SYS_i32ErrCode = SYS_TIMEOUT_ERR; /* Time-out error */
break;
}
}
/* Set power voltage level */
SYS->PLCTL = (SYS->PLCTL & (~SYS_PLCTL_PLSEL_Msk)) | (u32PowerLevel);
/* Wait for power level change busy flag is cleared */
u32TimeOutCount = SystemCoreClock; /* 1 second time-out */
while (SYS->PLSTS & SYS_PLSTS_PLCBUSY_Msk)
{
if (--u32TimeOutCount == 0)
{
g_SYS_i32ErrCode = SYS_TIMEOUT_ERR; /* Time-out error */
break;
}
}
}
/**
* @brief Set Reference Voltage
* @param[in] u32VRefCTL is reference voltage setting. Including :
* - \ref SYS_VREFCTL_VREF_PIN
* - \ref SYS_VREFCTL_VREF_1_6V
* - \ref SYS_VREFCTL_VREF_2_0V
* - \ref SYS_VREFCTL_VREF_2_5V
* - \ref SYS_VREFCTL_VREF_3_0V
* @return None
* @details This function select reference voltage.
* The register write-protection function should be disabled before using this function.
*/
void SYS_SetVRef(uint32_t u32VRefCTL)
{
/* Set reference voltage */
SYS->VREFCTL = (SYS->VREFCTL & (~SYS_VREFCTL_VREFCTL_Msk)) | (u32VRefCTL);
}
/*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */
/*@}*/ /* end of group SYS_Driver */
/*@}*/ /* end of group Standard_Driver */
#ifdef __cplusplus
}
#endif
|