1 /**
2   ******************************************************************************
3   * @file    rtl8721d_captouch.h
4   * @author
5   * @version V1.0.0
6   * @date    2017-10-16
7   * @brief   This file contains all the functions prototypes for the captouch.
8   ******************************************************************************
9   * @attention
10   *
11   * This module is a confidential and proprietary property of RealTek and
12   * possession or use of this module requires written permission of RealTek.
13   *
14   * Copyright(c) 2017, Realtek Semiconductor Corporation. All rights reserved.
15   ******************************************************************************
16   */
17 
18 
19 #ifndef _RTL8721D_CAPTOUCH_H_
20 #define _RTL8721D_CAPTOUCH_H_
21 
22 /** @addtogroup AmebaD_Periph_Driver
23   * @{
24   */
25 
26 /** @addtogroup CapTouch CapTouch
27   * @{
28   */
29 
30 /** @addtogroup CapTouch
31   * @verbatim
32   *****************************************************************************************
33   * Introduction
34   *****************************************************************************************
35   * CAPTOUCH:
36   *		- Base Address: CAPTOUCH_DEV
37   *		- Clock source : 32.768KHz
38   *		- Scan interval timer: 1.024KHz(32.768KHz/32)
39   *		- Debounce Timer: Configurable
40   *		- SocPs: Sleep Mode (clock gating & power gating)
41   *		- IRQ: CapTouch_IRQ
42   *
43   *****************************************************************************************
44   * How to use Normal CapTouch
45   *****************************************************************************************
46   *      To use the normal CapTouch mode, the following steps are mandatory:
47   *
48   *      1. Enable CapTouch peripheral clock
49   *
50   *      2. Configure the CapTouch pinmux.
51   *
52   *      3. Init Captouch parameters:
53   *			CapTouch_StructInit(CapTouch_InitTypeDef* CapTouch_InitStruct)
54   *
55   *      4. Init Hardware use step3 parameters:
56   *			CapTouch_Init(CAPTOUCH_TypeDef *CapTouch, CapTouch_InitTypeDef* CapTouch_InitStruct)
57   *
58   *      5. Enable the NVIC and the corresponding interrupt using following function if you need
59   *			to use interrupt mode.
60   *			CapTouch_INTConfig(): CapTouch IRQ Enable set
61   *			CapTouch_INTMask(): CapTouch IRQ mask set
62   *			InterruptRegister(): register the captouch irq handler
63   *			InterruptEn(): Enable the NVIC interrupt
64   *
65   *      6. Enable CapTouch module using CapTouch_Cmd().
66   *
67   *****************************************************************************************
68   * @endverbatim
69   */
70 
71 /* Exported types --------------------------------------------------------*/
72 /** @defgroup CapTouch_Exported_Types CapTouch Exported Types
73   * @{
74   */
75 
76 /**
77   * @brief  CapTouch Channel Initialization structure definition
78   */
79 typedef struct {
80 
81 	u16 CT_DiffThrehold;	/*!< Difference threshold data of touch judgement for channelx :
82 							1. Configured during development; (0x0~0xFFF) (0~4095)
83 							2. Init number=0x0, need to be configured
84 							3. recommend data=80%*(signal-baseline); */
85 
86 	u8 CT_MbiasCurrent;		/*!< Channelx mbias current tuning(sensitivity tuning).
87 							Touch bias current BIT[5] ~ BIT[0]: 8uA/4uA/2uA/1uA/0.5uA/0.25uA.
88 							The sensitivity parameter is used to increase or decrease the strength
89 							of the sensor signal (difference count). A higher value of sensitivity (bias current)
90 							setting leads to a stronger signal from the sensors (more difference
91 							count for the same capacitance change), but also increases the response
92 							time and average power consumption.*/
93 
94 	u8 CT_ETCNNoiseThr;		/*!< Specifies negetive noise threshold of ETC.
95 							This parameter must be set to a value in the 0x1-0xff range.
96 							The noise threshold indicates the raw data of the maximum capacitance change
97 							caused by environmental change. The CTC system provides configurable
98 							positive noise threshold and negative noise threshold for optimal calibration.
99 							The recommend value of noise threshold is 40%* touch threshold. Users need
100 							tune the two noise thresholds for different applications and noise environment.*/
101 
102 	u8 CT_ETCPNoiseThr;		/*!< Specifies positive threshold of ETC.
103 							This parameter must be set to a value in the 0x1-0xff range. */
104 
105 	u8 CT_CHEnable;			/*!< Specifies this channel is enable or not */
106 } CapTouch_CHInitTypeDef;
107 
108 /**
109   * @brief  CapTouch Initialization structure definition
110   */
111 typedef struct {
112 	u32 CT_DebounceEn;			/*!< Specifies CapTouch press event Debounce Enable.
113 							This parameter must be set to a value in the 0x0-1 range. The de-bounce
114 							parameter can be configured by register CTC_CTRL.
115 							For example, when de-bounce is enabled and configured as 00 (2 times scan),
116 							finger touch interrupt will not be sent to the host until 2 times continue
117 							finger touch event is detected. Sensor de-bounce function is suitable for
118 							both button application and proximity detection.*/
119 
120 	u32 CT_SampleCnt;			/*!< Specifies sample cnt for average function,sample cnt = 2*exp(CT_SampleCnt+2).
121 							This parameter can be a value of 0x0-0x7*/
122 
123 	u32 CT_ScanInterval;			/*!< Specifies scan interval of every key.
124 							This parameter must be set to a value in the 0x1-0xfff range. */
125 
126 	u32 CT_ETCStep;				/*!< Specifies baseline update setp of ETC.
127 							This parameter must be set to a value in the 0x0-0xfff range. */
128 
129 	u32 CT_ETCFactor;			/*!< Specifies CapTouch ETC Factor.
130 							This parameter must be set to a value in the 0x0-0xf range. */
131 
132 	u32 CT_ETCScanInterval;		/*!< Specifies ETC scan interval
133 							This parameter can be set to a value in the 0x1-0x7f range*/
134 
135 	CapTouch_CHInitTypeDef CT_Channel[5];			/*!< Specifies the initialization parameters for each channel */
136 
137 }CapTouch_InitTypeDef;
138 
139 /**
140   * @}
141   */
142 
143 /* Exported constants --------------------------------------------------------*/
144 /** @defgroup CapTouch_Exported_Constants CapTouch Exported Constants
145   * @{
146   */
147 
148 /** @defgroup CapTouch_Peripheral_definitions
149   * @{
150   */
151 #define IS_CAPTOUCH_ALL_PERIPH(PERIPH) ((PERIPH) == CAPTOUCH_DEV)
152 /**
153   * @}
154   */
155 
156 /** @defgroup CapTouch_INT_Related_definitions
157   * @{
158   */
159 #define  CT_CHX_PRESS_INT(x)			(0x00000001 << (x))
160 #define  CT_CHX_RELEASE_INT(x)			(0x00000001 << ((x) + 8))
161 
162 #define IS_CT_INT_EN(IT)					(((IT) & ~BIT_CT_ALL_INT_EN) == 0)
163 #define IS_CT_INT_CLR(IT)				(((IT) & ~BIT_CT_ALL_INT_CLR_MASK) == 0)
164 /**
165   * @}
166   */
167 
168 
169 /** @defgroup CapTouch_Channel_Related_definitions
170   * @{
171   */
172 #define  CT_CHANNEL_NUM			4
173 #define IS_CT_CHANNEL(CHANNEL_NUM)		(CHANNEL_NUM < CT_CHANNEL_NUM)
174 /**
175   * @}
176   */
177 
178 /** @defgroup CapTouch_Noise_Threshold_Type_definitions
179   * @{
180   */
181 #define  P_NOISE_THRES					0
182 #define  N_NOISE_THRES					1
183 /**
184   * @}
185   */
186 
187 
188 /**
189   * @}
190   */
191 
192 /* Exported functions --------------------------------------------------------*/
193 /** @defgroup CapTouch_Exported_Functions CapTouch Exported Functions
194   * @{
195   */
196 
197 /** @defgroup CapTouch_Exported_Normal_Functions CapTouch Normal Functions
198   * @{
199   */
200 void CapTouch_StructInit(CapTouch_InitTypeDef* CapTouch_InitStruct);
201 void CapTouch_Init(CAPTOUCH_TypeDef *CapTouch, CapTouch_InitTypeDef* CapTouch_InitStruct);
202 void CapTouch_Cmd(CAPTOUCH_TypeDef *CapTouch, u8 NewState);
203 void CapTouch_INTConfig(CAPTOUCH_TypeDef *CapTouch, uint32_t CapTouch_IT, u8 newState);
204 void CapTouch_INTClearPendingBit(CAPTOUCH_TypeDef *CapTouch, u32 CapTouch_IT);
205 u32 CapTouch_GetRawISR(CAPTOUCH_TypeDef *CapTouch);
206 u32 CapTouch_GetISR(CAPTOUCH_TypeDef *CapTouch);
207 
208 void CapTouch_SetScanInterval(CAPTOUCH_TypeDef *CapTouch, u32 Interval);
209 void CapTouch_ChCmd(CAPTOUCH_TypeDef *CapTouch, u8 Channel, u8 NewState);
210 u32 CapTouch_GetChStatus(CAPTOUCH_TypeDef *CapTouch, u32 Channel);
211 void CapTouch_SetChDiffThres(CAPTOUCH_TypeDef *CapTouch, u8 Channel, u32 Threshold);
212 void CapTouch_SetChMbias(CAPTOUCH_TypeDef *CapTouch, u8 Channel, u8 Mbias);
213 u32 CapTouch_GetChDiffThres(CAPTOUCH_TypeDef *CapTouch, u8 Channel);
214 u32 CapTouch_GetNoiseThres(CAPTOUCH_TypeDef *CapTouch, u8 Channel, u8 type);
215 u32 CapTouch_GetChBaseline(CAPTOUCH_TypeDef *CapTouch, u8 Channel);
216 u32 CapTouch_GetChAveData(CAPTOUCH_TypeDef *CapTouch, u8 Channel);
217 /**
218   * @}
219   */
220 
221 /** @defgroup CapTouch_Exported_Debug_Functions CapTouch Debug Functions
222   * @{
223   */
224 void CapTouch_DbgCmd(CAPTOUCH_TypeDef *CapTouch, u8 NewState);
225 void CapTouch_DbgDumpReg(CAPTOUCH_TypeDef *CapTouch);
226 void CapTouch_DbgDumpETC(CAPTOUCH_TypeDef *CapTouch, u32 ch);
227 u32 CapTouch_DbgRawData(CAPTOUCH_TypeDef *CapTouch);
228 
229 /**
230   * @}
231   */
232 
233 /**
234   * @}
235   */
236 
237 
238 /* Registers Definitions --------------------------------------------------------*/
239 /**************************************************************************//**
240  * @defgroup CapTouch_Register_Definitions CapTouch Register Definitions
241  * @{
242  *****************************************************************************/
243 
244 /**************************************************************************//**
245  * @defgroup CT_CTRL
246  * @{
247  *****************************************************************************/
248 #define BIT_CT_BL_ENABLE						((u32)0x0000001 << 8)		/*Bit[8], bits for base init enable*/
249 #define BIT_CT_DBN_CNT							((u32)0x0000003 << 5)		/*Bit[6:5], bits for debounce cnt*/
250 #define BIT_CT_DBN_ENABLE						((u32)0x0000001 << 4)		/*Bit[4], bits for base init enable*/
251 #define BIT_CT_ENABLE							((u32)0x0000001 << 0)		/*Bit[8], bits for captouch function enable*/
252 
253 /** @} */
254 
255 /**************************************************************************//**
256  * @defgroup CT_SP_CTRL
257  * @{
258  *****************************************************************************/
259 #define BIT_CT_SMP_AVE							((u32)0x00000007 << 16)	/*Bit[18:16], bits for every chanel sample cnt*/
260 #define BIT_CT_SCAN_INTERVAL					((u32)0x00000fff << 0)	/*Bit[11:0], bits for pre guard timer set*/
261 /** @} */
262 
263 /**************************************************************************//**
264  * @defgroup CT_ETC_CTRL
265  * @{
266  *****************************************************************************/
267 #define BIT_CT_ETC_N_THRES						((u32)0x000000ff << 24)	/*Bit[31:24], bits for Max negative threshold*/
268 #define BIT_CT_ETC_P_THRES						((u32)0x000000ff << 16)	/*Bit[23:16], bits for Max positive threshold*/
269 #define BIT_CT_ETC_STEP							((u32)0x0000000f << 12)	/*Bit[15:12], bits for ETC setp for every update*/
270 #define BIT_CT_ETC_FACTOR						((u32)0x0000000f << 8)	/*Bit[11:8], bits for ETC interval timer*/
271 #define BIT_CT_ETC_SCAN_INTERVAL				((u32)0x0000007f << 1)	/*Bit[7:1], bits for ETC scan interval timer set*/
272 #define BIT_CT_ETC_ENABLE						((u32)0x00000001 << 0)	/*Bit[0], bits for ETC function Enable*/
273 
274 /** @} */
275 
276 /**************************************************************************//**
277  * @defgroup CT_SNR
278  * @{
279  *****************************************************************************/
280 #define BIT_CT_SNR_NOISE						((u32)0x00000fff << 16)		/*Bit[27:16], bit raw noise data for SNR*/
281 #define BIT_CT_SNR_TOUCH						((u32)0x00000fff << 0)		/*Bit[11:0], bit raw touch data for SNR*/
282 /** @} */
283 
284 /**************************************************************************//**
285  * @defgroup CT_MODE_CTRL
286  * @{
287  *****************************************************************************/
288 #define BIT_CT_CHANNEL_SEL						((u32)0x00000007 << 5)		/*Bit[7:5], bits for captouch channel select*/
289 #define BIT_CT_AUTO_CHANNEL_ENABLE			((u32)0x00000001 << 4)		/*Bit[4], bits for captouch auto channel swith enable*/
290 #define BIT_CT_DBG_ENABLE						((u32)0x00000001 << 0)		/*Bit[0], bits for captouch debug mode enable*/
291 /** @} */
292 
293 /**************************************************************************//**
294  * @defgroup CT_FIFO_STATUS
295  * @{
296  *****************************************************************************/
297 #define BIT_CT_FIFO_OFFSET						((u32)0x00000007 << 4)		/*Bit[6:4], bits for key column select*/
298 #define BIT_CT_FIFO_EMPTY						((u32)0x00000001 << 1)		/*Bit[1], bit for fifo full flag*/
299 #define BIT_CT_FIFO_FULL						((u32)0x00000001 << 0)		/*Bit[0], bit for fifo full flag*/
300 
301 /** @} */
302 
303 /**************************************************************************//**
304  * @defgroup CT_FIFO
305  * @{
306  *****************************************************************************/
307 #define BIT_CT_FIFO_VLD							((u32)0x00000001 << 31)		/*Bit[31], bit for fifo data valid flag*/
308 #define BIT_CT_FIFO_DATA						((u32)0x00000fff << 0) 		/*Bit[11:0], bit for fifo raw data*/
309 /** @} */
310 
311 /**************************************************************************//**
312  * @defgroup CT_IER_ISR_RAWISR
313  * @{
314  *****************************************************************************/
315 #define BIT_CT_OVER_N_NOISE_THRESHOLD_INT	((u32)0x00000001 << 18)		/*Bit[18], bit for captouch over negetive noise thresh interrupt enable*/
316 #define BIT_CT_FIFO_OVERFLOW_INT				((u32)0x00000001 << 17)		/*Bit[17], bit for captouch fifo overflow interrupt enable*/
317 #define BIT_CT_OVER_P_NOISE_THRESHOLD_INT	((u32)0x00000001 << 16)		/*Bit[16], bit for captouch over positive noise thresh interrupt enable*/
318 #define BIT_CT_TOUCH_RELEASE_INT				((u32)0x0000001f << 8)		/*Bit[12:8], bit for captouch release interrupt enable*/
319 #define BIT_CT_TOUCH_PRESS_INT				((u32)0x0000001f <<0)		/*Bit[4:0], bit for captouch press interrupt enable*/
320 #define BIT_CT_ALL_INT_EN						((u32)0x00031f1f)
321 /** @} */
322 
323 /**************************************************************************//**
324  * @defgroup CT_ICR_ALL
325  * @{
326  *****************************************************************************/
327 #define BIT_CT_ALL_INT_CLR			((u32)0x00000001 << 0)		/*Bit[0], bit for clear all interrupt status*/
328 
329 /** @} */
330 
331 /**************************************************************************//**
332  * @defgroup CT_ICR
333  * @{
334  *****************************************************************************/
335 #define BIT_CT_N_NOISE_OVERFLOW_INT_CLR		((u32)0x00000001 << 18)		/*Bit[18], bit for captouch negetive noise interrupt clear*/
336 #define BIT_CT_FIFO_OVERFLOW_INT_CLR			((u32)0x00000001 << 17)		/*Bit[17], bit for captouch fifo overflow interrupt clear*/
337 #define BIT_CT_P_NOISE_OVERFLOW_INT_CLR		((u32)0x00000001 << 16)		/*Bit[16], bit for captouch positive noise interrupt clear*/
338 #define BIT_CT_TOUCH_RELEASE_INT_CLR			((u32)0x0000001f << 8)		/*Bit[12:8], bit for captouch release interrupt clear*/
339 #define BIT_CT_TOUCH_PRESS_INT_CLR			((u32)0x0000001f <<0)		/*Bit[4:0], bit for captouch press interrupt clear*/
340 #define BIT_CT_ALL_INT_CLR_MASK					((u32)0x00071f1f)
341 /** @} */
342 
343 /**************************************************************************//**
344  * @defgroup CT_CHX_CTRL
345  * @{
346  *****************************************************************************/
347 #define BIT_CT_CHX_TOUCH_THRES					((u32)0x00000fff << 16)		/*Bit[27:16], bit for touch difference threshold*/
348 #define BIT_CT_CHX_BASELINE						((u32)0x00000fff << 4)		/*Bit[15:4], bit for touch pad baseline*/
349 #define BIT_CT_CHX_ENABLE							((u32)0x00000001 << 0)		/*Bit[0], bit for touch pad channel enable*/
350 /** @} */
351 
352 /**************************************************************************//**
353  * @defgroup CT_CHX_ATHR
354  * @{
355  *****************************************************************************/
356 #define BIT_MASK_CHX_N_ENT							((u32)0x000000ff << 24)
357 #define BIT_MASK_CHX_P_ENT							((u32)0x000000ff << 16)
358 #define BIT_CT_CHX_TOUCH_ATHRES					((u32)0x00000fff << 0)		/*Bit[11:0], bit for touch difference absolute threshold*/
359 /** @} */
360 
361 /**************************************************************************//**
362  * @defgroup CT_CHX_MBIAS
363  * @{
364  *****************************************************************************/
365 #define BIT_CT_CHX_MBIAS							((u32)0x0000003f << 0)		/*Bit[6], bit for touch key mbias current*/
366 
367 /** @} */
368 
369 /**************************************************************************//**
370  * @defgroup CT_CHX_DATA
371  * @{
372  *****************************************************************************/
373 #define BIT_CT_CHX_POLARITY						((u32)0x00000001 << 28)		/*Bit[28], bit for diff data ploarity*/
374 #define BIT_CT_CHX_DIFF								((u32)0x00000fff <<16)		/*Bit[27:16], bit for diff between average data and baseline*/
375 #define BIT_CT_CHX_DATA							((u32)0x00000fff << 0)		/*Bit[11:0], bit for captouch channelx average data*/
376 /** @} */
377 
378 
379 /** @} */
380 
381 /**
382   * @}
383   */
384 
385 /**
386   * @}
387   */
388 
389 /* Other Definitions --------------------------------------------------------*/
390 
391 
392 #endif
393 
394 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
395