aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/testhal/TIVA/TM4C123x/I2C/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/testhal/TIVA/TM4C123x/I2C/main.c')
-rw-r--r--lib/chibios-contrib/testhal/TIVA/TM4C123x/I2C/main.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/TIVA/TM4C123x/I2C/main.c b/lib/chibios-contrib/testhal/TIVA/TM4C123x/I2C/main.c
new file mode 100644
index 000000000..02537106f
--- /dev/null
+++ b/lib/chibios-contrib/testhal/TIVA/TM4C123x/I2C/main.c
@@ -0,0 +1,127 @@
1/*
2 Copyright (C) 2014..2017 Marco Veeneman
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#include "ch.h"
18#include "hal.h"
19#include "chprintf.h"
20
21/* buffers depth */
22#define TEMP_RX_DEPTH 6
23#define TEMP_TX_DEPTH 6
24
25/* tmp275 specific addresses */
26#define TMP275_TEMP 0x00
27#define TMP275_CONF 0x01
28#define TMP275_TLOW 0x02
29#define TMP275_THIGH 0x03
30
31/* tmp275 config register */
32#define TMP275_CONF_SD 0x01
33#define TMP275_CONF_TM 0x02
34#define TMP275_CONF_POL 0x04
35#define TMP275_CONF_F0 0x08
36#define TMP275_CONF_F1 0x10
37#define TMP275_CONF_R0 0x20
38#define TMP275_CONF_R1 0x40
39#define TMP275_CONF_OS 0x80
40
41#define TMP275_ADDR 0b01001001
42
43static uint8_t rxbuf[TEMP_RX_DEPTH];
44static uint8_t txbuf[TEMP_TX_DEPTH];
45static i2cflags_t errors = 0;
46static uint16_t temperature;
47
48/* I2C configuration*/
49static const I2CConfig i2cfg =
50{
51 400000
52};
53
54/*
55 * Application entry point.
56 */
57int main(void)
58{
59 msg_t status = MSG_OK;
60 systime_t tmo = OSAL_MS2I(100);
61
62 /*
63 * System initializations.
64 * - HAL initialization, this also initializes the configured device drivers
65 * and performs the board-specific initializations.
66 * - Kernel initialization, the main() function becomes a thread and the
67 * RTOS is active.
68 */
69 halInit();
70 chSysInit();
71
72 /* Configure RX and TX pins for UART0.*/
73 palSetLineMode(LINE_UART0_RX, PAL_MODE_INPUT | PAL_MODE_ALTERNATE(1));
74 palSetLineMode(LINE_UART0_TX, PAL_MODE_INPUT | PAL_MODE_ALTERNATE(1));
75
76 /*
77 * Start the serial driver with the default configuration.
78 */
79 sdStart(&SD1, NULL);
80
81 /* Configure SCK and SCL pins for I2C0.*/
82 palSetLineMode(LINE_I2C0_SCL, PAL_MODE_OUTPUT_PUSHPULL | PAL_MODE_ALTERNATE(3));
83 palSetLineMode(LINE_I2C0_SDA, PAL_MODE_OUTPUT_OPENDRAIN | PAL_MODE_ALTERNATE(3));
84
85 /*
86 * Start the i2c driver with the custom configuration.
87 */
88 i2cStart(&I2CD1, &i2cfg);
89
90 chprintf((BaseSequentialStream *)&SD1, "\r\n**********************\r\n");
91 chprintf((BaseSequentialStream *)&SD1, "* TM4C123x I2C Demo. *\r\n");
92 chprintf((BaseSequentialStream *)&SD1, "**********************\r\n\r\n");
93
94 txbuf[0] = TMP275_CONF; // register address
95 txbuf[1] = TMP275_CONF_R0 | TMP275_CONF_R1; // set conversion resolution to 12 bits
96 i2cAcquireBus(&I2CD1);
97 status = i2cMasterTransmitTimeout(&I2CD1, TMP275_ADDR, txbuf, 2, rxbuf, 0, tmo);
98 i2cReleaseBus(&I2CD1);
99
100 if (status != MSG_OK){
101 errors = i2cGetErrors(&I2CD1);
102 chprintf((BaseSequentialStream *)&SD1, "ERROR: errors detected.\r\n");
103 }
104
105 /*
106 * Normal main() thread activity
107 */
108 while (TRUE) {
109 txbuf[0] = TMP275_TEMP; // register address
110 i2cAcquireBus(&I2CD1);
111 status = i2cMasterTransmitTimeout(&I2CD1, TMP275_ADDR, txbuf, 1, rxbuf, 2, tmo);
112 i2cReleaseBus(&I2CD1);
113
114 if (status != MSG_OK){
115 errors = i2cGetErrors(&I2CD1);
116 chprintf((BaseSequentialStream *)&SD1, "Status: %i\r\n", status);
117 }
118 else{
119 temperature = (((rxbuf[0] << 8) | rxbuf[1]) >> 4);
120 chprintf((BaseSequentialStream *)&SD1, "Temperature: %u,%4u\r\n", temperature >> 4, 625*(temperature & 0x0f));
121 }
122
123 chThdSleepMilliseconds(1000);
124 }
125
126 return 0;
127}