1 /**************************************************************************//** 2 * @file sys.c 3 * @version V1.0 4 * @brief M2L31 series SYS driver source file 5 * 6 * SPDX-License-Identifier: Apache-2.0 7 * @copyright (C) 2023 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 int32_t g_SYS_i32ErrCode = 0; /*!< SYS global error code */ 26 27 /** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions 28 @{ 29 */ 30 31 /** 32 * @brief Clear reset source 33 * @param[in] u32Src is system reset source. Including : 34 * - \ref SYS_RSTSTS_CPULKRF_Msk 35 * - \ref SYS_RSTSTS_CPURF_Msk 36 * - \ref SYS_RSTSTS_HRESETRF_Msk 37 * - \ref SYS_RSTSTS_SYSRF_Msk 38 * - \ref SYS_RSTSTS_BODRF_Msk 39 * - \ref SYS_RSTSTS_LVRF_Msk 40 * - \ref SYS_RSTSTS_WDTRF_Msk 41 * - \ref SYS_RSTSTS_PINRF_Msk 42 * - \ref SYS_RSTSTS_PORF_Msk 43 * @return None 44 * @details This function clear the selected system reset source. 45 */ SYS_ClearResetSrc(uint32_t u32Src)46void SYS_ClearResetSrc(uint32_t u32Src) 47 { 48 SYS->RSTSTS |= u32Src; 49 } 50 51 /** 52 * @brief Get Brown-out detector output status 53 * @param None 54 * @retval 0 System voltage is higher than BODVL setting or BODEN is 0. 55 * @retval 1 System voltage is lower than BODVL setting. 56 * @details This function get Brown-out detector output status. 57 */ SYS_GetBODStatus(void)58uint32_t SYS_GetBODStatus(void) 59 { 60 return ((SYS->BODCTL & SYS_BODCTL_BODOUT_Msk) >> SYS_BODCTL_BODOUT_Pos); 61 } 62 63 /** 64 * @brief Get reset status register value 65 * @param None 66 * @return Reset source 67 * @details This function get the system reset status register value. 68 */ SYS_GetResetSrc(void)69uint32_t SYS_GetResetSrc(void) 70 { 71 return (SYS->RSTSTS); 72 } 73 74 /** 75 * @brief Check if register is locked nor not 76 * @param None 77 * @retval 0 Write-protection function is disabled. 78 * 1 Write-protection function is enabled. 79 * @details This function check register write-protection bit setting. 80 */ SYS_IsRegLocked(void)81uint32_t SYS_IsRegLocked(void) 82 { 83 return (SYS->REGLCTL & 1UL) ? 0UL : 1UL; 84 } 85 86 /** 87 * @brief Get product ID 88 * @param None 89 * @return Product ID 90 * @details This function get product ID. 91 */ SYS_ReadPDID(void)92uint32_t SYS_ReadPDID(void) 93 { 94 return SYS->PDID; 95 } 96 97 /** 98 * @brief Reset chip with chip reset 99 * @param None 100 * @return None 101 * @details This function reset chip with chip reset. 102 * The register write-protection function should be disabled before using this function. 103 */ SYS_ResetChip(void)104void SYS_ResetChip(void) 105 { 106 SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk; 107 } 108 109 /** 110 * @brief Reset chip with CPU reset 111 * @param None 112 * @return None 113 * @details This function reset CPU with CPU reset. 114 * The register write-protection function should be disabled before using this function. 115 */ SYS_ResetCPU(void)116void SYS_ResetCPU(void) 117 { 118 SYS->IPRST0 |= SYS_IPRST0_CPURST_Msk; 119 } 120 121 /** 122 * @brief Reset selected module 123 * @param[in] u32ModuleIndex is module index. Including : 124 * - \ref ACMP01_RST 125 * - \ref ACMP2_RST 126 * - \ref CANFD0_RST 127 * - \ref CANFD1_RST 128 * - \ref CHIP_RST 129 * - \ref CPU_RST 130 * - \ref CRC_RST 131 * - \ref CRPT_RST 132 * - \ref DAC_RST 133 * - \ref EADC0_RST 134 * - \ref EBI_RST 135 * - \ref ECAP0_RST 136 * - \ref ECAP1_RST 137 * - \ref EPWM0_RST 138 * - \ref EPWM1_RST 139 * - \ref EQEI0_RST 140 * - \ref EQEI1_RST 141 * - \ref GPIO_RST 142 * - \ref I2C0_RST 143 * - \ref I2C1_RST 144 * - \ref I2C2_RST 145 * - \ref I2C3_RST 146 * - \ref LPADC0_RST 147 * - \ref LPGPIO_RST 148 * - \ref LPI2C0_RST 149 * - \ref LPPDMA0_RST 150 * - \ref LPSPI0_RST 151 * - \ref LPSRAM_RST 152 * - \ref LPTMR0_RST 153 * - \ref LPTMR1_RST 154 * - \ref LPUART0_RST 155 * - \ref OPA_RST 156 * - \ref OTG_RST 157 * - \ref PDMA0_RST 158 * - \ref PWM0_RST 159 * - \ref PWM1_RST 160 * - \ref QSPI0_RST 161 * - \ref SPI0_RST 162 * - \ref SPI1_RST 163 * - \ref SPI2_RST 164 * - \ref SPI3_RST 165 * - \ref TK_RST 166 * - \ref TMR0_RST 167 * - \ref TMR1_RST 168 * - \ref TMR2_RST 169 * - \ref TMR3_RST 170 * - \ref TRNG_RST 171 * - \ref TTMR0_RST 172 * - \ref TTMR1_RST 173 * - \ref UART0_RST 174 * - \ref UART1_RST 175 * - \ref UART2_RST 176 * - \ref UART3_RST 177 * - \ref UART4_RST 178 * - \ref UART5_RST 179 * - \ref UART6_RST 180 * - \ref UART7_RST 181 * - \ref USBD_RST 182 * - \ref USBH_RST 183 * - \ref USCI0_RST 184 * - \ref USCI1_RST 185 * - \ref UTCPD0_RST 186 * - \ref WDT_RST 187 * - \ref WWDT_RST 188 * @return None 189 * @details This function reset selected module. 190 */ SYS_ResetModule(uint32_t u32ModuleIndex)191void SYS_ResetModule(uint32_t u32ModuleIndex) 192 { 193 uint32_t u32tmpVal = 0UL, u32tmpAddr = 0UL; 194 195 if ((u32ModuleIndex & 0x80000000UL) == 0x80000000) 196 { 197 /* Support the IP RESET register NOT NEAR SYS_IPRST0 */ 198 if (((u32ModuleIndex & 0xFF000000UL) >> 24UL) == 0x80UL) 199 /* 0x80UL for LPSCC_IPRST0 */ 200 u32tmpAddr = (uint32_t)&LPSCC->IPRST0; 201 } 202 else 203 { 204 /* Support the IP RESET register NEAR SYS_IPRST0 */ 205 u32tmpAddr = (uint32_t)&SYS->IPRST0 + ((u32ModuleIndex >> 24UL)); 206 } 207 208 /* Generate reset signal to the corresponding module */ 209 u32tmpVal = (1UL << (u32ModuleIndex & 0x00ffffffUL)); 210 *(uint32_t *)u32tmpAddr |= u32tmpVal; 211 212 /* Release corresponding module from reset state */ 213 u32tmpVal = ~(1UL << (u32ModuleIndex & 0x00ffffffUL)); 214 *(uint32_t *)u32tmpAddr &= u32tmpVal; 215 } 216 217 /** 218 * @brief Enable and configure Brown-out detector function 219 * @param[in] i32Mode is reset or interrupt mode. Including : 220 * - \ref SYS_BODCTL_BOD_RST_EN 221 * - \ref SYS_BODCTL_BOD_INTERRUPT_EN 222 * @param[in] u32BODLevel is Brown-out voltage level. Including : 223 * - \ref SYS_BODCTL_BODVL_3_0V 224 * - \ref SYS_BODCTL_BODVL_2_8V 225 * - \ref SYS_BODCTL_BODVL_2_6V 226 * - \ref SYS_BODCTL_BODVL_2_4V 227 * - \ref SYS_BODCTL_BODVL_2_2V 228 * - \ref SYS_BODCTL_BODVL_2_0V 229 * - \ref SYS_BODCTL_BODVL_1_8V 230 * - \ref SYS_BODCTL_BODVL_1_6V 231 * - \ref SYS_BODCTL_BODVL_1_5V 232 * @return None 233 * @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level. 234 * The register write-protection function should be disabled before using this function. 235 */ SYS_EnableBOD(int32_t i32Mode,uint32_t u32BODLevel)236void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel) 237 { 238 /* Enable Brown-out Detector function */ 239 SYS->BODCTL |= SYS_BODCTL_BODEN_Msk; 240 241 /* Enable Brown-out interrupt or reset function */ 242 SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODRSTEN_Msk) | (uint32_t)i32Mode; 243 244 /* Select Brown-out Detector threshold voltage */ 245 SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODVL_Msk) | u32BODLevel; 246 } 247 248 /** 249 * @brief Disable Brown-out detector function 250 * @param None 251 * @return None 252 * @details This function disable Brown-out detector function. 253 * The register write-protection function should be disabled before using this function. 254 */ SYS_DisableBOD(void)255void SYS_DisableBOD(void) 256 { 257 SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk; 258 } 259 260 /** 261 * @brief Set Power Level 262 * @param[in] u32PowerLevel is power level setting. Including : 263 * - \ref SYS_PLCTL_PLSEL_PL1 264 * - \ref SYS_PLCTL_PLSEL_PL2 265 * @return Setting success or not 266 * @retval 0 Success 267 * @retval SYS_TIMEOUT_ERR Failed due to Power level register is busy 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 int32_t i32TimeOutCnt = 400; 274 275 /* Set power voltage level */ 276 SYS->PLCTL = (SYS->PLCTL & (~SYS_PLCTL_PLSEL_Msk)) | (u32PowerLevel); 277 while((SYS->PLSTS & SYS_PLSTS_PLCBUSY_Msk) == SYS_PLSTS_PLCBUSY_Msk) 278 { 279 if(i32TimeOutCnt-- <= 0) 280 { 281 return SYS_TIMEOUT_ERR; 282 } 283 } 284 return 0; 285 } 286 287 /** 288 * @brief Set Reference Voltage 289 * @param[in] u32VRefCTL is reference voltage setting. Including : 290 * - \ref SYS_VREFCTL_VREF_PIN 291 * - \ref SYS_VREFCTL_VREF_1_6V 292 * - \ref SYS_VREFCTL_VREF_2_0V 293 * - \ref SYS_VREFCTL_VREF_2_5V 294 * - \ref SYS_VREFCTL_VREF_3_0V 295 * @return None 296 * @details This function select reference voltage. 297 * The register write-protection function should be disabled before using this function. 298 */ SYS_SetVRef(uint32_t u32VRefCTL)299void SYS_SetVRef(uint32_t u32VRefCTL) 300 { 301 /* Set reference voltage */ 302 SYS->VREFCTL = (SYS->VREFCTL & (~SYS_VREFCTL_VREFCTL_Msk)) | (u32VRefCTL); 303 } 304 305 /** 306 * @brief Set System SRAM Power Mode 307 * @param[in] u32SRAMSel is SRAM region selection. Including : 308 * - \ref SYS_SRAMPC0_SRAM0PM_Msk 309 * - \ref SYS_SRAMPC0_SRAM1PM_Msk 310 * - \ref SYS_SRAMPC0_SRAM2PM_Msk 311 * - \ref SYS_SRAMPC0_SRAM3PM_Msk 312 * - \ref SYS_SRAMPC0_SRAM4PM_Msk 313 * - \ref SYS_SRAMPC0_SRAM5PM_Msk 314 * - \ref SYS_SRAMPC0_SRAM6PM_Msk 315 * @param[in] u32PowerMode is SRAM power mode. Including : 316 * - \ref SYS_SRAMPC0_SRAM_NORMAL 317 * - \ref SYS_SRAMPC0_SRAM_RETENTION 318 * - \ref SYS_SRAMPC0_SRAM_SHUT_DOWN 319 * @return Setting success or not 320 * @retval 0 Success 321 * @retval SYS_TIMEOUT_ERR Failed due to SRAM power register is busy 322 * @details This function set system SRAM power mode. 323 * The register write-protection function should be disabled before using this function. 324 */ SYS_SetSSRAMPowerMode(uint32_t u32SRAMSel,uint32_t u32PowerMode)325int32_t SYS_SetSSRAMPowerMode(uint32_t u32SRAMSel, uint32_t u32PowerMode) 326 { 327 int32_t i32TimeOutCnt; 328 uint32_t u32SRAMSelPos = SYS_SRAMPC0_SRAM0PM_Pos; 329 330 /* Get system SRAM power mode setting position */ 331 while(u32SRAMSelPos <= SYS_SRAMPC0_SRAM6PM_Pos) 332 { 333 if(u32SRAMSel & (BIT0 << u32SRAMSelPos)) 334 { 335 break; 336 } 337 else 338 { 339 u32SRAMSelPos++; 340 } 341 } 342 343 /* Set system SRAM power mode setting */ 344 SYS->SRAMPC0 = (SYS->SRAMPC0 & (~u32SRAMSel)) | (u32PowerMode << u32SRAMSelPos); 345 i32TimeOutCnt = 400; 346 while((SYS->SRAMPC0 & SYS_SRAMPC0_PCBUSY_Msk) == SYS_SRAMPC0_PCBUSY_Msk) 347 { 348 if(i32TimeOutCnt-- <= 0) 349 { 350 return SYS_TIMEOUT_ERR; 351 } 352 } 353 return 0; 354 } 355 356 /*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */ 357 358 /*@}*/ /* end of group SYS_Driver */ 359 360 /*@}*/ /* end of group Standard_Driver */ 361 362 #ifdef __cplusplus 363 } 364 #endif 365 366 /*** (C) COPYRIGHT 2023 Nuvoton Technology Corp. ***/ 367