nu_sys.c 8.6 KB

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