1 /**************************************************************************//** 2 * @file sys.c 3 * @version V3.00 4 * @brief M460 series SYS driver source file 5 * 6 * @copyright SPDX-License-Identifier: Apache-2.0 7 * @copyright Copyright (C) 2021 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 /** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions 26 @{ 27 */ 28 29 /** 30 * @brief Clear reset source 31 * @param[in] u32Src is system reset source. Including : 32 * - \ref SYS_RSTSTS_CPULKRF_Msk 33 * - \ref SYS_RSTSTS_CPURF_Msk 34 * - \ref SYS_RSTSTS_HRESETRF_Msk 35 * - \ref SYS_RSTSTS_MCURF_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)44void 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)56uint32_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)67uint32_t SYS_GetResetSrc(void) 68 { 69 return (SYS->RSTSTS); 70 } 71 72 /** 73 * @brief Check if register is locked or 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)79uint32_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)90uint32_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)102void 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)114void 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 PDMA0_RST 123 * - \ref PDMA1_RST 124 * - \ref EBI_RST 125 * - \ref EMAC0_RST 126 * - \ref SDH0_RST 127 * - \ref SDH1_RST 128 * - \ref CRC_RST 129 * - \ref CCAP_RST 130 * - \ref HSUSBD_RST 131 * - \ref HBI_RST 132 * - \ref CRPT_RST 133 * - \ref KS_RST 134 * - \ref SPIM_RST 135 * - \ref HSUSBH_RST 136 * - \ref CANFD0_RST 137 * - \ref CANFD1_RST 138 * - \ref CANFD2_RST 139 * - \ref CANFD3_RST 140 * - \ref GPIO_RST 141 * - \ref TMR0_RST 142 * - \ref TMR1_RST 143 * - \ref TMR2_RST 144 * - \ref TMR3_RST 145 * - \ref ACMP01_RST 146 * - \ref ACMP23_RST 147 * - \ref I2C0_RST 148 * - \ref I2C1_RST 149 * - \ref I2C2_RST 150 * - \ref I2C3_RST 151 * - \ref I2C4_RST 152 * - \ref QSPI0_RST 153 * - \ref QSPI1_RST 154 * - \ref SPI0_RST 155 * - \ref SPI1_RST 156 * - \ref SPI2_RST 157 * - \ref SPI3_RST 158 * - \ref SPI4_RST 159 * - \ref SPI5_RST 160 * - \ref SPI6_RST 161 * - \ref SPI7_RST 162 * - \ref SPI8_RST 163 * - \ref SPI9_RST 164 * - \ref SPI10_RST 165 * - \ref UART0_RST 166 * - \ref UART1_RST 167 * - \ref UART2_RST 168 * - \ref UART3_RST 169 * - \ref UART4_RST 170 * - \ref UART5_RST 171 * - \ref UART6_RST 172 * - \ref UART7_RST 173 * - \ref UART8_RST 174 * - \ref UART9_RST 175 * - \ref OTG_RST 176 * - \ref USBD_RST 177 * - \ref EADC0_RST 178 * - \ref EADC1_RST 179 * - \ref EADC2_RST 180 * - \ref I2S0_RST 181 * - \ref I2S1_RST 182 * - \ref HSOTG_RST 183 * - \ref TRNG_RST 184 * - \ref SC0_RST 185 * - \ref SC1_RST 186 * - \ref SC2_RST 187 * - \ref USCI0_RST 188 * - \ref PSIO_RST 189 * - \ref DAC_RST 190 * - \ref EPWM0_RST 191 * - \ref EPWM1_RST 192 * - \ref BPWM0_RST 193 * - \ref BPWM1_RST 194 * - \ref EQEI0_RST 195 * - \ref EQEI1_RST 196 * - \ref EQEI2_RST 197 * - \ref EQEI3_RST 198 * - \ref ECAP0_RST 199 * - \ref ECAP1_RST 200 * - \ref ECAP2_RST 201 * - \ref ECAP3_RST 202 * - \ref EADC1_RST 203 * - \ref KPI_RST 204 * @return None 205 * @details This function reset selected module. 206 * The register write-protection function should be disabled before using this function. 207 */ SYS_ResetModule(uint32_t u32ModuleIndex)208void SYS_ResetModule(uint32_t u32ModuleIndex) 209 { 210 uint32_t u32tmpVal = 0UL, u32tmpAddr = 0UL; 211 212 /* Generate reset signal to the corresponding module */ 213 u32tmpVal = (1UL << (u32ModuleIndex & 0x00ffffffUL)); 214 u32tmpAddr = (uint32_t)&SYS->IPRST0 + ((u32ModuleIndex >> 24UL)); 215 *(volatile uint32_t *)u32tmpAddr |= u32tmpVal; 216 217 /* Release corresponding module from reset state */ 218 u32tmpVal = ~(1UL << (u32ModuleIndex & 0x00ffffffUL)); 219 *(volatile uint32_t *)u32tmpAddr &= u32tmpVal; 220 } 221 222 /** 223 * @brief Enable and configure Brown-out detector function 224 * @param[in] i32Mode is reset or interrupt mode. Including : 225 * - \ref SYS_BODCTL_BOD_RST_EN 226 * - \ref SYS_BODCTL_BOD_INTERRUPT_EN 227 * @param[in] u32BODLevel is Brown-out voltage level. Including : 228 * - \ref SYS_BODCTL_BODVL_3_0V 229 * - \ref SYS_BODCTL_BODVL_2_8V 230 * - \ref SYS_BODCTL_BODVL_2_6V 231 * - \ref SYS_BODCTL_BODVL_2_4V 232 * - \ref SYS_BODCTL_BODVL_2_2V 233 * - \ref SYS_BODCTL_BODVL_2_0V 234 * - \ref SYS_BODCTL_BODVL_1_8V 235 * - \ref SYS_BODCTL_BODVL_1_6V 236 * @return None 237 * @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level. 238 * The register write-protection function should be disabled before using this function. 239 */ SYS_EnableBOD(int32_t i32Mode,uint32_t u32BODLevel)240void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel) 241 { 242 /* Enable Brown-out Detector function */ 243 /* Enable Brown-out interrupt or reset function */ 244 /* Select Brown-out Detector threshold voltage */ 245 SYS->BODCTL = (SYS->BODCTL & ~(SYS_BODCTL_BODRSTEN_Msk | SYS_BODCTL_BODVL_Msk)) | 246 ((uint32_t)i32Mode) | (u32BODLevel) | (SYS_BODCTL_BODEN_Msk); 247 } 248 249 /** 250 * @brief Disable Brown-out detector function 251 * @param None 252 * @return None 253 * @details This function disable Brown-out detector function. 254 * The register write-protection function should be disabled before using this function. 255 */ SYS_DisableBOD(void)256void SYS_DisableBOD(void) 257 { 258 SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk; 259 } 260 261 /** 262 * @brief Set Power Level 263 * @param[in] u32PowerLevel is power level setting. Including : 264 * - \ref SYS_PLCTL_PLSEL_PL0 : Supports system clock up to 200MHz. 265 * - \ref SYS_PLCTL_PLSEL_PL1 : Supports system clock up to 180MHz. 266 * @retval 0 Power level setting success. 267 * @retval -1 Power level setting failed. 268 * @details This function select power level. 269 * The register write-protection function should be disabled before using this function. 270 */ SYS_SetPowerLevel(uint32_t u32PowerLevel)271int32_t SYS_SetPowerLevel(uint32_t u32PowerLevel) 272 { 273 uint32_t u32TimeOutCnt = 0; 274 275 /* Wait for power level change busy flag is cleared */ 276 u32TimeOutCnt = SystemCoreClock; /* 1 second time-out */ 277 while(SYS->PLSTS & SYS_PLSTS_PLCBUSY_Msk) 278 { 279 if(--u32TimeOutCnt == 0) return -1; 280 } 281 282 /* Set power voltage level */ 283 SYS->PLCTL = (SYS->PLCTL & (~SYS_PLCTL_PLSEL_Msk)) | (u32PowerLevel); 284 285 /* Wait for power level change busy flag is cleared */ 286 u32TimeOutCnt = SystemCoreClock; /* 1 second time-out */ 287 while(SYS->PLSTS & SYS_PLSTS_PLCBUSY_Msk) 288 { 289 if(--u32TimeOutCnt == 0) return -1; 290 } 291 292 return 0; 293 } 294 295 /** 296 * @brief Set Reference Voltage 297 * @param[in] u32VRefCTL is reference voltage setting. Including : 298 * - \ref SYS_VREFCTL_VREF_PIN 299 * - \ref SYS_VREFCTL_VREF_1_6V 300 * - \ref SYS_VREFCTL_VREF_2_0V 301 * - \ref SYS_VREFCTL_VREF_2_5V 302 * - \ref SYS_VREFCTL_VREF_3_0V 303 * @return None 304 * @details This function select reference voltage. 305 * The register write-protection function should be disabled before using this function. 306 */ SYS_SetVRef(uint32_t u32VRefCTL)307void SYS_SetVRef(uint32_t u32VRefCTL) 308 { 309 /* Set reference voltage */ 310 SYS->VREFCTL = (SYS->VREFCTL & (~SYS_VREFCTL_VREFCTL_Msk)) | (u32VRefCTL); 311 } 312 313 /*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */ 314 315 /*@}*/ /* end of group SYS_Driver */ 316 317 /*@}*/ /* end of group Standard_Driver */ 318 319 #ifdef __cplusplus 320 } 321 #endif 322