]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/pdk.git/blob - packages/ti/board/diag/rotary_switch/src/rotary_switch.c
board-rtos: add to PDK
[processor-sdk/pdk.git] / packages / ti / board / diag / rotary_switch / src / rotary_switch.c
1 /*
2  * Copyright (c) 2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the 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 "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
34 /**
35  *
36  * \file  rotary_switch_test.c
37  *
38  * \brief This file contains rotary switch test functions.
39  *
40  ******************************************************************************/
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include <ti/drv/i2c/I2C.h>
47 #include <ti/drv/i2c/soc/I2C_soc.h>
48 #include <ti/drv/uart/UART_stdio.h>
50 #include <ti/csl/soc.h>
52 #include "diag_common_cfg.h"
54 #include "board.h"
55 #include "board_cfg.h"
57 #if defined(SOC_AM65XX)
58 /* Platform test return type */
59 typedef int16_t TEST_STATUS;
60 /* Platform test return codes */
61 #define TEST_PASS     (0)
62 #define TEST_FAIL     (-1)
63 #endif
65 extern const I2C_Config I2C_config[];
66 extern void BOARD_delay(uint32_t usecs);
69 /**
70  *  \brief    This function executes rotary switch detection test
71  *
72  *  \param    void
73  *
74  * \return
75  * \n         TEST_PASS  - Test Passed
76  * \n         TEST_FAIL  - Test Failed
77  */
78 static TEST_STATUS rotary_switch_detect_test(void)
79 {
80         uint32_t index;
81         I2C_Params i2cParams;
82     I2C_Handle handle = NULL;
83         uint8_t tx;
84         uint8_t rx;
85         I2C_Transaction i2cTransaction;
86         
87         for (index=0; I2C_config[index].fxnTablePtr != NULL; index++)
88         {
89                 ((I2C_HwAttrs *)I2C_config[index].hwAttrs)->enableIntr = false;
90         }
91         
92         I2C_init();
94     I2C_Params_init(&i2cParams);
95 #if defined (DIAG_COMPLIANCE_TEST)
96          i2cParams.bitRate = I2C_400kHz; /* 400KHz */
97 #endif
98     handle = I2C_open(BOARD_ROTARY_SWICH_INSTANCE, &i2cParams);
99         if(handle == NULL)
100         {
101                 UART_printf("I2C Handle open failed");
102                 return(TEST_FAIL);
103         }
104         
105     I2C_transactionInit(&i2cTransaction);
106         i2cTransaction.slaveAddress = BOARD_I2C_ROTARY_SWITCH;
107         i2cTransaction.writeBuf = &tx;
108         i2cTransaction.writeCount = 1;
109         i2cTransaction.readBuf = &rx;
110         i2cTransaction.readCount = 0;
111         tx = 0x0;
112         rx = 0x0;
113         I2C_transfer(handle, &i2cTransaction);
115     BOARD_delay(1000);
117         i2cTransaction.writeCount = 0;
118         i2cTransaction.readCount = 1;
119         
120         I2C_transfer(handle, &i2cTransaction);
121         if(rx == 0x0)
122     {
123         UART_printf ("Rotary switch Detection Failed!\n");
124         return(TEST_FAIL);
125     }
127     UART_printf ("\nRotary switch Detection Successful!\n");
129         I2C_close(handle);
130     return (TEST_PASS);
133 /**
134  *  \brief    This function executes rotary switch position indicator test
135  *
136  *  \param    void
137  *
138  * \return
139  * \n         TEST_PASS  - Test Passed
140  * \n         TEST_FAIL  - Test Failed
141  */
142 static TEST_STATUS rotary_switch_position_test(void)
144         uint32_t index;
145         I2C_Params i2cParams;
146     I2C_Handle handle = NULL;
147         TEST_STATUS testStatus;
148         uint8_t  regRdBuf;
149         uint8_t  regWrBuf;
150         I2C_Transaction i2cTransaction;
151         
152         for (index=0; I2C_config[index].fxnTablePtr != NULL; index++)
153         {
154                 ((I2C_HwAttrs *)I2C_config[index].hwAttrs)->enableIntr = false;
155         }
156         
157         I2C_init();
159     I2C_Params_init(&i2cParams);
161     handle = I2C_open(BOARD_ROTARY_SWICH_INSTANCE, &i2cParams);
162         if(handle == NULL)
163         {
164                 UART_printf("I2C Handle open failed");
165                 return(TEST_FAIL);
166         }
167         
168     I2C_transactionInit(&i2cTransaction);
169         i2cTransaction.slaveAddress = BOARD_I2C_ROTARY_SWITCH;
170         i2cTransaction.writeBuf = &regWrBuf;
171         i2cTransaction.writeCount = 0;
172         i2cTransaction.readBuf = &regRdBuf;
173         i2cTransaction.readCount = 1;
174         regWrBuf = 0x0;
175         regRdBuf = 0x0;
176         I2C_transfer(handle, &i2cTransaction);
178         BOARD_delay(1000);
179         
180         /*Make the higher nibble of Input Port Register to 0 to reset don't care bits*/
181         regRdBuf = regRdBuf & 0x0f;
182         if(regRdBuf == 0x01)
183         {
184                 UART_printf("The rotary switch is at position 1\n");
185                 I2C_close(handle);
186                 testStatus = TEST_PASS;
187         }
188         else if(regRdBuf == 0x02)
189         {
190                 UART_printf("The rotary switch is at position 2\n");
191                 I2C_close(handle);
192                 testStatus = TEST_PASS;
193         }
194         else if(regRdBuf == 0x03)
195         {
196                 UART_printf("The rotary switch is at position 3\n");
197                 I2C_close(handle);
198                 testStatus = TEST_PASS;
199         }
200         else if(regRdBuf == 0x04)
201         {
202                 UART_printf("The rotary switch is at position 4\n");
203                 I2C_close(handle);
204                 testStatus = TEST_PASS;
205         }
206         else if(regRdBuf == 0x05)
207         {
208                 UART_printf("The rotary switch is at position 5\n");
209                 I2C_close(handle);
210                 testStatus = TEST_PASS;
211         }
212         else if(regRdBuf == 0x06)
213         {
214                 UART_printf("The rotary switch is at position 6\n");
215                 I2C_close(handle);
216                 testStatus = TEST_PASS;
217         }
218         else if(regRdBuf == 0x07)
219         {
220                 UART_printf("The rotary switch is at position 7\n");
221                 I2C_close(handle);
222                 testStatus = TEST_PASS;
223         }
224         else if(regRdBuf == 0x08)
225         {
226                 UART_printf("The rotary switch is at position 8\n");
227                 I2C_close(handle);
228                 testStatus = TEST_PASS;
229         }
230         else if(regRdBuf == 0x09)
231         {
232                 UART_printf("The rotary switch is at position 9\n");
233                 I2C_close(handle);
234                 testStatus = TEST_PASS;
235         }
236         else if(regRdBuf == 0x00)
237         {
238                 UART_printf("The rotary switch is at position 0\n");
239                 I2C_close(handle);
240                 testStatus = TEST_PASS;
241         }
242         else
243         {
244                 UART_printf("Unknown position Rotary switch position test failed\n");
245                 I2C_close(handle);
246                 testStatus = TEST_FAIL;
247         }
249         return(testStatus);
252 /**
253  *  \brief    This function is used to perform Rotary switch detection
254  *            test and Rotary switch position test
255  *
256  *  \param    void
257  *
258  * \return
259  * \n         TEST_PASS  - Test Passed
260  * \n         TEST_FAIL  - Test Failed
261  */
262 static TEST_STATUS run_rotary_switch_test(void)
264         TEST_STATUS testStatus;
266     UART_printf("\nRunning Rotary switch Detect Test\n");
268     testStatus = rotary_switch_detect_test();
269     if(testStatus != TEST_PASS)
270         {
271                 UART_printf("\nRotary switch Detect Test Failed!!\n");
272                 return (testStatus);
273         }
274         else
275         {
276                 UART_printf("\nRotary switch Detect Test Passed!\n");
277         }
278         
279     UART_printf("\nRunning Rotary switch position Test\n\n");
281     testStatus = rotary_switch_position_test();
282     if(testStatus != TEST_PASS)
283     {
284             UART_printf("\nRotary switch position Test Failed!!\n");
285                 return (testStatus);
286         }
287         else
288         {
289                 UART_printf("\nRotary switch position Test Passed!\n");
290         }
292     return (testStatus);
295 /**
296  * \brief This function performs Rotary switch Test test
297  *
298  * \param void
299  *
300  * \return
301  * \n      TEST_PASS  - Test Passed
302  * \n      TEST_FAIL  - Test Failed
303  *
304  */
305 static TEST_STATUS rotaryswitchTest(void)
307         TEST_STATUS testStatus = 0;
308         int8_t p = 'r';
310         UART_printf("\n********************************\n");
311         UART_printf(  "       ROTARY SWITCH Test       \n");
312         UART_printf(  "********************************\n");
314         do {
315                 testStatus = run_rotary_switch_test();
316                 UART_printf("\nPress 'r' to run the test again,\n");
317                 UART_printf("or any other character to exit: ");
318 #if defined (DIAG_COMPLIANCE_TEST)
319                 p='y';
320 #else
321                 UART_scanFmt("%c", &p);
322 #endif
323         if (p == 'r') UART_printf("\nRunning rotary switch test again\n");
324         } while (p == 'r');
325         if(testStatus != 0)
326         {
327                 UART_printf("\nRotary switch Test Failed!\n");
328         }
329         else
330         {
331                 UART_printf("\nRotary switch Test Passed!\n");
332         }
334         UART_printf("\nRotary switch Tests Completed!!\n");
335         UART_printf("\n-----------------X-----------------\n\n\n");
337         return (testStatus);
339 } // rotaryswitchTest
341 /**
342  * \brief Invokes rotary switch test functions
343  *
344  */
345 int main(void)
347     TEST_STATUS      testStatus;
348     Board_initCfg    boardCfg;
350 #ifdef SOC_K2G
351     DIAG_IntrInit();
352 #endif
354 #ifdef PDK_RAW_BOOT
355     boardCfg = BOARD_INIT_PINMUX_CONFIG |
356                BOARD_INIT_UART_STDIO;
357 #else
358     boardCfg = BOARD_INIT_PINMUX_CONFIG | BOARD_INIT_UART_STDIO;
359 #endif
360     Board_init(boardCfg);
362 #if defined(SOC_AM65XX) && !defined (__aarch64__)
363     /* MCU I2C instance will be active by default for R5 core.
364      * Need update HW attrs to enable MAIN I2C instance.
365      */
366     enableMAINI2C(0, CSL_I2C0_CFG_BASE);
367 #endif
369     /* Invoke Rotary switch Test */
370     testStatus = rotaryswitchTest();
371     if(testStatus != TEST_PASS)
372     {
373         return (-1);
374     }
376     return (0);
378 /* Nothing past this point */