1 /*!
2 * @file apm32f0xx_comp.c
3 *
4 * @brief This file contains all the functions for the COMP peripheral
5 *
6 * @version V1.0.3
7 *
8 * @date 2022-09-20
9 *
10 * @attention
11 *
12 * Copyright (C) 2020-2022 Geehy Semiconductor
13 *
14 * You may not use this file except in compliance with the
15 * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16 *
17 * The program is only for reference, which is distributed in the hope
18 * that it will be useful and instructional for customers to develop
19 * their software. Unless required by applicable law or agreed to in
20 * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21 * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23 * and limitations under the License.
24 */
25
26 /* Includes */
27 #include "apm32f0xx_comp.h"
28 #include "apm32f0xx_rcm.h"
29
30 /** @addtogroup APM32F0xx_StdPeriphDriver
31 @{
32 */
33
34 /** @addtogroup COMP_Driver
35 @{
36 */
37
38 /** @defgroup COMP_Macros Macros
39 @{
40 */
41
42 /**@} end of group COMP_Macros */
43
44 /** @defgroup COMP_Enumerations Enumerations
45 @{
46 */
47
48 /**@} end of group COMP_Enumerations */
49
50 /** @defgroup COMP_Structures Structures
51 @{
52 */
53
54 /**@} end of group COMP_Structures */
55
56 /** @defgroup COMP_Variables Variables
57 @{
58 */
59
60 /**@} end of group COMP_Variables */
61
62 /** @defgroup COMP_Functions Functions
63 @{
64 */
65
66 /*!
67 * @brief Reset COMP peripheral registers to their default values.
68 *
69 * @param None
70 *
71 * @retval None
72 *
73 * @note Deinitialization can't be performed if the COMP configuration is locked.
74 * To unlock the configuration, perform a system reset.
75 */
COMP_Reset(void)76 void COMP_Reset(void)
77 {
78 COMP->CSTS = ((uint32_t)0x00000000);
79 }
80
81 /*!
82 * @brief Configs the COMP peripheral according to the specified parameters
83 * in COMP_InitStruct
84 *
85 * @param compSelect: the selected comparator.
86 * This parameter can be one of the following values:
87 * @arg COMP_SELECT_COMP1: COMP1 selected
88 * @arg COMP_SELECT_COMP2: COMP2 selected
89 *
90 * @param compConfig: pointer to an COMP_Config_T structure that contains
91 * the configuration information for the specified COMP peripheral.
92 *
93 * @retval None
94 *
95 * @note If the selected comparator is locked, initialization can't be performed.
96 * To unlock the configuration, perform a system reset.
97 *
98 * @note By default, PA1 is selected as COMP1 non inverting input.
99 * To use PA4 as COMP1 non inverting input call COMP_EnableSwitch() after COMP_Config()
100 *
101 */
COMP_Config(COMP_SELECT_T compSelect,COMP_Config_T * compConfig)102 void COMP_Config(COMP_SELECT_T compSelect, COMP_Config_T* compConfig)
103 {
104 if (compSelect == COMP_SELECT_COMP1)
105 {
106 COMP->CSTS_B.INVINSEL1 = compConfig->invertingInput;
107 COMP->CSTS_B.OUTSEL1 = compConfig->output;
108 COMP->CSTS_B.OPINV1 = compConfig->outputPol;
109 COMP->CSTS_B.HYSCFG1 = compConfig->hysterrsis;
110 COMP->CSTS_B.MOD1 = compConfig->mode;
111 }
112 else
113 {
114 COMP->CSTS_B.INVINSEL2 = compConfig->invertingInput;
115 COMP->CSTS_B.OUTSEL2 = compConfig->output;
116 COMP->CSTS_B.OPINV2 = compConfig->outputPol;
117 COMP->CSTS_B.HYSCFG2 = compConfig->hysterrsis;
118 COMP->CSTS_B.MOD2 = compConfig->mode;
119 }
120 }
121
122 /*!
123 * @brief Fills each compConfig member with initial value value.
124 *
125 * @param compConfig: pointer to an COMP_InitTypeDef structure which will
126 * be initialized.
127 *
128 * @retval None
129 */
COMP_ConfigStructInit(COMP_Config_T * compConfig)130 void COMP_ConfigStructInit(COMP_Config_T* compConfig)
131 {
132 compConfig->invertingInput = COMP_INVERTING_INPUT_1_4VREFINT;
133 compConfig->output = COMP_OUTPUT_NONE;
134 compConfig->outputPol = COMP_OUTPUTPOL_NONINVERTED;
135 compConfig->hysterrsis = COMP_HYSTERRSIS_NO;
136 compConfig->mode = COMP_MODE_HIGHSPEED;
137 }
138
139 /*!
140 * @brief Enable the COMP peripheral.
141 *
142 * @param compSelect: the selected comparator.
143 * This parameter can be one of the following values:
144 * @arg COMP_SELECT_COMP1: COMP1 selected
145 * @arg COMP_SELECT_COMP2: COMP2 selected
146 *
147 * @retval None
148 *
149 * @note If the selected comparator is locked, enable can't be performed.
150 * To unlock the configuration, perform a system reset.
151 */
COMP_Enable(COMP_SELECT_T compSelect)152 void COMP_Enable(COMP_SELECT_T compSelect)
153 {
154 if (compSelect == COMP_SELECT_COMP1)
155 {
156 COMP->CSTS_B.EN1 = SET;
157 }
158 else
159 {
160 COMP->CSTS_B.EN2 = SET;
161 }
162 }
163
164 /*!
165 * @brief Disable the COMP peripheral.
166 *
167 * @param compSelect: the selected comparator.
168 * This parameter can be one of the following values:
169 * @arg COMP_SELECT_COMP1: COMP1 selected
170 * @arg COMP_SELECT_COMP2: COMP2 selected
171 *
172 * @retval None
173 *
174 * @note If the selected comparator is locked, disable can't be performed.
175 * To unlock the configuration, perform a system reset.
176 */
COMP_Disable(COMP_SELECT_T compSelect)177 void COMP_Disable(COMP_SELECT_T compSelect)
178 {
179 if (compSelect == COMP_SELECT_COMP1)
180 {
181 COMP->CSTS_B.EN1 = RESET;
182 }
183 else
184 {
185 COMP->CSTS_B.EN2 = RESET;
186 }
187 }
188
189 /*!
190 * @brief Close the SW1 switch.
191 *
192 * @param None
193 *
194 * @retval None
195 *
196 * @note This switch is solely intended to redirect signals onto high
197 * impedance input, such as COMP1 non-inverting input (highly resistive switch)
198 */
COMP_EnableSwitch(void)199 void COMP_EnableSwitch(void)
200 {
201 COMP->CSTS_B.SW1 = SET;
202 }
203
204 /*!
205 * @brief Open the SW1 switch.
206 *
207 * @param None
208 *
209 * @retval None
210 */
COMP_DisableSwitch(void)211 void COMP_DisableSwitch(void)
212 {
213 COMP->CSTS_B.SW1 = RESET;
214 }
215
216 /*!
217 * @brief Return the output level (high or low) of the selected comparator.
218 *
219 * @param compSelect: the selected comparator.
220 * This parameter can be one of the following values:
221 * @arg COMP_SELECT_COMP1: COMP1 selected
222 * @arg COMP_SELECT_COMP2: COMP2 selected
223 *
224 * @retval Returns the selected comparator output level: low or high.
225 */
COMP_ReadOutPutLevel(COMP_SELECT_T compSelect)226 uint32_t COMP_ReadOutPutLevel(COMP_SELECT_T compSelect)
227 {
228 uint32_t compOUT = 0x00;
229 if (compSelect == COMP_SELECT_COMP1)
230 {
231 if ((COMP->CSTS & COMP_CSTS_COMP1OUT) != 0)
232 {
233 compOUT = COMP_OUTPUTLEVEL_HIGH;
234 }
235 else
236 compOUT = COMP_OUTPUTLEVEL_LOW;
237 }
238 else
239 {
240 if ((COMP->CSTS & COMP_CSTS_COMP2OUT) != 0)
241 {
242 compOUT = COMP_OUTPUTLEVEL_HIGH;
243 }
244 else
245 compOUT = COMP_OUTPUTLEVEL_LOW;
246 }
247 return (uint32_t)(compOUT);
248 }
249
250 /*!
251 * @brief Enablesthe window mode.
252 *
253 * @param None
254 *
255 * @retval None
256 */
COMP_EnableWindow(void)257 void COMP_EnableWindow(void)
258 {
259 COMP->CSTS_B.WMODEN = SET;
260 }
261
262 /*!
263 * @brief Disables the window mode.
264 *
265 * @param None
266 *
267 * @retval None
268 */
COMP_DisnableWindow(void)269 void COMP_DisnableWindow(void)
270 {
271 COMP->CSTS_B.WMODEN = RESET;
272 }
273
274 /*!
275 * @brief Lock the selected comparator configuration.
276 *
277 * @param compSelect: selects the comparator to be locked
278 * This parameter can be one of the following values:
279 * @arg COMP_SELECT_COMP1: COMP1 configuration is locked.
280 * @arg COMP_SELECT_COMP2: COMP2 configuration is locked.
281 *
282 * @retval None
283 */
COMP_ConfigLOCK(COMP_SELECT_T compSelect)284 void COMP_ConfigLOCK(COMP_SELECT_T compSelect)
285 {
286 if (compSelect == COMP_SELECT_COMP1)
287 {
288 COMP->CSTS_B.LOCK1 = SET;
289 }
290 else
291 {
292 COMP->CSTS_B.LOCK2 = SET;
293 }
294 }
295
296 /**@} end of group COMP_Functions */
297 /**@} end of group COMP_Driver */
298 /**@} end of group APM32F0xx_StdPeriphDriver */
299