• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 
9 #ifndef HPM_DAO_DRV_H
10 #define HPM_DAO_DRV_H
11 
12 #include "hpm_common.h"
13 #include "hpm_dao_regs.h"
14 #include "hpm_i2s_common.h"
15 
16 /**
17  * @brief DAO driver APIs
18  * @defgroup dao_interface DAO driver APIs
19  * @ingroup io_interfaces
20  * @{
21  */
22 
23 /**
24  * @brief DAO channel selection
25  */
26 #define DAO_CHANNEL_LEFT_ONLY DAO_CTRL_LEFT_EN_MASK
27 #define DAO_CHANNEL_RIGHT_ONLY DAO_CTRL_RIGHT_EN_MASK
28 #define DAO_CHANNEL_BOTH \
29     (DAO_CTRL_RIGHT_EN_MASK | DAO_CTRL_LEFT_EN_MASK)
30 
31 /**
32  * @brief DAO default output
33  */
34 #define DAO_DEFAULT_OUTPUT_ALL_LOW (0U)
35 #define DAO_DEFAULT_OUTPUT_ALL_HIGH (1U)
36 #define DAO_DEFAULT_OUTPUT_P_HIGH_N_LOW (2U)
37 #define DAO_DEFAULT_OUTPUT_DISABLED (3U)
38 
39 /**
40  * @brief DAO config
41  */
42 typedef struct dao_config {
43     bool enable_mono_output;
44     uint8_t default_output_level;
45     uint8_t channel_count;
46 } dao_config_t;
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /**
53  * @brief config high pass filter
54  *
55  * @param [in] ptr DAO base address
56  * @param [in] hpf_coef_ma high pass filter a coefficient's complement
57  * @param [in] hpf_coef_b high pass filter b coefficient
58  * @param [in] enable
59  *  @arg true: enable
60  *  @arg false: disable
61  */
dao_config_hpf(DAO_Type * ptr,uint32_t hpf_coef_ma,uint32_t hpf_coef_b,bool enable)62 static inline void dao_config_hpf(DAO_Type *ptr,
63                                      uint32_t hpf_coef_ma,
64                                      uint32_t hpf_coef_b,
65                                      bool enable)
66 {
67     ptr->HPF_MA = DAO_HPF_MA_COEF_SET(hpf_coef_ma);
68     ptr->HPF_B = DAO_HPF_B_COEF_SET(hpf_coef_b);
69     ptr->CTRL = (ptr->CTRL & ~DAO_CTRL_HPF_EN_MASK)
70         | (enable ? DAO_CTRL_HPF_EN_MASK : 0);
71 }
72 
73 /**
74  * @brief enable high pass filter
75  *
76  * @param [in] ptr DAO base address
77  */
dao_enable_hpf(DAO_Type * ptr)78 static inline void dao_enable_hpf(DAO_Type *ptr)
79 {
80     ptr->CTRL |= DAO_CTRL_HPF_EN_MASK;
81 }
82 
83 /**
84  * @brief disable high pass filter
85  *
86  * @param [in] ptr DAO base address
87  */
dao_disable_hpf(DAO_Type * ptr)88 static inline void dao_disable_hpf(DAO_Type *ptr)
89 {
90     ptr->CTRL &= ~DAO_CTRL_HPF_EN_MASK;
91 }
92 
93 /**
94  * @brief enable error irq
95  *
96  * @param [in] ptr DAO base address
97  */
dao_enable_error_irq(DAO_Type * ptr)98 static inline void dao_enable_error_irq(DAO_Type *ptr)
99 {
100     ptr->CTRL |= DAO_CTRL_SAT_ERR_IE_MASK;
101 }
102 
103 /**
104  * @brief disable error irq
105  *
106  * @param [in] ptr DAO base address
107  */
dao_disable_error_irq(DAO_Type * ptr)108 static inline void dao_disable_error_irq(DAO_Type *ptr)
109 {
110     ptr->CTRL &= ~DAO_CTRL_SAT_ERR_IE_MASK;
111 }
112 
113 /**
114  * @brief enable channel
115  *
116  * @param [in] ptr DAO base address
117  * @param [in] ch channel number
118  */
dao_enable_channel(DAO_Type * ptr,uint32_t ch)119 static inline void dao_enable_channel(DAO_Type *ptr, uint32_t ch)
120 {
121     ptr->CTRL |= ch;
122 }
123 
124 /**
125  * @brief disable channel
126  *
127  * @param [in] ptr DAO base address
128  * @param [in] ch channel number
129  */
dao_disable_channel(DAO_Type * ptr,uint32_t ch)130 static inline void dao_disable_channel(DAO_Type *ptr, uint32_t ch)
131 {
132     ptr->CTRL &= ~ch;
133 }
134 
135 /**
136  * @brief enable mono output
137  *
138  * @param [in] ptr DAO base address
139  */
dao_enable_mono_output(DAO_Type * ptr)140 static inline void dao_enable_mono_output(DAO_Type *ptr)
141 {
142     ptr->CTRL |= DAO_CTRL_MONO_MASK;
143 }
144 
145 /**
146  * @brief disable mono output
147  *
148  * @param [in] ptr DAO base address
149  */
dao_disable_mono_output(DAO_Type * ptr)150 static inline void dao_disable_mono_output(DAO_Type *ptr)
151 {
152     ptr->CTRL &= ~DAO_CTRL_MONO_MASK;
153 }
154 
155 /**
156  * @brief enable remap
157  *
158  * @param [in] ptr DAO base address
159  */
dao_enable_remap(DAO_Type * ptr)160 static inline void dao_enable_remap(DAO_Type *ptr)
161 {
162     ptr->CTRL |= DAO_CTRL_REMAP_MASK;
163 }
164 
165 /**
166  * @brief disable remap
167  *
168  * @param [in] ptr DAO base address
169  */
dao_disable_remap(DAO_Type * ptr)170 static inline void dao_disable_remap(DAO_Type *ptr)
171 {
172     ptr->CTRL &= ~DAO_CTRL_REMAP_MASK;
173 }
174 
175 /**
176  * @brief invert output
177  *
178  * @param [in] ptr DAO base address
179  * @param [in] invert
180  *  @arg true: invert output
181  *  @arg false: not invert output
182  */
dao_invert_output(DAO_Type * ptr,bool invert)183 static inline void dao_invert_output(DAO_Type *ptr, bool invert)
184 {
185     ptr->CTRL = (ptr->CTRL & DAO_CTRL_INVERT_MASK)
186                 | DAO_CTRL_INVERT_SET(invert);
187 }
188 
189 /**
190  * @brief force pads output with certain level
191  *
192  * @param [in] ptr DAO base address
193  * @param [in] output output level
194  */
dao_force_output(DAO_Type * ptr,uint8_t output)195 static inline void dao_force_output(DAO_Type *ptr, uint8_t output)
196 {
197     ptr->CTRL = (ptr->CTRL & DAO_CTRL_FALSE_LEVEL_MASK)
198                 | DAO_CTRL_FALSE_LEVEL_SET(output);
199 }
200 
201 /**
202  * @brief enable false run
203  * when false run mode is enabled, the module continues to consume data, no actual output on pads.
204  * @param [in] ptr DAO base address
205  * @param [in] enable
206  *  @arg true: enable
207  *  @arg false: disable
208  */
dao_enable_false_run(DAO_Type * ptr,bool enable)209 static inline void dao_enable_false_run(DAO_Type *ptr, bool enable)
210 {
211     ptr->CTRL = (ptr->CTRL & DAO_CTRL_FALSE_RUN_MASK)
212                 | DAO_CTRL_FALSE_RUN_SET(enable);
213 }
214 
215 /**
216  * @brief software reset
217  *
218  * @param [in] ptr DAO base address
219  */
dao_software_reset(DAO_Type * ptr)220 static inline void dao_software_reset(DAO_Type *ptr)
221 {
222     ptr->CMD |= DAO_CMD_SFTRST_MASK;
223     ptr->CMD &= ~DAO_CMD_SFTRST_MASK;
224 }
225 
226 /**
227  * @brief check whether DAO is running
228  *
229  * @param [in] ptr DAO base address
230  * @retval true if dao is running
231  */
dao_is_running(DAO_Type * ptr)232 static inline bool dao_is_running(DAO_Type *ptr)
233 {
234     return ptr->CMD & DAO_CMD_RUN_MASK;
235 }
236 
237 /**
238  * @brief start
239  *
240  * @param [in] ptr DAO base address
241  */
dao_start(DAO_Type * ptr)242 static inline void dao_start(DAO_Type *ptr)
243 {
244     ptr->CMD |= DAO_CMD_RUN_MASK;
245 }
246 
247 /**
248  * @brief stop
249  *
250  * @param [in] ptr DAO base address
251  */
dao_stop(DAO_Type * ptr)252 static inline void dao_stop(DAO_Type *ptr)
253 {
254     ptr->CMD &= ~DAO_CMD_RUN_MASK;
255 }
256 
257 /**
258  * @brief initlization
259  *
260  * @param [in] ptr DAO base address
261  * @param [in] config dao_config_t
262  * @retval hpm_stat_t status_invalid_argument or status_success
263  */
264 hpm_stat_t dao_init(DAO_Type *ptr, dao_config_t *config);
265 
266 /**
267  * @brief get default config
268  *
269  * @param [in] ptr DAO base address
270  * @param [out] config dao_config_t
271  */
272 void dao_get_default_config(DAO_Type *ptr, dao_config_t *config);
273 
274 /**
275  * @}
276  */
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif /* HPM_DAO_DRV_H */
283