1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __HAL_COMP_H__
9 #define __HAL_COMP_H__
10 
11 #include "hal_common.h"
12 
13 /*!
14  * @addtogroup COMP
15  * @{
16  */
17 
18 /*!
19  * @brief COMP driver version number.
20  */
21 #define COMP_DRIVER_VERSION 0u /*!< COMP_0. */
22 
23 /*!
24  * @brief COMP number.
25  */
26 #define COMP_CHANNEL_NUM 2
27 
28 /*!
29  * @brief COMP output filter type.
30  *
31  * If the compare result keeps n APB Clocks unchanged, the output is valid.
32  */
33 typedef enum
34 {
35     COMP_OutFilter_1   = 0, /*!< 1   PCLK filter. */
36     COMP_OutFilter_2   = 1, /*!< 2   PCLK filter. */
37     COMP_OutFilter_4   = 2, /*!< 4   PCLK filter. */
38     COMP_OutFilter_8   = 3, /*!< 8   PCLK filter. */
39     COMP_OutFilter_16  = 4, /*!< 16  PCLK filter. */
40     COMP_OutFilter_32  = 5, /*!< 32  PCLK filter. */
41     COMP_OutFilter_64  = 6, /*!< 64  PCLK filter. */
42     COMP_OutFilter_128 = 7, /*!< 128 PCLK filter. */
43 } COMP_OutFilter_Type;
44 
45 /*!
46  * @brief COMP hysteresis type.
47  *
48  * If V(InvInput) > V(PosInput), the compare result is high, if V(InvInput) < (V(PosInput) - hysteresis), the compare result is low.
49  */
50 typedef enum
51 {
52     COMP_Hysteresis_Alt0 = 0, /*!< Hysteresis Alt 0. */
53     COMP_Hysteresis_Alt1 = 1, /*!< Hysteresis Alt 1. */
54     COMP_Hysteresis_Alt2 = 2, /*!< Hysteresis Alt 2. */
55     COMP_Hysteresis_Alt3 = 3, /*!< Hysteresis Alt 3. */
56 } COMP_Hysteresis_Type;
57 
58 /*!
59  * @brief COMP output mux type.
60  */
61 typedef enum
62 {
63     COMP_OutMux_None = 0,  /*!< Not output to other peripheral input. */
64     COMP_OutMux_Alt0 = 2,  /*!< Output Mux 0. */
65     COMP_OutMux_Alt1 = 4,  /*!< Output Mux 1. */
66     COMP_OutMux_Alt2 = 6,  /*!< Output Mux 2. */
67     COMP_OutMux_Alt3 = 7,  /*!< Output Mux 3. */
68     COMP_OutMux_Alt4 = 8,  /*!< Output Mux 4. */
69     COMP_OutMux_Alt5 = 9,  /*!< Output Mux 5. */
70     COMP_OutMux_Alt6 = 10, /*!< Output Mux 6. */
71     COMP_OutMux_Alt7 = 11, /*!< Output Mux 7. */
72 } COMP_OutMux_Type;
73 
74 /*!
75  * @brief COMP input Mux type.
76  */
77 typedef enum
78 {
79     COMP_InMux_Alt0 = 0, /*!< Input Mux 0. */
80     COMP_InMux_Alt1 = 1, /*!< Input Mux 1. */
81     COMP_InMux_Alt2 = 2, /*!< Input Mux 2. */
82     COMP_InMux_Alt3 = 3, /*!< Input Mux 3. */
83     COMP_InMux_Alt4 = 4, /*!< Input Mux 4. */
84 } COMP_InMux_Type;
85 
86 /*!
87  * @brief COMP speed type.
88  */
89 typedef enum
90 {
91     COMP_Speed_High    = 0, /*!< High speed, high power. */
92     COMP_Speed_Middle  = 1, /*!< Middle speed, middle power. */
93     COMP_Speed_Low     = 2, /*!< Low speed, low power. */
94     COMP_Speed_DeepLow = 3, /*!< Deep low speed, deep low power. */
95 } COMP_Speed_Type;
96 
97 /*!
98  * @brief COMP external reference voltage source type.
99  */
100 typedef enum
101 {
102     COMP_ExtVrefSource_VREFINT = 0,  /*!< Internal reference voltage. */
103     COMP_ExtVrefSource_VDDA    = 1,  /*!< VDDA voltage.   */
104 } COMP_ExtVrefSource_Type;
105 
106 /*!
107  * @brief COMP external reference voltage type.
108  */
109 typedef enum
110 {
111     COMP_ExtVrefVolt_Alt0  = 0,  /*!< External reference voltage Alt 0. */
112     COMP_ExtVrefVolt_Alt1  = 1,  /*!< External reference voltage Alt 1. */
113     COMP_ExtVrefVolt_Alt2  = 2,  /*!< External reference voltage Alt 2. */
114     COMP_ExtVrefVolt_Alt3  = 3,  /*!< External reference voltage Alt 3. */
115     COMP_ExtVrefVolt_Alt4  = 4,  /*!< External reference voltage Alt 4. */
116     COMP_ExtVrefVolt_Alt5  = 5,  /*!< External reference voltage Alt 5. */
117     COMP_ExtVrefVolt_Alt6  = 6,  /*!< External reference voltage Alt 6. */
118     COMP_ExtVrefVolt_Alt7  = 7,  /*!< External reference voltage Alt 7. */
119     COMP_ExtVrefVolt_Alt8  = 8,  /*!< External reference voltage Alt 8. */
120     COMP_ExtVrefVolt_Alt9  = 9,  /*!< External reference voltage Alt 9. */
121     COMP_ExtVrefVolt_Alt10 = 10, /*!< External reference voltage Alt 10. */
122     COMP_ExtVrefVolt_Alt11 = 11, /*!< External reference voltage Alt 11. */
123     COMP_ExtVrefVolt_Alt12 = 12, /*!< External reference voltage Alt 12. */
124     COMP_ExtVrefVolt_Alt13 = 13, /*!< External reference voltage Alt 13. */
125     COMP_ExtVrefVolt_Alt14 = 14, /*!< External reference voltage Alt 14. */
126     COMP_ExtVrefVolt_Alt15 = 15, /*!< External reference voltage Alt 15. */
127 } COMP_ExtVrefVolt_Type;
128 
129 /*!
130  * @brief COMP round robin period type.
131  *
132  * wait n APB clock to compare next channel.
133  */
134 typedef enum
135 {
136     COMP_RoundRobinPeriod_1     = 0,  /*!< Wait 1   PCLK2 to compare next channel. */
137     COMP_RoundRobinPeriod_2     = 1,  /*!< Wait 2   PCLK2 to compare next channel. */
138     COMP_RoundRobinPeriod_4     = 2,  /*!< Wait 4   PCLK2 to compare next channel. */
139     COMP_RoundRobinPeriod_8     = 3,  /*!< Wait 8   PCLK2 to compare next channel. */
140     COMP_RoundRobinPeriod_16    = 4,  /*!< Wait 16  PCLK2 to compare next channel. */
141     COMP_RoundRobinPeriod_32    = 5,  /*!< Wait 32  PCLK2 to compare next channel. */
142     COMP_RoundRobinPeriod_64    = 6,  /*!< Wait 64  PCLK2 to compare next channel. */
143     COMP_RoundRobinPeriod_128   = 7,  /*!< Wait 128 PCLK2 to compare next channel. */
144 } COMP_RoundRobinPeriod_Type;
145 
146 /*!
147  * @brief COMP round robin channel Type.
148  */
149 typedef enum
150 {
151     COMP_RoundRobinChnGroup_Alt0 = 0, /*!< Compare the positive input 1 & 2.    */
152     COMP_RoundRobinChnGroup_Alt1 = 1, /*!< Compare the positive input 1, 2 & 3. */
153 } COMP_RoundRobinChnGroup_Type;
154 
155 /*!
156  * @brief This type of structure instance is used to keep the settings when calling the @ref COMP_Init() to initialize the COMP module.
157  */
158 typedef struct
159 {
160     COMP_OutFilter_Type  OutFilter;  /*!< Specify the output filter. */
161     COMP_Hysteresis_Type Hysteresis; /*!< Specify the hysteresis. */
162     bool                 OutInvert;  /*!< Specify the output invert. */
163     COMP_OutMux_Type     OutMux;     /*!< Specify the output. */
164     COMP_InMux_Type      PosInMux;   /*!< Specify the positive input. */
165     COMP_InMux_Type      InvInMux;   /*!< Specify the nagetive input. */
166     COMP_Speed_Type      Speed;      /*!< Specify the compare speed. */
167 } COMP_Init_Type;
168 
169 /*!
170  * @brief This type of structure instance is used to keep the settings when calling the @ref COMP_EnableExtVrefConf() to enable the COMP ext vref.
171  */
172 typedef struct
173 {
174     COMP_ExtVrefSource_Type VrefSource; /*!< Specify the Vref source.  */
175     COMP_ExtVrefVolt_Type   Volt;       /*!< Specify the Vref voltage. */
176 } COMP_ExtVrefConf_Type;
177 
178 /*!
179  * @brief This type of structure instance is used to keep the settings when calling the @ref COMP_EnableRoundRobinConf() to enable the COMP round robin.
180  */
181 typedef struct
182 {
183     COMP_RoundRobinPeriod_Type   Period;    /*!< Specify the round robin period.  */
184     bool                         InvInFix;  /*!< Specify the nagetive input fix.  */
185     COMP_RoundRobinChnGroup_Type ChnGroup;  /*!< Specify the round robin channel. */
186 } COMP_RoundRobinConf_Type;
187 
188 /*!
189  * @brief Initialize the COMP module.
190  *
191  * @param COMPx         COMP instance.
192  * @param channel       COMP channel.
193  * @param init          Pointer to the initialization structure. See to @ref COMP_Init_Type.
194  * @return              None.
195  */
196 void COMP_Init(COMP_Type * COMPx, uint32_t channel, COMP_Init_Type * init);
197 
198 /*!
199  * @brief Enable the COMP module.
200  *
201  * @param COMPx         COMP instance.
202  * @param channel       COMP channel.
203  * @param enable        'true' to enable the module, 'false' to disable the module.
204  * @return              None.
205  */
206 void COMP_Enable(COMP_Type * COMPx, uint32_t channel, bool enable);
207 
208 /*!
209  * @brief Keep the COMP settings not changed.
210  *
211  * @param COMPx         COMP instance.
212  * @param channel       COMP channel.
213  * @return              None.
214  */
215 void COMP_Lock(COMP_Type * COMPx, uint32_t channel);
216 
217 /*!
218  * @brief Get comp output status.
219  *
220  * @param COMPx         COMP instance.
221  * @param channel       COMP channel.
222  * @return              output status.
223  */
224 bool COMP_GetOutputStatus(COMP_Type * COMPx, uint32_t channel);
225 
226 /*!
227  * @brief Enable the COMP ext Vref.
228  *
229  * @param COMPx         COMP instance.
230  * @param init          Pointer to the initialization structure. See to @ref COMP_ExtVrefConf_Type, if null, disable ext Vref.
231  * @return              None.
232  */
233 void COMP_EnableExtVrefConf(COMP_Type * COMPx, COMP_ExtVrefConf_Type * conf);
234 
235 /*!
236  * @brief Enable polling function.
237  *
238  * Enable round robin function, the comp channel can get positive input 1, 2 & 3 status.
239  *
240  * @param COMPx COMP instance.
241  * @param channel COMP channel.
242  * @param init Pointer to the initialization structure. See to @ref COMP_RoundRobinConf_Type, if null, disable round robin.
243  * @return None.
244  */
245 void COMP_EnableRoundRobinConf(COMP_Type * COMPx, uint32_t channel, COMP_RoundRobinConf_Type * conf);
246 
247 /*!
248  * @brief Get round robin output status.
249  *
250  * @param COMPx COMP instance.
251  * @param channel COMP channel.
252  * @param pos_in Positive input Mux.
253  * @return Status of comp channel output level.
254  */
255 bool COMP_GetRoundRobinOutStatus(COMP_Type * COMPx, uint32_t channel, COMP_InMux_Type pos_in);
256 
257 /*!
258  *@}
259  */
260 
261 #endif /* __HAL_COMP_H__ */
262