1 /**
2   *********************************************************************************
3   *
4   * @file    ald_bkpc.c
5   * @brief   BKPC module driver.
6   *
7   * @version V1.0
8   * @date    15 Dec 2019
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          15 Dec 2019     AE Team         The first version
14   *
15   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
16   *
17   * SPDX-License-Identifier: Apache-2.0
18   *
19   * Licensed under the Apache License, Version 2.0 (the License); you may
20   * not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
27   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License.
30   **********************************************************************************
31   */
32 
33 #include "ald_conf.h"
34 
35 /** @addtogroup ES32FXXX_ALD
36   * @{
37   */
38 
39 /** @defgroup BKPC BKPC
40   * @brief BKPC module driver
41   * @{
42   */
43 #ifdef ALD_BKPC
44 
45 /** @defgroup BKPC_Public_Functions BKPC Public Functions
46   * @{
47   */
48 
49 /** @addtogroup BKPC_Public_Functions_Group1 Peripheral Control functions
50   * @brief Peripheral Control functions
51   *
52   * @verbatim
53   ==============================================================================
54               ##### Peripheral Control functions #####
55   ==============================================================================
56   [..]  This section provides functions allowing to:
57     (+) ald_bkpc_standby_wakeup_config() API can configure STANDBY wakeup.
58     (+) ald_bkpc_rtc_clock_config() API can configure RTC clock.
59     (+) ald_bkpc_tsense_clock_config() API can configure Tsense clock.
60 
61     @endverbatim
62   * @{
63   */
64 
65 /**
66   * @brief  Configure standby wakeup in backup field
67   * @param  port: Wakeup port
68   * @param  level: HIGH/LOW.
69   * @retval None
70   */
ald_bkpc_standby_wakeup_config(bkpc_wakeup_port_t port,bkpc_wakeup_level_t level)71 void ald_bkpc_standby_wakeup_config(bkpc_wakeup_port_t port, bkpc_wakeup_level_t level)
72 {
73 	assert_param(IS_BKPC_WAKEUP_PORT(port));
74 	assert_param(IS_BKPC_WAKEUP_LEVEL(level));
75 
76 	if (port == PMU_STANDBY_PORT_SEL_NONE) {
77 		BKPC_UNLOCK();
78 		CLEAR_BIT(BKPC->CR, BKPC_CR_WKPEN_MSK);
79 		SET_BIT(BKPC->CR, BKPC_CR_MRST_WKPEN_MSK);
80 		BKPC_LOCK();
81 		return;
82 	}
83 
84 	BKPC_UNLOCK();
85 	SET_BIT(BKPC->CR, BKPC_CR_WKPEN_MSK | BKPC_CR_MRST_WKPEN_MSK);
86 	MODIFY_REG(BKPC->CR, BKPC_CR_WKPS_MSK, port << BKPC_CR_WKPS_POSS);
87 	MODIFY_REG(BKPC->CR, BKPC_CR_WKPOL_MSK, level << BKPC_CR_WKPOL_POS);
88 	BKPC_LOCK();
89 
90 	return;
91 }
92 
93 /**
94   * @brief  Configure rtc clock in backup field
95   * @param  clock: Clock
96   * @retval None
97   */
ald_bkpc_rtc_clock_config(bkpc_preh_clk_t clock)98 void ald_bkpc_rtc_clock_config(bkpc_preh_clk_t clock)
99 {
100 	assert_param(IS_BKPC_PREH_CLOCK(clock));
101 
102 	BKPC_UNLOCK();
103 	MODIFY_REG(BKPC->PCCR, BKPC_PCCR_RTCCS_MSK, clock << BKPC_PCCR_RTCCS_POSS);
104 	BKPC_LOCK();
105 
106 	return;
107 }
108 
109 /**
110   * @brief  Configure tsense clock in backup field
111   * @param  clock: Clock
112   * @retval None
113   */
ald_bkpc_tsense_clock_config(bkpc_preh_clk_t clock)114 void ald_bkpc_tsense_clock_config(bkpc_preh_clk_t clock)
115 {
116 	assert_param(IS_BKPC_PREH_CLOCK(clock));
117 
118 	BKPC_UNLOCK();
119 	MODIFY_REG(BKPC->PCCR, BKPC_PCCR_TSENSECS_MSK, clock << BKPC_PCCR_TSENSECS_POSS);
120 	BKPC_LOCK();
121 
122 	return;
123 }
124 /**
125   * @}
126   */
127 
128 /** @addtogroup BKPC_Public_Functions_Group2 IO operation functions
129   * @brief IO operation functions
130   *
131   * @verbatim
132   ==============================================================================
133               ##### IO operation functions #####
134   ==============================================================================
135   [..]  This section provides functions allowing to:
136     (+) ald_bkpc_write_ram() API can write data in backup ram.
137     (+) ald_bkpc_read_ram() API can read data from backup ram.
138 
139     @endverbatim
140   * @{
141   */
142 
143 /**
144   * @brief  Write data into backup ram.
145   * @param  idx: Index of backup word.
146   * @param  value: Value which will be written to backup ram.
147   * @retval None
148   */
ald_bkpc_write_ram(uint8_t idx,uint32_t value)149 void ald_bkpc_write_ram(uint8_t idx, uint32_t value)
150 {
151 	assert_param(IS_BKPC_RAM_IDX(idx));
152 
153 	RTC_UNLOCK();
154 	WRITE_REG(RTC->BKPR[idx], value);
155 	RTC_LOCK();
156 
157 	return;
158 }
159 
160 /**
161   * @brief  Read data from backup ram.
162   * @param  idx: Index of backup word.
163   * @retval The data.
164   */
ald_bkpc_read_ram(uint8_t idx)165 uint32_t ald_bkpc_read_ram(uint8_t idx)
166 {
167 	assert_param(IS_BKPC_RAM_IDX(idx));
168 
169 	return READ_REG(RTC->BKPR[idx]);
170 }
171 /**
172   * @}
173   */
174 
175 /**
176   * @}
177   */
178 #endif /* ALD_BKPC */
179 /**
180   * @}
181   */
182 
183 /**
184   * @}
185   */
186