aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/testex/STM32/STM32F4xx/I2C-HTS221/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F4xx/I2C-HTS221/main.c')
-rw-r--r--lib/chibios/testex/STM32/STM32F4xx/I2C-HTS221/main.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F4xx/I2C-HTS221/main.c b/lib/chibios/testex/STM32/STM32F4xx/I2C-HTS221/main.c
new file mode 100644
index 000000000..578526b9f
--- /dev/null
+++ b/lib/chibios/testex/STM32/STM32F4xx/I2C-HTS221/main.c
@@ -0,0 +1,133 @@
1/*
2 ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
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
20#include "chprintf.h"
21#include "hts221.h"
22
23#define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
24
25/*===========================================================================*/
26/* HTS221 related. */
27/*===========================================================================*/
28
29/* HTS221 Driver: This object represent an HTS221 instance */
30static HTS221Driver HTS221D1;
31
32static int32_t hygroraw;
33static int32_t thermoraw;
34
35static float hygrocooked;
36static float thermocooked;
37
38static const I2CConfig i2ccfg = {
39 OPMODE_I2C,
40 400000,
41 FAST_DUTY_CYCLE_2,
42};
43
44static const HTS221Config hts221cfg = {
45 &I2CD1,
46 &i2ccfg,
47 NULL,
48 NULL,
49 NULL,
50 NULL,
51 HTS221_ODR_7HZ,
52#if HTS221_USE_ADVANCED || defined(__DOXYGEN__)
53 HTS221_BDU_CONTINUOUS,
54 HTS221_AVGH_256,
55 HTS221_AVGT_256
56#endif
57};
58
59/*===========================================================================*/
60/* Generic code. */
61/*===========================================================================*/
62
63static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
64/*
65 * LED blinker thread, times are in milliseconds.
66 */
67static THD_WORKING_AREA(waThread1, 128);
68static THD_FUNCTION(Thread1, arg) {
69
70 (void)arg;
71 chRegSetThreadName("blinker");
72 while (true) {
73 palToggleLine(LINE_LED_GREEN);
74 chThdSleepMilliseconds(500);
75 }
76}
77
78/*
79 * Application entry point.
80 */
81int main(void) {
82
83 /*
84 * System initializations.
85 * - HAL initialization, this also initializes the configured device drivers
86 * and performs the board-specific initializations.
87 * - Kernel initialization, the main() function becomes a thread and the
88 * RTOS is active.
89 */
90 halInit();
91 chSysInit();
92
93 /* Configuring I2C SCK and I2C SDA related GPIOs .*/
94 palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
95 PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
96 palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
97 PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
98
99 /* Activates the serial driver 1 using the driver default configuration.*/
100 sdStart(&SD2, NULL);
101
102 /* Creates the blinker thread.*/
103 chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
104
105 /* HTS221 Object Initialization.*/
106 hts221ObjectInit(&HTS221D1);
107
108 /* Activates the HTS221 driver.*/
109 hts221Start(&HTS221D1, &hts221cfg);
110
111 /* Normal main() thread activity, printing MEMS data on the SD2. */
112 while (true) {
113 hts221HygrometerReadRaw(&HTS221D1, &hygroraw);
114 chprintf(chp, "HTS221D1 Hygrometer raw data...\r\n");
115 chprintf(chp, "Hum: %d\r\n", hygroraw);
116
117 hts221ThermometerReadRaw(&HTS221D1, &thermoraw);
118 chprintf(chp, "HTS221D1 Thermometer raw data...\r\n");
119 chprintf(chp, "Temp: %d\r\n", &thermoraw);
120
121 hts221HygrometerReadCooked(&HTS221D1, &hygrocooked);
122 chprintf(chp, "HTS221D1 Hygrometer cooked data...\r\n");
123 chprintf(chp, "Hum: %.2f\r\n", hygrocooked);
124
125 hts221ThermometerReadCooked(&HTS221D1, &thermocooked);
126 chprintf(chp, "HTS221D1 Thermometer cooked data...\r\n");
127 chprintf(chp, "Temp: %.2f\r\n", thermocooked);
128
129 chThdSleepMilliseconds(100);
130 cls(chp);
131 }
132 hts221Stop(&HTS221D1);
133}