1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_dac.h"
10 
11 /*******************************************************************************
12  * Prototypes
13  ******************************************************************************/
14 /*!
15  * @brief Get instance number for DAC module.
16  *
17  * @param base DAC peripheral base address
18  */
19 static uint32_t DAC_GetInstance(LPDAC_Type *base);
20 
21 /*******************************************************************************
22  * Variables
23  ******************************************************************************/
24 /*! @brief Pointers to DAC bases for each instance. */
25 static LPDAC_Type *const s_dacBases[] = LPDAC_BASE_PTRS;
26 
27 /*! @brief Pointers to DAC clocks for each instance. */
28 static const clock_ip_name_t s_dacClocks[] = LPDAC_CLOCKS;
29 
30 /*******************************************************************************
31  * Code
32  ******************************************************************************/
DAC_GetInstance(LPDAC_Type * base)33 static uint32_t DAC_GetInstance(LPDAC_Type *base)
34 {
35     uint32_t instance;
36 
37     /* Find the instance index from base address mappings. */
38     for (instance = 0; instance < ARRAY_SIZE(s_dacBases); instance++)
39     {
40         if (s_dacBases[instance] == base)
41         {
42             break;
43         }
44     }
45 
46     assert(instance < ARRAY_SIZE(s_dacBases));
47 
48     return instance;
49 }
50 
DAC_Init(LPDAC_Type * base,const dac_config_t * config)51 void DAC_Init(LPDAC_Type *base, const dac_config_t *config)
52 {
53     assert(NULL != config);
54 
55     uint32_t tmp32 = 0U;
56 
57     /* Enable the clock. */
58     CLOCK_EnableClock(s_dacClocks[DAC_GetInstance(base)]);
59 
60     /* Reset the logic. */
61     DAC_SetReset(base, kDAC_ResetLogic);
62     DAC_ClearReset(base, kDAC_ResetLogic);
63 
64     /* Reset the FIFO. */
65     DAC_SetReset(base, kDAC_ResetFIFO);
66     DAC_ClearReset(base, kDAC_ResetFIFO);
67 
68     /* Configuration. */
69     if (kDAC_FIFOTriggerBySoftwareMode == config->fifoTriggerMode)
70     {
71         tmp32 |= LPDAC_GCR_TRGSEL_MASK; /* Software trigger. */
72     }
73     switch (config->fifoWorkMode)
74     {
75         case kDAC_FIFOWorkAsNormalMode: /* Normal FIFO. */
76             tmp32 |= LPDAC_GCR_FIFOEN_MASK;
77             break;
78         case kDAC_FIFOWorkAsSwingMode:
79             tmp32 |= LPDAC_GCR_FIFOEN_MASK | LPDAC_GCR_SWMD_MASK; /* Enable swing mode. */
80             break;
81         default: /* kDAC_FIFODisabled. */
82             break;
83     }
84     if (config->enableLowPowerMode)
85     {
86         tmp32 |= LPDAC_GCR_LPEN_MASK; /* Enable low power. */
87     }
88     if (kDAC_ReferenceVoltageSourceAlt2 == config->referenceVoltageSource)
89     {
90         tmp32 |= LPDAC_GCR_DACRFS_MASK;
91     }
92     base->GCR = tmp32;
93     base->FCR = LPDAC_FCR_WML(config->fifoWatermarkLevel);
94 
95     /* Now, the DAC is disabled. It needs to be enabled in application. */
96 }
97 
DAC_GetDefaultConfig(dac_config_t * config)98 void DAC_GetDefaultConfig(dac_config_t *config)
99 {
100     assert(config != NULL);
101 
102     config->fifoWatermarkLevel = 0U;
103     config->fifoTriggerMode = kDAC_FIFOTriggerByHardwareMode;
104     config->fifoWorkMode = kDAC_FIFODisabled;
105     config->enableLowPowerMode = false;
106     config->referenceVoltageSource = kDAC_ReferenceVoltageSourceAlt1;
107 }
108 
DAC_Deinit(LPDAC_Type * base)109 void DAC_Deinit(LPDAC_Type *base)
110 {
111     /* Disable the module. */
112     DAC_Enable(base, false);
113 
114     /* Disable the clock. */
115     CLOCK_DisableClock(s_dacClocks[DAC_GetInstance(base)]);
116 }
117