]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/pdk.git/blob - packages/ti/board/diag/button/src/button_test.c
66d9e235e73607e3d64430aba25253b83e33a7b7
[processor-sdk/pdk.git] / packages / ti / board / diag / button / src / button_test.c
1 /*
2  * Copyright (C) 2016-2020 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
34 /**
35  * \file   button_test.c
36  *
37  * \brief  Push button diagnostic test file
38  *
39  *  Targeted Functionality: Verification of basic functionality of
40  *  available push buttons.
41  *
42  *  Operation: This application demonstrates the working of GPIO based button
43  *             keypad. It waits for a key press event from the button keypad.
44  *             On a key press the diagnostic test sequentially moves to the
45  *             other buttons in the keypad.
46  *
47  *  Supported SoCs: AM335x, AM437x, AM65xx, TPR12,AM64x.
48  *
49  *  Supported Platforms: skAM335x, skAM437x, evmAM437x am65xx_evm, am65xx_idk,
50  *                       tpr12_evm,am64x_evm.
51  *
52  */
54 #include "button_test.h"
56 #if defined(SOC_TPR12)
57 extern GPIO_v2_Config GPIO_v2_config;
58 #endif
60 uint32_t buttonOffset[2]={0, 0};
61 uint32_t powerOffset[2]={0, 0};
62 uint32_t buttonStart[2]={0, 0};
64 /* Variable to hold the GPIO key info. */
65 gpioInfo_t KeyScn[4];
67 /**
68  *  \brief   Push Button diagnostic test main function
69  *
70  *  \return  int - Push Button diagnostic test status.
71  *            S_PASS - if the board id matches
72  *            E_FAIL - if the board id fails.
73  *
74  */
75 int main(void)
76 {
77     int status = S_PASS;
78 #if (defined(SOC_AM65XX) || defined(SOC_TPR12) || defined(SOC_AM64X))
79     Board_IDInfo_v2 info;
80 #else
81     Board_IDInfo boardInfo;
82 #endif
83     keyPadInfo_t boardKeyPad;
84     Board_initCfg boardCfg;
86 #ifdef SOC_K2G
87     DIAG_IntrInit();
88 #endif
90 #ifdef PDK_RAW_BOOT
91     boardCfg = BOARD_INIT_MODULE_CLOCK |
92                BOARD_INIT_PINMUX_CONFIG |
93                BOARD_INIT_UART_STDIO;
94 #else
95     boardCfg = BOARD_INIT_UART_STDIO;
96 #endif
97     Board_init(boardCfg);
99     UART_printf("\n*********************************************\n"); 
100     UART_printf  ("*                 Button Test               *\n");
101     UART_printf  ("*********************************************\n");
103 #if (defined(SOC_AM65XX) || defined(SOC_TPR12) || defined(SOC_AM64X))
104     Board_getIDInfo_v2(&info, BOARD_I2C_EEPROM_ADDR);
105 #else
106     Board_getIDInfo(&boardInfo);
107 #endif
109     /* Update the KeyPad information. */
110 #if (defined(SOC_AM65XX) || defined(SOC_TPR12) || defined(SOC_AM64X))
111     status = BoardDiag_GetKeyPadInfo(info.boardInfo.boardName, &boardKeyPad);
112 #else
113     status = BoardDiag_GetKeyPadInfo(boardInfo.boardName, &boardKeyPad);
114 #endif
115     if(S_PASS == status)
116     {
117         status = BoardDiag_ButtonTest(&boardKeyPad);
119         if(S_PASS == status)
120         {
121             UART_printf("Test PASSED! \n");
122         }
123         else
124         {
125             UART_printf("Test Failed!\n");
126         }
127     }
129     return status;
132 /**
133  * \brief   This function is used enable the power key by making the power
134  *          key GPIO line high.
135  *
136  * \param   *pBoardKeyPad     [IN]      Structure pointer consisting of the
137  *                                      Keypad information of the board
138  * \param   num               [IN]      Power key number.
139  *
140  * \retval  S_PASS  if the board id matches
141  * \retval  E_FAIL  if the board id fails.
142  *
143  */
144 void BoardDiag_BtnPwr(keyPadInfo_t *pBoardKeyPad, uint32_t num)
146     uint32_t i = 0U;
148     for(i = 0; i < pBoardKeyPad->pwrKeyNum; i++)
149     {
150         if(i == num)
151         {
152             GPIO_write(pBoardKeyPad->pwrKeyIdx + i, GPIO_PIN_VAL_HIGH);
153         }
154         else
155         {
156             GPIO_write(pBoardKeyPad->pwrKeyIdx + i, GPIO_PIN_VAL_LOW);
157         }
158     }
161 /**
162  * \brief   This function is used to perform the push button diagnostic test.
163  *
164  * \param   *pBoardKeyPad     [IN]      Structure pointer consisting of the
165  *                                      Keypad information of the board.
166  *
167  * \retval  S_PASS  if the board id matches
168  * \retval  E_FAIL  if the board id fails.
169  *
170  */
171 int32_t BoardDiag_ButtonTest(keyPadInfo_t *pBoardKeyPad)
173     uint32_t retVal = S_PASS;
174     uint32_t i, j, k;
176     /* Initialize the GPIO driver. */
177     UART_printf("\nRunning button test...\n");
178 #if defined(SOC_AM65XX)
179     GPIO_v0_HwAttrs gpioCfg;
180     GPIO_socGetInitCfg(0, &gpioCfg);
181     gpioCfg.baseAddr = CSL_WKUP_GPIO0_BASE;
182     GPIO_socSetInitCfg(0, &gpioCfg);
183     GPIO_init();
184 #elif defined(SOC_AM64X)
185     GPIO_init();
187     GPIO_v0_HwAttrs gpioCfg;
188     GPIO_socGetInitCfg(1, &gpioCfg);
189     gpioCfg.baseAddr = CSL_MCU_GPIO0_BASE;
190     GPIO_socSetInitCfg(1, &gpioCfg);
191     GPIO_init();
192 #else
193 #if defined(SOC_TPR12)
194     GPIO_v2_updateConfig(&GPIO_v2_config);
195 #endif
196     GPIO_init();
197 #endif
199     for(k=0; k < pBoardKeyPad->buttonSet; k++)
200     {
201         for(i=0; i< pBoardKeyPad->pwrKeyNum; i++)
202         {
203             for(j=0; j < pBoardKeyPad->scnKeyNum; j++)
204             {
205                 retVal = BoardDiag_KeyPressCheck(pBoardKeyPad,
206                                                  buttonStart[k] + (i*powerOffset[k]) + (j*buttonOffset[k]),
207                                                  j, i);
208             }
209         }
210     }
212     return retVal;
215 /**
216  * \brief   This is used for keep on checking for a specified button to
217  *          be pressed.
218  *
219  * \param   *pBoardKeyPad     [IN]      Structure pointer consisting of the
220  *                                      Keypad information of the board.
221  * \param   button            [IN]      Push button switch number.
222  * \param   scnKey            [IN]      Push button switch index.
223  * \param   pwrKey            [IN]      Power key number.
224  *
225  * \retval  S_PASS  if the board id matches
226  * \retval  E_FAIL  if the board id fails.
227  *
228  */
229 int32_t BoardDiag_KeyPressCheck(keyPadInfo_t *pBoardKeyPad,
230                                 uint32_t button,
231                                 uint32_t scnKey,
232                                 uint32_t pwrKey)
234     int32_t ret_val=0;
235     uint32_t level = 0U;
236 #if defined (DIAG_COMPLIANCE_TEST)
237     UART_printf(  "Button SW %2d            ", button);
239     level = GPIO_read(scnKey);
241     UART_printf("Button SW %2d Value - %d\n", button, level);
242     ret_val = S_PASS;
244     return ret_val;
245 #else
246     UART_printf(  "Button SW %2d            ", button);
247     UART_printf("WAIT      Waiting for button press");
249 #if (defined(SOC_AM65XX) || defined(SOC_TPR12) || defined(SOC_AM64X))
250     do
251     {
252 #if defined(SOC_TPR12)
253         level = GPIO_read(GPIO_v2_config.pinConfigs[scnKey].pinIndex);
254 #else
255         level = GPIO_read(scnKey);
256 #endif
257 #if defined(SOC_TPR12)
258     }while(level == 0);
259 #else
260     }while(level == 1);
261 #endif
262     UART_printf("...   Button %2d Pressed \n", button);
264     UART_printf(  "Button SW %2d            ", button);
265     UART_printf("WAIT      Waiting for button release...");
266     do
267     {
268 #if defined(SOC_TPR12)
269         level = GPIO_read(GPIO_v2_config.pinConfigs[scnKey].pinIndex);
270 #else
271         level = GPIO_read(scnKey);
272 #endif
273 #if defined(SOC_TPR12)
274     }while(level == 1);
275 #else
276     }while(level == 0);
277 #endif
278     UART_printf(" Button %2d released\n", button);
279 #else
280     BoardDiag_BtnPwr(pBoardKeyPad, pwrKey);
281     do
282     {
283         level = (GPIO_read(scnKey));
284     }while(level != 1);
285 #endif
287     UART_printf("Button SW %2d            ", button);
289 #if (!(defined(SOC_AM65XX) || defined(SOC_TPR12) || defined(SOC_AM64X)))
290     if(level != 1)
291     {
292         UART_printf("FAIL                                            \n");
293         ret_val = E_FAIL;
294     }
295     else
296     {
297         UART_printf("PASS                                            \n");
298         ret_val = S_PASS;
299     }
300 #else
301     UART_printf("PASS                                            \n");
302     ret_val = S_PASS;
303 #endif
304     return ret_val;
305 #endif
308 /**
309  * \brief   Get the Board keypad information as per the board Id.
310  *
311  * \param   *pBoardName       [IN]      Pointer to hold the board name
312  * \param   *pBoardKeyPad     [OUT]     Structure pointer for holding the
313  *                                      Keypad information of the board.
314  *
315  * \retval  S_PASS  if the board id matches
316  * \retval  E_FAIL  if the board id fails.
317  *
318  */
319 int32_t BoardDiag_GetKeyPadInfo(char *pBoardName, keyPadInfo_t *pBoardKeyPad)
321     int32_t status = S_PASS;
323     /* Check if the board is AM437x GPEVM by comparing the string
324        read from EEPROM. */
325     if (strncmp("AM43__GP", pBoardName, BOARD_NAME_LENGTH) == 0U)
326     {
327         pBoardKeyPad->buttonSet = 1;
328         pBoardKeyPad->scnKeyNum = 3;
329         pBoardKeyPad->pwrKeyNum = 2;
330         pBoardKeyPad->pwrKeyIdx = 3;
331         buttonStart[0]=4;
332         buttonOffset[0]=1;
333         powerOffset[0]=3;
334         /* Update the GPIO data for keypad inputs. */
335         KeyScn[0].instance=3;
336         KeyScn[0].pin=21;
338         KeyScn[1].instance=4;
339         KeyScn[1].pin=3;
341         KeyScn[2].instance=4;
342         KeyScn[2].pin=2;
343     }
344     /* Check if the board is SKAM437x by comparing the string read
345        from EEPROM. */
346     else if (strncmp("AM43__SK", pBoardName, BOARD_NAME_LENGTH) == 0U)
347     {
348         pBoardKeyPad->buttonSet = 1;
349         pBoardKeyPad->scnKeyNum = 2;
350         pBoardKeyPad->pwrKeyNum = 2;
351         pBoardKeyPad->pwrKeyIdx = 2;
352         buttonStart[0]=6;
353         buttonOffset[0]=1;
354         powerOffset[0]=2;
356         /* Update the GPIO data for keypad inputs. */
357         KeyScn[0].instance=5;
358         KeyScn[0].pin=5;
360         KeyScn[1].instance=5;
361         KeyScn[1].pin=6;
362     }
363     /* Check if the board is SKAM335x by comparing the string read
364        from EEPROM. */
365     else if (strncmp("A335X_SK", pBoardName, BOARD_NAME_LENGTH) == 0U)
366     {
367         pBoardKeyPad->buttonSet = 1;
368         pBoardKeyPad->scnKeyNum = 4;
369         pBoardKeyPad->pwrKeyNum = 1;
370         pBoardKeyPad->pwrKeyIdx = 0;
371         buttonStart[0]=1;
372         buttonOffset[0]=1;
373         powerOffset[0]=1;
375         /*
376         ** Note: For skAM335x board the order of key press on the keypad should
377                  be in the following order
378         1: Key1: SW3 corresponding to SW3 switch on board.
379         2: Key2: SW1 corresponding to SW1 switch on board.
380         3: Key3: SW2 corresponding to SW2 switch on board.
381         4: Key4: SW4 corresponding to SW4 switch on board.
382         */
383         KeyScn[0].instance=0;
384         KeyScn[0].pin=30;
386         KeyScn[1].instance=2;
387         KeyScn[1].pin=3;
389         KeyScn[2].instance=2;
390         KeyScn[2].pin=2;
392         KeyScn[3].instance=2;
393         KeyScn[3].pin=5;
394     }
395     /* Check if the board is EVM AM65XX by comparing the string read from
396        EEPROM. */
397     else if (strncmp("AM6-COMP", pBoardName, BOARD_NAME_LENGTH) == 0U)
398     {
400         pBoardKeyPad->buttonSet = 1;
401         pBoardKeyPad->scnKeyNum = 2;
402         pBoardKeyPad->pwrKeyNum = 1;
403         pBoardKeyPad->pwrKeyIdx = 0;
404         buttonStart[0]=5;
405         buttonOffset[0]=1;
406         powerOffset[0]=1;
408         /* Update the GPIO data for keypad inputs. */
409         KeyScn[0].instance=0;
410         KeyScn[0].pin=24;
412         KeyScn[1].instance=0;
413         KeyScn[1].pin=27;
414     }
415 #if defined(SOC_TPR12)
416     /* TODO: Need to update after programming board ID EEPROM
417      * Check if the board is EVM TPR12 by comparing the string read from
418        EEPROM. */
419     else if(status == S_PASS)
420     {
422         pBoardKeyPad->buttonSet = 1;
423         pBoardKeyPad->scnKeyNum = 1;
424         pBoardKeyPad->pwrKeyNum = 1;
425         pBoardKeyPad->pwrKeyIdx = 0;
426         buttonStart[0]=2;
427         buttonOffset[0]=1;
428         powerOffset[0]=1;
430         /* Update the GPIO data for keypad inputs. */
431         KeyScn[0].instance=0;
432         KeyScn[0].pin=28;
434     }
435 #endif
436     /* Check if the board is EVM AM64X by comparing the string read from
437        EEPROM. */
438     else if (strncmp("AM64-COMP", pBoardName, BOARD_NAME_LENGTH) == 0U)
439     {
441         pBoardKeyPad->buttonSet = 1;
442         pBoardKeyPad->scnKeyNum = 2;
443         pBoardKeyPad->pwrKeyNum = 1;
444         pBoardKeyPad->pwrKeyIdx = 0;
445         buttonStart[0]=5;
446         buttonOffset[0]=1;
447         powerOffset[0]=1;
449         /* Update the GPIO data for keypad inputs. */
450         KeyScn[0].instance=1;
451         KeyScn[0].pin=43;
453         KeyScn[1].instance=0;
454         KeyScn[1].pin=6;
455     }
456     else
457     {
458         status = E_FAIL;
459         UART_printf("The test is not supported on this board\n");
460     }
462     return status;