1 /**
2 ******************************************************************************
3 * @file rtl8721dlp_keyscan.c
4 * @author
5 * @version V1.0.0
6 * @date 2017-10-16
7 * @brief This file contains all the functions prototypes for the keyscan firmware
8 * library, including the following functionalities of keyscan controller:
9 * - Initialization
10 * - get key press/release status and index
11 * - Keyscan sleep mode control
12 * - Interrupts and flags management
13 ******************************************************************************
14 * @attention
15 *
16 * This module is a confidential and proprietary property of RealTek and
17 * possession or use of this module requires written permission of RealTek.
18 *
19 * Copyright(c) 2017, Realtek Semiconductor Corporation. All rights reserved.
20 ******************************************************************************
21 */
22
23 #include "ameba_soc.h"
24 #include "rtl8721d_keyscan.h"
25
26 /**
27 * @brief Fills each KeyScan_InitStruct member with its default value.
28 * @param KeyScan_InitStruct: pointer to an KeyScan_InitTypeDef structure which will be initialized.
29 * @retval None
30 */
KeyScan_StructInit(KeyScan_InitTypeDef * KeyScan_InitStruct)31 void KeyScan_StructInit(KeyScan_InitTypeDef* KeyScan_InitStruct)
32 {
33 /* Load HAL initial data structure default value */
34 KeyScan_InitStruct->KS_ClkDiv = 49;
35 KeyScan_InitStruct->KS_ColSel = 0x03;
36 KeyScan_InitStruct->KS_RowSel = 0x03;
37
38 KeyScan_InitStruct->KS_WorkMode = KS_REGULAR_SCAN_MODE;
39
40 KeyScan_InitStruct->KS_DebounceCnt = 0x3e7;
41 KeyScan_InitStruct->KS_IntervalCnt = 0x7cf;
42 KeyScan_InitStruct->KS_ReleaseCnt = 0x3e7;
43
44 KeyScan_InitStruct->KS_ThreholdLevel = 8;
45 KeyScan_InitStruct->KS_LimitLevel = 2;
46 KeyScan_InitStruct->KS_OverCtrl = KS_FIFO_OVER_CTRL_DIS_NEW;
47 }
48
49 /**
50 * @brief Initializes the KeyScan peripheral according to the specified
51 * parameters in the KeyScan_InitStruct.
52 * @param KeyScan: selected KeyScan peripheral.
53 * @param KeyScan_InitStruct: pointer to a KeyScan_InitTypeDef structure that contains
54 * the configuration information for the specified KeyScan peripheral.
55 * @retval None
56 */
KeyScan_Init(KEYSCAN_TypeDef * KeyScan,KeyScan_InitTypeDef * KeyScan_InitStruct)57 void KeyScan_Init(KEYSCAN_TypeDef *KeyScan, KeyScan_InitTypeDef* KeyScan_InitStruct)
58 {
59 /* Check the parameters */
60 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
61 assert_param(KeyScan_InitStruct->KS_ClkDiv <= 0xfff);
62 assert_param((KeyScan_InitStruct->KS_ColSel >= 0x1) && (KeyScan_InitStruct->KS_ColSel <= 0xff));
63 assert_param((KeyScan_InitStruct->KS_RowSel >= 0x1) && (KeyScan_InitStruct->KS_RowSel <= 0xff));
64 assert_param(KeyScan_InitStruct->KS_DebounceCnt <= 0xfff);
65 assert_param(KeyScan_InitStruct->KS_IntervalCnt <= 0xfff);
66 assert_param(KeyScan_InitStruct->KS_ReleaseCnt <= 0xfff);
67 assert_param(IS_KS_WORK_MODE(KeyScan_InitStruct->KS_WorkMode));
68 assert_param(IS_KS_FIFO_OVER_CTRL(KeyScan_InitStruct->KS_OverCtrl));
69 assert_param(IS_KS_FIFO_LIMIT_LEVEL(KeyScan_InitStruct->KS_LimitLevel));
70 assert_param(IS_KS_FIFO_TH_LEVEL(KeyScan_InitStruct->KS_ThreholdLevel));
71
72 /* Disable the IC first */
73 KeyScan->KS_CTRL &= (~BIT_KS_RUN_ENABLE);
74
75 /* Mask all keyscan interrupt */
76 KeyScan->KS_IMR &= (~BIT_KS_ALL_INT_MSK);
77
78 /* clock divider config */
79 KeyScan->KS_CLK_DIV &= (~BIT_KS_CLK_DIV);
80 KeyScan->KS_CLK_DIV |= KeyScan_InitStruct->KS_ClkDiv;
81
82 /* Config scan mode*/
83 KeyScan->KS_CTRL &= (~BIT_KS_WORK_MODE);
84 KeyScan->KS_CTRL |= KeyScan_InitStruct->KS_WorkMode;
85
86 KeyScan->KS_FIFO_CFG = (KeyScan_InitStruct->KS_OverCtrl) | (KeyScan_InitStruct->KS_ThreholdLevel << 16) |\
87 (KeyScan_InitStruct->KS_LimitLevel << 24);
88
89 /* time count config */
90 KeyScan->KS_TIM_CFG0 &= (~BIT_KS_DEB_TIMER);
91 KeyScan->KS_TIM_CFG0 |= KeyScan_InitStruct->KS_DebounceCnt;
92 KeyScan->KS_TIM_CFG1 = (KeyScan_InitStruct->KS_IntervalCnt << 16) | KeyScan_InitStruct->KS_ReleaseCnt;
93
94 /* Set col map, config which col to work */
95 KeyScan->KS_COL_CFG = KeyScan_InitStruct->KS_ColSel;
96
97 /* Set row map, config which row to work */
98 KeyScan->KS_ROW_CFG = KeyScan_InitStruct->KS_RowSel;
99
100 /* clear all interrupt status and status flag */
101 KeyScan->KS_ICR |= BIT_KS_ALL_INT_CLR;
102
103 return;
104 }
105
106 /**
107 * @brief Enables or disables the specified KeyScan interrupts mask.
108 * @param KeyScan: selected KeyScan peripheral.
109 * @param KeyScan_IT: specifies the KeyScan interrupt sources to be enabled or masked.
110 * This parameter can be one or combinations of the following values:
111 * @arg BIT_KS_SCAN_EVENT_INT_MSK: Mask Scan event interrupt status
112 * @arg BIT_KS_FIFO_LIMIT_INT_MSK: Mask FIFO limit interrupt status
113 * @arg BIT_KS_FIFO_OVERFLOW_INT_MSK: Mask FIFO overflow interrupt
114 * @arg BIT_KS_FIFO_FULL_INT_MSK: Mask FIFO full interrupt
115 * @arg BIT_KS_SCAN_FINISH_INT_MSK: Mask scan finish interrupt
116 * @arg BIT_KS_FIFO_NOTEMPTY_INT_MSK: Mask FIFO nonempty interrupt
117 * @arg BIT_KS_ALL_RELEASE_INT_MSK: Mask All Release interrupt
118 * @param NewState: new state of the specified KeyScan interrupts mask.
119 * This parameter can be: ENABLE or DISABLE.
120 * @retval None
121 */
KeyScan_INTConfig(KEYSCAN_TypeDef * KeyScan,uint32_t KeyScan_IT,u8 newState)122 void KeyScan_INTConfig(KEYSCAN_TypeDef *KeyScan, uint32_t KeyScan_IT, u8 newState)
123 {
124 /* Check the parameters */
125 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
126
127 if (newState == ENABLE) {
128 /* Enable KeyScan interrupts */
129 KeyScan->KS_IMR |= KeyScan_IT;
130 } else {
131 /* Disable KeyScan interrupts */
132 KeyScan->KS_IMR &= (~KeyScan_IT);
133 }
134 }
135
136 /**
137 * @brief Clears the specified KeyScan interrupt pending bit.
138 * @param KeyScan: selected KeyScan peripheral.
139 * @param KeyScan_IT: specifies the KeyScan interrupt to be cleared.
140 * This parameter can be one or combinations of the following values:
141 * @arg BIT_KS_FIFO_LIMIT_INT_STATUS: FIFO limit interrupt status
142 * @arg BIT_KS_FIFO_OVERFLOW_INT_STATUS: FIFO overflow interrupt status
143 * @arg BIT_KS_SCAN_FINISH_INT_STATUS: Scan finish interrupt status
144 * @arg BIT_KS_ALL_RELEASE_INT_STATUS: All Release interrupt status
145 * @note BIT_KS_SCAN_EVENT_INT_STATUS is automatically cleared by hardware when the data is read.
146 * BIT_KS_FIFO_FULL_INT_STATUS is automatically cleared by hardware when the buffer level
147 * goes below the BIT_KS_FIFO_THREHOLD_LEVEL threshold.
148 * BIT_KS_FIFO_NOTEMPTY_INT_STATUS is automatically cleared by hardware when the FIFO is empty.
149 * @retval None
150 */
KeyScan_ClearINT(KEYSCAN_TypeDef * KeyScan,u32 KeyScan_IT)151 void KeyScan_ClearINT(KEYSCAN_TypeDef *KeyScan, u32 KeyScan_IT)
152 {
153 /* Check the parameters */
154 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
155
156 KeyScan->KS_ICR |= KeyScan_IT;
157 }
158
159 /**
160 * @brief Get KeyScan Raw Interrupt Status.
161 * @param KeyScan: selected KeyScan peripheral.
162 * @retval raw interrupt status
163 */
KeyScan_GetRawINT(KEYSCAN_TypeDef * KeyScan)164 u32 KeyScan_GetRawINT(KEYSCAN_TypeDef *KeyScan)
165 {
166 /* Check the parameters */
167 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
168
169 return KeyScan->KS_ISR_RAW;
170 }
171
172 /**
173 * @brief Get KeyScan interrupt status.
174 * @param KeyScan: selected KeyScan peripheral.
175 * @retval interrupt status
176 */
KeyScan_GetINT(KEYSCAN_TypeDef * KeyScan)177 u32 KeyScan_GetINT(KEYSCAN_TypeDef *KeyScan)
178 {
179 /* Check the parameters */
180 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
181
182 return KeyScan->KS_ISR;
183 }
184
185 /**
186 * @brief Get data number of keyscan FIFO.
187 * @param KeyScan: selected KeyScan peripheral.
188 * @retval data number of keyscan FIFO
189 */
KeyScan_GetDataNum(KEYSCAN_TypeDef * KeyScan)190 u8 KeyScan_GetDataNum(KEYSCAN_TypeDef *KeyScan)
191 {
192 /* Check the parameters */
193 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
194
195 return (KeyScan->KS_DATA_NUM & BIT_KS_FIFO_DATA_LEVEL);
196 }
197
198 /**
199 * @brief Clears the FIFO data.
200 * @param KeyScan: selected KeyScan peripheral.
201 * @retval None
202 */
KeyScan_ClearFIFOData(KEYSCAN_TypeDef * KeyScan)203 void KeyScan_ClearFIFOData(KEYSCAN_TypeDef *KeyScan)
204 {
205 u32 TempVal;
206
207 /* Check the parameters */
208 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
209
210 TempVal = KeyScan->KS_FIFO_CFG;
211 KeyScan->KS_FIFO_CFG = (TempVal | BIT_KS_FIFO_CLR);
212 }
213
214 /**
215 * @brief Get status of keyscan FIFO.
216 * @param KeyScan: selected KeyScan peripheral.
217 * @param KeyScan_Flag: specifies the flag to check.
218 * This parameter can be one of the following values:
219 * @arg BIT_KS_FIFO_FULL
220 * @arg BIT_KS_FIFO_EMPTY
221 * @retval the new statue of the specified flag.
222 * This parameter can be: _TRUE or _FALSE.
223 */
KeyScan_GetFIFOState(KEYSCAN_TypeDef * KeyScan,u32 KeyScan_Flag)224 BOOL KeyScan_GetFIFOState(KEYSCAN_TypeDef *KeyScan, u32 KeyScan_Flag)
225 {
226 /* Check the parameters */
227 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
228
229 if ((KeyScan->KS_DATA_NUM & KeyScan_Flag) != 0) {
230 return _TRUE;
231 } else {
232 return _FALSE;
233 }
234 }
235
236 /**
237 * @brief Read data from keyscan FIFO.
238 * @param KeyScan: selected KeyScan peripheral.
239 * @param *outBuf: buffer to save data read from KeyScan FIFO.
240 * @param count: number of data to be read.
241 * @retval None
242 */
KeyScan_Read(KEYSCAN_TypeDef * KeyScan,u32 * outBuf,u8 count)243 void KeyScan_Read(KEYSCAN_TypeDef *KeyScan, u32 *outBuf, u8 count)
244 {
245 u32 TempVal;
246
247 /* Check the parameters */
248 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
249
250 for (TempVal = 0; TempVal < count; TempVal++) {
251 *outBuf++ = KeyScan->KS_DATA_ENTRY;
252 }
253
254 return;
255 }
256
257 /**
258 * @brief Enables or disables the specified KeyScan peripheral.
259 * @param KeyScan: selected KeyScan peripheral.
260 * @param NewState: new state of the KeyScan peripheral.
261 * This parameter can be: ENABLE or DISABLE.
262 * @retval None
263 */
KeyScan_Cmd(KEYSCAN_TypeDef * KeyScan,u8 NewState)264 void KeyScan_Cmd(KEYSCAN_TypeDef *KeyScan, u8 NewState)
265 {
266 /* Check the parameters */
267 assert_param(IS_KEYSCAN_ALL_PERIPH(KeyScan));
268
269 if (NewState != DISABLE) {
270 /* Enable the KeyScan peripheral */
271 KeyScan->KS_CTRL |= BIT_KS_RUN_ENABLE;
272 } else {
273 /* Disable the KeyScan peripheral */
274 KeyScan->KS_CTRL &= (~BIT_KS_RUN_ENABLE);
275 }
276 }
277 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
278