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