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