1 /***************************************************************************//**
2 * \file cy_ctdac.c
3 * \version 2.0.2
4 *
5 * Provides the public functions for the API for the CTDAC driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2017-2020 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 *     http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *******************************************************************************/
24 
25 #include "cy_ctdac.h"
26 
27 #ifdef CY_IP_MXS40PASS_CTDAC
28 
29 #if defined(__cplusplus)
30 extern "C" {
31 #endif
32 CY_MISRA_DEVIATE_BLOCK_START('MISRA C-2012 Rule 11.3', 32, \
33 'CTDAC_Type will typecast to either CTDAC_V1_Type or CTDAC_V2_Type but not both on PDL initialization based on the target device at compile time.')
34 
35 /** Static function to configure the clock */
36 static void Cy_CTDAC_ConfigureClock(cy_en_ctdac_update_t updateMode, cy_en_divider_types_t dividerType,
37                                            uint32_t dividerNum, uint32_t dividerIntValue, uint32_t dividerFracValue);
38 
39 const cy_stc_ctdac_fast_config_t Cy_CTDAC_Fast_VddaRef_UnbufferedOut =
40 {
41     /*.refSource      */ CY_CTDAC_REFSOURCE_VDDA,
42     /*.outputBuffer   */ CY_CTDAC_OUTPUT_UNBUFFERED,
43 };
44 
45 const cy_stc_ctdac_fast_config_t Cy_CTDAC_Fast_VddaRef_BufferedOut =
46 {
47     /*.refSource      */ CY_CTDAC_REFSOURCE_VDDA,
48     /*.outputBuffer   */ CY_CTDAC_OUTPUT_BUFFERED,
49 };
50 
51 const cy_stc_ctdac_fast_config_t Cy_CTDAC_Fast_OA1Ref_UnbufferedOut =
52 {
53     /*.refSource      */ CY_CTDAC_REFSOURCE_EXTERNAL,
54     /*.outputBuffer   */ CY_CTDAC_OUTPUT_UNBUFFERED,
55 };
56 
57 const cy_stc_ctdac_fast_config_t Cy_CTDAC_Fast_OA1Ref_BufferedOut =
58 {
59     /*.refSource      */ CY_CTDAC_REFSOURCE_EXTERNAL,
60     /*.outputBuffer   */ CY_CTDAC_OUTPUT_BUFFERED,
61 };
62 
63 /*******************************************************************************
64 * Function Name: Cy_CTDAC_Init
65 ****************************************************************************//**
66 *
67 * Initialize all CTDAC configuration registers
68 *
69 * \param base
70 * Pointer to structure describing registers
71 *
72 * \param config
73 * Pointer to structure containing configuration data
74 *
75 * \return
76 * Status of initialization, \ref CY_CTDAC_SUCCESS or \ref CY_CTDAC_BAD_PARAM
77 *
78 * \funcusage
79 *
80 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_INIT_CUSTOM
81 *
82 *******************************************************************************/
Cy_CTDAC_Init(CTDAC_Type * base,const cy_stc_ctdac_config_t * config)83 cy_en_ctdac_status_t Cy_CTDAC_Init(CTDAC_Type *base, const cy_stc_ctdac_config_t *config)
84 {
85     CY_ASSERT_L1(NULL != base);
86     CY_ASSERT_L1(NULL != config);
87 
88     cy_en_ctdac_status_t result;
89     uint32_t ctdacCtrl = CY_CTDAC_DEINIT;
90     uint32_t setSwitch = CY_CTDAC_DEINIT;
91     uint32_t clearSwitch = CY_CTDAC_DEINIT;
92 
93     if ((NULL == base) || (NULL == config))
94     {
95        result = CY_CTDAC_BAD_PARAM;
96     }
97     else
98     {
99 
100         CY_ASSERT_L3(CY_CTDAC_REFSOURCE(config->refSource));
101         CY_ASSERT_L3(CY_CTDAC_FORMAT(config->formatMode));
102         CY_ASSERT_L3(CY_CTDAC_UPDATE(config->updateMode));
103         CY_ASSERT_L3(CY_CTDAC_DEGLITCH(config->deglitchMode));
104         CY_ASSERT_L3(CY_CTDAC_OUTPUTMODE(config->outputMode));
105         CY_ASSERT_L3(CY_CTDAC_OUTPUTBUFFER(config->outputBuffer));
106         CY_ASSERT_L3(CY_CTDAC_DEEPSLEEP(config->deepSleep));
107         CY_ASSERT_L2(CY_CTDAC_DEGLITCHCYCLES(config->deglitchCycles));
108 
109         /* Handle the deglitch counts */
110         ctdacCtrl |= (config->deglitchCycles << CTDAC_CTDAC_CTRL_DEGLITCH_CNT_Pos) & CTDAC_CTDAC_CTRL_DEGLITCH_CNT_Msk;
111 
112         /* Handle the deglitch mode */
113         ctdacCtrl |= (uint32_t)config->deglitchMode;
114 
115         /* Handle the update mode */
116         if ((config->updateMode == CY_CTDAC_UPDATE_STROBE_EDGE_IMMEDIATE) \
117         || (config->updateMode == CY_CTDAC_UPDATE_STROBE_EDGE_SYNC) \
118         || (config->updateMode == CY_CTDAC_UPDATE_STROBE_LEVEL))
119         {
120             ctdacCtrl |= CTDAC_CTDAC_CTRL_DSI_STROBE_EN_Msk;
121         }
122 
123         if (config->updateMode == CY_CTDAC_UPDATE_STROBE_LEVEL)
124         {
125             ctdacCtrl |= CTDAC_CTDAC_CTRL_DSI_STROBE_LEVEL_Msk;
126         }
127 
128         /* Handle the sign format */
129         ctdacCtrl |= (uint32_t)config->formatMode;
130 
131         /* Handle the Deep Sleep mode */
132         ctdacCtrl |= (uint32_t)config->deepSleep;
133 
134         /* Handle the output mode */
135         ctdacCtrl |= (uint32_t)config->outputMode;
136 
137         /* Handle the reference source */
138         switch(config->refSource)
139         {
140         case CY_CTDAC_REFSOURCE_VDDA:
141 
142             /* Close the CVD switch to use Vdda as the reference source */
143             setSwitch |= CTDAC_CTDAC_SW_CTDD_CVD_Msk;
144             break;
145         case CY_CTDAC_REFSOURCE_EXTERNAL:
146         default:
147             clearSwitch |= CTDAC_CTDAC_SW_CLEAR_CTDD_CVD_Msk;
148             break;
149         }
150 
151         /* Handle the output buffer switch CO6 */
152         switch(config->outputBuffer)
153         {
154         case CY_CTDAC_OUTPUT_UNBUFFERED:
155 
156             /* Close the CO6 switch to send output to a direct pin unbuffered */
157             setSwitch |= CTDAC_CTDAC_SW_CTDO_CO6_Msk;
158             break;
159         case CY_CTDAC_OUTPUT_BUFFERED:
160         default:
161             clearSwitch |= CTDAC_CTDAC_SW_CTDO_CO6_Msk;
162             break;
163         }
164 
165         CTDAC_INTR_MASK(base)         = (uint32_t)config->enableInterrupt << CTDAC_INTR_VDAC_EMPTY_Pos;
166         CTDAC_INTR_MASK(base)         = CTDAC_INTR_VDAC_EMPTY_Msk;
167         CTDAC_CTDAC_SW(base)          = setSwitch;
168         CTDAC_CTDAC_SW_CLEAR(base)    = clearSwitch;
169         CTDAC_CTDAC_VAL(base)         = (((uint32_t)config->value) << CTDAC_CTDAC_VAL_VALUE_Pos) & CTDAC_CTDAC_VAL_VALUE_Msk;
170         CTDAC_CTDAC_VAL_NXT(base)     = (((uint32_t)config->nextValue) << CTDAC_CTDAC_VAL_NXT_VALUE_Pos) & CTDAC_CTDAC_VAL_NXT_VALUE_Msk;
171 
172         if (config->configClock)
173         {
174             Cy_CTDAC_ConfigureClock(config->updateMode, config->dividerType, config->dividerNum, config->dividerIntValue, config->dividerFracValue);
175         }
176 
177         CTDAC_CTDAC_CTRL(base)        = ctdacCtrl;
178         result                  = CY_CTDAC_SUCCESS;
179     }
180 
181     return result;
182 }
183 
184 /*******************************************************************************
185 * Function Name: Cy_CTDAC_DeInit
186 ****************************************************************************//**
187 *
188 * Reset CTDAC registers back to power on reset defaults.
189 *
190 * \note
191 * Does not disable the clock.
192 *
193 * \param base
194 * Pointer to structure describing registers
195 *
196 * \param deInitRouting
197 * If true, all switches are reset to their default state.
198 * If false, switch registers are untouched.
199 *
200 * \return
201 * Status of initialization, \ref CY_CTDAC_SUCCESS, or \ref CY_CTDAC_BAD_PARAM
202 *
203 * \funcusage
204 *
205 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_DEINIT
206 *
207 *******************************************************************************/
Cy_CTDAC_DeInit(CTDAC_Type * base,bool deInitRouting)208 cy_en_ctdac_status_t Cy_CTDAC_DeInit(CTDAC_Type *base, bool deInitRouting)
209 {
210     CY_ASSERT_L1(NULL != base);
211 
212     cy_en_ctdac_status_t result;
213 
214     if (NULL == base)
215     {
216        result = CY_CTDAC_BAD_PARAM;
217     }
218     else
219     {
220         CTDAC_CTDAC_CTRL(base)        = CY_CTDAC_DEINIT;
221         CTDAC_INTR_MASK(base)         = CY_CTDAC_DEINIT;
222         CTDAC_CTDAC_VAL(base)         = CY_CTDAC_DEINIT;
223         CTDAC_CTDAC_VAL_NXT(base)     = CY_CTDAC_DEINIT;
224 
225         if (deInitRouting)
226         {
227             CTDAC_CTDAC_SW_CLEAR(base) = CY_CTDAC_DEINIT;
228         }
229 
230         result                  = CY_CTDAC_SUCCESS;
231     }
232 
233     return result;
234 }
235 
236 /*******************************************************************************
237 * Function Name: Cy_CTDAC_FastInit
238 ****************************************************************************//**
239 *
240 * Initialize the CTDAC to one of the common use modes.
241 * This function provides a quick and easy method of configuring the CTDAC when using
242 * the PDL driver for device configuration.
243 *
244 * The other configuration options are set to:
245 *   - .formatMode       = \ref CY_CTDAC_FORMAT_UNSIGNED
246 *   - .updateMode       = \ref CY_CTDAC_UPDATE_BUFFERED_WRITE
247 *   - .deglitchMode     = \ref CY_CTDAC_DEGLITCHMODE_NONE
248 *   - .outputMode       = \ref CY_CTDAC_OUTPUT_VALUE
249 *   - .deepSleep        = \ref CY_CTDAC_DEEPSLEEP_DISABLE
250 *   - .deglitchCycles   = \ref CY_CTDAC_DEINIT
251 *   - .value            = \ref CY_CTDAC_UNSIGNED_MID_CODE_VALUE
252 *   - .nextValue        = \ref CY_CTDAC_UNSIGNED_MID_CODE_VALUE
253 *   - .enableInterrupt  = true
254 *   - .configClock      = true
255 *   - .dividerType      = \ref CY_CTDAC_FAST_CLKCFG_TYPE
256 *   - .dividerNum       = \ref CY_CTDAC_FAST_CLKCFG_NUM
257 *   - .dividerInitValue = \ref CY_CTDAC_FAST_CLKCFG_DIV
258 *   - .dividerFracValue = \ref CY_CTDAC_DEINIT
259 *
260 * A separate call to \ref Cy_CTDAC_Enable is needed to turn on the hardware.
261 *
262 * \param base
263 * Pointer to structure describing registers
264 *
265 * \param config
266 * Pointer to structure containing configuration data for quick initialization.
267 * Define your own or use one of the provided structures:
268 * - \ref Cy_CTDAC_Fast_VddaRef_UnbufferedOut
269 * - \ref Cy_CTDAC_Fast_VddaRef_BufferedOut
270 * - \ref Cy_CTDAC_Fast_OA1Ref_UnbufferedOut
271 * - \ref Cy_CTDAC_Fast_OA1Ref_BufferedOut
272 *
273 * \return
274 * Status of initialization, \ref CY_CTDAC_SUCCESS or \ref CY_CTDAC_BAD_PARAM
275 *
276 * \funcusage
277 *
278 * The following code snippets configures VDDA as the reference source and
279 * routes the output directly to Pin 6 (unbuffered).
280 *
281 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_FAST_INIT
282 *
283 * \funcusage
284 *
285 * The following code snippet shows how the CTDAC and CTB blocks can
286 * quickly be configured to work together. The code
287 * configures the CTDAC to use a buffered output,
288 * a buffered reference source from the internal bandgap voltage, and closes
289 * all required analog routing switches.
290 *
291 * \image html ctdac_fast_init_funcusage.png
292 * \image latex ctdac_fast_init_funcusage.png
293 *
294 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_FAST_INIT_CTB
295 *
296 *******************************************************************************/
Cy_CTDAC_FastInit(CTDAC_Type * base,const cy_stc_ctdac_fast_config_t * config)297 cy_en_ctdac_status_t Cy_CTDAC_FastInit(CTDAC_Type *base, const cy_stc_ctdac_fast_config_t *config)
298 {
299     CY_ASSERT_L1(NULL != base);
300     CY_ASSERT_L1(NULL != config);
301 
302     cy_en_ctdac_status_t result;
303     uint32_t ctdacCtrl;
304     uint32_t setSwitch = CY_CTDAC_DEINIT;
305     uint32_t clearSwitch = CY_CTDAC_DEINIT;
306 
307     if ((NULL == base) || (NULL == config))
308     {
309        result = CY_CTDAC_BAD_PARAM;
310     }
311     else
312     {
313         CY_ASSERT_L3(CY_CTDAC_REFSOURCE(config->refSource));
314         CY_ASSERT_L3(CY_CTDAC_OUTPUTBUFFER(config->outputBuffer));
315 
316         ctdacCtrl = (uint32_t) CY_CTDAC_DEGLITCHMODE_NONE \
317                     | (uint32_t) CY_CTDAC_UPDATE_BUFFERED_WRITE \
318                     | (uint32_t) CY_CTDAC_FORMAT_UNSIGNED \
319                     | (uint32_t) CY_CTDAC_DEEPSLEEP_DISABLE \
320                     | (uint32_t) CY_CTDAC_OUTPUT_VALUE;
321 
322         /* Handle the reference source */
323         switch(config->refSource)
324         {
325         case CY_CTDAC_REFSOURCE_VDDA:
326 
327             /* Close the CVD switch to use Vdda as the reference source */
328             setSwitch |= CTDAC_CTDAC_SW_CTDD_CVD_Msk;
329             break;
330         case CY_CTDAC_REFSOURCE_EXTERNAL:
331         default:
332             clearSwitch |= CTDAC_CTDAC_SW_CLEAR_CTDD_CVD_Msk;
333             break;
334         }
335 
336         /* Handle the output buffer switch CO6 */
337         switch(config->outputBuffer)
338         {
339         case CY_CTDAC_OUTPUT_UNBUFFERED:
340 
341             /* Close the CO6 switch to send output to a direct pin unbuffered */
342             setSwitch |= CTDAC_CTDAC_SW_CTDO_CO6_Msk;
343             break;
344         case CY_CTDAC_OUTPUT_BUFFERED:
345         default:
346             clearSwitch |= CTDAC_CTDAC_SW_CTDO_CO6_Msk;
347             break;
348         }
349 
350         CTDAC_INTR_MASK(base)         = CTDAC_INTR_VDAC_EMPTY_Msk;
351         CTDAC_CTDAC_SW(base)          = setSwitch;
352         CTDAC_CTDAC_SW_CLEAR(base)    = clearSwitch;
353         CTDAC_CTDAC_VAL(base)         = CY_CTDAC_UNSIGNED_MID_CODE_VALUE;
354         CTDAC_CTDAC_VAL_NXT(base)     = CY_CTDAC_UNSIGNED_MID_CODE_VALUE;
355 
356         /* For fast configuration, the DAC clock is the Peri clock divided by 100. */
357         Cy_CTDAC_ConfigureClock(CY_CTDAC_UPDATE_BUFFERED_WRITE, CY_CTDAC_FAST_CLKCFG_TYPE, CY_CTDAC_FAST_CLKCFG_NUM, CY_CTDAC_FAST_CLKCFG_DIV, CY_CTDAC_DEINIT);
358 
359         CTDAC_CTDAC_CTRL(base)        = ctdacCtrl;
360         result                  = CY_CTDAC_SUCCESS;
361     }
362 
363     return result;
364 }
365 
366 /*******************************************************************************
367 * Function Name: Cy_CTDAC_ConfigureClock
368 ****************************************************************************//**
369 *
370 * Private function for configuring the CTDAC clock based on the desired
371 * update mode. This function is called by \ref Cy_CTDAC_Init.
372 *
373 * \param updateMode
374 * Update mode value. See \ref cy_en_ctdac_update_t for values.
375 *
376 * \param dividerType
377 * Specifies which type of divider to use; see \ref cy_en_divider_types_t for values.
378 *
379 * \param dividerNum
380 * Specifies which divider of the selected type to configure.
381 *
382 * \param dividerIntValue
383 * The integer divider value.
384 *
385 * \param dividerFracValue
386 * The fraction part of the divider.
387 *
388 * \return None
389 *
390 *******************************************************************************/
Cy_CTDAC_ConfigureClock(cy_en_ctdac_update_t updateMode,cy_en_divider_types_t dividerType,uint32_t dividerNum,uint32_t dividerIntValue,uint32_t dividerFracValue)391 static void Cy_CTDAC_ConfigureClock(cy_en_ctdac_update_t updateMode, cy_en_divider_types_t dividerType,
392                                            uint32_t dividerNum, uint32_t dividerIntValue, uint32_t dividerFracValue)
393 {
394     if (updateMode == CY_CTDAC_UPDATE_DIRECT_WRITE)
395     { /* In direct mode, there is not a clock */
396     }
397     else if(updateMode == CY_CTDAC_UPDATE_STROBE_EDGE_IMMEDIATE)
398     {
399 
400         /* In this mode, the Peri Clock is divided by 1 to give a constant logic high on the CTDAC clock. */
401         (void)Cy_SysClk_PeriphDisableDivider(dividerType, dividerNum);
402 
403         (void)Cy_SysClk_PeriphAssignDivider(PCLK_PASS_CLOCK_CTDAC, dividerType, dividerNum);
404 
405         if ((dividerType == CY_SYSCLK_DIV_8_BIT) || (dividerType == CY_SYSCLK_DIV_16_BIT))
406         {
407             (void)Cy_SysClk_PeriphSetDivider(dividerType, dividerNum, CY_CTDAC_STROBE_EDGE_IMMEDIATE_DIV);
408         }
409         else
410         {
411             (void)Cy_SysClk_PeriphSetFracDivider(dividerType, dividerNum, CY_CTDAC_STROBE_EDGE_IMMEDIATE_DIV, CY_CTDAC_STROBE_EDGE_IMMEDIATE_DIV_FRAC);
412         }
413 
414         (void)Cy_SysClk_PeriphEnableDivider(dividerType, dividerNum);
415     }
416     else
417     {
418 
419         /* All other modes, require a CTDAC clock configured to the desired user frequency */
420         (void)Cy_SysClk_PeriphDisableDivider(dividerType, dividerNum);
421 
422         (void)Cy_SysClk_PeriphAssignDivider(PCLK_PASS_CLOCK_CTDAC, dividerType, dividerNum);
423 
424         if ((dividerType == CY_SYSCLK_DIV_8_BIT) || (dividerType == CY_SYSCLK_DIV_16_BIT))
425         {
426             (void)Cy_SysClk_PeriphSetDivider(dividerType, dividerNum, dividerIntValue);
427         }
428         else
429         {
430             (void)Cy_SysClk_PeriphSetFracDivider(dividerType, dividerNum, dividerIntValue, dividerFracValue);
431         }
432         (void)Cy_SysClk_PeriphEnableDivider(dividerType, dividerNum);
433     }
434 
435 }
436 
437 /*******************************************************************************
438 * Function Name: Cy_CTDAC_SetSignMode
439 ****************************************************************************//**
440 *
441 * Set whether to interpret the DAC value as signed or unsigned.
442 * In unsigned mode, the DAC value register is used without any decoding.
443 * In signed  mode, the MSB is inverted by adding 0x800 to the DAC value.
444 * This converts the lowest signed number, 0x800, to the lowest unsigned
445 * number, 0x000.
446 *
447 * \param base
448 * Pointer to structure describing registers
449 *
450 * \param formatMode
451 * Mode can be signed or unsigned. See \ref cy_en_ctdac_format_t for values.
452 *
453 * \return None
454 *
455 * \funcusage
456 *
457 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_SET_SIGN_MODE
458 *
459 *******************************************************************************/
Cy_CTDAC_SetSignMode(CTDAC_Type * base,cy_en_ctdac_format_t formatMode)460 void Cy_CTDAC_SetSignMode(CTDAC_Type *base, cy_en_ctdac_format_t formatMode)
461 {
462     CY_ASSERT_L3(CY_CTDAC_FORMAT(formatMode));
463 
464     uint32_t ctdacCtrl;
465 
466     /* Clear the CTDAC_MODE bits */
467     ctdacCtrl = CTDAC_CTDAC_CTRL(base) & ~CTDAC_CTDAC_CTRL_CTDAC_MODE_Msk;
468 
469     CTDAC_CTDAC_CTRL(base) = ctdacCtrl | (uint32_t)formatMode;
470 }
471 
472 /*******************************************************************************
473 * Function Name: Cy_CTDAC_SetDeepSleepMode
474 ****************************************************************************//**
475 *
476 * Enable or disable the DAC hardware operation in Deep Sleep mode.
477 *
478 * \param base
479 * Pointer to structure describing registers
480 *
481 * \param deepSleep
482 * Enable or disable Deep Sleep operation. Select value from \ref cy_en_ctdac_deep_sleep_t.
483 *
484 * \return None
485 *
486 * \funcusage
487 *
488 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_SET_DEEPSLEEP_MODE
489 *
490 *******************************************************************************/
Cy_CTDAC_SetDeepSleepMode(CTDAC_Type * base,cy_en_ctdac_deep_sleep_t deepSleep)491 void Cy_CTDAC_SetDeepSleepMode(CTDAC_Type *base, cy_en_ctdac_deep_sleep_t deepSleep)
492 {
493     CY_ASSERT_L3(CY_CTDAC_DEEPSLEEP(deepSleep));
494 
495     uint32_t ctdacCtrl;
496 
497     ctdacCtrl = CTDAC_CTDAC_CTRL(base) & ~CTDAC_CTDAC_CTRL_DEEPSLEEP_ON_Msk;
498 
499     CTDAC_CTDAC_CTRL(base) = ctdacCtrl | (uint32_t)deepSleep;
500 }
501 
502 /*******************************************************************************
503 * Function Name: Cy_CTDAC_SetOutputMode
504 ****************************************************************************//**
505 *
506 * Set the output mode of the CTDAC:
507 *   - \ref CY_CTDAC_OUTPUT_HIGHZ : Disable the output
508 *   - \ref CY_CTDAC_OUTPUT_VALUE : Enable the output and drive the value
509 *       stored in the CTDAC_VAL register.
510 *   - \ref CY_CTDAC_OUTPUT_VALUE_PLUS1 : Enable the output and drive the
511 *       value stored in the CTDAC_VAL register plus 1.
512 *   - \ref CY_CTDAC_OUTPUT_VSSA : Output pulled to VSSA through 1.1 MOhm (typ) resistor.
513 *   - \ref CY_CTDAC_OUTPUT_VREF : Output pulled to VREF through 1.1 MOhm (typ) resistor.
514 *
515 * \param base
516 * Pointer to structure describing registers
517 *
518 * \param outputMode
519 * Select a value from \ref cy_en_ctdac_output_mode_t.
520 *
521 * \return None
522 *
523 * \funcusage
524 *
525 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_SET_OUTPUT_MODE
526 *
527 *******************************************************************************/
Cy_CTDAC_SetOutputMode(CTDAC_Type * base,cy_en_ctdac_output_mode_t outputMode)528 void Cy_CTDAC_SetOutputMode(CTDAC_Type *base, cy_en_ctdac_output_mode_t outputMode)
529 {
530     CY_ASSERT_L3(CY_CTDAC_OUTPUTMODE(outputMode));
531 
532     uint32_t ctdacCtrl;
533 
534     /* Clear out the three affected bits */
535     ctdacCtrl = CTDAC_CTDAC_CTRL(base) & ~(CTDAC_CTDAC_CTRL_OUT_EN_Msk | CTDAC_CTDAC_CTRL_DISABLED_MODE_Msk | CTDAC_CTDAC_CTRL_CTDAC_RANGE_Msk);
536 
537     CTDAC_CTDAC_CTRL(base) = ctdacCtrl | (uint32_t)outputMode;
538 }
539 
540 /*******************************************************************************
541 * Function Name: Cy_CTDAC_SetDeglitchMode
542 ****************************************************************************//**
543 *
544 * Enable deglitching on the unbuffered path, buffered path, both, or
545 * disable deglitching. The deglitch mode should match the configured output path.
546 *
547 * \param base
548 * Pointer to structure describing registers
549 *
550 * \param deglitchMode
551 * Deglitching mode selection. See \ref cy_en_ctdac_deglitch_t for values.
552 *
553 * \return None
554 *
555 * \funcusage
556 *
557 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_SET_DEGLITCH_MODE
558 *
559 *******************************************************************************/
Cy_CTDAC_SetDeglitchMode(CTDAC_Type * base,cy_en_ctdac_deglitch_t deglitchMode)560 void Cy_CTDAC_SetDeglitchMode(CTDAC_Type *base, cy_en_ctdac_deglitch_t deglitchMode)
561 {
562     CY_ASSERT_L3(CY_CTDAC_DEGLITCH(deglitchMode));
563 
564     uint32_t ctdacCtrl;
565 
566     /* Clear out DEGLITCH_CO6 and DEGLITCH_C0S bits */
567     ctdacCtrl = CTDAC_CTDAC_CTRL(base) & ~(CTDAC_CTDAC_CTRL_DEGLITCH_COS_Msk | CTDAC_CTDAC_CTRL_DEGLITCH_CO6_Msk);
568 
569     CTDAC_CTDAC_CTRL(base) = ctdacCtrl | (uint32_t)deglitchMode;
570 }
571 
572 /*******************************************************************************
573 * Function Name: Cy_CTDAC_SetDeglitchCycles
574 ****************************************************************************//**
575 *
576 * Set the number of deglitch cycles (0 to 63) that will be used.
577 * To calculate the deglitch time:
578 *
579 *       (DEGLITCH_CNT + 1) / PERI_CLOCK_FREQ
580 *
581 * The optimal deglitch time is 700 ns.
582 *
583 * \param base
584 * Pointer to structure describing registers
585 *
586 * \param deglitchCycles
587 * Number of cycles to deglitch
588 *
589 * \return None
590 *
591 * \funcusage
592 *
593 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_SET_DEGLITCH_CYCLES
594 *
595 *******************************************************************************/
Cy_CTDAC_SetDeglitchCycles(CTDAC_Type * base,uint32_t deglitchCycles)596 void Cy_CTDAC_SetDeglitchCycles(CTDAC_Type *base, uint32_t deglitchCycles)
597 {
598     CY_ASSERT_L2(CY_CTDAC_DEGLITCHCYCLES(deglitchCycles));
599 
600     uint32_t ctdacCtrl;
601 
602     ctdacCtrl = (CTDAC_CTDAC_CTRL(base)) & ~CTDAC_CTDAC_CTRL_DEGLITCH_CNT_Msk;
603 
604     CTDAC_CTDAC_CTRL(base) = ctdacCtrl | ((deglitchCycles << CTDAC_CTDAC_CTRL_DEGLITCH_CNT_Pos) & CTDAC_CTDAC_CTRL_DEGLITCH_CNT_Msk);
605 }
606 
607 /*******************************************************************************
608 * Function Name: Cy_CTDAC_SetRef
609 ****************************************************************************//**
610 *
611 * Set the CTDAC reference source to Vdda or an external reference.
612 * The external reference must come from Opamp1 of the CTB.
613 *
614 * \param base
615 * Pointer to structure describing registers
616 *
617 * \param refSource
618 * The reference source. Select a value from \ref cy_en_ctdac_ref_source_t.
619 *
620 * \return None
621 *
622 * \funcusage
623 *
624 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_SET_REF
625 *
626 *******************************************************************************/
Cy_CTDAC_SetRef(CTDAC_Type * base,cy_en_ctdac_ref_source_t refSource)627 void Cy_CTDAC_SetRef(CTDAC_Type *base, cy_en_ctdac_ref_source_t refSource)
628 {
629     CY_ASSERT_L3(CY_CTDAC_REFSOURCE(refSource));
630 
631     switch(refSource)
632     {
633     case CY_CTDAC_REFSOURCE_VDDA:
634 
635         /* Close the CVD switch to use Vdda as the reference source */
636         CTDAC_CTDAC_SW(base) |= CTDAC_CTDAC_SW_CTDD_CVD_Msk;
637         break;
638     case CY_CTDAC_REFSOURCE_EXTERNAL:
639     default:
640         CTDAC_CTDAC_SW_CLEAR(base) = CTDAC_CTDAC_SW_CLEAR_CTDD_CVD_Msk;
641         break;
642     }
643 }
644 
645 /*******************************************************************************
646 * Function Name: Cy_CTDAC_SetAnalogSwitch
647 ****************************************************************************//**
648 *
649 * Provide firmware control of the CTDAC switches. Each call to this function
650 * can open a set of switches or close a set of switches.
651 *
652 * \note
653 * The switches are configured by the reference
654 * source and output mode selections during initialization.
655 *
656 * \param base
657 * Pointer to structure describing registers
658 *
659 * \param switchMask
660 * The mask of the switches to either open or close.
661 * Select one or more values from \ref cy_en_ctdac_switches_t and "OR" them together.
662 *
663 * \param state
664 * Open or close the switch(es). Select a value from \ref cy_en_ctdac_switch_state_t.
665 *
666 * \return None
667 *
668 * \funcusage
669 *
670 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_SET_ANALOG_SWITCH
671 *
672 *******************************************************************************/
Cy_CTDAC_SetAnalogSwitch(CTDAC_Type * base,uint32_t switchMask,cy_en_ctdac_switch_state_t state)673 void Cy_CTDAC_SetAnalogSwitch(CTDAC_Type *base, uint32_t switchMask, cy_en_ctdac_switch_state_t state)
674 {
675     CY_ASSERT_L2(CY_CTDAC_SWITCHMASK(switchMask));
676     CY_ASSERT_L3(CY_CTDAC_SWITCHSTATE(state));
677 
678     switch(state)
679     {
680     case CY_CTDAC_SWITCH_CLOSE:
681         CTDAC_CTDAC_SW(base) |= switchMask;
682         break;
683     case CY_CTDAC_SWITCH_OPEN:
684     default:
685 
686         /* Unlike the close case, do not OR the register. Set 1 to clear.*/
687         CTDAC_CTDAC_SW_CLEAR(base) = switchMask;
688         break;
689     }
690 }
691 
692 /*******************************************************************************
693 * Function Name: Cy_CTDAC_DeepSleepCallback
694 ****************************************************************************//**
695 *
696 * Callback to prepare the CTDAC before entering and after exiting Deep Sleep
697 * mode. If deglitching is used, it is disabled before entering Deep Sleep
698 * to ensure the deglitch switches are closed. This is needed only
699 * if the CTDAC will be enabled in DeepSleep. Upon wakeup, deglitching will
700 * be re-enabled if it was previously used.
701 *
702 * \param callbackParams
703 * Pointer to structure of type \ref cy_stc_syspm_callback_params_t.
704 * The context pointer should point to \ref cy_stc_ctdac_context_t.
705 *
706 * \param mode
707 * Callback mode, see \ref cy_en_syspm_callback_mode_t
708 *
709 * \return
710 * See \ref cy_en_syspm_status_t
711 *
712 * \funcusage
713 * \snippet ctdac/snippet/main.c CTDAC_SNIPPET_DEEP_SLEEP_CALLBACK
714 *
715 *******************************************************************************/
Cy_CTDAC_DeepSleepCallback(cy_stc_syspm_callback_params_t * callbackParams,cy_en_syspm_callback_mode_t mode)716 cy_en_syspm_status_t Cy_CTDAC_DeepSleepCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode)
717 {
718     cy_en_syspm_status_t returnValue = CY_SYSPM_SUCCESS;
719 
720     CTDAC_V1_Type *ctdacBase = (CTDAC_V1_Type *)callbackParams->base;
721     cy_stc_ctdac_context_t *ctdacContext = (cy_stc_ctdac_context_t *)callbackParams->context;
722 
723     if (CY_SYSPM_BEFORE_TRANSITION == mode)
724     { /* Actions that should be done before entering the Deep Sleep mode */
725 
726         /* Store the state of the deglitch switches before turning deglitch off */
727         ctdacContext->deglitchModeBeforeSleep = CTDAC_CTDAC_CTRL(ctdacBase) & (CTDAC_CTDAC_CTRL_DEGLITCH_CO6_Msk | CTDAC_CTDAC_CTRL_DEGLITCH_COS_Msk);
728 
729         /* Turn deglitch off before entering Deep Sleep */
730         CTDAC_CTDAC_CTRL(ctdacBase) &= ~(CTDAC_CTDAC_CTRL_DEGLITCH_CO6_Msk | CTDAC_CTDAC_CTRL_DEGLITCH_COS_Msk);
731     }
732     else if (CY_SYSPM_AFTER_TRANSITION == mode)
733     { /* Actions that should be done after exiting the Deep Sleep mode */
734 
735         /* Re-enable the deglitch mode that was configured before Deep Sleep entry */
736         CTDAC_CTDAC_CTRL(ctdacBase) |= ctdacContext->deglitchModeBeforeSleep;
737     }
738     else
739     { /* Does nothing in other modes */
740     }
741 
742     return returnValue;
743 }
744 
745 CY_MISRA_BLOCK_END('MISRA C-2012 Rule 11.3')
746 
747 #if defined(__cplusplus)
748 }
749 #endif
750 
751 #endif /* CY_IP_MXS40PASS_CTDAC */
752 
753 /* [] END OF FILE */
754