1 /*******************************************************************************
2 * \file cy_lpcomp.c
3 * \version 1.30
4 *
5 * \brief
6 * This file provides the driver code to the API for the Low Power Comparator
7 * component.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright 2016-2020 Cypress Semiconductor Corporation
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *******************************************************************************/
26
27 #include "cy_device.h"
28
29 #if defined (CY_IP_MXLPCOMP)
30
31 #include "cy_lpcomp.h"
32
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36
37 static cy_stc_lpcomp_context_t cy_lpcomp_context;
38
39 /*******************************************************************************
40 * Function Name: Cy_LPComp_Init
41 ****************************************************************************//**
42 *
43 * Initializes LPCOMP and returns the LPCOMP register address.
44 *
45 * \param *base
46 * LPCOMP registers structure pointer.
47 *
48 * \param *config
49 * The pointer to the configuration structure for PDL.
50 *
51 * \param channel
52 * The LPCOMP channel index.
53 *
54 * \return cy_en_lpcomp_status_t
55 * *base checking result. If the pointer is NULL, returns error.
56 *
57 *******************************************************************************/
Cy_LPComp_Init(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,const cy_stc_lpcomp_config_t * config)58 cy_en_lpcomp_status_t Cy_LPComp_Init(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, const cy_stc_lpcomp_config_t* config)
59 {
60 cy_en_lpcomp_status_t ret = CY_LPCOMP_BAD_PARAM;
61
62 if ((base != NULL) && (config != NULL))
63 {
64 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
65 CY_ASSERT_L3(CY_LPCOMP_IS_OUT_MODE_VALID(config->outputMode));
66 CY_ASSERT_L3(CY_LPCOMP_IS_HYSTERESIS_VALID(config->hysteresis));
67 CY_ASSERT_L3(CY_LPCOMP_IS_POWER_VALID(config->power));
68 CY_ASSERT_L3(CY_LPCOMP_IS_INTR_MODE_VALID(config->intType));
69
70 Cy_LPComp_GlobalEnable(base);
71
72 if (CY_LPCOMP_CHANNEL_0 == channel)
73 {
74 LPCOMP_CMP0_CTRL(base) = _VAL2FLD(LPCOMP_CMP0_CTRL_HYST0, (uint32_t)config->hysteresis) |
75 _VAL2FLD(LPCOMP_CMP0_CTRL_DSI_BYPASS0, (uint32_t)config->outputMode) |
76 _VAL2FLD(LPCOMP_CMP0_CTRL_DSI_LEVEL0, (uint32_t)config->outputMode >> 1u);
77 }
78 else
79 {
80 LPCOMP_CMP1_CTRL(base) = _VAL2FLD(LPCOMP_CMP1_CTRL_HYST1, (uint32_t)config->hysteresis) |
81 _VAL2FLD(LPCOMP_CMP1_CTRL_DSI_BYPASS1, (uint32_t)config->outputMode) |
82 _VAL2FLD(LPCOMP_CMP1_CTRL_DSI_LEVEL1, (uint32_t)config->outputMode >> 1u);
83 }
84
85 /* Save intType to use it in the Cy_LPComp_Enable() function */
86 cy_lpcomp_context.intType[(uint8_t)channel - 1u] = config->intType;
87
88 /* Save power to use it in the Cy_LPComp_Enable() function */
89 cy_lpcomp_context.power[(uint8_t)channel - 1u] = config->power;
90
91 ret = CY_LPCOMP_SUCCESS;
92 }
93
94 return (ret);
95 }
96
97
98 /*******************************************************************************
99 * Function Name: Cy_LPComp_Enable
100 ****************************************************************************//**
101 *
102 * Enables the LPCOMP and sets the LPCOMP interrupt mode.
103 *
104 * \param *base
105 * The LPCOMP register structure pointer.
106 *
107 * \param channel
108 * The LPCOMP channel index.
109 *
110 * \return None
111 *
112 *******************************************************************************/
Cy_LPComp_Enable(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel)113 void Cy_LPComp_Enable(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel)
114 {
115 cy_en_lpcomp_pwr_t powerSpeed;
116
117 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
118
119 powerSpeed = cy_lpcomp_context.power[(uint8_t)channel - 1u];
120
121 /* Set power */
122 Cy_LPComp_SetPower(base, channel, powerSpeed);
123
124 /* Make delay before enabling the comparator interrupt to prevent false triggering */
125 if (CY_LPCOMP_MODE_ULP == powerSpeed)
126 {
127 Cy_SysLib_DelayUs(CY_LPCOMP_ULP_POWER_DELAY);
128 }
129 else if (CY_LPCOMP_MODE_LP == powerSpeed)
130 {
131 Cy_SysLib_DelayUs(CY_LPCOMP_LP_POWER_DELAY);
132 }
133 else
134 {
135 Cy_SysLib_DelayUs(CY_LPCOMP_NORMAL_POWER_DELAY);
136 }
137
138 /* Enable the comparator interrupt */
139 Cy_LPComp_SetInterruptTriggerMode(base, channel, cy_lpcomp_context.intType[(uint8_t)channel - 1u]);
140 }
141
142
143 /*******************************************************************************
144 * Function Name: Cy_LPComp_Disable
145 ****************************************************************************//**
146 *
147 * Disables the LPCOMP power and sets the interrupt mode to disabled.
148 *
149 * \param *base
150 * The LPCOMP register structure pointer.
151 *
152 * \param channel
153 * The LPCOMP channel index.
154 *
155 * \return None
156 *
157 *******************************************************************************/
Cy_LPComp_Disable(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel)158 void Cy_LPComp_Disable(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel)
159 {
160 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
161
162 /* Disable the comparator interrupt */
163 Cy_LPComp_SetInterruptTriggerMode(base, channel, CY_LPCOMP_INTR_DISABLE);
164
165 /* Set power off */
166 Cy_LPComp_SetPower(base, channel, CY_LPCOMP_MODE_OFF);
167 }
168
169
170 /*******************************************************************************
171 * Function Name: Cy_LPComp_SetInterruptTriggerMode
172 ****************************************************************************//**
173 *
174 * Sets the interrupt edge-detect mode.
175 * This also controls the value provided on the output.
176 * \note Interrupts can be enabled after the block is enabled and the appropriate
177 * start-up time has elapsed:
178 * 3 us for the normal power mode;
179 * 6 us for the LP mode;
180 * 50 us for the ULP mode.
181 *
182 * \param *base
183 * The LPCOMP register structure pointer.
184 *
185 * \param channel
186 * The LPCOMP channel index.
187 *
188 * \param intType
189 * Interrupt edge trigger selection
190 * CY_LPCOMP_INTR_DISABLE (=0) - Disabled, no interrupt will be detected
191 * CY_LPCOMP_INTR_RISING (=1) - Rising edge
192 * CY_LPCOMP_INTR_FALLING (=2) - Falling edge
193 * CY_LPCOMP_INTR_BOTH (=3) - Both rising and falling edges.
194 *
195 * \return None
196 *
197 *******************************************************************************/
Cy_LPComp_SetInterruptTriggerMode(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_int_t intType)198 void Cy_LPComp_SetInterruptTriggerMode(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_int_t intType)
199 {
200 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
201 CY_ASSERT_L3(CY_LPCOMP_IS_INTR_MODE_VALID(intType));
202
203 if (CY_LPCOMP_CHANNEL_0 == channel)
204 {
205 LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_INTTYPE0, (uint32_t)intType);
206 }
207 else
208 {
209 LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), LPCOMP_CMP1_CTRL_INTTYPE1, (uint32_t)intType);
210 }
211
212 /* Save interrupt type to use it in the Cy_LPComp_Enable() function */
213 cy_lpcomp_context.intType[(uint8_t)channel - 1u] = intType;
214 }
215
216
217 /*******************************************************************************
218 * Function Name: Cy_LPComp_SetPower
219 ****************************************************************************//**
220 *
221 * Sets the drive power and speeds to one of the four settings.
222 * \note Interrupts can be enabled after the block is enabled and the appropriate
223 * start-up time has elapsed:
224 * 3 us for the normal power mode;
225 * 6 us for the LP mode;
226 * 50 us for the ULP mode.
227 * Otherwise, unexpected interrupts events can occur.
228 *
229 * \param *base
230 * The LPCOMP register structure pointer.
231 *
232 * \param channel
233 * The LPCOMP channel index.
234 *
235 * \param power
236 * The power setting sets an operation mode of the component:
237 * CY_LPCOMP_OFF_POWER (=0) - Off power
238 * CY_LPCOMP_MODE_ULP (=1) - Slow/ultra low power
239 * CY_LPCOMP_MODE_LP (=2) - Medium/low power
240 * CY_LPCOMP_MODE_NORMAL(=3) - Fast/normal power
241 *
242 * \return None
243 *
244 *******************************************************************************/
Cy_LPComp_SetPower(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_pwr_t power)245 void Cy_LPComp_SetPower(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_pwr_t power)
246 {
247 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
248 CY_ASSERT_L3(CY_LPCOMP_IS_POWER_VALID(power));
249
250 if (CY_LPCOMP_CHANNEL_0 == channel)
251 {
252 LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_MODE0, (uint32_t)power);
253 }
254 else
255 {
256 LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), LPCOMP_CMP1_CTRL_MODE1, (uint32_t)power);
257 }
258 }
259
260
261 /*******************************************************************************
262 * Function Name: Cy_LPComp_SetHysteresis
263 ****************************************************************************//**
264 *
265 * Adds the 30mV hysteresis to the comparator.
266 *
267 * \param *base
268 * The LPCOMP register structure pointer.
269 *
270 * \param channel
271 * The LPCOMP channel index.
272 *
273 * \param hysteresis
274 * Sets an operation mode of the component
275 * CY_LPCOMP_HYST_ENABLE (=1) - Enables HYST
276 * CY_LPCOMP_HYST_DISABLE(=0) - Disable HYST.
277 *
278 * \return None
279 *
280 *******************************************************************************/
Cy_LPComp_SetHysteresis(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_hyst_t hysteresis)281 void Cy_LPComp_SetHysteresis(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_hyst_t hysteresis)
282 {
283 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
284 CY_ASSERT_L3(CY_LPCOMP_IS_HYSTERESIS_VALID(hysteresis));
285
286 if (CY_LPCOMP_CHANNEL_0 == channel)
287 {
288 LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_HYST0, (uint32_t)hysteresis);
289 }
290 else
291 {
292 LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base) , LPCOMP_CMP1_CTRL_HYST1, (uint32_t)hysteresis);
293 }
294 }
295
296
297 /*******************************************************************************
298 * Function Name: Cy_LPComp_SetInputs
299 ****************************************************************************//**
300 *
301 * Sets the comparator input sources. The comparator inputs can be connected
302 * to the dedicated GPIO pins or AMUXBUSA/AMUXBUSB. Additionally, the negative
303 * comparator input can be connected to the local VREF.
304 * At least one unconnected input causes a comparator undefined output.
305 *
306 * \note Connection to AMUXBUSA/AMUXBUSB requires closing the additional
307 * switches which are a part of the IO system. These switches can be configured
308 * using the HSIOM->AMUX_SPLIT_CTL[3] register.
309 * Refer to the appropriate Technical Reference Manual (TRM) of a device
310 * for a detailed description.
311 *
312 * \param *base
313 * The LPCOMP register structure pointer.
314 *
315 * \param channel
316 * The LPCOMP channel index.
317 *
318 * \param inputP
319 * Positive input selection
320 * CY_LPCOMP_SW_GPIO (0x01u)
321 * CY_LPCOMP_SW_AMUXBUSA (0x02u) - Hi-Z in hibernate mode
322 * CY_LPCOMP_SW_AMUXBUSB (0x04u) - Hi-Z in the hibernate mode.
323 *
324 * \param inputN
325 * Negative input selection
326 * CY_LPCOMP_SW_GPIO (0x01u)
327 * CY_LPCOMP_SW_AMUXBUSA (0x02u) - Hi-Z in hibernate mode
328 * CY_LPCOMP_SW_AMUXBUSB (0x04u) - Hi-Z in hibernate mode
329 * CY_LPCOMP_SW_LOCAL_VREF (0x08u) - the negative input only for a crude REF.
330 *
331 * \return None
332 *
333 *******************************************************************************/
Cy_LPComp_SetInputs(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_inputs_t inputP,cy_en_lpcomp_inputs_t inputN)334 void Cy_LPComp_SetInputs(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_inputs_t inputP, cy_en_lpcomp_inputs_t inputN)
335 {
336 uint32_t input;
337
338 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
339 CY_ASSERT_L3(CY_LPCOMP_IS_INPUT_P_VALID(inputP));
340 CY_ASSERT_L3(CY_LPCOMP_IS_INPUT_N_VALID(inputN));
341
342 switch(inputP)
343 {
344 case CY_LPCOMP_SW_AMUXBUSA:
345 {
346 input = (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_AP0_Msk : LPCOMP_CMP1_SW_CMP1_AP1_Msk;
347 HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_SR, 3u);
348 break;
349 }
350 case CY_LPCOMP_SW_AMUXBUSB:
351 {
352 input = (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_BP0_Msk : LPCOMP_CMP1_SW_CMP1_BP1_Msk;
353 HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_SR, 3u);
354 break;
355 }
356 default:
357 {
358 input = (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_IP0_Msk : LPCOMP_CMP1_SW_CMP1_IP1_Msk;
359 break;
360 }
361 }
362
363 switch(inputN)
364 {
365 case CY_LPCOMP_SW_AMUXBUSA:
366 {
367 input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_AN0_Msk : LPCOMP_CMP1_SW_CMP1_AN1_Msk;
368 HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_SR, 3u);
369 break;
370 }
371 case CY_LPCOMP_SW_AMUXBUSB:
372 {
373 input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_BN0_Msk : LPCOMP_CMP1_SW_CMP1_BN1_Msk;
374 HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_SR, 3u);
375 break;
376 }
377 case CY_LPCOMP_SW_LOCAL_VREF:
378 {
379 input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_VN0_Msk : LPCOMP_CMP1_SW_CMP1_VN1_Msk;
380 break;
381 }
382 default:
383 {
384 input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_IN0_Msk : LPCOMP_CMP1_SW_CMP1_IN1_Msk;
385 break;
386 }
387 }
388
389 if (CY_LPCOMP_CHANNEL_0 == channel)
390 {
391 LPCOMP_CMP0_SW_CLEAR(base) = CY_LPCOMP_CMP0_SW_POS_Msk | CY_LPCOMP_CMP0_SW_NEG_Msk;
392 LPCOMP_CMP0_SW(base) = input;
393 }
394 else
395 {
396 LPCOMP_CMP1_SW_CLEAR(base) = CY_LPCOMP_CMP1_SW_POS_Msk | CY_LPCOMP_CMP1_SW_NEG_Msk;
397 LPCOMP_CMP1_SW(base) = input;
398 }
399 }
400
401
402 /*******************************************************************************
403 * Function Name: Cy_LPComp_SetOutputMode
404 ****************************************************************************//**
405 *
406 * Sets the type of the comparator DSI output.
407 *
408 * \param *base
409 * The LPCOMP register structure pointer.
410 *
411 * \param channel
412 * The LPCOMP channel index.
413 *
414 * \param outType
415 * Interrupt edge trigger selection
416 * CY_LPCOMP_OUT_PULSE (=0) - the DSI output with the pulse option, no bypass
417 * CY_LPCOMP_OUT_DIRECT (=1) - the bypass mode, the direct output of the comparator
418 * CY_LPCOMP_OUT_SYNC (=2) - DSI output with the level option, it is similar to the
419 * bypass mode but it is 1 cycle slow than the bypass.
420 * [DSI_LEVELx : DSI_BYPASSx] = [Bit11 : Bit10]
421 * 0 : 0 = 0x00 -> Pulse (PULSE)
422 * 1 : 0 = 0x02 -> Level (SYNC)
423 * x : 1 = 0x01 -> Bypass (Direct).
424 *
425 * \return None
426 *
427 *******************************************************************************/
Cy_LPComp_SetOutputMode(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_out_t outType)428 void Cy_LPComp_SetOutputMode(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_out_t outType)
429 {
430 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
431 CY_ASSERT_L3(CY_LPCOMP_IS_OUT_MODE_VALID(outType));
432
433 if (CY_LPCOMP_CHANNEL_0 == channel)
434 {
435 LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), CY_LPCOMP_CMP0_OUTPUT_CONFIG, (uint32_t)outType);
436 }
437 else
438 {
439 LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), CY_LPCOMP_CMP1_OUTPUT_CONFIG, (uint32_t)outType);
440 }
441 }
442
443
444 /*******************************************************************************
445 * Function Name: Cy_LPComp_DeepSleepCallback
446 ****************************************************************************//**
447 *
448 * This function checks the current power mode of LPComp and then disables the
449 * LPComp block if there is no wake-up source from LPComp in the deep-sleep mode.
450 * It stores the state of the LPComp enable and then disables the LPComp block
451 * before going to the low power modes, and recovers the LPComp power state after
452 * wake-up using the stored value.
453 *
454 * \param *callbackParams
455 * The \ref cy_stc_syspm_callback_params_t structure with the callback
456 * parameters which consists of mode, base and context fields:
457 * *base - LPComp register structure pointer;
458 * *context - Context for the call-back function;
459 * mode
460 * CY_SYSPM_CHECK_READY - No action for this state.
461 * CY_SYSPM_CHECK_FAIL - No action for this state.
462 * CY_SYSPM_BEFORE_TRANSITION - Checks the LPComp interrupt mask and the power
463 * mode, and then disables or enables the LPComp block
464 * according to the condition.
465 * Stores the LPComp state to recover the state after
466 * wake up.
467 * CY_SYSPM_AFTER_TRANSITION - Enables the LPComp block, if it was disabled
468 * before the sleep mode.
469 *
470 * \param mode
471 * Callback mode, see \ref cy_en_syspm_callback_mode_t
472 *
473 * \return
474 * \ref cy_en_syspm_status_t
475 *
476 *******************************************************************************/
Cy_LPComp_DeepSleepCallback(cy_stc_syspm_callback_params_t * callbackParams,cy_en_syspm_callback_mode_t mode)477 cy_en_syspm_status_t Cy_LPComp_DeepSleepCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode)
478 {
479 cy_en_syspm_status_t ret = CY_SYSPM_FAIL;
480 LPCOMP_Type *locBase = (LPCOMP_Type *) (callbackParams->base);
481 static uint32_t enabled_status;
482
483 switch(mode)
484 {
485 case CY_SYSPM_CHECK_READY:
486 {
487 ret = CY_SYSPM_SUCCESS;
488 }
489 break;
490
491 case CY_SYSPM_CHECK_FAIL:
492 {
493 ret = CY_SYSPM_SUCCESS;
494 }
495 break;
496
497 case CY_SYSPM_BEFORE_TRANSITION:
498 {
499 /* Save the LPComp the enabled/disabled status. */
500 enabled_status = _FLD2VAL(LPCOMP_CONFIG_ENABLED, LPCOMP_CONFIG(locBase));
501
502 if (0u != enabled_status)
503 {
504 /* Disable the LPComp block when there is no wake-up source from any channel. */
505 if( !(((_FLD2VAL(LPCOMP_CMP0_CTRL_MODE0, LPCOMP_CMP0_CTRL(locBase)) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
506 _FLD2BOOL(LPCOMP_INTR_MASK_COMP0_MASK, LPCOMP_INTR_MASK(locBase))) ||
507 ((_FLD2VAL(LPCOMP_CMP1_CTRL_MODE1, LPCOMP_CMP1_CTRL(locBase)) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
508 _FLD2BOOL(LPCOMP_INTR_MASK_COMP1_MASK, LPCOMP_INTR_MASK(locBase)))))
509
510 {
511 /* Disable the LPComp block to avoid leakage. */
512 Cy_LPComp_GlobalDisable(locBase);
513 }
514 else
515 {
516 /* Set LPComp the status to the not changed state. */
517 enabled_status = 0u;
518 }
519 }
520 else
521 {
522 /* The LPComp block was already disabled and
523 * the system is allowed to go to the low power mode.
524 */
525 }
526
527 ret = CY_SYSPM_SUCCESS;
528 }
529 break;
530
531 case CY_SYSPM_AFTER_TRANSITION:
532 {
533 /* Enable LPComp to operate if it was enabled
534 * before entering to the low power mode.
535 */
536 if (0u != enabled_status)
537 {
538 Cy_LPComp_GlobalEnable(locBase);
539 }
540 else
541 {
542 /* The LPComp block was disabled before calling this API
543 * with mode = CY_SYSPM_CHECK_READY.
544 */
545 }
546
547 ret = CY_SYSPM_SUCCESS;
548 }
549 break;
550
551 default:
552 /* Unknown state */
553 break;
554 }
555
556 return (ret);
557 }
558
559
560 /*******************************************************************************
561 * Function Name: Cy_LPComp_HibernateCallback
562 ****************************************************************************//**
563 *
564 * This function checks the current power mode of LPComp and then disable the
565 * LPComp block, if there is no wake-up source from LPComp in the hibernate mode.
566 *
567 * \param *callbackParams
568 * The \ref cy_stc_syspm_callback_params_t structure with the callback
569 * parameters which consists of mode, base and context fields:
570 * *base - LPComp register structure pointer;
571 * *context - Context for the call-back function;
572 * mode
573 * CY_SYSPM_CHECK_READY - No action for this state.
574 * CY_SYSPM_CHECK_FAIL - No action for this state.
575 * CY_SYSPM_BEFORE_TRANSITION - Checks the wake-up source from the hibernate mode
576 * of the LPComp block, and then disables or enables
577 * the LPComp block according to the condition.
578 *
579 * \param mode
580 * Callback mode, see \ref cy_en_syspm_callback_mode_t
581 *
582 * \return
583 * \ref cy_en_syspm_status_t
584 *
585 *******************************************************************************/
Cy_LPComp_HibernateCallback(cy_stc_syspm_callback_params_t * callbackParams,cy_en_syspm_callback_mode_t mode)586 cy_en_syspm_status_t Cy_LPComp_HibernateCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode)
587 {
588 cy_en_syspm_status_t ret = CY_SYSPM_FAIL;
589 LPCOMP_Type *locBase = (LPCOMP_Type *) (callbackParams->base);
590 static uint32_t enabled_status;
591
592 switch(mode)
593 {
594 case CY_SYSPM_CHECK_READY:
595 {
596 ret = CY_SYSPM_SUCCESS;
597 }
598 break;
599
600 case CY_SYSPM_CHECK_FAIL:
601 {
602 ret = CY_SYSPM_SUCCESS;
603 }
604 break;
605
606 case CY_SYSPM_BEFORE_TRANSITION:
607 {
608 /* Save the LPComp the enabled/disabled status. */
609 enabled_status = _FLD2VAL(LPCOMP_CONFIG_ENABLED, LPCOMP_CONFIG(locBase));
610
611 if (0u != enabled_status)
612 {
613 /* Disable the LPComp block when there is no wake-up source from any channel. */
614 if( (!(((_FLD2VAL(LPCOMP_CMP0_CTRL_MODE0, LPCOMP_CMP0_CTRL(locBase))) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
615 _FLD2BOOL(CY_LPCOMP_WAKEUP_PIN0, SRSS_PWR_HIBERNATE))) ||
616 ((_FLD2VAL(LPCOMP_CMP1_CTRL_MODE1, LPCOMP_CMP1_CTRL(locBase)) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
617 _FLD2BOOL(CY_LPCOMP_WAKEUP_PIN1, SRSS_PWR_HIBERNATE)))
618
619 {
620 /* Disable the LPComp block to avoid leakage. */
621 Cy_LPComp_GlobalDisable(locBase);
622 }
623 else
624 {
625 /* Set LPComp the status to the not changed state. */
626 enabled_status = 0u;
627 }
628 }
629 else
630 {
631 /* The LPComp block was already disabled and
632 * the system is allowed to go to the low power mode.
633 */
634 }
635
636 ret = CY_SYSPM_SUCCESS;
637 }
638 break;
639
640 default:
641 /* Unknown state */
642 break;
643 }
644
645 return (ret);
646 }
647
648 #if defined(__cplusplus)
649 }
650 #endif
651
652 #endif /* CY_IP_MXLPCOMP */
653
654 /* [] END OF FILE */
655