1 /**
2   *********************************************************************************
3   *
4   * @file    ald_acmp.c
5   * @brief   ACMP module driver.
6   *
7   * @version V1.0
8   * @date    26 Jun 2019
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          26 Jun 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 ACMP ACMP
40   * @brief ACMP module driver
41   * @{
42   */
43 #ifdef ALD_ACMP
44 
45 /** @defgroup ACMP_Public_Functions ACMP Public Functions
46   * @{
47   */
48 
49 /** @defgroup ACMP_Public_Functions_Group1 Initialization functions
50   * @brief Initialization and Configuration functions
51   * @{
52   */
53 
54 /**
55   * @brief  Initializes the ACMP mode according to the specified parameters in
56   *         the acmp_init_t and create the associated handle.
57   * @param  hperh: Pointer to a acmp_handle_t structure that contains
58   *         the configuration information for the specified ACMP module.
59   * @retval Status, see @ref ald_status_t.
60   */
ald_acmp_init(acmp_handle_t * hperh)61 ald_status_t ald_acmp_init(acmp_handle_t *hperh)
62 {
63 	uint32_t tmp = 0;
64 
65 	assert_param(IS_ACMP_TYPE(hperh->perh));
66 	assert_param(IS_ACMP_MODE_TYPE(hperh->init.mode));
67 	assert_param(IS_ACMP_WARM_UP_TIME_TYPE(hperh->init.warm_time));
68 	assert_param(IS_ACMP_HYSTSEL_TYPE(hperh->init.hystsel));
69 	assert_param(IS_ACMP_POS_INPUT_TYPE(hperh->init.p_port));
70 	assert_param(IS_ACMP_NEG_INPUT_TYPE(hperh->init.n_port));
71 	assert_param(IS_ACMP_INACTVAL_TYPE(hperh->init.inactval));
72 	assert_param(IS_FUNC_STATE(hperh->init.out_inv));
73 	assert_param(IS_ACMP_EDGE_TYPE(hperh->init.edge));
74 	assert_param(hperh->init.vdd_level < 64);
75 
76 	__LOCK(hperh);
77 	tmp = ((hperh->init.mode << ACMP_CON_MODSEL_POSS) | (hperh->init.warm_time << ACMP_CON_WARMUPT_POSS) |
78 		(hperh->init.inactval << ACMP_CON_INACTV_POS) | (hperh->init.hystsel << ACMP_CON_HYSTSEL_POSS));
79 
80 	hperh->perh->CON = tmp;
81 
82 	tmp = 0;
83 
84 	tmp |= ((hperh->init.p_port << ACMP_INPUTSEL_PSEL_POSS) | (hperh->init.n_port << ACMP_INPUTSEL_NSEL_POSS) |
85 		(hperh->init.vdd_level << ACMP_INPUTSEL_VDDLVL_POSS));
86 	hperh->perh->INPUTSEL = tmp;
87 
88 	if (hperh->init.out_inv)
89 		SET_BIT(hperh->perh->CON, ACMP_CON_OUTINV_MSK);
90 	else
91 		CLEAR_BIT(hperh->perh->CON, ACMP_CON_OUTINV_MSK);
92 
93 	switch (hperh->init.edge) {
94 	case ACMP_EDGE_NONE:
95 		CLEAR_BIT(hperh->perh->CON, ACMP_CON_FALLEN_MSK);
96 		CLEAR_BIT(hperh->perh->CON, ACMP_CON_RISEEN_MSK);
97 		break;
98 
99 	case ACMP_EDGE_FALL:
100 		SET_BIT(hperh->perh->CON, ACMP_CON_FALLEN_MSK);
101 		CLEAR_BIT(hperh->perh->CON, ACMP_CON_RISEEN_MSK);
102 		break;
103 
104 	case ACMP_EDGE_RISE:
105 		CLEAR_BIT(hperh->perh->CON, ACMP_CON_FALLEN_MSK);
106 		SET_BIT(hperh->perh->CON, ACMP_CON_RISEEN_MSK);
107 		break;
108 
109 	case ACMP_EDGE_ALL:
110 		SET_BIT(hperh->perh->CON, ACMP_CON_FALLEN_MSK);
111 		SET_BIT(hperh->perh->CON, ACMP_CON_RISEEN_MSK);
112 		break;
113 
114 	default:
115 		break;
116 	}
117 
118 	SET_BIT(hperh->perh->CON, ACMP_CON_EN_MSK);
119 
120 	tmp = 0;
121 	while (READ_BIT(hperh->perh->STAT, ACMP_STAT_ACT_MSK) == 0) {
122 		if (tmp++ >= 600000) {
123 			__UNLOCK(hperh);
124 			return ERROR;
125 		}
126 	}
127 
128 	__UNLOCK(hperh);
129 	return OK;
130 }
131 /**
132   * @}
133   */
134 
135 /** @defgroup ACMP_Public_Functions_Group2 Interrupt operation functions
136   * @brief ACMP Interrupt operation functions
137   * @{
138   */
139 
140 /**
141   * @brief  Enables or disables the specified ACMP interrupts.
142   * @param  hperh: Pointer to a acmp_handle_t structure that contains
143   *         the configuration information for the specified ACMP module.
144   * @param  it: Specifies the ACMP interrupt sources to be enabled or disabled.
145   *         This parameter can be one of the @ref acmp_it_t.
146   * @param  state: New status
147   *           - ENABLE
148   *           - DISABLE
149   * @retval None
150   */
ald_acmp_interrupt_config(acmp_handle_t * hperh,acmp_it_t it,type_func_t state)151 void ald_acmp_interrupt_config(acmp_handle_t *hperh, acmp_it_t it, type_func_t state)
152 {
153 	assert_param(IS_ACMP_TYPE(hperh->perh));
154 	assert_param(IS_ACMP_IT_TYPE(it));
155 	assert_param(IS_FUNC_STATE(state));
156 
157 	if (state)
158 		hperh->perh->IES = it;
159 	else
160 		hperh->perh->IEC = it;
161 
162 	return;
163 }
164 
165 /**
166   * @brief  Checks whether the specified ACMP interrupt has set or not.
167   * @param  hperh: Pointer to a acmp_handle_t structure that contains
168   *         the configuration information for the specified ACMP module.
169   * @param  it: Specifies the ACMP interrupt sources to be enabled or disabled.
170   *         This parameter can be one of the @ref acmp_it_t.
171   * @retval it_status_t
172   *           - SET
173   *           - RESET
174   */
ald_acmp_get_it_status(acmp_handle_t * hperh,acmp_it_t it)175 it_status_t ald_acmp_get_it_status(acmp_handle_t *hperh, acmp_it_t it)
176 {
177 	assert_param(IS_ACMP_TYPE(hperh->perh));
178 	assert_param(IS_ACMP_IT_TYPE(it));
179 
180 	if (hperh->perh->IEV & it)
181 		return SET;
182 
183 	return RESET;
184 }
185 
186 /**
187   * @brief  Checks whether the specified ACMP interrupt has occurred or not.
188   * @param  hperh: Pointer to a acmp_handle_t structure that contains
189   *         the configuration information for the specified ACMP module.
190   * @param  flag: Specifies the ACMP interrupt source to check.
191   *         This parameter can be one of the @ref acmp_flag_t.
192   * @retval flag_status_t
193   *           - SET
194   *           - RESET
195   */
ald_acmp_get_flag_status(acmp_handle_t * hperh,acmp_flag_t flag)196 flag_status_t ald_acmp_get_flag_status(acmp_handle_t *hperh, acmp_flag_t flag)
197 {
198 	assert_param(IS_ACMP_TYPE(hperh->perh));
199 	assert_param(IS_ACMP_FLAG_TYPE(flag));
200 
201 	if (hperh->perh->RIF & flag)
202 		return SET;
203 
204 	return RESET;
205 }
206 
207 /**
208   * @brief  Get the status of interrupt flag and interupt source.
209   * @param  hperh: Pointer to a acmp_handle_t structure that contains
210   *               the configuration information for the specified ACMP.
211   * @param  flag: Specifies the ACMP interrupt flag.
212   * @retval Status:
213   *           - 0: RESET
214   *           - 1: SET
215   */
ald_acmp_get_mask_flag_status(acmp_handle_t * hperh,acmp_flag_t flag)216 flag_status_t ald_acmp_get_mask_flag_status(acmp_handle_t *hperh, acmp_flag_t flag)
217 {
218 	assert_param(IS_ACMP_TYPE(hperh->perh));
219 	assert_param(IS_ACMP_FLAG_TYPE(flag));
220 
221 	if (hperh->perh->IFM & flag)
222 		return SET;
223 
224 	return RESET;
225 }
226 
227 /** @brief  Clear the specified ACMP it flags.
228   * @param  hperh: Pointer to a acmp_handle_t structure that contains
229   *         the configuration information for the specified ACMP module.
230   * @param  flag: specifies the it flag.
231   *         This parameter can be one of the @ref acmp_flag_t.
232   * @retval None
233   */
ald_acmp_clear_flag_status(acmp_handle_t * hperh,acmp_flag_t flag)234 void ald_acmp_clear_flag_status(acmp_handle_t *hperh, acmp_flag_t flag)
235 {
236 	assert_param(IS_ACMP_TYPE(hperh->perh));
237 	assert_param(IS_ACMP_FLAG_TYPE(flag));
238 
239 	hperh->perh->IFC = flag;
240 	return;
241 }
242 /**
243   * @}
244   */
245 /** @defgroup ACMP_Public_Functions_Group3 Output value functions
246   * @brief ACMP Output value functions
247   * @{
248   */
249 /**
250   * @brief  This function handles ACMP interrupt request.
251   * @param  hperh: Pointer to a acmp_handle_t structure that contains
252   *         the configuration information for the specified ACMP module.
253   * @retval None
254   */
ald_acmp_irq_handler(acmp_handle_t * hperh)255 void ald_acmp_irq_handler(acmp_handle_t *hperh)
256 {
257 	if ((ald_acmp_get_mask_flag_status(hperh, ACMP_FLAG_WARMUP)) == SET) {
258 		ald_acmp_clear_flag_status(hperh, ACMP_FLAG_WARMUP);
259 
260 		if (hperh->acmp_warmup_cplt_cbk)
261 			hperh->acmp_warmup_cplt_cbk(hperh);
262 	}
263 
264 	if ((ald_acmp_get_mask_flag_status(hperh, ACMP_FLAG_EDGE)) == SET) {
265 		ald_acmp_clear_flag_status(hperh, ACMP_FLAG_EDGE);
266 
267 		if (hperh->acmp_edge_cplt_cbk)
268 			hperh->acmp_edge_cplt_cbk(hperh);
269 	}
270 
271 	return;
272 }
273 
274 /**
275   * @brief  This function config acmp output.
276   * @param  hperh: Pointer to a acmp_handle_t structure that contains
277   *         the configuration information for the specified ACMP module.
278   * @param  state: ENABLE/DISABLE.
279   * @retval None
280   */
ald_acmp_out_config(acmp_handle_t * hperh,type_func_t state)281 void ald_acmp_out_config(acmp_handle_t *hperh, type_func_t state)
282 {
283 	assert_param(IS_ACMP_TYPE(hperh->perh));
284 	assert_param(IS_FUNC_STATE(state));
285 
286 	if (state)
287 		SET_BIT(hperh->perh->PORT, ACMP_PORT_PEN_MSK);
288 	else
289 		CLEAR_BIT(hperh->perh->PORT, ACMP_PORT_PEN_MSK);
290 
291 	return;
292 }
293 
294 /**
295   * @brief  This function output acmp result.
296   * @param  hperh: Pointer to a acmp_handle_t structure that contains
297   *         the configuration information for the specified ACMP module.
298   * @retval output value.
299   */
ald_acmp_out_result(acmp_handle_t * hperh)300 uint8_t ald_acmp_out_result(acmp_handle_t *hperh)
301 {
302 	assert_param(IS_ACMP_TYPE(hperh->perh));
303 
304 	return (READ_BIT(hperh->perh->STAT, ACMP_STAT_OUT_MSK) >> ACMP_STAT_OUT_POS);
305 }
306 
307 /** @brief  Check whether the specified ACMP flag is set or not.
308   * @param  hperh: Pointer to a acmp_handle_t structure that contains
309   *         the configuration information for the specified ACMP module.
310   * @param  status: specifies the status to check.
311   *         This parameter can be one of the @ref acmp_status_t.
312   * @retval flag_status_t
313   *           - SET
314   *           - RESET
315   */
ald_acmp_get_status(acmp_handle_t * hperh,acmp_status_t status)316 flag_status_t ald_acmp_get_status(acmp_handle_t *hperh, acmp_status_t status)
317 {
318 	assert_param(IS_ACMP_TYPE(hperh->perh));
319 	assert_param(IS_ACMP_STATUS_TYPE(status));
320 
321 	if (hperh->perh->STAT & status)
322 		return SET;
323 
324 	return RESET;
325 }
326 /**
327   * @}
328   */
329 /**
330   * @}
331   */
332 #endif /* ALD_ACMP */
333 /**
334   * @}
335   */
336 /**
337   * @}
338   */
339