nu_sys.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************//**
  2. * @file sys.c
  3. * @version V3.00
  4. * $Revision: 3 $
  5. * $Date: 18/07/05 4:42p $
  6. * @brief M031 Series System Manager (SYS) Driver Source File
  7. *
  8. * @note
  9. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  10. * SPDX-License-Identifier: Apache-2.0
  11. *****************************************************************************/
  12. #include "M031Series.h"
  13. /** @addtogroup Standard_Driver Standard Driver
  14. @{
  15. */
  16. /** @addtogroup SYS_Driver SYS Driver
  17. @{
  18. */
  19. /** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions
  20. @{
  21. */
  22. /**
  23. * @brief Clear reset source
  24. * @param[in] u32Src is reset source. Including :
  25. * - \ref SYS_RSTSTS_CPULKRF_Msk
  26. * - \ref SYS_RSTSTS_CPURF_Msk
  27. * - \ref SYS_RSTSTS_SYSRF_Msk
  28. * - \ref SYS_RSTSTS_BODRF_Msk
  29. * - \ref SYS_RSTSTS_LVRF_Msk
  30. * - \ref SYS_RSTSTS_WDTRF_Msk
  31. * - \ref SYS_RSTSTS_PINRF_Msk
  32. * - \ref SYS_RSTSTS_PORF_Msk
  33. * @return None
  34. * @details This function clear the selected reset source.
  35. */
  36. void SYS_ClearResetSrc(uint32_t u32Src)
  37. {
  38. SYS->RSTSTS = u32Src;
  39. }
  40. /**
  41. * @brief Get Brown-out detector output status
  42. * @param None
  43. * @retval 0 System voltage is higher than BODVL setting or BODEN is 0.
  44. * @retval 1 System voltage is lower than BODVL setting.
  45. * @details This function get Brown-out detector output status.
  46. */
  47. uint32_t SYS_GetBODStatus(void)
  48. {
  49. return ((SYS->BODCTL & SYS_BODCTL_BODOUT_Msk) >> SYS_BODCTL_BODOUT_Pos);
  50. }
  51. /**
  52. * @brief Get reset status register value
  53. * @param None
  54. * @return Reset source
  55. * @details This function get the system reset status register value.
  56. */
  57. uint32_t SYS_GetResetSrc(void)
  58. {
  59. return (SYS->RSTSTS);
  60. }
  61. /**
  62. * @brief Check if register is locked or not
  63. * @param None
  64. * @retval 0 Write-protection function is disabled.
  65. * 1 Write-protection function is enabled.
  66. * @details This function check register write-protection bit setting.
  67. */
  68. uint32_t SYS_IsRegLocked(void)
  69. {
  70. return !(SYS->REGLCTL & 0x1);
  71. }
  72. /**
  73. * @brief Get product ID
  74. * @param None
  75. * @return Product ID
  76. * @details This function get product ID.
  77. */
  78. uint32_t SYS_ReadPDID(void)
  79. {
  80. return SYS->PDID;
  81. }
  82. /**
  83. * @brief Reset chip with chip reset
  84. * @param None
  85. * @return None
  86. * @details This function reset chip with chip reset.
  87. * The register write-protection function should be disabled before using this function.
  88. */
  89. void SYS_ResetChip(void)
  90. {
  91. SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk;
  92. }
  93. /**
  94. * @brief Reset chip with CPU reset
  95. * @param None
  96. * @return None
  97. * @details This function reset CPU with CPU reset.
  98. * The register write-protection function should be disabled before using this function.
  99. */
  100. void SYS_ResetCPU(void)
  101. {
  102. SYS->IPRST0 |= SYS_IPRST0_CPURST_Msk;
  103. }
  104. /**
  105. * @brief Reset selected module
  106. * @param[in] u32ModuleIndex is module index. Including :
  107. * - \ref PDMA_RST
  108. * - \ref EBI_RST
  109. * - \ref HDIV_RST
  110. * - \ref CRC_RST
  111. * - \ref GPIO_RST
  112. * - \ref TMR0_RST
  113. * - \ref TMR1_RST
  114. * - \ref TMR2_RST
  115. * - \ref TMR3_RST
  116. * - \ref ACMP01_RST
  117. * - \ref I2C0_RST
  118. * - \ref I2C1_RST
  119. * - \ref QSPI0_RST
  120. * - \ref SPI0_RST
  121. * - \ref UART0_RST
  122. * - \ref UART1_RST
  123. * - \ref UART2_RST
  124. * - \ref UART3_RST
  125. * - \ref UART4_RST
  126. * - \ref UART5_RST
  127. * - \ref UART6_RST
  128. * - \ref UART7_RST
  129. * - \ref USBD_RST
  130. * - \ref ADC_RST
  131. * - \ref USCI0_RST
  132. * - \ref USCI1_RST
  133. * - \ref PWM0_RST
  134. * - \ref PWM1_RST
  135. * - \ref BPWM0_RST
  136. * - \ref BPWM1_RST
  137. * @return None
  138. * @details This function reset selected module.
  139. */
  140. void SYS_ResetModule(uint32_t u32ModuleIndex)
  141. {
  142. /* Generate reset signal to the corresponding module */
  143. *(volatile uint32_t *)((uint32_t)&SYS->IPRST0 + (u32ModuleIndex >> 24)) |= 1 << (u32ModuleIndex & 0x00ffffff);
  144. /* Release corresponding module from reset state */
  145. *(volatile uint32_t *)((uint32_t)&SYS->IPRST0 + (u32ModuleIndex >> 24)) &= ~(1 << (u32ModuleIndex & 0x00ffffff));
  146. }
  147. /**
  148. * @brief Enable and configure Brown-out detector function
  149. * @param[in] i32Mode is reset or interrupt mode. Including :
  150. * - \ref SYS_BODCTL_BOD_RST_EN
  151. * - \ref SYS_BODCTL_BOD_INTERRUPT_EN
  152. * @param[in] u32BODLevel is Brown-out voltage level. Including :
  153. * - \ref SYS_BODCTL_BODVL_2_5V
  154. * - \ref SYS_BODCTL_BODVL_2_0V
  155. * @return None
  156. * @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level.
  157. * The register write-protection function should be disabled before using this function.
  158. */
  159. void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel)
  160. {
  161. /* Enable Brown-out Detector function */
  162. SYS->BODCTL |= SYS_BODCTL_BODEN_Msk;
  163. /* Enable Brown-out interrupt or reset function */
  164. SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODRSTEN_Msk) | i32Mode;
  165. /* Select Brown-out Detector threshold voltage */
  166. SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODVL_Msk) | u32BODLevel;
  167. }
  168. /**
  169. * @brief Disable Brown-out detector function
  170. * @param None
  171. * @return None
  172. * @details This function disable Brown-out detector function.
  173. * The register write-protection function should be disabled before using this function.
  174. */
  175. void SYS_DisableBOD(void)
  176. {
  177. SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk;
  178. }
  179. /*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */
  180. /*@}*/ /* end of group SYS_Driver */
  181. /*@}*/ /* end of group Standard_Driver */
  182. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/