summaryrefslogtreecommitdiff
path: root/bsp/nuvoton_nuc472/Libraries/StdDriver/src/sys_.c
blob: ec6c2410f43d282599870b5d80950c43f62b968d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*************************************************************************//**
 * @file     sys.c
 * @version  V1.00
 * $Revision: 16 $
 * $Date: 15/10/21 1:39p $
 * @brief    NUC472/NUC442 SYS driver source file
 *
 * @note
 * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/

#include  "NUC472_442.h"
/** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  @{
*/

/** @addtogroup NUC472_442_SYS_Driver SYS Driver
  @{
*/


/** @addtogroup NUC472_442_SYS_EXPORTED_FUNCTIONS SYS Exported Functions
  @{
*/

/**
  * @brief  This function clear the selected system reset source
  * @param[in]  u32RstSrc is system reset source. Including:
    *           - \ref SYS_RSTSTS_PORF_Msk
    *           - \ref SYS_RSTSTS_PINRF_Msk
    *           - \ref SYS_RSTSTS_WDTRF_Msk
    *           - \ref SYS_RSTSTS_LVRF_Msk
    *           - \ref SYS_RSTSTS_BODRF_Msk
    *           - \ref SYS_RSTSTS_SYSRF_Msk
    *           - \ref SYS_RSTSTS_CPURF_Msk
  * @return None
  */
void SYS_ClearResetSrc(uint32_t u32RstSrc)
{
    SYS->RSTSTS |= u32RstSrc;
}

/**
  * @brief  This function get Brown-out detector output status
  * @return 0: System voltage is higher than BODVL setting or BODEN is 0.
  *         1: System voltage is lower than BODVL setting.
  *         Note : If the BOD_EN is 0, this function always return 0.
  */
uint32_t SYS_GetBODStatus()
{
    return (SYS->BODCTL & SYS_BODCTL_BODOUT_Msk);
}

/**
  * @brief  This function get the system reset source register value
  * @return Reset source
  */
uint32_t SYS_GetResetSrc(void)
{
    return (SYS->RSTSTS);
}

/**
  * @brief  This function check register write-protection bit setting
  * @return 0: Write-protection function is disabled.
  *         1: Write-protection function is enabled.
  */
uint32_t SYS_IsRegLocked(void)
{
    return !(SYS->REGLCTL & SYS_REGLCTL_REGLCTL_Msk);
}

/**
  * @brief  This function enable register write-protection function
  * @return None
  * @details To lock the protected register to forbid write access
  */
void SYS_LockReg(void)
{
    SYS->REGLCTL = 0;
}


/**
  * @brief  This function disable register write-protection function
  * @return None
  * @details To unlock the protected register to allow write access
  */
void SYS_UnlockReg(void)
{
    while(SYS->REGLCTL != SYS_REGLCTL_REGLCTL_Msk) {
        SYS->REGLCTL = 0x59;
        SYS->REGLCTL = 0x16;
        SYS->REGLCTL = 0x88;
    }
}

/**
  * @brief  This function get product ID.
  * @return Product ID
  */
uint32_t  SYS_ReadPDID(void)
{
    return SYS->PDID;
}

/**
  * @brief  This function reset chip.
  * @return None
  */
void SYS_ResetChip(void)
{
    SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk;
}

/**
  * @brief  This function reset CPU.
  * @return None
  */
void SYS_ResetCPU(void)
{
    SYS->IPRST0 |= SYS_IPRST0_CPURST_Msk;
}

/**
  * @brief  This function reset selected modules.
  * @param[in]  u32ModuleIndex is module index. Including :
  * - \ref PDMA_RST
  * - \ref EBI_RST
  * - \ref USBH_RST
  * - \ref EMAC_RST
  * - \ref SDH_RST
  * - \ref CRC_RST
  * - \ref CAP_RST
  * - \ref CRYPTO_RST
  * - \ref GPIO_RST
  * - \ref TMR0_RST
  * - \ref TMR1_RST
  * - \ref TMR2_RST
  * - \ref TMR3_RST
  * - \ref ACMP_RST
  * - \ref I2C0_RST
  * - \ref I2C1_RST
  * - \ref I2C2_RST
  * - \ref I2C3_RST
  * - \ref SPI0_RST
  * - \ref SPI1_RST
  * - \ref SPI2_RST
  * - \ref SPI3_RST
  * - \ref UART0_RST
  * - \ref UART1_RST
  * - \ref UART2_RST
  * - \ref UART3_RST
  * - \ref UART4_RST
  * - \ref UART5_RST
  * - \ref CAN0_RST
  * - \ref CAN1_RST
  * - \ref OTG_RST
  * - \ref USBD_RST
  * - \ref ADC_RST
  * - \ref I2S0_RST
  * - \ref I2S1_RST
  * - \ref PS2_RST
  * - \ref SC0_RST
  * - \ref SC1_RST
  * - \ref SC2_RST
  * - \ref SC3_RST
  * - \ref SC4_RST
  * - \ref SC5_RST
  * - \ref I2C4_RST
  * - \ref PWM0_RST
  * - \ref PWM1_RST
  * - \ref QEI0_RST
  * - \ref QEI1_RST
  * @return None
  */
void SYS_ResetModule(uint32_t u32ModuleIndex)
{
    *(volatile uint32_t *)((uint32_t)&(SYS->IPRST0) + (u32ModuleIndex>>24)) |= 1<<(u32ModuleIndex & 0x00ffffff);
    *(volatile uint32_t *)((uint32_t)&(SYS->IPRST0) + (u32ModuleIndex>>24)) &= ~(1<<(u32ModuleIndex & 0x00ffffff));
}

/**
  * @brief  This function configure BOD function.
  *         Configure BOD reset or interrupt mode and set Brown-out voltage level.
  *         Enable Brown-out function
  * @param[in]  i32Mode is reset or interrupt mode. Including :
  *         - \ref SYS_BODCTL_BODRSTEN
  *         - \ref SYS_BODCTL_BODINTEN
  * @param[in]  u32BODLevel is Brown-out voltage level. Including :
  *         - \ref SYS_BODCTL_BODVL_2_2V
  *         - \ref SYS_BODCTL_BODVL_2_7V
  *         - \ref SYS_BODCTL_BODVL_3_8V
  *         - \ref SYS_BODCTL_BODVL_4_5V
  *
  * @return None
  */
void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel)
{
    SYS->BODCTL &= ~(SYS_BODCTL_BODVL_Msk|SYS_BODCTL_BODRSTEN_Msk);
    SYS->BODCTL |=(i32Mode|u32BODLevel|SYS_BODCTL_BODEN_Msk);
}

/**
  * @brief  This function disable BOD function.
  * @return None
  */
void SYS_DisableBOD(void)
{
    SYS->BODCTL  &= ~SYS_BODCTL_BODEN_Msk;
}



/*@}*/ /* end of group NUC472_442_SYS_EXPORTED_FUNCTIONS */

/*@}*/ /* end of group NUC472_442_SYS_Driver */

/*@}*/ /* end of group NUC472_442_Device_Driver */

/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/