]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Nuvoton_Code/StdDriver/src/sys.c
Add Cortex M23 GCC and IAR ports. Add demo projects for Nuvoton NuMaker-PFM-2351.
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC / Nuvoton_Code / StdDriver / src / sys.c
1 /**************************************************************************//**\r
2  * @file     sys.c\r
3  * @version  V3.00\r
4  * @brief    M2351 series System Manager (SYS) driver source file\r
5  *\r
6  * @note\r
7  * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.\r
8 *****************************************************************************/\r
9 #include "NuMicro.h"\r
10 /** @addtogroup Standard_Driver Standard Driver\r
11   @{\r
12 */\r
13 \r
14 /** @addtogroup SYS_Driver SYS Driver\r
15   @{\r
16 */\r
17 \r
18 \r
19 /** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions\r
20   @{\r
21 */\r
22 \r
23 /**\r
24   * @brief      Clear reset source\r
25   * @param[in]  u32Src is system reset source. Including :\r
26   *             - \ref SYS_RSTSTS_CPULKRF_Msk\r
27   *             - \ref SYS_RSTSTS_CPURF_Msk\r
28   *             - \ref SYS_RSTSTS_SYSRF_Msk\r
29   *             - \ref SYS_RSTSTS_BODRF_Msk\r
30   *             - \ref SYS_RSTSTS_LVRF_Msk\r
31   *             - \ref SYS_RSTSTS_WDTRF_Msk\r
32   *             - \ref SYS_RSTSTS_PINRF_Msk\r
33   *             - \ref SYS_RSTSTS_PORF_Msk\r
34   * @return     None\r
35   * @details    This function clear the selected system reset source.\r
36   */\r
37 void SYS_ClearResetSrc(uint32_t u32Src)\r
38 {\r
39     SYS->RSTSTS = u32Src;\r
40 }\r
41 \r
42 /**\r
43   * @brief      Get Brown-out detector output status\r
44   * @param      None\r
45   * @retval     0 System voltage is higher than BODVL setting or BODEN is 0.\r
46   * @retval     1 System voltage is lower than BODVL setting.\r
47   * @details    This function get Brown-out detector output status.\r
48   */\r
49 uint32_t SYS_GetBODStatus(void)\r
50 {\r
51     return ((SYS->BODCTL & SYS_BODCTL_BODOUT_Msk) >> SYS_BODCTL_BODOUT_Pos);\r
52 }\r
53 \r
54 /**\r
55   * @brief      Get reset status register value\r
56   * @param      None\r
57   * @return     Reset source\r
58   * @details    This function get the system reset status register value.\r
59   */\r
60 uint32_t SYS_GetResetSrc(void)\r
61 {\r
62     return (SYS->RSTSTS);\r
63 }\r
64 \r
65 /**\r
66   * @brief      Check if register is locked nor not\r
67   * @param      None\r
68   * @retval     0 Write-protection function is disabled.\r
69   *             1 Write-protection function is enabled.\r
70   * @details    This function check register write-protection bit setting.\r
71   */\r
72 uint32_t SYS_IsRegLocked(void)\r
73 {\r
74     return SYS->REGLCTL & 1UL ? 0UL : 1UL;\r
75 }\r
76 \r
77 /**\r
78   * @brief      Get product ID\r
79   * @param      None\r
80   * @return     Product ID\r
81   * @details    This function get product ID.\r
82   */\r
83 uint32_t  SYS_ReadPDID(void)\r
84 {\r
85     return SYS->PDID;\r
86 }\r
87 \r
88 /**\r
89   * @brief      Reset chip with chip reset\r
90   * @param      None\r
91   * @return     None\r
92   * @details    This function reset chip with chip reset.\r
93   *             The register write-protection function should be disabled before using this function.\r
94   */\r
95 void SYS_ResetChip(void)\r
96 {\r
97     SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk;\r
98 }\r
99 \r
100 /**\r
101   * @brief      Reset chip with CPU reset\r
102   * @param      None\r
103   * @return     None\r
104   * @details    This function reset CPU with CPU reset.\r
105   *             The register write-protection function should be disabled before using this function.\r
106   */\r
107 void SYS_ResetCPU(void)\r
108 {\r
109     SYS->IPRST0 |= SYS_IPRST0_CPURST_Msk;\r
110 }\r
111 \r
112 /**\r
113   * @brief      Reset selected module\r
114   * @param[in]  u32ModuleIndex is module index. Including :\r
115   *             - \ref PDMA0_RST\r
116   *             - \ref PDMA1_RST\r
117   *             - \ref EBI_RST\r
118   *             - \ref USBH_RST\r
119   *             - \ref SDH0_RST\r
120   *             - \ref CRC_RST\r
121   *             - \ref CRPT_RST\r
122   *             - \ref GPIO_RST\r
123   *             - \ref TMR0_RST\r
124   *             - \ref TMR1_RST\r
125   *             - \ref TMR2_RST\r
126   *             - \ref TMR3_RST\r
127   *             - \ref ACMP01_RST\r
128   *             - \ref I2C0_RST\r
129   *             - \ref I2C1_RST\r
130   *             - \ref I2C2_RST\r
131   *             - \ref QSPI0_RST\r
132   *             - \ref SPI0_RST\r
133   *             - \ref SPI1_RST\r
134   *             - \ref SPI2_RST\r
135   *             - \ref SPI3_RST\r
136   *             - \ref UART0_RST\r
137   *             - \ref UART1_RST\r
138   *             - \ref UART2_RST\r
139   *             - \ref UART3_RST\r
140   *             - \ref UART4_RST\r
141   *             - \ref UART5_RST\r
142   *             - \ref CAN0_RST\r
143   *             - \ref OTG_RST\r
144   *             - \ref USBD_RST\r
145   *             - \ref EADC_RST\r
146   *             - \ref I2S0_RST\r
147   *             - \ref TRNG_RST\r
148   *             - \ref SC0_RST\r
149   *             - \ref SC1_RST\r
150   *             - \ref SC2_RST\r
151   *             - \ref USCI0_RST\r
152   *             - \ref USCI1_RST\r
153   *             - \ref DAC_RST\r
154   *             - \ref EPWM0_RST\r
155   *             - \ref EPWM1_RST\r
156   *             - \ref BPWM0_RST\r
157   *             - \ref BPWM1_RST\r
158   *             - \ref QEI0_RST\r
159   *             - \ref QEI1_RST\r
160   *             - \ref ECAP0_RST\r
161   *             - \ref ECAP1_RST\r
162   * @return     None\r
163   * @details    This function reset selected module.\r
164   */\r
165 void SYS_ResetModule(uint32_t u32ModuleIndex)\r
166 {\r
167     uint32_t u32TmpVal = 0UL, u32TmpAddr = 0UL;\r
168 \r
169     /* Generate reset signal to the corresponding module */\r
170     u32TmpVal = (1UL << (u32ModuleIndex & 0x00ffffffUL));\r
171     u32TmpAddr = (uint32_t)&SYS->IPRST0 + ((u32ModuleIndex >> 24UL));\r
172     *(uint32_t *)u32TmpAddr |= u32TmpVal;\r
173 \r
174     /* Release corresponding module from reset state */\r
175     u32TmpVal = ~(1UL << (u32ModuleIndex & 0x00ffffffUL));\r
176     *(uint32_t *)u32TmpAddr &= u32TmpVal;\r
177 }\r
178 \r
179 /**\r
180   * @brief      Enable and configure Brown-out detector function\r
181   * @param[in]  i32Mode is reset or interrupt mode. Including :\r
182   *             - \ref SYS_BODCTL_BOD_RST_EN\r
183   *             - \ref SYS_BODCTL_BOD_INTERRUPT_EN\r
184   * @param[in]  u32BODLevel is Brown-out voltage level. Including :\r
185   *             - \ref SYS_BODCTL_BODVL_1_6V\r
186   *             - \ref SYS_BODCTL_BODVL_1_8V\r
187   *             - \ref SYS_BODCTL_BODVL_2_0V\r
188   *             - \ref SYS_BODCTL_BODVL_2_2V\r
189   *             - \ref SYS_BODCTL_BODVL_2_4V\r
190   *             - \ref SYS_BODCTL_BODVL_2_6V\r
191   *             - \ref SYS_BODCTL_BODVL_2_8V\r
192   *             - \ref SYS_BODCTL_BODVL_3_0V\r
193   * @return     None\r
194   * @details    This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level.\r
195   *             The register write-protection function should be disabled before using this function.\r
196   */\r
197 void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel)\r
198 {\r
199     /* Enable Brown-out Detector function */\r
200     SYS->BODCTL |= SYS_BODCTL_BODEN_Msk;\r
201 \r
202     /* Enable Brown-out interrupt or reset function */\r
203     SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODRSTEN_Msk) | (uint32_t)i32Mode;\r
204 \r
205     /* Select Brown-out Detector threshold voltage */\r
206     SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODVL_Msk) | u32BODLevel;\r
207 }\r
208 \r
209 /**\r
210   * @brief      Disable Brown-out detector function\r
211   * @param      None\r
212   * @return     None\r
213   * @details    This function disable Brown-out detector function.\r
214   *             The register write-protection function should be disabled before using this function.\r
215   */\r
216 void SYS_DisableBOD(void)\r
217 {\r
218     SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk;\r
219 }\r
220 \r
221 \r
222 /**\r
223   * @brief      Set Power Level\r
224   * @param[in]  u32PowerLevel is power level setting. Including :\r
225   *             - \ref SYS_PLCTL_PLSEL_PL0\r
226   *             - \ref SYS_PLCTL_PLSEL_PL1\r
227   * @return     None\r
228   * @details    This function select power level.\r
229   *             The register write-protection function should be disabled before using this function.\r
230   */\r
231 void SYS_SetPowerLevel(uint32_t u32PowerLevel)\r
232 {\r
233     /* Set power voltage level */\r
234     SYS->PLCTL = (SYS->PLCTL & (~SYS_PLCTL_PLSEL_Msk)) | (u32PowerLevel);\r
235 }\r
236 \r
237 \r
238 /**\r
239   * @brief      Set Main Voltage Regulator Type\r
240   * @param[in]  u32PowerRegulator is main voltage regulator type. Including :\r
241   *             - \ref SYS_PLCTL_MVRS_LDO\r
242   *             - \ref SYS_PLCTL_MVRS_DCDC\r
243   * @retval     0  main voltage regulator type setting is not finished\r
244   * @retval     1  main voltage regulator type setting is finished\r
245   * @details    This function set main voltage regulator type.\r
246   *             The main voltage regulator type setting to DCDC cannot finished if the inductor is not detected.\r
247   *             The register write-protection function should be disabled before using this function.\r
248   */\r
249 uint32_t SYS_SetPowerRegulator(uint32_t u32PowerRegulator)\r
250 {\r
251     int32_t i32TimeOutCnt = 400;\r
252     uint32_t u32Ret = 1U;\r
253     uint32_t u32PowerRegStatus;\r
254 \r
255     /* Get main voltage regulator type status */\r
256     u32PowerRegStatus = SYS->PLSTS & SYS_PLSTS_CURMVR_Msk;\r
257 \r
258     /* Set main voltage regulator type */\r
259     if((u32PowerRegulator == SYS_PLCTL_MVRS_DCDC) && (u32PowerRegStatus == SYS_PLSTS_CURMVR_LDO))\r
260     {\r
261 \r
262         /* Set main voltage regulator type to DCDC if status is LDO */\r
263         SYS->PLCTL |= SYS_PLCTL_MVRS_Msk;\r
264 \r
265         /* Wait induction detection and main voltage regulator type change ready */\r
266         while((SYS->PLSTS & SYS_PLSTS_CURMVR_Msk) != SYS_PLSTS_CURMVR_DCDC)\r
267         {\r
268             if(i32TimeOutCnt-- <= 0)\r
269             {\r
270                 u32Ret = 0U;    /* Main voltage regulator type change time-out */\r
271                 break;\r
272             }\r
273         }\r
274 \r
275     }\r
276     else if(u32PowerRegulator == SYS_PLCTL_MVRS_LDO)\r
277     {\r
278 \r
279         /* Set main voltage regulator type to LDO if status is DCDC */\r
280         SYS->PLCTL &= (~SYS_PLCTL_MVRS_Msk);\r
281 \r
282         /* Wait main voltage regulator type change ready */\r
283         while((SYS->PLSTS & SYS_PLSTS_CURMVR_Msk) != SYS_PLSTS_CURMVR_LDO)\r
284         {\r
285             if(i32TimeOutCnt-- <= 0)\r
286             {\r
287                 u32Ret = 0U;    /* Main voltage regulator type change time-out */\r
288                 break;\r
289             }\r
290         }\r
291 \r
292     }\r
293 \r
294     /* Clear main voltage regulator type change error flag */\r
295     if(SYS->PLSTS & SYS_PLSTS_MVRCERR_Msk)\r
296     {\r
297         SYS->PLSTS = SYS_PLSTS_MVRCERR_Msk;\r
298         u32Ret = 0U;\r
299     }\r
300 \r
301     return u32Ret;\r
302 }\r
303 \r
304 /**\r
305   * @brief      Set System SRAM Power Mode\r
306   * @param[in]  u32SRAMSel is SRAM region selection. Including :\r
307   *             - \ref SYS_SRAMPCTL_SRAM0PM0_Msk\r
308   *             - \ref SYS_SRAMPCTL_SRAM0PM1_Msk\r
309   *             - \ref SYS_SRAMPCTL_SRAM0PM2_Msk\r
310   *             - \ref SYS_SRAMPCTL_SRAM0PM3_Msk\r
311   *             - \ref SYS_SRAMPCTL_SRAM1PM0_Msk\r
312   *             - \ref SYS_SRAMPCTL_SRAM1PM1_Msk\r
313   *             - \ref SYS_SRAMPCTL_SRAM1PM2_Msk\r
314   *             - \ref SYS_SRAMPCTL_SRAM1PM3_Msk\r
315   * @param[in]  u32PowerMode is SRAM power mode. Including :\r
316   *             - \ref SYS_SRAMPCTL_SRAM_NORMAL\r
317   *             - \ref SYS_SRAMPCTL_SRAM_RETENTION\r
318   *             - \ref SYS_SRAMPCTL_SRAM_POWER_SHUT_DOWN\r
319   * @return     None\r
320   * @details    This function set system SRAM power mode.\r
321   *             The register write-protection function should be disabled before using this function.\r
322   */\r
323 void SYS_SetSSRAMPowerMode(uint32_t u32SRAMSel, uint32_t u32PowerMode)\r
324 {\r
325     uint32_t u32SRAMSelPos = 8UL;\r
326 \r
327     /* Get system SRAM power mode setting position */\r
328     while(u32SRAMSelPos < 24UL)\r
329     {\r
330         if(u32SRAMSel & (1 << u32SRAMSelPos))\r
331         {\r
332             break;\r
333         }\r
334         else\r
335         {\r
336             u32SRAMSelPos++;\r
337         }\r
338     }\r
339 \r
340     /* Set system SRAM power mode setting */\r
341     SYS->SRAMPCTL = (SYS->SRAMPCTL & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos);\r
342 }\r
343 \r
344 /**\r
345   * @brief      Set Peripheral SRAM Power Mode\r
346   * @param[in]  u32SRAMSel is SRAM region selection. Including :\r
347   *             - \ref SYS_SRAMPPCT_CAN_Msk\r
348   *             - \ref SYS_SRAMPPCT_USBD_Msk\r
349   *             - \ref SYS_SRAMPPCT_PDMA0_Msk\r
350   *             - \ref SYS_SRAMPPCT_PDMA1_Msk\r
351   *             - \ref SYS_SRAMPPCT_FMC_Msk\r
352   * @param[in]  u32PowerMode is SRAM power mode. Including :\r
353   *             - \ref SYS_SRAMPPCT_SRAM_NORMAL\r
354   *             - \ref SYS_SRAMPPCT_SRAM_RETENTION\r
355   *             - \ref SYS_SRAMPPCT_SRAM_POWER_SHUT_DOWN\r
356   * @return     None\r
357   * @details    This function set peripheral SRAM power mode.\r
358   *             The register write-protection function should be disabled before using this function.\r
359   */\r
360 void SYS_SetPSRAMPowerMode(uint32_t u32SRAMSel, uint32_t u32PowerMode)\r
361 {\r
362     uint32_t u32SRAMSelPos = 0UL;\r
363 \r
364     /* Get peripheral SRAM power mode setting position */\r
365     while(u32SRAMSelPos < 10UL)\r
366     {\r
367         if(u32SRAMSel & (1 << u32SRAMSelPos))\r
368         {\r
369             break;\r
370         }\r
371         else\r
372         {\r
373             u32SRAMSelPos++;\r
374         }\r
375     }\r
376 \r
377     /* Set peripheral SRAM power mode setting */\r
378     SYS->SRAMPPCT = (SYS->SRAMPPCT & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos);\r
379 }\r
380 \r
381 \r
382 /*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */\r
383 \r
384 /*@}*/ /* end of group SYS_Driver */\r
385 \r
386 /*@}*/ /* end of group Standard_Driver */\r
387 \r
388 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/\r