[processor-sdk/performance-audio-sr.git] / pdk_k2g_1_0_1 / packages / ti / board / diag / temperature / src / temperature_test.c
1 /**
2 * \file main.c
3 *
4 * \brief Example application main file. This application will toggle the led.
5 * The led toggling will be done inside an callback function, which
6 * will be called by Interrupt Service Routine. Interrupts are
7 * triggered manually and no external source is used to trigger
8 * interrupts.
9 *
10 */
12 /*
13 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the
25 * distribution.
26 *
27 * Neither the name of Texas Instruments Incorporated nor the names of
28 * its contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 */
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include <ti/drv/i2c/I2C.h>
51 #if defined (SOC_K2G)
52 #include <ti/drv/i2c/soc/I2C_soc.h>
53 #else
54 #include <ti/drv/i2c/soc/I2C_v1.h>
55 #endif
56 #include <ti/drv/uart/UART_stdio.h>
58 #include "board.h"
59 #include "board_cfg.h"
61 /**********************************************************************
62 ************************** Internal functions ************************
63 **********************************************************************/
65 extern const I2C_Config I2C_config[];
66 void AppDelay(uint32_t delayVal);
68 /* Board specific definitions */
69 #if defined (EVM_K2G)
70 #define I2C_INSTANCE (1U)
71 #else
72 #define I2C_INSTANCE (0U)
73 #endif
75 /**********************************************************************
76 ************************** Global Variables **************************
77 **********************************************************************/
79 uint16_t readTemp(I2C_Handle h)
80 {
81 uint8_t tx[1];
82 uint8_t rx[2];
83 uint16_t val;
84 I2C_Transaction t;
85 t.slaveAddress = 0x48;
86 t.writeBuf = tx;
87 t.writeCount = 1;
88 t.readBuf = rx;
89 t.readCount = 0;
90 tx[0] = 0x0;
91 I2C_transfer(h, &t);
92 AppDelay(1000);
93 t.writeCount = 0;
94 t.readCount = 2;
95 I2C_transfer(h, &t);
96 val = (rx[0] << 4) | (rx[1] >> 4);
97 UART_printf("got rx[0] = %d, rx[1] = %d, val = %d\n", rx[0], rx[1], val);
98 return val;
99 }
101 int convertTemp(uint16_t temp)
102 {
103 return (temp * 625) / 10000;
104 }
106 /*
107 * ======== test function ========
108 */
109 int temperature_test()
110 {
111 int ret = 0;
112 int i;
113 uint16_t temp;
114 I2C_Params i2cParams;
115 I2C_Handle handle = NULL;
117 for (i=0; I2C_config[i].fxnTablePtr != NULL; i++)
118 {
119 ((I2C_HwAttrs *)I2C_config[i].hwAttrs)->enableIntr = false;
120 }
122 I2C_init();
124 I2C_Params_init(&i2cParams);
126 handle = I2C_open(I2C_INSTANCE, &i2cParams);
128 UART_printf("\n*********************************************\n");
129 UART_printf ("* Temperature Sensor Test *\n");
130 UART_printf ("*********************************************\n");
131 UART_printf("Testing temperature sensor... \n");
132 temp = readTemp(handle);
133 UART_printf("Temperature = %d C\n", convertTemp(temp));
134 UART_printf("Test PASSED!\n");
135 return ret;
136 }
138 /*
139 * ======== main ========
140 */
141 int main(void)
142 {
143 Board_initCfg boardCfg;
144 #ifdef PDK_RAW_BOOT
145 boardCfg = BOARD_INIT_MODULE_CLOCK |
146 BOARD_INIT_PINMUX_CONFIG |
147 BOARD_INIT_UART_STDIO;
148 #else
149 boardCfg = BOARD_INIT_UART_STDIO;
150 #endif
151 Board_init(boardCfg);
152 return temperature_test();
153 }
155 void AppDelay(uint32_t delayVal)
156 {
157 uint32_t cnt = 0;
158 while(cnt < delayVal)
159 {
160 asm("");
161 cnt++;
162 }
163 }