summaryrefslogtreecommitdiff
path: root/bsp/wch/arm/libraries/CH32F20x_StdPeriph_Driver/StdPeriph_Driver/src/ch32f20x_bkp.c
blob: e62605791a330ecdd263ff4647d7be58a8df6c85 (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
/********************************** (C) COPYRIGHT  *******************************
* File Name          : ch32f20x_bkp.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/08/08
* Description        : This file provides all the BKP firmware functions.
*******************************************************************************/ 
#include "ch32f20x_bkp.h"
#include "ch32f20x_rcc.h"

/* BKP registers bit mask */

/* OCTLR register bit mask */
#define OCTLR_CAL_MASK    ((uint16_t)0xFF80)
#define OCTLR_MASK        ((uint16_t)0xFC7F)

/*******************************************************************************
* Function Name  : BKP_DeInit
* Description    : Deinitializes the BKP peripheral registers to their default 
*      reset values.
* Input          : None
* Return         : None
*******************************************************************************/	
void BKP_DeInit(void)
{
  RCC_BackupResetCmd(ENABLE);
  RCC_BackupResetCmd(DISABLE);
}

/*******************************************************************************
* Function Name  : BKP_TamperPinLevelConfig
* Description    : Configures the Tamper Pin active level.
* Input          : BKP_TamperPinLevel: specifies the Tamper Pin active level.
*                    BKP_TamperPinLevel_High: Tamper pin active on high level.
*                    BKP_TamperPinLevel_Low: Tamper pin active on low level.
* Return         : None
*******************************************************************************/	
void BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)
{
	if(BKP_TamperPinLevel)
	{
		BKP->TPCTLR |= ((uint32_t)1<<1);
	}
	else
	{
		BKP->TPCTLR &= ~((uint32_t)1<<1);
	}
}

/*******************************************************************************
* Function Name  : BKP_TamperPinCmd
* Description    : Enables or disables the Tamper Pin activation.
* Input          : NewState: ENABLE or DISABLE.
* Return         : None
*******************************************************************************/	
void BKP_TamperPinCmd(FunctionalState NewState)
{
	if(NewState)
	{
		BKP->TPCTLR |= ((uint32_t)1<<0);
	}
	else
	{
		BKP->TPCTLR &= ~((uint32_t)1<<0);		
	}
}

/*******************************************************************************
* Function Name  : BKP_ITConfig
* Description    : Enables or disables the Tamper Pin Interrupt.
* Input          : NewState: ENABLE or DISABLE.
* Return         : None
*******************************************************************************/	
void BKP_ITConfig(FunctionalState NewState)
{
	if(NewState)
	{
		BKP->TPCSR |= ((uint32_t)1<<2);
	}
	else
	{
		BKP->TPCSR &= ~((uint32_t)1<<2);		
	}	
}

/*******************************************************************************
* Function Name  : BKP_RTCOutputConfig
* Description    : Select the RTC output source to output on the Tamper pin.
* Input          : BKP_RTCOutputSource: specifies the RTC output source.
*                    BKP_RTCOutputSource_None: no RTC output on the Tamper pin.
*                    BKP_RTCOutputSource_CalibClock: output the RTC clock with 
*      frequency divided by 64 on the Tamper pin.
*                    BKP_RTCOutputSource_Alarm: output the RTC Alarm pulse signal 
*      on the Tamper pin.
*                    BKP_RTCOutputSource_Second: output the RTC Second pulse 
*      signal on the Tamper pin. 
* Return         : None
*******************************************************************************/	
void BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)
{
  uint16_t tmpreg = 0;

  tmpreg = BKP->OCTLR;
  tmpreg &= OCTLR_MASK;
  tmpreg |= BKP_RTCOutputSource;
  BKP->OCTLR = tmpreg;
}

/*******************************************************************************
* Function Name  : BKP_SetRTCCalibrationValue
* Description    : Sets RTC Clock Calibration value.
* Input          : CalibrationValue: specifies the RTC Clock Calibration value.
*                    This parameter must be a number between 0 and 0x1F.
* Return         : None
*******************************************************************************/
void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)
{
  uint16_t tmpreg = 0;

  tmpreg = BKP->OCTLR;
  tmpreg &= OCTLR_CAL_MASK;
  tmpreg |= CalibrationValue;
  BKP->OCTLR = tmpreg;
}

/*******************************************************************************
* Function Name  : BKP_WriteBackupRegister
* Description    : Writes user data to the specified Data Backup Register.
* Input          : BKP_DR: specifies the Data Backup Register.
*                  Data: data to write.
* Return         : None
*******************************************************************************/
void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data)
{
  __IO uint32_t tmp = 0;

  tmp = (uint32_t)BKP_BASE; 
  tmp += BKP_DR;
  *(__IO uint32_t *) tmp = Data;
}

/*******************************************************************************
* Function Name  : BKP_ReadBackupRegister
* Description    : Reads data from the specified Data Backup Register.
* Input          : BKP_DR: specifies the Data Backup Register.
*                    This parameter can be BKP_DRx where x:[1, 42].
* Return         : None
*******************************************************************************/
uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR)
{
  __IO uint32_t tmp = 0;

  tmp = (uint32_t)BKP_BASE; 
  tmp += BKP_DR;

  return (*(__IO uint16_t *) tmp);
}

/*******************************************************************************
* Function Name  : BKP_GetFlagStatus
* Description    : Checks whether the Tamper Pin Event flag is set or not.
* Input          : None
* Return         : FlagStatus: SET or RESET.
*******************************************************************************/
FlagStatus BKP_GetFlagStatus(void)
{
	if(BKP->TPCSR & (1<<8))
	{
		return SET;
	}
	else
	{
		return RESET;		
	}
}

/*******************************************************************************
* Function Name  : BKP_ClearFlag
* Description    : Clears Tamper Pin Event pending flag.
* Input          : None
* Return         : None
*******************************************************************************/
void BKP_ClearFlag(void)
{
  BKP->TPCSR |= BKP_CTE;
}

/*******************************************************************************
* Function Name  : BKP_GetITStatus
* Description    : Checks whether the Tamper Pin Interrupt has occurred or not.
* Input          : None
* Return         : ITStatus: SET or RESET.
*******************************************************************************/
ITStatus BKP_GetITStatus(void)
{
	if(BKP->TPCSR & ((uint32_t)1<<9))
	{
		return SET;
	}
	else
	{
		return RESET;		
	}
}

/*******************************************************************************
* Function Name  : BKP_ClearITPendingBit
* Description    : Clears Tamper Pin Interrupt pending bit.
* Input          : None
* Return         : None
*******************************************************************************/
void BKP_ClearITPendingBit(void)
{
  BKP->TPCSR |= BKP_CTI;
}