1 /*!
2  * Copyright 2022 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_dac14.h"
9 
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.dac14"
13 #endif
14 
15 /*******************************************************************************
16  * Prototypes
17  ******************************************************************************/
18 /*!
19  * @brief Get instance number for DAC14 module.
20  *
21  * @param base DAC14 peripheral base address
22  */
23 static uint32_t DAC14_GetInstance(HPDAC_Type *base);
24 
25 /*******************************************************************************
26  * Variables
27  ******************************************************************************/
28 /*! @brief Pointers to DAC14 bases for each instance. */
29 static HPDAC_Type *const s_dac14Bases[] = HPDAC_BASE_PTRS;
30 
31 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
32 /*! @brief Pointers to DAC14 clocks for each instance. */
33 static const clock_ip_name_t s_dac14Clocks[] = HPDAC_CLOCKS;
34 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
35 
36 /*******************************************************************************
37  * Code
38  ******************************************************************************/
DAC14_GetInstance(HPDAC_Type * base)39 static uint32_t DAC14_GetInstance(HPDAC_Type *base)
40 {
41     uint32_t instance;
42 
43     /* Find the instance index from base address mappings. */
44     for (instance = 0; instance < ARRAY_SIZE(s_dac14Bases); instance++)
45     {
46         if (s_dac14Bases[instance] == base)
47         {
48             break;
49         }
50     }
51 
52     assert(instance < ARRAY_SIZE(s_dac14Bases));
53 
54     return instance;
55 }
56 
57 /*!
58  * brief Initialize the DAC14 module with common configuartion.
59  *
60  * The clock will be enabled in this function.
61  *
62  * param base DAC14 peripheral base address.
63  * param config Pointer to configuration structure.
64  */
DAC14_Init(HPDAC_Type * base,const dac14_config_t * config)65 void DAC14_Init(HPDAC_Type *base, const dac14_config_t *config)
66 {
67     assert(NULL != config);
68 
69     uint32_t tmp32 = 0U;
70 
71 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
72     /* Enable the clock. */
73     CLOCK_EnableClock(s_dac14Clocks[DAC14_GetInstance(base)]);
74 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
75 
76     /* Software reset and FIFO reset. */
77     DAC14_DoSoftwareReset(base);
78     DAC14_DoFIFOReset(base);
79 
80     /* Opamp is used as buffer. */
81     if (config->enableOpampBuffer)
82     {
83         tmp32 |= HPDAC_GCR_BUF_EN_MASK;
84     }
85     /*Enable the DAC system.*/
86     if (config->enableDAC)
87     {
88         tmp32 |= HPDAC_GCR_DACEN_MASK;
89     }
90 
91     if (config->WorkMode != kDAC14_BufferWorkMode)
92     {
93         /*Use software trigger source.*/
94         if (kDAC14_SoftwareTriggerSource == config->TriggerSource)
95         {
96             tmp32 |= HPDAC_GCR_TRGSEL_MASK;
97         }
98 
99         if (config->WorkMode != kDAC14_SwingBackWorkMode)
100         {
101             if (config->WorkMode != kDAC14_PeriodTriggerAndSwingBackWorkMode)
102             {
103                 /*Configurtion FIFO watermarklevel.*/
104                 base->FCR = HPDAC_FCR_WML(config->fifoWatermarkLevel);
105 
106                 if (config->WorkMode == kDAC14_PeriodTriggerWorkMode)
107                 {
108                     tmp32 |= HPDAC_GCR_FIFOEN_MASK | HPDAC_GCR_PTGEN_MASK; /* Enable period trigger mode. */
109                     /* Set trigger number and width. */
110                     base->PCR = HPDAC_PCR_PTG_NUM(config->periodicTriggerNumber) |
111                                 HPDAC_PCR_PTG_PERIOD(config->periodicTriggerWidth);
112                 }
113                 else
114                 {
115                     tmp32 |= HPDAC_GCR_FIFOEN_MASK; /* Enable FIFO mode.*/
116                 }
117             }
118             else
119             {
120                 /* Enable period trigger mode and swing back mode. */
121                 tmp32 |= HPDAC_GCR_FIFOEN_MASK | HPDAC_GCR_PTGEN_MASK | HPDAC_GCR_SWMD_MASK;
122                 /* Set trigger number and width. */
123                 base->PCR = HPDAC_PCR_PTG_NUM(config->periodicTriggerNumber) |
124                             HPDAC_PCR_PTG_PERIOD(config->periodicTriggerWidth);
125             }
126         }
127         else
128         {
129             tmp32 |= HPDAC_GCR_FIFOEN_MASK | HPDAC_GCR_SWMD_MASK; /* Enable swing mode. */
130         }
131     }
132     base->GCR = tmp32;
133 }
134 
135 /*!
136  * brief Get the default settings for initialization's configuration.
137  *
138  * This function initializes the user configuration structure to a default value. The default values are:
139  * code
140     config->fifoWatermarkLevel = 0U;
141     config->TriggerSource    = kDAC14_HardwareTriggerSource;
142     config->WorkMode       = kDAC14_BufferWorkMode;
143     config->enableOpampBuffer  = false;
144     config->enableADC  = false;
145     config->periodicTriggerNumber = 0U;
146     config->periodicTriggerWidth  = 0U;
147  * endcode
148  *
149  * param config Pointer to configuration structure.
150  */
DAC14_GetDefaultConfig(dac14_config_t * config)151 void DAC14_GetDefaultConfig(dac14_config_t *config)
152 {
153     assert(config != NULL);
154 
155     /* Initializes the configure structure to zero. */
156     (void)memset(config, 0, sizeof(*config));
157 
158     config->fifoWatermarkLevel    = 0U;
159     config->TriggerSource         = kDAC14_HardwareTriggerSource;
160     config->WorkMode              = kDAC14_BufferWorkMode;
161     config->enableOpampBuffer     = false;
162     config->enableDAC             = false;
163     config->periodicTriggerNumber = 0U;
164     config->periodicTriggerWidth  = 0U;
165 }
166 /*!
167  * brief De-initialize the DAC14 module.
168  *
169  * The clock will be disabled in this function.
170  *
171  * param base DAC14 peripheral base address.
172  */
DAC14_Deinit(HPDAC_Type * base)173 void DAC14_Deinit(HPDAC_Type *base)
174 {
175     /* Disable the module. */
176     DAC14_Enable(base, false);
177 
178 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
179     /* Disable the clock. */
180     CLOCK_DisableClock(s_dac14Clocks[DAC14_GetInstance(base)]);
181 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
182 }
183