1 /*********************************************************************************************************//**
2  * @file    ht32f5xxxx_dac.c
3  * @version $Rev:: 7904         $
4  * @date    $Date:: 2024-07-26 #$
5  * @brief   This file provides all the DAC firmware functions.
6  *************************************************************************************************************
7  * @attention
8  *
9  * Firmware Disclaimer Information
10  *
11  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14  *    other intellectual property laws.
15  *
16  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18  *    other than HOLTEK and the customer.
19  *
20  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24  *
25  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26  ************************************************************************************************************/
27 
28 /* Includes ------------------------------------------------------------------------------------------------*/
29 #include "ht32f5xxxx_dac.h"
30 
31 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
32   * @{
33   */
34 
35 /** @defgroup DAC DAC
36   * @brief DAC driver modules
37   * @{
38   */
39 
40 
41 /* Private constants ---------------------------------------------------------------------------------------*/
42 /** @defgroup DAC_Private_Define DAC private definitions
43   * @{
44   */
45 #define DAC_ENABLE_BIT       (0x00000001)
46 /**
47   * @}
48   */
49 
50 /* Global functions ----------------------------------------------------------------------------------------*/
51 /** @defgroup DAC_Exported_Functions DAC exported functions
52   * @{
53   */
54 /*********************************************************************************************************//**
55  * @brief Deinitialize the HT_DACn peripheral registers to their default reset values.
56  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
57  * @retval None
58  ************************************************************************************************************/
DAC_DeInit(HT_DAC_TypeDef * HT_DACn)59 void DAC_DeInit(HT_DAC_TypeDef* HT_DACn)
60 {
61   RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
62 
63   /* Check the parameters                                                                                   */
64   Assert_Param(IS_DAC(HT_DACn));
65 
66   if (HT_DACn == HT_DAC0)
67   {
68     RSTCUReset.Bit.DAC0 = 1;
69   }
70   #if (LIBCFG_DAC1)
71   else if (HT_DACn == HT_DAC1)
72   {
73     RSTCUReset.Bit.DAC1 = 1;
74   }
75   #endif
76   RSTCU_PeripReset(RSTCUReset, ENABLE);
77 }
78 
79 /*********************************************************************************************************//**
80  * @brief Configure the DAC conversion mode.
81  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
82  * @param ModeSel: specify the conversion mode
83  *   This parameter can be one of the following values:
84  *     @arg ASYNC_MODE : asynchronous conversion mode
85  *     @arg SYNC_MODE  : synchronous conversion mode
86  * @retval None
87  ************************************************************************************************************/
DAC_ModeConfig(HT_DAC_TypeDef * HT_DACn,u8 ModeSel)88 void DAC_ModeConfig(HT_DAC_TypeDef* HT_DACn, u8 ModeSel)
89 {
90   /* Check the parameters                                                                                   */
91   Assert_Param(IS_DAC(HT_DACn));
92   Assert_Param(IS_DAC_CONVERSION_MODE(ModeSel));
93 
94   HT_DACn->CFGR = ModeSel;
95 }
96 
97 /*********************************************************************************************************//**
98  * @brief Configure the specified DAC channel reference voltage.
99  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
100  * @param DAC_Ch: the DAC channel to configure
101 *   This parameter can be one of the following values:
102 *     @arg DAC_CH0 : DAC channel 0
103 *     @arg DAC_CH1 : DAC channel 1
104  * @param RefSel: DAC reference voltage source
105  *   This parameter can be one of the following values:
106  *     @arg DAC_REFERENCE_VDDA : VDDA
107  *     @arg DAC_REFERENCE_VREF : VREF
108  * @retval None
109  ************************************************************************************************************/
DAC_ReferenceConfig(HT_DAC_TypeDef * HT_DACn,u8 DAC_Ch,u32 RefSel)110 void DAC_ReferenceConfig(HT_DAC_TypeDef* HT_DACn, u8 DAC_Ch, u32 RefSel)
111 {
112   HT_DACCH_TypeDef *DACnCH = (HT_DACCH_TypeDef *)((u32)&HT_DACn->DACCH0 + DAC_Ch * 8 * 4);
113 
114   /* Check the parameters                                                                                   */
115   Assert_Param(IS_DAC(HT_DACn));
116   Assert_Param(IS_DAC_CHANNEL(DAC_Ch));
117   Assert_Param(IS_DAC_REFERENCE(RefSel));
118 
119   DACnCH->CR = (DACnCH->CR & ~(3UL << 14)) | RefSel;
120 }
121 
122 /*********************************************************************************************************//**
123  * @brief Configure the specified DAC channel resolution.
124  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
125  * @param DAC_Ch: the DAC channel to configure
126 *   This parameter can be one of the following values:
127 *     @arg DAC_CH0 : DAC channel 0
128 *     @arg DAC_CH1 : DAC channel 1
129  * @param ResoSel: DAC Channel Resolution
130  *   This parameter can be one of the following values:
131  *     @arg DAC_RESOLUTION_8BIT  : 8-bit resolution
132  *     @arg DAC_RESOLUTION_12BIT : 12-bit resolution
133  * @retval None
134  ************************************************************************************************************/
DAC_ResolutionConfig(HT_DAC_TypeDef * HT_DACn,u8 DAC_Ch,u32 ResoSel)135 void DAC_ResolutionConfig(HT_DAC_TypeDef* HT_DACn, u8 DAC_Ch, u32 ResoSel)
136 {
137   HT_DACCH_TypeDef *DACnCH = (HT_DACCH_TypeDef *)((u32)&HT_DACn->DACCH0 + DAC_Ch * 8 * 4);
138 
139   /* Check the parameters                                                                                   */
140   Assert_Param(IS_DAC(HT_DACn));
141   Assert_Param(IS_DAC_CHANNEL(DAC_Ch));
142   Assert_Param(IS_DAC_RESOLUTION(ResoSel));
143 
144   DACnCH->CR = (DACnCH->CR & ~(1UL << 2)) | ResoSel;
145 }
146 
147 /*********************************************************************************************************//**
148  * @brief Enable or Disable the specified DAC Channel output buffer.
149  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
150  * @param DAC_Ch: the DAC channel to configure
151 *   This parameter can be one of the following values:
152 *     @arg DAC_CH0 : DAC channel 0
153 *     @arg DAC_CH1 : DAC channel 1
154  * @param NewState: This parameter can be ENABLE or DISABLE.
155  * @retval None
156  ************************************************************************************************************/
DAC_OutBufCmd(HT_DAC_TypeDef * HT_DACn,u8 DAC_Ch,ControlStatus NewState)157 void DAC_OutBufCmd(HT_DAC_TypeDef* HT_DACn, u8 DAC_Ch, ControlStatus NewState)
158 {
159   HT_DACCH_TypeDef *DACnCH = (HT_DACCH_TypeDef *)((u32)&HT_DACn->DACCH0 + DAC_Ch * 8 * 4);
160 
161   /* Check the parameters                                                                                   */
162   Assert_Param(IS_DAC(HT_DACn));
163   Assert_Param(IS_DAC_CHANNEL(DAC_Ch));
164   Assert_Param(IS_CONTROL_STATUS(NewState));
165 
166   if (NewState != DISABLE)
167   {
168     DACnCH->CR = (DACnCH->CR & ~(3UL << 6)) | (2UL << 6);
169   }
170   else
171   {
172     DACnCH->CR = (DACnCH->CR & ~(3UL << 6)) | (1UL << 6);
173   }
174 }
175 
176 /*********************************************************************************************************//**
177  * @brief Enable or Disable the specified DAC channel.
178  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
179  * @param DAC_Ch: the DAC channel to configure
180 *   This parameter can be one of the following values:
181 *     @arg DAC_CH0 : DAC channel 0
182 *     @arg DAC_CH1 : DAC channel 1
183  * @param NewState: This parameter can be ENABLE or DISABLE.
184  * @retval None
185  ************************************************************************************************************/
DAC_Cmd(HT_DAC_TypeDef * HT_DACn,u8 DAC_Ch,ControlStatus NewState)186 void DAC_Cmd(HT_DAC_TypeDef* HT_DACn, u8 DAC_Ch, ControlStatus NewState)
187 {
188   HT_DACCH_TypeDef *DACnCH = (HT_DACCH_TypeDef *)((u32)&HT_DACn->DACCH0 + DAC_Ch * 8 * 4);
189 
190   /* Check the parameters                                                                                   */
191   Assert_Param(IS_DAC(HT_DACn));
192   Assert_Param(IS_DAC_CHANNEL(DAC_Ch));
193   Assert_Param(IS_CONTROL_STATUS(NewState));
194 
195   if (NewState != DISABLE)
196   {
197     DACnCH->CR |= DAC_ENABLE_BIT;
198   }
199   else
200   {
201     DACnCH->CR &= ~(DAC_ENABLE_BIT);
202   }
203 }
204 
205 /*********************************************************************************************************//**
206  * @brief Set the data holding register value for the specified DAC channel.
207  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
208  * @param DAC_Ch: the DAC channel to configure
209 *   This parameter can be one of the following values:
210 *     @arg DAC_CH0 : DAC channel 0
211 *     @arg DAC_CH1 : DAC channel 1
212  * @param Data: next conversion data.
213  * @retval None
214  ************************************************************************************************************/
DAC_SetData(HT_DAC_TypeDef * HT_DACn,u8 DAC_Ch,u32 Data)215 void DAC_SetData(HT_DAC_TypeDef* HT_DACn, u8 DAC_Ch, u32 Data)
216 {
217   HT_DACCH_TypeDef *DACnCH = (HT_DACCH_TypeDef *)((u32)&HT_DACn->DACCH0 + DAC_Ch * 8 * 4);
218 
219   /* Check the parameters                                                                                   */
220   Assert_Param(IS_DAC(HT_DACn));
221   Assert_Param(IS_DAC_CHANNEL(DAC_Ch));
222 
223   DACnCH->DHR = Data;
224 }
225 
226 /*********************************************************************************************************//**
227  * @brief Return the data output register value of the specified DAC channel.
228  * @param HT_DACn: where HT_DACn is the selected DAC from the DAC peripherals.
229  * @param DAC_Ch: the DAC channel to configure
230 *   This parameter can be one of the following values:
231 *     @arg DAC_CH0 : DAC channel 0
232 *     @arg DAC_CH1 : DAC channel 1
233  * @return The selected DAC channel data output value.
234  ************************************************************************************************************/
DAC_GetOutData(HT_DAC_TypeDef * HT_DACn,u8 DAC_Ch)235 u16 DAC_GetOutData(HT_DAC_TypeDef* HT_DACn, u8 DAC_Ch)
236 {
237   HT_DACCH_TypeDef *DACnCH = (HT_DACCH_TypeDef *)((u32)&HT_DACn->DACCH0 + DAC_Ch * 8 * 4);
238 
239   /* Check the parameters                                                                                   */
240   Assert_Param(IS_DAC(HT_DACn));
241   Assert_Param(IS_DAC_CHANNEL(DAC_Ch));
242 
243   return ((u16)DACnCH->DOR);
244 }
245 
246 
247 /**
248   * @}
249   */
250 
251 
252 /**
253   * @}
254   */
255 
256 /**
257   * @}
258   */
259