--- /dev/null
+/**************************************************************************//**\r
+ * @file sys.c\r
+ * @version V3.00\r
+ * @brief M2351 series System Manager (SYS) driver source file\r
+ *\r
+ * @note\r
+ * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.\r
+*****************************************************************************/\r
+#include "NuMicro.h"\r
+/** @addtogroup Standard_Driver Standard Driver\r
+ @{\r
+*/\r
+\r
+/** @addtogroup SYS_Driver SYS Driver\r
+ @{\r
+*/\r
+\r
+\r
+/** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions\r
+ @{\r
+*/\r
+\r
+/**\r
+ * @brief Clear reset source\r
+ * @param[in] u32Src is system reset source. Including :\r
+ * - \ref SYS_RSTSTS_CPULKRF_Msk\r
+ * - \ref SYS_RSTSTS_CPURF_Msk\r
+ * - \ref SYS_RSTSTS_SYSRF_Msk\r
+ * - \ref SYS_RSTSTS_BODRF_Msk\r
+ * - \ref SYS_RSTSTS_LVRF_Msk\r
+ * - \ref SYS_RSTSTS_WDTRF_Msk\r
+ * - \ref SYS_RSTSTS_PINRF_Msk\r
+ * - \ref SYS_RSTSTS_PORF_Msk\r
+ * @return None\r
+ * @details This function clear the selected system reset source.\r
+ */\r
+void SYS_ClearResetSrc(uint32_t u32Src)\r
+{\r
+ SYS->RSTSTS = u32Src;\r
+}\r
+\r
+/**\r
+ * @brief Get Brown-out detector output status\r
+ * @param None\r
+ * @retval 0 System voltage is higher than BODVL setting or BODEN is 0.\r
+ * @retval 1 System voltage is lower than BODVL setting.\r
+ * @details This function get Brown-out detector output status.\r
+ */\r
+uint32_t SYS_GetBODStatus(void)\r
+{\r
+ return ((SYS->BODCTL & SYS_BODCTL_BODOUT_Msk) >> SYS_BODCTL_BODOUT_Pos);\r
+}\r
+\r
+/**\r
+ * @brief Get reset status register value\r
+ * @param None\r
+ * @return Reset source\r
+ * @details This function get the system reset status register value.\r
+ */\r
+uint32_t SYS_GetResetSrc(void)\r
+{\r
+ return (SYS->RSTSTS);\r
+}\r
+\r
+/**\r
+ * @brief Check if register is locked nor not\r
+ * @param None\r
+ * @retval 0 Write-protection function is disabled.\r
+ * 1 Write-protection function is enabled.\r
+ * @details This function check register write-protection bit setting.\r
+ */\r
+uint32_t SYS_IsRegLocked(void)\r
+{\r
+ return SYS->REGLCTL & 1UL ? 0UL : 1UL;\r
+}\r
+\r
+/**\r
+ * @brief Get product ID\r
+ * @param None\r
+ * @return Product ID\r
+ * @details This function get product ID.\r
+ */\r
+uint32_t SYS_ReadPDID(void)\r
+{\r
+ return SYS->PDID;\r
+}\r
+\r
+/**\r
+ * @brief Reset chip with chip reset\r
+ * @param None\r
+ * @return None\r
+ * @details This function reset chip with chip reset.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+void SYS_ResetChip(void)\r
+{\r
+ SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk;\r
+}\r
+\r
+/**\r
+ * @brief Reset chip with CPU reset\r
+ * @param None\r
+ * @return None\r
+ * @details This function reset CPU with CPU reset.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+void SYS_ResetCPU(void)\r
+{\r
+ SYS->IPRST0 |= SYS_IPRST0_CPURST_Msk;\r
+}\r
+\r
+/**\r
+ * @brief Reset selected module\r
+ * @param[in] u32ModuleIndex is module index. Including :\r
+ * - \ref PDMA0_RST\r
+ * - \ref PDMA1_RST\r
+ * - \ref EBI_RST\r
+ * - \ref USBH_RST\r
+ * - \ref SDH0_RST\r
+ * - \ref CRC_RST\r
+ * - \ref CRPT_RST\r
+ * - \ref GPIO_RST\r
+ * - \ref TMR0_RST\r
+ * - \ref TMR1_RST\r
+ * - \ref TMR2_RST\r
+ * - \ref TMR3_RST\r
+ * - \ref ACMP01_RST\r
+ * - \ref I2C0_RST\r
+ * - \ref I2C1_RST\r
+ * - \ref I2C2_RST\r
+ * - \ref QSPI0_RST\r
+ * - \ref SPI0_RST\r
+ * - \ref SPI1_RST\r
+ * - \ref SPI2_RST\r
+ * - \ref SPI3_RST\r
+ * - \ref UART0_RST\r
+ * - \ref UART1_RST\r
+ * - \ref UART2_RST\r
+ * - \ref UART3_RST\r
+ * - \ref UART4_RST\r
+ * - \ref UART5_RST\r
+ * - \ref CAN0_RST\r
+ * - \ref OTG_RST\r
+ * - \ref USBD_RST\r
+ * - \ref EADC_RST\r
+ * - \ref I2S0_RST\r
+ * - \ref TRNG_RST\r
+ * - \ref SC0_RST\r
+ * - \ref SC1_RST\r
+ * - \ref SC2_RST\r
+ * - \ref USCI0_RST\r
+ * - \ref USCI1_RST\r
+ * - \ref DAC_RST\r
+ * - \ref EPWM0_RST\r
+ * - \ref EPWM1_RST\r
+ * - \ref BPWM0_RST\r
+ * - \ref BPWM1_RST\r
+ * - \ref QEI0_RST\r
+ * - \ref QEI1_RST\r
+ * - \ref ECAP0_RST\r
+ * - \ref ECAP1_RST\r
+ * @return None\r
+ * @details This function reset selected module.\r
+ */\r
+void SYS_ResetModule(uint32_t u32ModuleIndex)\r
+{\r
+ uint32_t u32TmpVal = 0UL, u32TmpAddr = 0UL;\r
+\r
+ /* Generate reset signal to the corresponding module */\r
+ u32TmpVal = (1UL << (u32ModuleIndex & 0x00ffffffUL));\r
+ u32TmpAddr = (uint32_t)&SYS->IPRST0 + ((u32ModuleIndex >> 24UL));\r
+ *(uint32_t *)u32TmpAddr |= u32TmpVal;\r
+\r
+ /* Release corresponding module from reset state */\r
+ u32TmpVal = ~(1UL << (u32ModuleIndex & 0x00ffffffUL));\r
+ *(uint32_t *)u32TmpAddr &= u32TmpVal;\r
+}\r
+\r
+/**\r
+ * @brief Enable and configure Brown-out detector function\r
+ * @param[in] i32Mode is reset or interrupt mode. Including :\r
+ * - \ref SYS_BODCTL_BOD_RST_EN\r
+ * - \ref SYS_BODCTL_BOD_INTERRUPT_EN\r
+ * @param[in] u32BODLevel is Brown-out voltage level. Including :\r
+ * - \ref SYS_BODCTL_BODVL_1_6V\r
+ * - \ref SYS_BODCTL_BODVL_1_8V\r
+ * - \ref SYS_BODCTL_BODVL_2_0V\r
+ * - \ref SYS_BODCTL_BODVL_2_2V\r
+ * - \ref SYS_BODCTL_BODVL_2_4V\r
+ * - \ref SYS_BODCTL_BODVL_2_6V\r
+ * - \ref SYS_BODCTL_BODVL_2_8V\r
+ * - \ref SYS_BODCTL_BODVL_3_0V\r
+ * @return None\r
+ * @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel)\r
+{\r
+ /* Enable Brown-out Detector function */\r
+ SYS->BODCTL |= SYS_BODCTL_BODEN_Msk;\r
+\r
+ /* Enable Brown-out interrupt or reset function */\r
+ SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODRSTEN_Msk) | (uint32_t)i32Mode;\r
+\r
+ /* Select Brown-out Detector threshold voltage */\r
+ SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODVL_Msk) | u32BODLevel;\r
+}\r
+\r
+/**\r
+ * @brief Disable Brown-out detector function\r
+ * @param None\r
+ * @return None\r
+ * @details This function disable Brown-out detector function.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+void SYS_DisableBOD(void)\r
+{\r
+ SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk;\r
+}\r
+\r
+\r
+/**\r
+ * @brief Set Power Level\r
+ * @param[in] u32PowerLevel is power level setting. Including :\r
+ * - \ref SYS_PLCTL_PLSEL_PL0\r
+ * - \ref SYS_PLCTL_PLSEL_PL1\r
+ * @return None\r
+ * @details This function select power level.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+void SYS_SetPowerLevel(uint32_t u32PowerLevel)\r
+{\r
+ /* Set power voltage level */\r
+ SYS->PLCTL = (SYS->PLCTL & (~SYS_PLCTL_PLSEL_Msk)) | (u32PowerLevel);\r
+}\r
+\r
+\r
+/**\r
+ * @brief Set Main Voltage Regulator Type\r
+ * @param[in] u32PowerRegulator is main voltage regulator type. Including :\r
+ * - \ref SYS_PLCTL_MVRS_LDO\r
+ * - \ref SYS_PLCTL_MVRS_DCDC\r
+ * @retval 0 main voltage regulator type setting is not finished\r
+ * @retval 1 main voltage regulator type setting is finished\r
+ * @details This function set main voltage regulator type.\r
+ * The main voltage regulator type setting to DCDC cannot finished if the inductor is not detected.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+uint32_t SYS_SetPowerRegulator(uint32_t u32PowerRegulator)\r
+{\r
+ int32_t i32TimeOutCnt = 400;\r
+ uint32_t u32Ret = 1U;\r
+ uint32_t u32PowerRegStatus;\r
+\r
+ /* Get main voltage regulator type status */\r
+ u32PowerRegStatus = SYS->PLSTS & SYS_PLSTS_CURMVR_Msk;\r
+\r
+ /* Set main voltage regulator type */\r
+ if((u32PowerRegulator == SYS_PLCTL_MVRS_DCDC) && (u32PowerRegStatus == SYS_PLSTS_CURMVR_LDO))\r
+ {\r
+\r
+ /* Set main voltage regulator type to DCDC if status is LDO */\r
+ SYS->PLCTL |= SYS_PLCTL_MVRS_Msk;\r
+\r
+ /* Wait induction detection and main voltage regulator type change ready */\r
+ while((SYS->PLSTS & SYS_PLSTS_CURMVR_Msk) != SYS_PLSTS_CURMVR_DCDC)\r
+ {\r
+ if(i32TimeOutCnt-- <= 0)\r
+ {\r
+ u32Ret = 0U; /* Main voltage regulator type change time-out */\r
+ break;\r
+ }\r
+ }\r
+\r
+ }\r
+ else if(u32PowerRegulator == SYS_PLCTL_MVRS_LDO)\r
+ {\r
+\r
+ /* Set main voltage regulator type to LDO if status is DCDC */\r
+ SYS->PLCTL &= (~SYS_PLCTL_MVRS_Msk);\r
+\r
+ /* Wait main voltage regulator type change ready */\r
+ while((SYS->PLSTS & SYS_PLSTS_CURMVR_Msk) != SYS_PLSTS_CURMVR_LDO)\r
+ {\r
+ if(i32TimeOutCnt-- <= 0)\r
+ {\r
+ u32Ret = 0U; /* Main voltage regulator type change time-out */\r
+ break;\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ /* Clear main voltage regulator type change error flag */\r
+ if(SYS->PLSTS & SYS_PLSTS_MVRCERR_Msk)\r
+ {\r
+ SYS->PLSTS = SYS_PLSTS_MVRCERR_Msk;\r
+ u32Ret = 0U;\r
+ }\r
+\r
+ return u32Ret;\r
+}\r
+\r
+/**\r
+ * @brief Set System SRAM Power Mode\r
+ * @param[in] u32SRAMSel is SRAM region selection. Including :\r
+ * - \ref SYS_SRAMPCTL_SRAM0PM0_Msk\r
+ * - \ref SYS_SRAMPCTL_SRAM0PM1_Msk\r
+ * - \ref SYS_SRAMPCTL_SRAM0PM2_Msk\r
+ * - \ref SYS_SRAMPCTL_SRAM0PM3_Msk\r
+ * - \ref SYS_SRAMPCTL_SRAM1PM0_Msk\r
+ * - \ref SYS_SRAMPCTL_SRAM1PM1_Msk\r
+ * - \ref SYS_SRAMPCTL_SRAM1PM2_Msk\r
+ * - \ref SYS_SRAMPCTL_SRAM1PM3_Msk\r
+ * @param[in] u32PowerMode is SRAM power mode. Including :\r
+ * - \ref SYS_SRAMPCTL_SRAM_NORMAL\r
+ * - \ref SYS_SRAMPCTL_SRAM_RETENTION\r
+ * - \ref SYS_SRAMPCTL_SRAM_POWER_SHUT_DOWN\r
+ * @return None\r
+ * @details This function set system SRAM power mode.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+void SYS_SetSSRAMPowerMode(uint32_t u32SRAMSel, uint32_t u32PowerMode)\r
+{\r
+ uint32_t u32SRAMSelPos = 8UL;\r
+\r
+ /* Get system SRAM power mode setting position */\r
+ while(u32SRAMSelPos < 24UL)\r
+ {\r
+ if(u32SRAMSel & (1 << u32SRAMSelPos))\r
+ {\r
+ break;\r
+ }\r
+ else\r
+ {\r
+ u32SRAMSelPos++;\r
+ }\r
+ }\r
+\r
+ /* Set system SRAM power mode setting */\r
+ SYS->SRAMPCTL = (SYS->SRAMPCTL & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos);\r
+}\r
+\r
+/**\r
+ * @brief Set Peripheral SRAM Power Mode\r
+ * @param[in] u32SRAMSel is SRAM region selection. Including :\r
+ * - \ref SYS_SRAMPPCT_CAN_Msk\r
+ * - \ref SYS_SRAMPPCT_USBD_Msk\r
+ * - \ref SYS_SRAMPPCT_PDMA0_Msk\r
+ * - \ref SYS_SRAMPPCT_PDMA1_Msk\r
+ * - \ref SYS_SRAMPPCT_FMC_Msk\r
+ * @param[in] u32PowerMode is SRAM power mode. Including :\r
+ * - \ref SYS_SRAMPPCT_SRAM_NORMAL\r
+ * - \ref SYS_SRAMPPCT_SRAM_RETENTION\r
+ * - \ref SYS_SRAMPPCT_SRAM_POWER_SHUT_DOWN\r
+ * @return None\r
+ * @details This function set peripheral SRAM power mode.\r
+ * The register write-protection function should be disabled before using this function.\r
+ */\r
+void SYS_SetPSRAMPowerMode(uint32_t u32SRAMSel, uint32_t u32PowerMode)\r
+{\r
+ uint32_t u32SRAMSelPos = 0UL;\r
+\r
+ /* Get peripheral SRAM power mode setting position */\r
+ while(u32SRAMSelPos < 10UL)\r
+ {\r
+ if(u32SRAMSel & (1 << u32SRAMSelPos))\r
+ {\r
+ break;\r
+ }\r
+ else\r
+ {\r
+ u32SRAMSelPos++;\r
+ }\r
+ }\r
+\r
+ /* Set peripheral SRAM power mode setting */\r
+ SYS->SRAMPPCT = (SYS->SRAMPPCT & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos);\r
+}\r
+\r
+\r
+/*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */\r
+\r
+/*@}*/ /* end of group SYS_Driver */\r
+\r
+/*@}*/ /* end of group Standard_Driver */\r
+\r
+/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/\r