1 /******************************************************************************
2 *
3 * @brief providing APIs for configuring KBI.
4 *
5 *******************************************************************************
6 *
7 * provide APIs for configuring KBI
8 ******************************************************************************/
9 #include "common.h"
10 #include "kbi.h"
11 /******************************************************************************
12 * External objects
13 ******************************************************************************/
14 
15 /******************************************************************************
16 * Global variables
17 ******************************************************************************/
18 KBI_CallbackType KBI_Callback[KBI_MAX_NO] = {(KBI_CallbackType)NULL};
19 
20 /******************************************************************************
21 * Constants and macros
22 ******************************************************************************/
23 
24 /******************************************************************************
25 * Local types
26 ******************************************************************************/
27 
28 /******************************************************************************
29 * Local function prototypes
30 ******************************************************************************/
31 
32 /******************************************************************************
33 * Local variables
34 ******************************************************************************/
35 
36 /******************************************************************************
37 * Local functions
38 ******************************************************************************/
39 /******************************************************************************
40 * KBI api list
41 *
42 *//*! @addtogroup kbi_api_list
43 * @{
44 *******************************************************************************/
45 
46 
47 /******************************************************************************
48 * Global functions
49 ******************************************************************************/
50 
51 /*****************************************************************************//*!
52 *
53 * @brief initialize KBI module.
54 *
55 * @param[in]  pKBI        pointer to KBI module.
56 * @param[in]  pConfig     pointer to KBI configuration structure.
57 *
58 * @return none.
59 *
60 * @ Pass/ Fail criteria: none.
61 *
62 * @see KBI_DeInit.
63 *
64 *****************************************************************************/
KBI_Init(KBI_Type * pKBI,KBI_ConfigType * pConfig)65 void KBI_Init(KBI_Type *pKBI, KBI_ConfigType *pConfig)
66 {
67 #if defined(CPU_NV32)
68     uint16_t    i;
69     uint8_t     sc = 0;
70     uint8_t     u8Port;
71     uint8_t     u8PinPos;
72     uint16_t    u16PinMapping[KBI_MAX_NO][8] =
73     {
74         {
75             0, 1, 2, 3, 8, 9, 10, 11           /* KBI0 pins position in GPIOA register */
76         },
77         {
78             24, 25, 26, 27, 28, 29, 30, 31      /* KBI1 pins position in GPIOA register */
79         }
80     };
81 #elif defined(CPU_NV32M3)
82     uint16_t    i;
83     uint8_t     sc = 0;
84     uint8_t     u8Port;
85     uint8_t     u8PinPos;
86     uint16_t    u16PinMapping[KBI_MAX_NO][8] =
87     {
88         {
89             0, 1, 2, 3, 8, 9, 10, 11           /* KBI0 pins position in GPIOA register */
90         },
91         {
92             20, 21, 16, 17, 18, 19, 12, 13      /* KBI1 pins position in GPIOA register */
93         }
94     };
95 #elif defined(CPU_NV32M4)
96      uint32_t    i;
97      uint32_t     sc = 0;
98      uint32_t     u8Port;
99      uint32_t     u8PinPos;
100 
101      uint32_t    u16PinMapping[KBI_MAX_NO][KBI_MAX_PINS_PER_PORT] =
102     {
103         {/* KBI0P0~KBI0P31 pins position in GPIOA register */
104             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
105         },
106         {/* KBI1P0~KBI1P31 pins position in GPIOB register */
107 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
108         }
109     };
110 #endif
111 
112 
113     if(KBI0 == pKBI)
114     {
115         SIM->SCGC   |= SIM_SCGC_KBI0_MASK;             /* enable clock to KBI0 */\
116         u8Port      =  0;
117     }
118     else if (KBI1 == pKBI)
119     {
120         SIM->SCGC   |= SIM_SCGC_KBI1_MASK;             /* enable clock to KBI1 */
121         u8Port      =  1;
122     }
123 
124     /* mask keyboard interrupts first */
125     sc          = pConfig->sBits.bMode;
126     pKBI->SC    = sc;
127 
128     /* configure KBI pin polarity and others */
129     for (i = 0; i < KBI_MAX_PINS_PER_PORT; i++)
130     {
131         if(pConfig->sPin[i].bEn)
132         {
133             pKBI->PE    |= (1<<i);                      /* enable this KBI pin */
134             pKBI->ES    = (pKBI->ES & ~(1<<i)) | (pConfig->sPin[i].bEdge << i);
135             u8PinPos = u16PinMapping[u8Port][i];
136             ASSERT(!(u8PinPos & 0x80));
137 		#if defined(CPU_NV32)|| defined(CPU_NV32M3)
138             FGPIOA->PIDR  &= ~(1<<u8PinPos);              /* enable GPIO input */
139             FGPIOA->PDDR  &= ~(1<<u8PinPos);              /* configure pin as input */
140             PORT->PUEL  |= (1<<u8PinPos);                 /* enable pullup for the pin */
141         #elif defined(CPU_NV32M4)
142 		   if (u8Port == 0)   /* KBI0 */
143            {
144 		   	FGPIOA->PIDR  &= ~(1<<u8PinPos);              /* enable GPIO input */
145             FGPIOA->PDDR  &= ~(1<<u8PinPos);              /* configure pin as input */
146             PORT->PUE0  |= (1<<u8PinPos);                 /*enable pullup for the pin */
147            }
148 		   else if (u8Port == 1)   /* KBI1 */
149            {
150 		   	FGPIOB->PIDR  &= ~(1<<u8PinPos);              /* enable GPIO input */
151             FGPIOB->PDDR  &= ~(1<<u8PinPos);              /* configure pin as input */
152             PORT->PUE1  |= (1<<u8PinPos);                 /*enable pullup for the pin */
153            }
154 		#endif
155 		}
156     }
157 
158     #if defined(CPU_NV32M4)
159     /*Reset KBI_SP register*/
160 	sc = pConfig->sBits.bRstKbsp<<KBI_SC_RSTKBSP_SHIFT;
161 	pKBI->SC    |= sc;
162 
163     /*Real KBI_SP register enable*/
164 	sc = pConfig->sBits.bKbspEn<<KBI_SC_KBSPEN_SHIFT;
165 	pKBI->SC    |= sc;
166 	#endif
167 
168 	/* write to KBACK to clear any false interrupts */
169     pKBI->SC    = sc;
170 
171     /* enable interrupt if needed */
172     if(pConfig->sBits.bIntEn)
173     {
174         pKBI->SC    |=  KBI_SC_KBIE_MASK;
175 
176         if(KBI0 == pKBI)
177         {
178             NVIC_EnableIRQ(KBI0_IRQn);
179         }
180         else
181         {
182             NVIC_EnableIRQ(KBI1_IRQn);
183         }
184     }
185 }
186 
187 /*****************************************************************************//*!
188 *
189 * @brief set up KBI callback routine.
190 *
191 * @param[in] pKBI          pointer to KBI module.
192 * @param[in] pfnCallback   pointer to callback routine.
193 *
194 * @return none.
195 *
196 * @ Pass/ Fail criteria: none.
197 *
198 *****************************************************************************/
KBI_SetCallback(KBI_Type * pKBI,KBI_CallbackType pfnCallback)199 void KBI_SetCallback(KBI_Type *pKBI, KBI_CallbackType pfnCallback)
200 {
201     if(KBI0 == pKBI)
202     {
203         KBI_Callback[0] = pfnCallback;
204     }
205     else
206     {
207         KBI_Callback[1] = pfnCallback;
208     }
209 }
210 
211 /*****************************************************************************//*!
212 *
213 * @brief deinit the kbi module.
214 *
215 * @param[in]  pKBI       pointer to KBI module.
216 *
217 * @return none.
218 *
219 * @ Pass/ Fail criteria: none.
220 *
221 * @see KBI_Init.
222 *
223 *****************************************************************************/
KBI_DeInit(KBI_Type * pKBI)224 void KBI_DeInit(KBI_Type *pKBI)
225 {
226     if(KBI0 == pKBI)
227     {
228         NVIC_DisableIRQ(KBI0_IRQn);
229     }
230     else
231     {
232         NVIC_DisableIRQ(KBI1_IRQn);
233     }
234 
235     pKBI->PE = 0;
236     pKBI->SC = 0;
237     pKBI->ES = 0;
238 
239     if(KBI0 == pKBI)
240     {
241         SIM->SCGC   &= ~SIM_SCGC_KBI0_MASK;             /* disable clock to KBI0 */
242     }
243     else
244     {
245         SIM->SCGC   &= ~SIM_SCGC_KBI1_MASK;             /* disable clock to KBI1 */
246     }
247 }
248 
249 /*! @} End of acmp_api_list                                                  */
250 
251 /*****************************************************************************//*!
252 *
253 * @brief button group 0 (KBI0) interrupt service routine.
254 *
255 * @param  none.
256 *
257 * @return none.
258 *
259 * @ Pass/ Fail criteria: none.
260 *
261 *****************************************************************************/
262 
KBI0_Isr(void)263 void KBI0_Isr(void)
264 {
265   KBI0->SC |= KBI_SC_KBACK_MASK;                        /* clear interrupt flag */
266 
267   if(KBI_Callback[0])
268   {
269       KBI_Callback[0]();
270   }
271 }
272 
273 
274 
275 /*****************************************************************************//*!
276 *
277 * @brief button group 0 (KBI0) interrupt service routine.
278 *
279 * @param  none.
280 *
281 * @return none.
282 *
283 * @ Pass/ Fail criteria: none.
284 *
285 *****************************************************************************/
286 
KBI1_Isr(void)287 void KBI1_Isr(void)
288 {
289   KBI1->SC |= KBI_SC_KBACK_MASK;                        /* clear interrupt flag */
290 
291   if(KBI_Callback[1])
292   {
293       KBI_Callback[1]();
294   }
295 }
296 
297