1 /**
2   ******************************************************************************
3   * @file    rtl8721d_comparator.h
4   * @author
5   * @version V1.0.0
6   * @date    2016-05-17
7   * @brief   This file contains all the functions prototypes for the Comparator firmware
8   *          library.
9   ******************************************************************************
10   * @attention
11   *
12   * This module is a confidential and proprietary property of RealTek and
13   * possession or use of this module requires written permission of RealTek.
14   *
15   * Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved.
16   ******************************************************************************
17   */
18 
19 #ifndef _RTL8721D_COMPARE_H_
20 #define _RTL8721D_COMPARE_H_
21 
22 /** @addtogroup AmebaD_Periph_Driver
23   * @{
24   */
25 
26 /** @defgroup Comparator
27   * @brief Comparator driver modules
28   * @{
29   */
30 
31 /** @addtogroup Comparator
32   * @verbatim
33   *****************************************************************************************
34   * Introduction
35   *****************************************************************************************
36   * Comparator:
37   * 	- Base Address: Comparator
38   * 	- Channel: 4
39   * 	- Adjustable internal comparison voltage, 0~3.3v, each step 0.1v
40   * 	- Adjustable internal divider resistors
41   * 	- Cooperate with ADC comparator-assist mode
42   *
43   *****************************************************************************************
44   * How to use Comparator
45   *****************************************************************************************
46   * 	  To use Comparator, the following steps are mandatory:
47   *
48   *      1. Enable the ADC & Comparator interface clock:
49   *			RCC_PeriphClockCmd(APBPeriph_ADC, APBPeriph_ADC_CLOCK, ENABLE);
50   *
51   *      2. Fill the CMP_InitStruct with the desired parameters.
52   *			CMP_StructInit(CMP_InitTypeDef *CMP_InitStruct)
53   *
54   *      3. Init Comparator with the parameters in CMP_InitStruct.
55   *			CMP_Init(CMP_InitTypeDef* CMP_InitStruct)
56   *
57   *      4. Activate the Comparator:
58   *			CMP_Cmd(ENABLE).
59   *
60   *      5.  Enbale specified mode:
61   *			CMP_SWTrigCmd(ENABLE)/CMP_AutoCSwCmd(ENABLE)/ \
62   *                            CMP_TimerTrigCmd(Tim_Idx, PeriodMs, ENABLE)
63   *
64   *****************************************************************************************
65   * @endverbatim
66   */
67 
68 /* Exported types ------------------------------------------------------------*/
69 
70 /** @defgroup COMP_Exported_Types COMP Exported Types
71   * @{
72   */
73 
74 /**
75   * @brief	Comparator Init structure definition
76   */
77 typedef struct {
78 	u8 CMP_ChIndex;			/*!< Specifies the channel index */
79 
80 	u8 CMP_Ref0;			/*!< Specifies the internal reference voltage0, the value can be 0~31,
81 									Vref0 = CMP_Ref0*0.1v */
82 	u8 CMP_Ref1;			/*!< Specifies the internal reference voltage1, the value can be 0~31,
83 									Vref1 = CMP_Ref1*0.1v */
84 	u8 CMP_WakeType;		/*!< Specifies this channel wakeup system or ADC when criterion matches.
85 									This parameter can be a value or combination of
86 									@ref COMP_Compare_Wakeup_Type_Definitions */
87 	u8 CMP_WakeSysCtrl;		/*!< Specifies the criteria of when comparator channel should wakeup
88 									system, which can be a value of
89 									@ref COMP_Compare_Control_Definitions */
90 	u8 CMP_WakeADCCtrl;		/*!< Specifies the criteria of when comparator channel should wakeup
91 									ADC, which can be a value of
92 									@ref COMP_Compare_Control_Definitions */
93 } CMP_CHTypeDef;
94 
95 typedef struct {
96 
97 	CMP_CHTypeDef CMP_ChanCtrl[4];  /*!< Specifies the comparator channel control parameters */
98 
99 } CMP_InitTypeDef;
100 /**
101   * @}
102   */
103 
104 
105 /* Exported constants --------------------------------------------------------*/
106 
107 /** @defgroup COMP_Exported_Constants COMP Exported Constants
108   * @{
109   */
110 
111 /** @defgroup COMP_Chn_Selection
112   * @{
113   */
114 #define COMP_CH0							((u8)0x00)
115 #define COMP_CH1							((u8)0x01)
116 #define COMP_CH2							((u8)0x02)
117 #define COMP_CH3							((u8)0x03)
118 #define COMP_CH_NUM						(4)
119 
120 #define IS_COMP_CHN_SEL(SEL)     (((SEL) == COMP_CH0) || \
121 									((SEL) == COMP_CH1) || \
122 									((SEL) == COMP_CH2) || \
123 									((SEL) == COMP_CH3))
124 
125 /**
126   * @}
127   */
128 
129 /** @defgroup COMP_Compare_Control_Definitions
130   * @{
131   */
132 #define COMP_SMALLER_THAN_REF0			((u8)0x00)	/*!< Vin < Vref0 */
133 #define COMP_GREATER_THAN_REF1			((u8)0x01)	/*!< Vin >= Vref1 */
134 #define COMP_WITHIN_REF0_AND_REF1			((u8)0x02)	/*!< Vin > Vref0 &&  Vin < Vref1 */
135 
136 /**
137   * @}
138   */
139 
140 /** @defgroup COMP_Compare_Wakeup_Type_Definitions
141   * @{
142   */
143 #define COMP_WK_SYS						BIT(1)
144 #define COMP_WK_ADC						BIT(0)
145 #define COMP_WK_NONE						0
146 
147 /**
148   * @}
149   */
150 
151 
152 /**
153   * @}
154   */
155 
156 /** @defgroup COMP_Exported_Functions COMP Exported Functions
157   * @{
158   */
159 _LONG_CALL_ void CMP_StructInit(CMP_InitTypeDef *CMP_InitStruct);
160 _LONG_CALL_ void CMP_Init(CMP_InitTypeDef* CMP_InitStruct);
161 _LONG_CALL_ void CMP_Cmd(u32 NewState);
162 _LONG_CALL_ u32 CMP_Busy(void);
163 _LONG_CALL_ u32 CMP_GetISR(void);
164 _LONG_CALL_ void CMP_INTClearPendingBit(u32 Cmp_IT);
165 _LONG_CALL_ u32 CMP_GetCompStatus(u8 channel);
166 _LONG_CALL_ u32 CMP_GetLastChan(void);
167 _LONG_CALL_ void CMP_ResetCSwList(void);
168 _LONG_CALL_ void CMP_AutoCSwCmd(u32 NewState);
169 _LONG_CALL_ void CMP_TimerTrigCmd(u8 Tim_Idx, u32 PeriodMs, u32 NewState);
170 
171 /**
172   * @}
173   */
174 
175 /* Registers Definitions --------------------------------------------------------*/
176 /**************************************************************************//**
177  * @defgroup COMP_Register_Definitions COMP Register Definitions
178  * @{
179  *****************************************************************************/
180 
181 /**************************************************************************//**
182  * @defgroup REG_COMP_REF_CHx
183  * @{
184  *****************************************************************************/
185 #define BIT_SHIFT_COMP_REF1						16
186 #define BIT_MASK_COMP_REF1						(u32)(0x0000001F << BIT_SHIFT_COMP_REF1)
187 #define BIT_SHIFT_COMP_REF0						0
188 #define BIT_MASK_COMP_REF0						(u32)(0x0000001F << BIT_SHIFT_COMP_REF0)
189 
190 /** @} */
191 
192 /**************************************************************************//**
193  * @defgroup REG_COMP_INTR_CTRL
194  * @{
195  *****************************************************************************/
196  #define BIT_SHIFT_COMP_WK_SYS_CTRL(x)			(17 + 3*x)
197  #define BIT_MASK_COMP_WK_SYS_CTRL(x)			(u32)(0x00000003 << BIT_SHIFT_COMP_WK_SYS_CTRL(x))
198  #define BIT_SHIFT_COMP_WK_SYS_EN(x)			(16 + 3*x)
199  #define BIT_COMP_WK_SYS_EN(x)					(u32)(0x00000001 << BIT_SHIFT_COMP_WK_SYS_EN(x))
200 
201  #define BIT_SHIFT_COMP_WK_ADC_CTRL(x)			(1 + 3*x)
202  #define BIT_MASK_COMP_WK_ADC_CTRL(x)			(u32)(0x00000003 << BIT_SHIFT_COMP_WK_ADC_CTRL(x))
203  #define BIT_SHIFT_COMP_WK_ADC_EN(x)			(3*x)
204  #define BIT_COMP_WK_ADC_EN(x)					(u32)(0x00000001 << BIT_SHIFT_COMP_WK_ADC_EN(x))
205 
206 /** @} */
207 
208 /**************************************************************************//**
209  * @defgroup REG_COMP_AUTOSW_EN
210  * @{
211  *****************************************************************************/
212 #define BIT_COMP_AUTOSW_EN					BIT(0)
213 
214 /** @} */
215 
216 /**************************************************************************//**
217  * @defgroup REG_COMP_EN_TRIG
218  * @{
219  *****************************************************************************/
220 #define BIT_COMP_DBG_EN						BIT(2)
221 #define BIT_COMP_EN_TRIG						BIT(1)
222 #define BIT_COMP_ENABLE							BIT(0)
223 
224 /** @} */
225 
226 
227 /**************************************************************************//**
228  * @defgroup REG_COMP_CHSW_LIST
229  * @{
230  *****************************************************************************/
231 #define BIT_SHIFT_COMP_CHSW(x)					(4*x)
232 #define BIT_COMP_CHSW(x)						(u32)(0x0000000F << BIT_SHIFT_COMP_CHSW(x))
233 
234 /** @} */
235 
236 /**************************************************************************//**
237  * @defgroup REG_COMP_LAST_CH
238  * @{
239  *****************************************************************************/
240  #define BIT_COMP_LAST_CH						(u32)(0x00000003)
241 
242 /** @} */
243 
244  /**************************************************************************//**
245  * @defgroup REG_COMP_BUSY_STS
246  * @{
247  *****************************************************************************/
248  #define BIT_COMP_BUSY_STS						BIT(0)
249 /** @} */
250 
251   /**************************************************************************//**
252  * @defgroup REG_COMP_CH_STS
253  * @{
254  *****************************************************************************/
255  #define BIT_SHIFT_COMP_CH_STS(x)				(2*x)
256  #define BIT_COMP_CH_STS(x)						(u32)(0x3 << BIT_SHIFT_COMP_CH_STS(x))
257 
258 /** @} */
259 
260 /**************************************************************************//**
261  * @defgroup REG_COMP_RST_LIST
262  * @{
263  *****************************************************************************/
264 #define BIT_COMP_RST_LIST						BIT(0)
265 /** @} */
266 
267 /**************************************************************************//**
268  * @defgroup REG_COMP_AUTO_SHUT
269  * @{
270  *****************************************************************************/
271 #define BIT_COMP_AUTO_SHUT						BIT(0)
272 /** @} */
273 
274 /**************************************************************************//**
275  * @defgroup REG_COMP_EXT_TRIG_CTRL
276  * @{
277  *****************************************************************************/
278 #define BIT_COMP_EXT_WK_TIMER					BIT(0)
279 /** @} */
280 
281 /**************************************************************************//**
282  * @defgroup REG_COMP_EXT_TRIG_TIMER_SEL
283  * @{
284  *****************************************************************************/
285 #define BIT_COMP_EXT_WK_TIMER_SEL				(u32)(0x00000007)
286 
287 /** @} */
288 
289  /**************************************************************************//**
290   * @defgroup REG_COMP_EXT_WK_SHUT_CTRL
291   * @{
292   *****************************************************************************/
293 #define BIT_SHIFT_COMP_CHSW_CNT				(8)
294 #define BIT_MASK_COMP_CHSW_CNT				(u32)(0x000000FF << BIT_SHIFT_COMP_CHSW_CNT)
295 #define BIT_MASK_COMP_EXT_WK_SHUT_CNT		(u32)(0x000000FF)
296 
297  /** @} */
298 
299  /**************************************************************************//**
300   * @defgroup REG_COMP_ANALOG
301   * @{
302   *****************************************************************************/
303 #define BIT_SD_POSEDGE							BIT(1)
304 
305  /** @} */
306 
307 
308 /** @} */
309 
310 /**
311   * @}
312   */
313 
314 /**
315   * @}
316   */
317 
318 /* Other Definitions --------------------------------------------------------*/
319 
320 
321 #endif /* _RTL8721D_COMPARE_H_ */
322 
323 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
324