1 /**************************************************************************//** 2 * @file sys.c 3 * @version V3.00 4 * @brief M2354 series System Manager (SYS) driver source file 5 * 6 * @copyright SPDX-License-Identifier: Apache-2.0 7 * @copyright Copyright (C) 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 KS_RST 123 * - \ref GPIO_RST 124 * - \ref TMR0_RST 125 * - \ref TMR1_RST 126 * - \ref TMR2_RST 127 * - \ref TMR3_RST 128 * - \ref TMR4_RST 129 * - \ref TMR5_RST 130 * - \ref ACMP01_RST 131 * - \ref I2C0_RST 132 * - \ref I2C1_RST 133 * - \ref I2C2_RST 134 * - \ref QSPI0_RST 135 * - \ref SPI0_RST 136 * - \ref SPI1_RST 137 * - \ref SPI2_RST 138 * - \ref SPI3_RST 139 * - \ref UART0_RST 140 * - \ref UART1_RST 141 * - \ref UART2_RST 142 * - \ref UART3_RST 143 * - \ref UART4_RST 144 * - \ref UART5_RST 145 * - \ref CAN0_RST 146 * - \ref OTG_RST 147 * - \ref USBD_RST 148 * - \ref EADC_RST 149 * - \ref I2S0_RST 150 * - \ref LCD_RST 151 * - \ref TRNG_RST 152 * - \ref SC0_RST 153 * - \ref SC1_RST 154 * - \ref SC2_RST 155 * - \ref USCI0_RST 156 * - \ref USCI1_RST 157 * - \ref DAC_RST 158 * - \ref EPWM0_RST 159 * - \ref EPWM1_RST 160 * - \ref BPWM0_RST 161 * - \ref BPWM1_RST 162 * - \ref QEI0_RST 163 * - \ref QEI1_RST 164 * - \ref ECAP0_RST 165 * - \ref ECAP1_RST 166 * @return None 167 * @details This function reset selected module. 168 */ SYS_ResetModule(uint32_t u32ModuleIndex)169void SYS_ResetModule(uint32_t u32ModuleIndex) 170 { 171 uint32_t u32TmpVal = 0UL, u32TmpAddr = 0UL; 172 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 178 /* Release corresponding module from reset state */ 179 u32TmpVal = ~(1UL << (u32ModuleIndex & 0x00ffffffUL)); 180 *(uint32_t *)u32TmpAddr &= u32TmpVal; 181 } 182 183 /** 184 * @brief Enable and configure Brown-out detector function 185 * @param[in] i32Mode is reset or interrupt mode. Including : 186 * - \ref SYS_BODCTL_BOD_RST_EN 187 * - \ref SYS_BODCTL_BOD_INTERRUPT_EN 188 * @param[in] u32BODLevel is Brown-out voltage level. Including : 189 * - \ref SYS_BODCTL_BODVL_1_6V 190 * - \ref SYS_BODCTL_BODVL_1_8V 191 * - \ref SYS_BODCTL_BODVL_2_0V 192 * - \ref SYS_BODCTL_BODVL_2_2V 193 * - \ref SYS_BODCTL_BODVL_2_4V 194 * - \ref SYS_BODCTL_BODVL_2_6V 195 * - \ref SYS_BODCTL_BODVL_2_8V 196 * - \ref SYS_BODCTL_BODVL_3_0V 197 * @return None 198 * @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level. 199 * The register write-protection function should be disabled before using this function. 200 */ SYS_EnableBOD(int32_t i32Mode,uint32_t u32BODLevel)201void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel) 202 { 203 /* Enable Brown-out Detector function */ 204 /* Enable Brown-out interrupt or reset function */ 205 /* Select Brown-out Detector threshold voltage */ 206 while(SYS->BODCTL & SYS_BODCTL_WRBUSY_Msk); 207 SYS->BODCTL = (SYS->BODCTL & ~(SYS_BODCTL_BODRSTEN_Msk | SYS_BODCTL_BODVL_Msk)) | 208 ((uint32_t)i32Mode) | (u32BODLevel) | (SYS_BODCTL_BODEN_Msk); 209 } 210 211 /** 212 * @brief Disable Brown-out detector function 213 * @param None 214 * @return None 215 * @details This function disable Brown-out detector function. 216 * The register write-protection function should be disabled before using this function. 217 */ SYS_DisableBOD(void)218void SYS_DisableBOD(void) 219 { 220 while(SYS->BODCTL & SYS_BODCTL_WRBUSY_Msk); 221 SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk; 222 } 223 224 225 /** 226 * @brief Set Power Level 227 * @param[in] u32PowerLevel is power level setting. Including : 228 * - \ref SYS_PLCTL_PLSEL_PL0 : Supports system clock up to 96MHz. 229 * - \ref SYS_PLCTL_PLSEL_PL1 : Supports system clock up to 84MHz. 230 * - \ref SYS_PLCTL_PLSEL_PL2 : Supports system clock up to 48MHz. 231 * - \ref SYS_PLCTL_PLSEL_PL3 : Supports system clock up to 4MHz. 232 * @return None 233 * @details This function select power level. 234 * The register write-protection function should be disabled before using this function. 235 */ SYS_SetPowerLevel(uint32_t u32PowerLevel)236void SYS_SetPowerLevel(uint32_t u32PowerLevel) 237 { 238 /* Set power voltage level */ 239 while(SYS->PLCTL & SYS_PLCTL_WRBUSY_Msk); 240 SYS->PLCTL = (SYS->PLCTL & (~SYS_PLCTL_PLSEL_Msk)) | (u32PowerLevel); 241 while(SYS->PLSTS & SYS_PLSTS_PLCBUSY_Msk); 242 } 243 244 245 /** 246 * @brief Set Main Voltage Regulator Type 247 * @param[in] u32PowerRegulator is main voltage regulator type. Including : 248 * - \ref SYS_PLCTL_MVRS_LDO 249 * - \ref SYS_PLCTL_MVRS_DCDC 250 * @retval 0 main voltage regulator type setting is not finished 251 * @retval 1 main voltage regulator type setting is finished 252 * @details This function set main voltage regulator type. 253 * The main voltage regulator type setting to DCDC cannot finished if the inductor is not detected. 254 * The register write-protection function should be disabled before using this function. 255 */ SYS_SetPowerRegulator(uint32_t u32PowerRegulator)256uint32_t SYS_SetPowerRegulator(uint32_t u32PowerRegulator) 257 { 258 int32_t i32TimeOutCnt = 400; 259 uint32_t u32Ret = 1U; 260 uint32_t u32PowerRegStatus; 261 262 /* Get main voltage regulator type status */ 263 u32PowerRegStatus = SYS->PLSTS & SYS_PLSTS_CURMVR_Msk; 264 265 /* Set main voltage regulator type */ 266 if((u32PowerRegulator == SYS_PLCTL_MVRS_DCDC) && (u32PowerRegStatus == SYS_PLSTS_CURMVR_LDO)) 267 { 268 269 /* Set main voltage regulator type to DCDC if status is LDO */ 270 while(SYS->PLCTL & SYS_PLCTL_WRBUSY_Msk); 271 SYS->PLCTL |= SYS_PLCTL_MVRS_Msk; 272 273 /* Wait inductor detection and main voltage regulator type change ready */ 274 while((SYS->PLSTS & SYS_PLSTS_CURMVR_Msk) != SYS_PLSTS_CURMVR_DCDC) 275 { 276 if(i32TimeOutCnt-- <= 0) 277 { 278 u32Ret = 0U; /* Main voltage regulator type change time-out */ 279 break; 280 } 281 } 282 283 } 284 else if(u32PowerRegulator == SYS_PLCTL_MVRS_LDO) 285 { 286 287 /* Set main voltage regulator type to LDO if status is DCDC */ 288 while(SYS->PLCTL & SYS_PLCTL_WRBUSY_Msk); 289 SYS->PLCTL &= (~SYS_PLCTL_MVRS_Msk); 290 291 /* Wait main voltage regulator type change ready */ 292 while((SYS->PLSTS & SYS_PLSTS_CURMVR_Msk) != SYS_PLSTS_CURMVR_LDO) 293 { 294 if(i32TimeOutCnt-- <= 0) 295 { 296 u32Ret = 0U; /* Main voltage regulator type change time-out */ 297 break; 298 } 299 } 300 301 } 302 303 /* Clear main voltage regulator type change error flag */ 304 if(SYS->PLSTS & SYS_PLSTS_MVRCERR_Msk) 305 { 306 SYS->PLSTS = SYS_PLSTS_MVRCERR_Msk; 307 u32Ret = 0U; 308 } 309 310 return u32Ret; 311 } 312 313 /** 314 * @brief Set System SRAM Power Mode 315 * @param[in] u32SRAMSel is SRAM region selection. Including : 316 * - \ref SYS_SRAMPC0_SRAM0PM0_Msk : 0x2000_0000 - 0x2000_0FFF 317 * - \ref SYS_SRAMPC0_SRAM0PM1_Msk : 0x2000_1000 - 0x2000_1FFF 318 * - \ref SYS_SRAMPC0_SRAM0PM2_Msk : 0x2000_2000 - 0x2000_3FFF 319 * - \ref SYS_SRAMPC0_SRAM0PM3_Msk : 0x2000_4000 - 0x2000_5FFF 320 * - \ref SYS_SRAMPC0_SRAM0PM4_Msk : 0x2000_6000 - 0x2000_7FFF 321 * - \ref SYS_SRAMPC0_SRAM1PM0_Msk : 0x2000_8000 - 0x2000_BFFF 322 * - \ref SYS_SRAMPC0_SRAM1PM1_Msk : 0x2000_C000 - 0x2000_FFFF 323 * - \ref SYS_SRAMPC0_SRAM1PM2_Msk : 0x2001_0000 - 0x2001_3FFF 324 * - \ref SYS_SRAMPC0_SRAM1PM3_Msk : 0x2001_4000 - 0x2001_7FFF 325 * - \ref SYS_SRAMPC0_SRAM1PM4_Msk : 0x2001_8000 - 0x2001_BFFF 326 * - \ref SYS_SRAMPC0_SRAM1PM5_Msk : 0x2001_C000 - 0x2001_FFFF 327 * - \ref SYS_SRAMPC0_SRAM1PM6_Msk : 0x2002_0000 - 0x2002_3FFF 328 * - \ref SYS_SRAMPC0_SRAM1PM7_Msk : 0x2002_4000 - 0x2002_7FFF 329 * - \ref SYS_SRAMPC0_SRAM2PM0_Msk : 0x2002_8000 - 0x2002_BFFF 330 * - \ref SYS_SRAMPC0_SRAM2PM1_Msk : 0x2002_C000 - 0x2002_FFFF 331 * - \ref SYS_SRAMPC1_SRAM2PM2_Msk : 0x2003_0000 - 0x2003_3FFF 332 * - \ref SYS_SRAMPC1_SRAM2PM3_Msk : 0x2003_4000 - 0x2003_7FFF 333 * - \ref SYS_SRAMPC1_SRAM2PM4_Msk : 0x2003_8000 - 0x2003_BFFF 334 * - \ref SYS_SRAMPC1_SRAM2PM5_Msk : 0x2003_C000 - 0x2003_FFFF 335 * @param[in] u32PowerMode is SRAM power mode. Including : 336 * - \ref SYS_SRAMPC0_SRAM_NORMAL 337 * - \ref SYS_SRAMPC0_SRAM_RETENTION 338 * - \ref SYS_SRAMPC0_SRAM_POWER_SHUT_DOWN 339 * - \ref SYS_SRAMPC1_SRAM_NORMAL 340 * - \ref SYS_SRAMPC1_SRAM_RETENTION 341 * - \ref SYS_SRAMPC1_SRAM_POWER_SHUT_DOWN 342 * @return None 343 * @details This function set system SRAM power mode. 344 * The register write-protection function should be disabled before using this function. 345 */ SYS_SetSSRAMPowerMode(uint32_t u32SRAMSel,uint32_t u32PowerMode)346void SYS_SetSSRAMPowerMode(uint32_t u32SRAMSel, uint32_t u32PowerMode) 347 { 348 uint32_t u32SRAMSelPos = 0UL; 349 350 /* Get system SRAM power mode setting position */ 351 while(u32SRAMSelPos < 30UL) 352 { 353 if(u32SRAMSel & (1 << u32SRAMSelPos)) 354 { 355 break; 356 } 357 else 358 { 359 u32SRAMSelPos++; 360 } 361 } 362 363 /* Set system SRAM power mode setting */ 364 if(u32PowerMode & BIT31) 365 { 366 while(SYS->SRAMPC1 & SYS_SRAMPC1_PCBUSY_Msk); 367 SYS->SRAMPC1 = (SYS->SRAMPC1 & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos); 368 } 369 else 370 { 371 while(SYS->SRAMPC0 & SYS_SRAMPC0_PCBUSY_Msk); 372 SYS->SRAMPC0 = (SYS->SRAMPC0 & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos); 373 374 } 375 } 376 377 /** 378 * @brief Set Peripheral SRAM Power Mode 379 * @param[in] u32SRAMSel is SRAM region selection. Including : 380 * - \ref SYS_SRAMPC1_CAN_Msk 381 * - \ref SYS_SRAMPC1_USBD_Msk 382 * - \ref SYS_SRAMPC1_PDMA0_Msk 383 * - \ref SYS_SRAMPC1_PDMA1_Msk 384 * - \ref SYS_SRAMPC1_FMCCACHE_Msk 385 * - \ref SYS_SRAMPC1_RSA_Msk 386 * - \ref SYS_SRAMPC1_KS_Msk 387 * @param[in] u32PowerMode is SRAM power mode. Including : 388 * - \ref SYS_SRAMPC1_SRAM_NORMAL 389 * - \ref SYS_SRAMPC1_SRAM_RETENTION 390 * - \ref SYS_SRAMPC1_SRAM_POWER_SHUT_DOWN 391 * @return None 392 * @details This function set peripheral SRAM power mode. 393 * The register write-protection function should be disabled before using this function. 394 */ SYS_SetPSRAMPowerMode(uint32_t u32SRAMSel,uint32_t u32PowerMode)395void SYS_SetPSRAMPowerMode(uint32_t u32SRAMSel, uint32_t u32PowerMode) 396 { 397 uint32_t u32SRAMSelPos = 16UL; 398 399 /* Get peripheral SRAM power mode setting position */ 400 while(u32SRAMSelPos < 30UL) 401 { 402 if(u32SRAMSel & (1 << u32SRAMSelPos)) 403 { 404 break; 405 } 406 else 407 { 408 u32SRAMSelPos++; 409 } 410 } 411 412 /* Set peripheral SRAM power mode setting */ 413 while(SYS->SRAMPC1 & SYS_SRAMPC1_PCBUSY_Msk); 414 SYS->SRAMPC1 = (SYS->SRAMPC1 & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos); 415 } 416 417 /** 418 * @brief Set Reference Voltage 419 * @param[in] u32VRefCTL is reference voltage setting. Including : 420 * - \ref SYS_VREFCTL_VREF_PIN 421 * - \ref SYS_VREFCTL_VREF_1_6V 422 * - \ref SYS_VREFCTL_VREF_2_0V 423 * - \ref SYS_VREFCTL_VREF_2_5V 424 * - \ref SYS_VREFCTL_VREF_3_0V 425 * - \ref SYS_VREFCTL_VREF_AVDD 426 * @return None 427 * @details This function select reference voltage. 428 * The register write-protection function should be disabled before using this function. 429 */ SYS_SetVRef(uint32_t u32VRefCTL)430void SYS_SetVRef(uint32_t u32VRefCTL) 431 { 432 /* Set reference voltage */ 433 SYS->VREFCTL = (SYS->VREFCTL & (~SYS_VREFCTL_VREFCTL_Msk)) | (u32VRefCTL); 434 } 435 436 437 /**@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */ 438 439 /**@}*/ /* end of group SYS_Driver */ 440 441 /**@}*/ /* end of group Standard_Driver */ 442