1 /*******************************************************************************
2 * \file cy_lpcomp.c
3 * \version 1.70
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 * (c) (2016-2024), Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 *     http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *******************************************************************************/
28 
29 #include "cy_device.h"
30 
31 #if defined (CY_IP_MXLPCOMP) || defined (CY_IP_MXS22LPCOMP) || defined (CY_IP_MXS40LPCOMP)
32 
33 #include "cy_lpcomp.h"
34 
35 #if defined(__cplusplus)
36 extern "C" {
37 #endif
38 
39 #if defined (CY_IP_MXLPCOMP)
40 static cy_stc_lpcomp_context_t cy_lpcomp_context;
41 #endif
42 
43 #if defined (CY_IP_MXS22LPCOMP)
44 #include "cy_systrimm.h"
45 
46 /* Start trimming data for COMP_CH0 */
47 #define TRIMM_START_IDX_COMP_CH0                                                                (90UL)
48 /* Start trimming data for COMP_CH1 */
49 #define TRIMM_START_IDX_COMP_CH1                                                                (91UL)
50 
51 /* Forward declarations */
52 #if (0) /* Until SORT Si */
53 static cy_en_lpcomp_status_t LPComp_LoadTrimmValues(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel);
54 #endif /* Until SORT Si */
55 #endif /* CY_IP_MXS22LPCOMP */
56 
57 /*******************************************************************************
58 * Function Name: Cy_LPComp_Init_Ext
59 ****************************************************************************//**
60 *
61 * Cy_LPComp_Init_Ext() is an extended version of existing function Cy_LPComp_Init().
62 * This implementation follows the thread-safe approach and is preferable for usage.
63 *
64 * This function initializes the low-power comparator and returns
65 * the status of the initialization.
66 *
67 * \note Interrupt edge-detect mode and drive power mode are not written to the registers
68 * during the function execution. This can result in unexpected interrupts
69 * when the comparator is enabled. Instead, the configurations are saved to the
70 * context and applied in the \ref Cy_LPComp_Enable_Ext() function.
71 *
72 * \param *base
73 *     The low-power comparator registers structure pointer.
74 *
75 * \param channel
76 *     The low-power comparator channel index.
77 *
78 * \param *config
79 *     The pointer to the configuration structure for PDL.
80 *
81 * \param *context
82 * The pointer to the context structure \ref cy_stc_lpcomp_context_t allocated by the user.
83 * The structure is used during the low-power comparator operation
84 * for internal configuration and data retention. The user must not modify
85 * anything in this structure.
86 *
87 * \return cy_en_lpcomp_status_t
88 *     *base checking result. If the pointer is NULL, returns an error.
89 *
90 *******************************************************************************/
Cy_LPComp_Init_Ext(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,const cy_stc_lpcomp_config_t * config,cy_stc_lpcomp_context_t * context)91 cy_en_lpcomp_status_t Cy_LPComp_Init_Ext(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel, const cy_stc_lpcomp_config_t *config,
92                                         cy_stc_lpcomp_context_t *context)
93 {
94     cy_en_lpcomp_status_t ret = CY_LPCOMP_BAD_PARAM;
95 
96     if ((base != NULL) && (config != NULL) && (context != NULL))
97     {
98         CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
99         CY_ASSERT_L3(CY_LPCOMP_IS_OUT_MODE_VALID(config->outputMode));
100         CY_ASSERT_L3(CY_LPCOMP_IS_HYSTERESIS_VALID(config->hysteresis));
101         CY_ASSERT_L3(CY_LPCOMP_IS_POWER_VALID(config->power));
102         CY_ASSERT_L3(CY_LPCOMP_IS_INTR_MODE_VALID(config->intType));
103 
104         Cy_LPComp_GlobalEnable(base);
105 
106         if (CY_LPCOMP_CHANNEL_0 == channel)
107         {
108             LPCOMP_CMP0_CTRL(base) = _VAL2FLD(LPCOMP_CMP0_CTRL_HYST0, (uint32_t)config->hysteresis) |
109                               _VAL2FLD(LPCOMP_CMP0_CTRL_DSI_BYPASS0, (uint32_t)config->outputMode) |
110                               _VAL2FLD(LPCOMP_CMP0_CTRL_DSI_LEVEL0, (uint32_t)config->outputMode >> 1u);
111         }
112         else
113         {
114             LPCOMP_CMP1_CTRL(base) = _VAL2FLD(LPCOMP_CMP1_CTRL_HYST1, (uint32_t)config->hysteresis) |
115                               _VAL2FLD(LPCOMP_CMP1_CTRL_DSI_BYPASS1, (uint32_t)config->outputMode) |
116                               _VAL2FLD(LPCOMP_CMP1_CTRL_DSI_LEVEL1, (uint32_t)config->outputMode >> 1u);
117         }
118 
119         /* Save intType to use it in the Cy_LPComp_Enable_Ext() function */
120         context->intType[(uint8_t)channel - 1u] = config->intType;
121 
122         /* Save power to use it in the Cy_LPComp_Enable_Ext() function */
123         context->power[(uint8_t)channel - 1u] = config->power;
124 
125 #if defined (CY_IP_MXS22LPCOMP)
126         /* Read and apply trimming values */
127 #if (0) /* Until SORT Si */
128         ret = LPComp_LoadTrimmValues(base, channel);
129 #endif /* Until SORT Si */
130         ret = CY_LPCOMP_SUCCESS;
131 #else
132         ret = CY_LPCOMP_SUCCESS;
133 #endif /* CY_IP_MXS22LPCOMP */
134 
135     }
136 
137     return (ret);
138 }
139 
140 
141 #if defined (CY_IP_MXLPCOMP)
142 /*******************************************************************************
143 * Function Name: Cy_LPComp_Init
144 ****************************************************************************//**
145 *
146 * Initializes the low-power comparator and returns
147 * the status of the initialization.
148 *
149 * \param *base
150 *     The low-power comparator registers structure pointer.
151 *
152 * \param channel
153 *     The low-power comparator channel index.
154 *
155 * \param *config
156 *     The pointer to the configuration structure for PDL.
157 *
158 * \return cy_en_lpcomp_status_t
159 *     *base checking result. If the pointer is NULL, returns an error.
160 *
161 *******************************************************************************/
Cy_LPComp_Init(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,const cy_stc_lpcomp_config_t * config)162 cy_en_lpcomp_status_t Cy_LPComp_Init(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, const cy_stc_lpcomp_config_t* config)
163 {
164     cy_en_lpcomp_status_t ret = CY_LPCOMP_BAD_PARAM;
165 
166     if ((base != NULL) && (config != NULL))
167     {
168         CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
169         CY_ASSERT_L3(CY_LPCOMP_IS_OUT_MODE_VALID(config->outputMode));
170         CY_ASSERT_L3(CY_LPCOMP_IS_HYSTERESIS_VALID(config->hysteresis));
171         CY_ASSERT_L3(CY_LPCOMP_IS_POWER_VALID(config->power));
172         CY_ASSERT_L3(CY_LPCOMP_IS_INTR_MODE_VALID(config->intType));
173 
174         Cy_LPComp_GlobalEnable(base);
175 
176         if (CY_LPCOMP_CHANNEL_0 == channel)
177         {
178             LPCOMP_CMP0_CTRL(base) = _VAL2FLD(LPCOMP_CMP0_CTRL_HYST0, (uint32_t)config->hysteresis) |
179                               _VAL2FLD(LPCOMP_CMP0_CTRL_DSI_BYPASS0, (uint32_t)config->outputMode) |
180                               _VAL2FLD(LPCOMP_CMP0_CTRL_DSI_LEVEL0, (uint32_t)config->outputMode >> 1u);
181         }
182         else
183         {
184             LPCOMP_CMP1_CTRL(base) = _VAL2FLD(LPCOMP_CMP1_CTRL_HYST1, (uint32_t)config->hysteresis) |
185                               _VAL2FLD(LPCOMP_CMP1_CTRL_DSI_BYPASS1, (uint32_t)config->outputMode) |
186                               _VAL2FLD(LPCOMP_CMP1_CTRL_DSI_LEVEL1, (uint32_t)config->outputMode >> 1u);
187         }
188 
189         /* Save intType to use it in the Cy_LPComp_Enable() function */
190         cy_lpcomp_context.intType[(uint8_t)channel - 1u] = config->intType;
191 
192         /* Save power to use it in the Cy_LPComp_Enable() function */
193         cy_lpcomp_context.power[(uint8_t)channel - 1u] = config->power;
194 
195         ret = CY_LPCOMP_SUCCESS;
196     }
197 
198     return (ret);
199 }
200 #endif
201 
202 
203 /*******************************************************************************
204 * Function Name: Cy_LPComp_Enable_Ext
205 ****************************************************************************//**
206 *
207 * Cy_LPComp_Enable_Ext() is an extended version of existing function Cy_LPComp_Enable().
208 * This implementation follows the thread-safe approach and is preferable for usage.
209 *
210 * This function enables the low-power comparator and sets
211 * interrupt edge-detect and drive power modes.
212 *
213 * \param *base
214 *     The low-power comparator register structure pointer.
215 *
216 * \param channel
217 *     The low-power comparator channel index.
218 *
219 * \param *context
220 * The pointer to the context structure \ref cy_stc_lpcomp_context_t allocated by the user.
221 * The structure is used during the low-power comparator operation
222 * for internal configuration and data retention. The user must not modify
223 * anything in this structure.
224 *
225 * \return None.
226 *
227 *******************************************************************************/
Cy_LPComp_Enable_Ext(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_stc_lpcomp_context_t * context)228 void Cy_LPComp_Enable_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_stc_lpcomp_context_t *context)
229 {
230     cy_en_lpcomp_pwr_t powerSpeed;
231 
232     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
233 
234     powerSpeed = context->power[(uint8_t)channel - 1u];
235 
236     /* Set power */
237     Cy_LPComp_SetPower_Ext(base, channel, powerSpeed, context);
238 
239     /* Make delay before enabling the comparator interrupt to prevent false triggering */
240     if (CY_LPCOMP_MODE_ULP == powerSpeed)
241     {
242         Cy_SysLib_DelayUs(CY_LPCOMP_ULP_POWER_DELAY);
243     }
244     else if (CY_LPCOMP_MODE_LP == powerSpeed)
245     {
246         Cy_SysLib_DelayUs(CY_LPCOMP_LP_POWER_DELAY);
247     }
248     else
249     {
250         Cy_SysLib_DelayUs(CY_LPCOMP_NORMAL_POWER_DELAY);
251     }
252 
253     /* Enable the comparator interrupt */
254     Cy_LPComp_SetInterruptTriggerMode_Ext(base, channel, context->intType[(uint8_t)channel - 1u], context);
255 }
256 
257 
258 #if defined (CY_IP_MXLPCOMP)
259 /*******************************************************************************
260 * Function Name: Cy_LPComp_Enable
261 ****************************************************************************//**
262 *
263 * Enables the low-power comparator and sets Interrupt mode.
264 *
265 * \param *base
266 *     The low-power comparator register structure pointer.
267 *
268 * \param channel
269 *     The low-power comparator channel index.
270 *
271 * \return None.
272 *
273 *******************************************************************************/
Cy_LPComp_Enable(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel)274 void Cy_LPComp_Enable(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel)
275 {
276     cy_en_lpcomp_pwr_t powerSpeed;
277 
278     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
279 
280     powerSpeed = cy_lpcomp_context.power[(uint8_t)channel - 1u];
281 
282     /* Set power */
283     Cy_LPComp_SetPower(base, channel, powerSpeed);
284 
285     /* Make delay before enabling the comparator interrupt to prevent false triggering */
286     if (CY_LPCOMP_MODE_ULP == powerSpeed)
287     {
288         Cy_SysLib_DelayUs(CY_LPCOMP_ULP_POWER_DELAY);
289     }
290     else if (CY_LPCOMP_MODE_LP == powerSpeed)
291     {
292         Cy_SysLib_DelayUs(CY_LPCOMP_LP_POWER_DELAY);
293     }
294     else
295     {
296         Cy_SysLib_DelayUs(CY_LPCOMP_NORMAL_POWER_DELAY);
297     }
298 
299     /* Enable the comparator interrupt */
300     Cy_LPComp_SetInterruptTriggerMode(base, channel, cy_lpcomp_context.intType[(uint8_t)channel - 1u]);
301 }
302 #endif
303 
304 
305 /*******************************************************************************
306 * Function Name: Cy_LPComp_Disable_Ext
307 ****************************************************************************//**
308 *
309 * Cy_LPComp_Disable_Ext() is an extended version of existing function Cy_LPComp_Disable().
310 * This implementation follows the thread-safe approach and is preferable for usage.
311 *
312 * This function disables the low-power comparator power and sets interrupt edge-detect mode
313 * to the disabled state.
314 *
315 * \note This function disables the comparator interrupt to prevent a false trigger
316 * that may happen due to stop working of the comparator unpowered output. \n
317 *
318 * \note Disabled states for comparator interrupt and drive power are not preserved in the context.
319 * The actual configurations for interrupt edge-detect and drive power
320 * are restored from the context in: \ref Cy_LPComp_Enable_Ext().
321 *
322 * \param *base
323 *     The low-power comparator register structure pointer.
324 *
325 * \param channel
326 *     The low-power comparator channel index.
327 *
328 * \param *context
329 * The pointer to the context structure \ref cy_stc_lpcomp_context_t allocated by the user.
330 * The structure is used during the low-power comparator operation
331 * for internal configuration and data retention. The user must not modify
332 * anything in this structure.
333 *
334 * \return None.
335 *
336 *******************************************************************************/
Cy_LPComp_Disable_Ext(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_stc_lpcomp_context_t * context)337 void Cy_LPComp_Disable_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_stc_lpcomp_context_t *context)
338 {
339     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
340 
341     /* Disable the comparator interrupt */
342     Cy_LPComp_SetInterruptTriggerMode_Ext(base, channel, CY_LPCOMP_INTR_DISABLE, context);
343 
344     /* Set power off */
345     Cy_LPComp_SetPower_Ext(base, channel, CY_LPCOMP_MODE_OFF, context);
346 }
347 
348 
349 #if defined (CY_IP_MXLPCOMP)
350 /*******************************************************************************
351 * Function Name: Cy_LPComp_Disable
352 ****************************************************************************//**
353 *
354 * Disables the low-power comparator power and sets Interrupt mode to the disabled state.
355 *
356 * \param *base
357 *     The low-power comparator register structure pointer.
358 *
359 * \param channel
360 *     The low-power comparator channel index.
361 *
362 * \return None.
363 *
364 *******************************************************************************/
Cy_LPComp_Disable(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel)365 void Cy_LPComp_Disable(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel)
366 {
367     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
368 
369     /* Disable the comparator interrupt */
370     Cy_LPComp_SetInterruptTriggerMode(base, channel, CY_LPCOMP_INTR_DISABLE);
371 
372     /* Set power off */
373     Cy_LPComp_SetPower(base, channel, CY_LPCOMP_MODE_OFF);
374 }
375 #endif
376 
377 
378 /*******************************************************************************
379 * Function Name: Cy_LPComp_SetInterruptTriggerMode_Ext
380 ****************************************************************************//**
381 *
382 * Cy_LPComp_SetInterruptTriggerMode_Ext() is an extended version of existing function
383 * Cy_LPComp_SetInterruptTriggerMode().
384 * This implementation follows the thread-safe approach and is preferable for usage.
385 *
386 * This function sets the interrupt edge-detect mode and controls the value
387 * provided on the output.
388 *
389 * \note The interrupt edge-detect mode is preserved in the context,
390 * except state CY_LPCOMP_INTR_DISABLE. \n
391 *
392 * \note  The mode of the interrupt is restored after the block is enabled and appropriate
393 * start-up time has elapsed:
394 * * 3 us is for normal power mode;
395 * * 6 us for LP mode;
396 * * 50 us for ULP mode.
397 *
398 * \param *base
399 *     The low-power comparator register structure pointer.
400 *
401 * \param channel
402 *     The low-power comparator channel index.
403 *
404 * \param intType
405 *   Interrupt edge trigger selection:
406 *   * CY_LPCOMP_INTR_DISABLE (=0) - disabled, no interrupt will be detected;
407 *   * CY_LPCOMP_INTR_RISING  (=1) - the rising edge;
408 *   * CY_LPCOMP_INTR_FALLING (=2) - the falling edge;
409 *   * CY_LPCOMP_INTR_BOTH    (=3) - both rising and falling edges.
410 *
411 * \param *context
412 * The pointer to the context structure \ref cy_stc_lpcomp_context_t allocated by the user.
413 * The structure is used during the low-power comparator operation
414 * for internal configuration and data retention. The user must not modify
415 * anything in this structure.
416 *
417 * \return None.
418 *
419 *******************************************************************************/
Cy_LPComp_SetInterruptTriggerMode_Ext(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_int_t intType,cy_stc_lpcomp_context_t * context)420 void Cy_LPComp_SetInterruptTriggerMode_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_int_t intType,
421                                           cy_stc_lpcomp_context_t *context)
422 {
423     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
424     CY_ASSERT_L3(CY_LPCOMP_IS_INTR_MODE_VALID(intType));
425 
426     if (CY_LPCOMP_CHANNEL_0 == channel)
427     {
428         LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_INTTYPE0, (uint32_t)intType);
429     }
430     else
431     {
432         LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), LPCOMP_CMP1_CTRL_INTTYPE1, (uint32_t)intType);
433     }
434 
435     /* Save interrupt type to use it in the Cy_LPComp_Enable_Ext() function */
436     if (intType != CY_LPCOMP_INTR_DISABLE)
437     {
438         context->intType[(uint8_t)channel - 1u] = intType;
439     }
440 }
441 
442 
443 #if defined (CY_IP_MXLPCOMP)
444 /*******************************************************************************
445 * Function Name: Cy_LPComp_SetInterruptTriggerMode
446 ****************************************************************************//**
447 *
448 * This function sets the interrupt edge-detect mode and controls the value
449 * provided on the output.
450 * \note  Interrupts can be enabled after the block is enabled and appropriate
451 * start-up time has elapsed:
452 * * 3 us is for normal power mode;
453 * * 6 us for LP mode;
454 * * 50 us for ULP mode.
455 *
456 * \param *base
457 *     The low-power comparator register structure pointer.
458 *
459 * \param channel
460 *     The low-power comparator channel index.
461 *
462 * \param intType
463 *   The interrupt edge trigger selection:
464 *   * CY_LPCOMP_INTR_DISABLE (=0) - disabled, no interrupt will be detected;
465 *   * CY_LPCOMP_INTR_RISING  (=1) - the rising edge;
466 *   * CY_LPCOMP_INTR_FALLING (=2) - the falling edge;
467 *   * CY_LPCOMP_INTR_BOTH    (=3) - both rising and falling edges.
468 *
469 * \return None.
470 *
471 *******************************************************************************/
Cy_LPComp_SetInterruptTriggerMode(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_int_t intType)472 void Cy_LPComp_SetInterruptTriggerMode(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_int_t intType)
473 {
474     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
475     CY_ASSERT_L3(CY_LPCOMP_IS_INTR_MODE_VALID(intType));
476 
477     if (CY_LPCOMP_CHANNEL_0 == channel)
478     {
479         LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_INTTYPE0, (uint32_t)intType);
480     }
481     else
482     {
483         LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), LPCOMP_CMP1_CTRL_INTTYPE1, (uint32_t)intType);
484     }
485 
486     /* Save interrupt type to use it in the Cy_LPComp_Enable() function */
487     cy_lpcomp_context.intType[(uint8_t)channel - 1u] = intType;
488 }
489 #endif
490 
491 
492 /*******************************************************************************
493 * Function Name: Cy_LPComp_SetPower_Ext
494 ****************************************************************************//**
495 *
496 * Cy_LPComp_SetPower_Ext() is an extended version of existing function Cy_LPComp_SetPower().
497 * This implementation follows the thread-safe approach and is preferable for usage.
498 *
499 * This function sets the drive power and speeds to one of the four settings.
500 *
501 * \note The drive power mode is preserved in the context,
502 * except state CY_LPCOMP_MODE_OFF. \n
503 *
504 * \note  The mode of the interrupt is restored after the block is enabled and appropriate
505 * start-up time has elapsed:
506 * * 3 us for normal power mode;
507 * * 6 us for LP mode;
508 * * 50 us for ULP mode.
509 * Otherwise, unexpected interrupts can occur.
510 *
511 * \param *base
512 *     The low-power comparator register structure pointer.
513 *
514 * \param channel
515 *     The low-power comparator channel index.
516 *
517 * \param power
518 *     Defines the component power mode:
519 *     * CY_LPCOMP_OFF_POWER   (=0) - off power;
520 *     * CY_LPCOMP_MODE_ULP    (=1) - slow/ultra low power;
521 *     * CY_LPCOMP_MODE_LP     (=2) - medium/low power;
522 *     * CY_LPCOMP_MODE_NORMAL (=3) - fast/normal power.
523 *
524 * \param *context
525 * The pointer to the context structure \ref cy_stc_lpcomp_context_t allocated by the user.
526 * The structure is used during the low-power comparator operation
527 * for internal configuration and data retention. The user must not modify
528 * anything in this structure.
529 *
530 * \return None.
531 *
532 *******************************************************************************/
Cy_LPComp_SetPower_Ext(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_pwr_t power,cy_stc_lpcomp_context_t * context)533 void Cy_LPComp_SetPower_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_pwr_t power,
534                            cy_stc_lpcomp_context_t *context)
535 {
536     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
537     CY_ASSERT_L3(CY_LPCOMP_IS_POWER_VALID(power));
538 
539     if (CY_LPCOMP_CHANNEL_0 == channel)
540     {
541         LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_MODE0, (uint32_t)power);
542     }
543     else
544     {
545         LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), LPCOMP_CMP1_CTRL_MODE1, (uint32_t)power);
546     }
547 
548     /* Save power to use it in the Cy_LPComp_Enable_Ext() function */
549     if (power != CY_LPCOMP_MODE_OFF)
550     {
551         context->power[(uint8_t)channel - 1u] = power;
552     }
553 }
554 
555 
556 #if defined (CY_IP_MXLPCOMP)
557 /*******************************************************************************
558 * Function Name: Cy_LPComp_SetPower
559 ****************************************************************************//**
560 *
561 * Sets the drive power and speeds to one of the four settings.
562 * \note Interrupts can be enabled after the block is enabled and appropriate
563 * start-up time has elapsed:
564 * * 3 us for normal power mode;
565 * * 6 us for LP mode;
566 * * 50 us for ULP mode.
567 * Otherwise, unexpected interrupts can occur.
568 *
569 * \param *base
570 *     The low-power comparator register structure pointer.
571 *
572 * \param channel
573 *     The low-power comparator channel index.
574 *
575 * \param power
576 *     Defines the component power mode:
577 *     * CY_LPCOMP_OFF_POWER   (=0) - off power;
578 *     * CY_LPCOMP_MODE_ULP    (=1) - slow/ultra low power;
579 *     * CY_LPCOMP_MODE_LP     (=2) - medium/low power;
580 *     * CY_LPCOMP_MODE_NORMAL (=3) - fast/normal power.
581 *
582 * \return None.
583 *
584 *******************************************************************************/
Cy_LPComp_SetPower(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_pwr_t power)585 void Cy_LPComp_SetPower(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_pwr_t power)
586 {
587     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
588     CY_ASSERT_L3(CY_LPCOMP_IS_POWER_VALID(power));
589 
590     if (CY_LPCOMP_CHANNEL_0 == channel)
591     {
592         LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_MODE0, (uint32_t)power);
593     }
594     else
595     {
596         LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), LPCOMP_CMP1_CTRL_MODE1, (uint32_t)power);
597     }
598 }
599 #endif
600 
601 
602 /*******************************************************************************
603 * Function Name: Cy_LPComp_SetHysteresis
604 ****************************************************************************//**
605 *
606 * Adds the 30mV hysteresis to the comparator.
607 *
608 * \param *base
609 *     The low-power comparator register structure pointer.
610 *
611 * \param channel
612 *     The low-power comparator channel index.
613 *
614 * \param hysteresis
615 *   Defines the component operation mode:
616 *   * CY_LPCOMP_HYST_ENABLE  (=1) - enables HYST;
617 *   * CY_LPCOMP_HYST_DISABLE (=0) - disables HYST.
618 *
619 * \return None.
620 *
621 *******************************************************************************/
Cy_LPComp_SetHysteresis(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_hyst_t hysteresis)622 void Cy_LPComp_SetHysteresis(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_hyst_t hysteresis)
623 {
624     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
625     CY_ASSERT_L3(CY_LPCOMP_IS_HYSTERESIS_VALID(hysteresis));
626 
627     if (CY_LPCOMP_CHANNEL_0 == channel)
628     {
629         LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), LPCOMP_CMP0_CTRL_HYST0, (uint32_t)hysteresis);
630     }
631     else
632     {
633         LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base) , LPCOMP_CMP1_CTRL_HYST1, (uint32_t)hysteresis);
634     }
635 }
636 
637 
638 /*******************************************************************************
639 * Function Name: Cy_LPComp_SetInputs
640 ****************************************************************************//**
641 *
642 * Sets the comparator input sources. The comparator inputs can be connected
643 * to the dedicated GPIO pins or AMUXBUSA/AMUXBUSB. Additionally, the negative
644 * comparator input can be connected to the local VREF.
645 * Even one unconnected input causes a comparator undefined output.
646 *
647 * \note Connection to AMUXBUSA/AMUXBUSB requires closing the additional
648 * switches, which are part of the IO system. These switches can be configured
649 * using the HSIOM->AMUX_SPLIT_CTL[3] register.
650 * For details, refer to the appropriate Technical Reference Manual (TRM).
651 *
652 * \param *base
653 *     The low-power comparator register structure pointer.
654 *
655 * \param channel
656 *     The low-power comparator channel index.
657 *
658 * \param inputP
659 *   Positive input selection:
660 *   * CY_LPCOMP_SW_GPIO (0x01u);
661 *   * CY_LPCOMP_SW_AMUXBUSA (0x02u) - Hi-Z in Hibernate mode;
662 *   * CY_LPCOMP_SW_AMUXBUSB (0x04u) - Hi-Z in Hibernate mode.
663 * \note: the option AMUXBUS should NOT be selected in case of usage device from CAT1D family.
664 *
665 * \param inputN
666 *   Negative input selection:
667 *   * CY_LPCOMP_SW_GPIO (0x01u);
668 *   * CY_LPCOMP_SW_AMUXBUSA   (0x02u) - Hi-Z in Hibernate mode;
669 *   * CY_LPCOMP_SW_AMUXBUSB   (0x04u) - Hi-Z in Hibernate mode;
670 *   * CY_LPCOMP_SW_LOCAL_VREF (0x08u) - the negative input only for a crude REF.
671 * \note: the option AMUXBUS should NOT be selected in case of usage device from CAT1D family.
672 *
673 * \return None.
674 *
675 *******************************************************************************/
Cy_LPComp_SetInputs(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_inputs_t inputP,cy_en_lpcomp_inputs_t inputN)676 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)
677 {
678     uint32_t input;
679 
680     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
681     CY_ASSERT_L3(CY_LPCOMP_IS_INPUT_P_VALID(inputP));
682     CY_ASSERT_L3(CY_LPCOMP_IS_INPUT_N_VALID(inputN));
683 
684     switch(inputP)
685     {
686         case CY_LPCOMP_SW_AMUXBUSA:
687         {
688             input = (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_AP0_Msk : LPCOMP_CMP1_SW_CMP1_AP1_Msk;
689             HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_SR, 3u);
690             break;
691         }
692         case CY_LPCOMP_SW_AMUXBUSB:
693         {
694             input = (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_BP0_Msk : LPCOMP_CMP1_SW_CMP1_BP1_Msk;
695             HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_SR, 3u);
696             break;
697         }
698         default:
699         {
700             input = (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_IP0_Msk : LPCOMP_CMP1_SW_CMP1_IP1_Msk;
701             break;
702         }
703     }
704 
705     switch(inputN)
706     {
707         case CY_LPCOMP_SW_AMUXBUSA:
708         {
709             input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_AN0_Msk : LPCOMP_CMP1_SW_CMP1_AN1_Msk;
710             HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_SR, 3u);
711             break;
712         }
713         case CY_LPCOMP_SW_AMUXBUSB:
714         {
715             input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_BN0_Msk : LPCOMP_CMP1_SW_CMP1_BN1_Msk;
716             HSIOM_AMUX_SPLIT_CTL(3U) = _CLR_SET_FLD32U(HSIOM_AMUX_SPLIT_CTL(3U), CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_SR, 3u);
717             break;
718         }
719         case CY_LPCOMP_SW_LOCAL_VREF:
720         {
721             input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_VN0_Msk : LPCOMP_CMP1_SW_CMP1_VN1_Msk;
722             break;
723         }
724         default:
725         {
726             input |= (channel == CY_LPCOMP_CHANNEL_0) ? LPCOMP_CMP0_SW_CMP0_IN0_Msk : LPCOMP_CMP1_SW_CMP1_IN1_Msk;
727             break;
728         }
729     }
730 
731     if (CY_LPCOMP_CHANNEL_0 == channel)
732     {
733         LPCOMP_CMP0_SW_CLEAR(base) = CY_LPCOMP_CMP0_SW_POS_Msk | CY_LPCOMP_CMP0_SW_NEG_Msk;
734         LPCOMP_CMP0_SW(base) = input;
735     }
736     else
737     {
738         LPCOMP_CMP1_SW_CLEAR(base) = CY_LPCOMP_CMP1_SW_POS_Msk | CY_LPCOMP_CMP1_SW_NEG_Msk;
739         LPCOMP_CMP1_SW(base) = input;
740     }
741 }
742 
743 
744 /*******************************************************************************
745 * Function Name: Cy_LPComp_SetOutputMode
746 ****************************************************************************//**
747 *
748 * Sets the type of the comparator DSI output.
749 *
750 * \param *base
751 *     The low-power comparator register structure pointer.
752 *
753 * \param channel
754 *     The low-power comparator channel index.
755 *
756 * \param outType
757 *   Interrupt-edge trigger selection:
758 *   * CY_LPCOMP_OUT_PULSE  (=0) - the DSI output with the pulse option, no bypass;
759 *   * CY_LPCOMP_OUT_DIRECT (=1) - Bypass mode, the direct output of the comparator;
760 *   * CY_LPCOMP_OUT_SYNC   (=2) - the DSI output with the level option, it is similar to
761 *   Bypass mode, but it is 1 cycle slower.
762 *   [DSI_LEVELx : DSI_BYPASSx] = [Bit11 : Bit10]
763 *   0 : 0 = 0x00 -> Pulse (PULSE)
764 *   1 : 0 = 0x02 -> Level (SYNC)
765 *   x : 1 = 0x01 -> Bypass (Direct).
766 *
767 * \return None.
768 *
769 *******************************************************************************/
Cy_LPComp_SetOutputMode(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_out_t outType)770 void Cy_LPComp_SetOutputMode(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_out_t outType)
771 {
772     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
773     CY_ASSERT_L3(CY_LPCOMP_IS_OUT_MODE_VALID(outType));
774 
775     if (CY_LPCOMP_CHANNEL_0 == channel)
776     {
777         LPCOMP_CMP0_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_CTRL(base), CY_LPCOMP_CMP0_OUTPUT_CONFIG, (uint32_t)outType);
778     }
779     else
780     {
781         LPCOMP_CMP1_CTRL(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_CTRL(base), CY_LPCOMP_CMP1_OUTPUT_CONFIG, (uint32_t)outType);
782     }
783 }
784 
785 
786 /*******************************************************************************
787 * Function Name: Cy_LPComp_DeepSleepCallback
788 ****************************************************************************//**
789 *
790 * This function checks the current low-power comparator power mode and disables
791 * the low-power comparator block, if there is no wake-up source from
792 * the low-power comparator in Deep-Sleep mode.
793 * This function stores the state of the low-power comparator "Enable", disables
794 * the low-power comparator block before going to low power modes and then
795 * recovers the low-power comparator power "after a wake-up" state using
796 * the stored value.
797 *
798 * \param *callbackParams
799 *     A \ref cy_stc_syspm_callback_params_t structure with callback
800 *     parameters that consists of mode, base and context fields:
801 *    *base - a low-power comparator register structure pointer.
802 *    *context - context for the call-back function.
803 *     mode
804 *     * CY_SYSPM_CHECK_READY - no action for this state.
805 *     * CY_SYSPM_CHECK_FAIL - no action for this state.
806 *     * CY_SYSPM_BEFORE_TRANSITION - checks the low-power comparator interrupt mask
807 *                            and power mode and then disables or enables the
808 *                            low-power comparator block per condition.
809 *                            Stores the low-power comparator state
810 *                            to recover the state after a wake up.
811 *     * CY_SYSPM_AFTER_TRANSITION - enables the low-power comparator block
812 *                            if it was disabled before Sleep mode.
813 *
814 * \param mode
815 * Callback mode, see \ref cy_en_syspm_callback_mode_t
816 *
817 * \return
818 * \ref cy_en_syspm_status_t
819 *
820 *******************************************************************************/
Cy_LPComp_DeepSleepCallback(cy_stc_syspm_callback_params_t * callbackParams,cy_en_syspm_callback_mode_t mode)821 cy_en_syspm_status_t Cy_LPComp_DeepSleepCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode)
822 {
823     cy_en_syspm_status_t ret = CY_SYSPM_FAIL;
824     LPCOMP_Type *locBase = (LPCOMP_Type *) (callbackParams->base);
825     static uint32_t enabled_status;
826 
827     switch(mode)
828     {
829         case CY_SYSPM_CHECK_READY:
830         {
831             ret = CY_SYSPM_SUCCESS;
832         }
833         break;
834 
835         case CY_SYSPM_CHECK_FAIL:
836         {
837             ret = CY_SYSPM_SUCCESS;
838         }
839         break;
840 
841         case CY_SYSPM_BEFORE_TRANSITION:
842         {
843             /* Save the low-power comparator the enabled/disabled status. */
844             enabled_status = _FLD2VAL(LPCOMP_CONFIG_ENABLED, LPCOMP_CONFIG(locBase));
845 
846             if (0u != enabled_status)
847             {
848                 /* Disable the low-power comparator block when there is no wake-up source from any channel. */
849                 if( !(((_FLD2VAL(LPCOMP_CMP0_CTRL_MODE0, LPCOMP_CMP0_CTRL(locBase)) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
850                         _FLD2BOOL(LPCOMP_INTR_MASK_COMP0_MASK, LPCOMP_INTR_MASK(locBase))) ||
851                       ((_FLD2VAL(LPCOMP_CMP1_CTRL_MODE1, LPCOMP_CMP1_CTRL(locBase)) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
852                         _FLD2BOOL(LPCOMP_INTR_MASK_COMP1_MASK, LPCOMP_INTR_MASK(locBase)))))
853 
854                 {
855                     /* Disable the low-power comparator block to avoid leakage. */
856                     Cy_LPComp_GlobalDisable(locBase);
857                 }
858                 else
859                 {
860                     /* Set low-power comparator the status to the not changed state. */
861                     enabled_status = 0u;
862                 }
863             }
864             else
865             {
866                 /* The low-power comparator block was already disabled and
867                 *  the system is allowed to go to the low power mode.
868                 */
869             }
870 
871             ret = CY_SYSPM_SUCCESS;
872         }
873         break;
874 
875         case CY_SYSPM_AFTER_TRANSITION:
876         {
877             /* Enable the low-power comparator to operate if it was enabled
878             * before entering to the low power mode.
879             */
880             if (0u != enabled_status)
881             {
882                 Cy_LPComp_GlobalEnable(locBase);
883             }
884             else
885             {
886                 /* The low-power comparator block was disabled before calling this API
887                 * with mode = CY_SYSPM_CHECK_READY.
888                 */
889             }
890 
891             ret = CY_SYSPM_SUCCESS;
892         }
893         break;
894 
895         default:
896             /* Unknown state */
897             break;
898     }
899 
900     return (ret);
901 }
902 
903 
904 /*******************************************************************************
905 * Function Name: Cy_LPComp_HibernateCallback
906 ****************************************************************************//**
907 *
908 *  This function checks the current low-power comparator power mode and disables
909 * the low-power comparator block, if there is no wake-up source from
910 * the low-power comparator in Hibernate mode.
911 *
912 * \param *callbackParams
913 *     A \ref cy_stc_syspm_callback_params_t structure with callback
914 *     parameters that consists of mode, base and context fields:
915 *    *base - a low-power comparator register structure pointer.
916 *    *context - context for the call-back function.
917 *     mode
918 *     * CY_SYSPM_CHECK_READY - no action for this state.
919 *     * CY_SYSPM_CHECK_FAIL - no action for this state.
920 *     * CY_SYSPM_BEFORE_TRANSITION - checks the wake-up source from Hibernate mode
921 *                            of the low-power comparator block,
922 *                            and then disables or enables the low-power comparator
923 *                            block per condition.
924 *
925 * \param mode
926 * Callback mode, see \ref cy_en_syspm_callback_mode_t
927 *
928 * \return
929 * \ref cy_en_syspm_status_t
930 *
931 *******************************************************************************/
Cy_LPComp_HibernateCallback(cy_stc_syspm_callback_params_t * callbackParams,cy_en_syspm_callback_mode_t mode)932 cy_en_syspm_status_t Cy_LPComp_HibernateCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode)
933 {
934     cy_en_syspm_status_t ret = CY_SYSPM_FAIL;
935     LPCOMP_Type *locBase = (LPCOMP_Type *) (callbackParams->base);
936     static uint32_t enabled_status;
937 
938     switch(mode)
939     {
940         case CY_SYSPM_CHECK_READY:
941         {
942             ret = CY_SYSPM_SUCCESS;
943         }
944         break;
945 
946         case CY_SYSPM_CHECK_FAIL:
947         {
948             ret = CY_SYSPM_SUCCESS;
949         }
950         break;
951 
952         case CY_SYSPM_BEFORE_TRANSITION:
953         {
954             /* Save the low-power comparator the enabled/disabled status. */
955             enabled_status = _FLD2VAL(LPCOMP_CONFIG_ENABLED, LPCOMP_CONFIG(locBase));
956 
957             if (0u != enabled_status)
958             {
959                 /* Disable the low-power comparator block when there is no wake-up source from any channel. */
960                 if(!(((_FLD2VAL(LPCOMP_CMP0_CTRL_MODE0, LPCOMP_CMP0_CTRL(locBase)) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
961                        _FLD2BOOL(CY_LPCOMP_WAKEUP_PIN0, SRSS_PWR_HIBERNATE)) ||
962                      ((_FLD2VAL(LPCOMP_CMP1_CTRL_MODE1, LPCOMP_CMP1_CTRL(locBase)) == (uint32_t)CY_LPCOMP_MODE_ULP) &&
963                        _FLD2BOOL(CY_LPCOMP_WAKEUP_PIN1, SRSS_PWR_HIBERNATE))))
964                 {
965                     /* Disable the low-power comparator block to avoid leakage. */
966                     Cy_LPComp_GlobalDisable(locBase);
967                 }
968                 else
969                 {
970                     /* Set low-power comparator the status to the not changed state. */
971                     enabled_status = 0u;
972                 }
973             }
974             else
975             {
976                 /* The low-power comparator block was already disabled and
977                 *  the system is allowed to go to the low power mode.
978                 */
979             }
980 
981             ret = CY_SYSPM_SUCCESS;
982         }
983         break;
984 
985         default:
986             /* Unknown state */
987             break;
988     }
989 
990     return (ret);
991 }
992 
993 
994 #if defined (CY_IP_MXS22LPCOMP)
995 /*******************************************************************************
996 * Function Name: Cy_LPComp_GetTrim
997 ****************************************************************************//**
998 *
999 * This function returns current trim settings for a particular channel of
1000 * low-power comparator.
1001 *
1002 * \param *base
1003 *     The low-power comparator register structure pointer.
1004 *
1005 * \param channel
1006 *     The low-power comparator channel index.
1007 *
1008 * \param *trim
1009 *     The low-power comparator trim structure pointer, see \ref cy_en_lpcomp_trim_t.
1010 *
1011 * \return None.
1012 *
1013 *******************************************************************************/
Cy_LPComp_GetTrim(LPCOMP_Type const * base,cy_en_lpcomp_channel_t channel,cy_en_lpcomp_trim_t * trim)1014 void Cy_LPComp_GetTrim(LPCOMP_Type const * base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_trim_t * trim)
1015 {
1016     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
1017 
1018     if (CY_LPCOMP_CHANNEL_0 == channel)
1019     {
1020         trim->enable = _FLD2BOOL(LPCOMP_CMP0_OFFSET_TRIM_CMP0_EN, LPCOMP_CMP0_OFFSET_TRIM(base));
1021         CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 10.8', 2, 'Intentional typecast of uint32_t to enum')
1022         trim->polarity = (cy_en_lpcomp_trim_polarity_t)_FLD2VAL(LPCOMP_CMP0_OFFSET_TRIM_CMP0_POLARITY, LPCOMP_CMP0_OFFSET_TRIM(base));
1023         trim->magnitude = (cy_en_lpcomp_trim_magnitude_t)_FLD2VAL(LPCOMP_CMP0_OFFSET_TRIM_CMP0_MAGNITUDE, LPCOMP_CMP0_OFFSET_TRIM(base));
1024         CY_MISRA_BLOCK_END('MISRA C-2012 Rule 10.8')
1025     }
1026     else
1027     {
1028         trim->enable = _FLD2BOOL(LPCOMP_CMP1_OFFSET_TRIM_CMP1_EN, LPCOMP_CMP1_OFFSET_TRIM(base));
1029         CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 10.8', 2, 'Intentional typecast of uint32_t to enum')
1030         trim->polarity = (cy_en_lpcomp_trim_polarity_t)_FLD2VAL(LPCOMP_CMP1_OFFSET_TRIM_CMP1_POLARITY, LPCOMP_CMP1_OFFSET_TRIM(base));
1031         trim->magnitude = (cy_en_lpcomp_trim_magnitude_t)_FLD2VAL(LPCOMP_CMP1_OFFSET_TRIM_CMP1_MAGNITUDE, LPCOMP_CMP1_OFFSET_TRIM(base));
1032         CY_MISRA_BLOCK_END('MISRA C-2012 Rule 10.8')
1033     }
1034 }
1035 
1036 
1037 /*******************************************************************************
1038 * Function Name: Cy_LPComp_SetTrim
1039 ****************************************************************************//**
1040 *
1041 * This function applies trim settings to the particular channel of
1042 * low-power comparator.
1043 *
1044 * \param *base
1045 *     The low-power comparator register structure pointer.
1046 *
1047 * \param channel
1048 *     The low-power comparator channel index.
1049 *
1050 * \param *trim
1051 *     The low-power comparator trim structure pointer, see \ref cy_en_lpcomp_trim_t.
1052 *
1053 * \return None.
1054 *
1055 *******************************************************************************/
Cy_LPComp_SetTrim(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel,const cy_en_lpcomp_trim_t * trim)1056 void Cy_LPComp_SetTrim(LPCOMP_Type * base, cy_en_lpcomp_channel_t channel, const cy_en_lpcomp_trim_t * trim)
1057 {
1058     CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
1059     CY_ASSERT_L3(CY_LPCOMP_IS_TRIM_VALID(trim));
1060 
1061     if (CY_LPCOMP_CHANNEL_0 == channel)
1062     {
1063         LPCOMP_CMP0_OFFSET_TRIM(base) = _BOOL2FLD(LPCOMP_CMP0_OFFSET_TRIM_CMP0_EN,        trim->enable) |
1064                                          _VAL2FLD(LPCOMP_CMP0_OFFSET_TRIM_CMP0_POLARITY,  trim->polarity) |
1065                                          _VAL2FLD(LPCOMP_CMP0_OFFSET_TRIM_CMP0_MAGNITUDE, trim->magnitude);
1066     }
1067     else
1068     {
1069         LPCOMP_CMP1_OFFSET_TRIM(base) = _BOOL2FLD(LPCOMP_CMP1_OFFSET_TRIM_CMP1_EN,        trim->enable) |
1070                                          _VAL2FLD(LPCOMP_CMP1_OFFSET_TRIM_CMP1_POLARITY,  trim->polarity) |
1071                                          _VAL2FLD(LPCOMP_CMP1_OFFSET_TRIM_CMP1_MAGNITUDE, trim->magnitude);
1072     }
1073 }
1074 
1075 
1076 #if (0) /* Until SORT Si */
1077 /*******************************************************************************
1078 * Function Name: LPComp_LoadTrimmValues
1079 ****************************************************************************//**
1080 *
1081 * Used to load trimming values from RRAM into MMIO for further use at runtime.
1082 *
1083 * \param *base
1084 *     The low-power comparator registers structure pointer.
1085 *
1086 * \param channel
1087 *     The low-power comparator channel index.
1088 *
1089 * \return
1090 * Status of operation, \ref cy_en_lpcomp_status_t.
1091 *
1092 *******************************************************************************/
LPComp_LoadTrimmValues(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel)1093 static cy_en_lpcomp_status_t LPComp_LoadTrimmValues(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel)
1094 {
1095     cy_en_lpcomp_status_t ret = CY_LPCOMP_TRIMM_ERR;
1096 
1097     uint32_t trimmData[TRIMM_SECTION_SIZE_BYTES / sizeof(int)];
1098 
1099     /* Read array of trimming constants */
1100     if (Cy_Trimm_ReadRRAMdata(TRIMM_SECTION_SIZE_BYTES, trimmData))
1101     {
1102         ret = CY_LPCOMP_SUCCESS;
1103 
1104         if (CY_LPCOMP_CHANNEL_0 == channel)
1105         {
1106             LPCOMP_CMP0_OFFSET_TRIM(base) = trimmData[TRIMM_START_IDX_COMP_CH0];
1107         }
1108         else
1109         {
1110             LPCOMP_CMP1_OFFSET_TRIM(base) = trimmData[TRIMM_START_IDX_COMP_CH1];
1111         }
1112     }
1113 
1114     return ret;
1115 }
1116 #endif /* Until SORT Si */
1117 
1118 #endif /* CY_IP_MXS22LPCOMP */
1119 
1120 #if defined(__cplusplus)
1121 }
1122 #endif
1123 
1124 #endif /* CY_IP_MXLPCOMP, CY_IP_MXS22LPCOMP, and CY_IP_MXS40LPCOMP */
1125 
1126 /* [] END OF FILE */
1127