aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/testex/STM32/STM32F4xx/I2C-LPS22HB/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F4xx/I2C-LPS22HB/main.c')
-rw-r--r--lib/chibios/testex/STM32/STM32F4xx/I2C-LPS22HB/main.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F4xx/I2C-LPS22HB/main.c b/lib/chibios/testex/STM32/STM32F4xx/I2C-LPS22HB/main.c
new file mode 100644
index 000000000..362716db7
--- /dev/null
+++ b/lib/chibios/testex/STM32/STM32F4xx/I2C-LPS22HB/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 "lps22hb.h"
22
23#define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
24
25/*===========================================================================*/
26/* LPS22HB related. */
27/*===========================================================================*/
28
29/* LPS22HB Driver: This object represent an LPS22HB instance */
30static LPS22HBDriver LPS22HBD1;
31
32static int32_t baroraw;
33static int32_t thermoraw;
34
35static float barocooked;
36static float thermocooked;
37
38static const I2CConfig i2ccfg = {
39 OPMODE_I2C,
40 400000,
41 FAST_DUTY_CYCLE_2,
42};
43
44static const LPS22HBConfig lps22hbcfg = {
45 &I2CD1,
46 &i2ccfg,
47 LPS22HB_SAD_VCC,
48 NULL,
49 NULL,
50 NULL,
51 NULL,
52 LPS22HB_ODR_10HZ,
53#if LPS22HB_USE_ADVANCED
54 LPS22HB_BDU_CONTINUOUS,
55 LPS22HB_LP_ODR_9
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 2 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 /* LPS22HB Object Initialization.*/
106 lps22hbObjectInit(&LPS22HBD1);
107
108 /* Activates the LPS22HB driver.*/
109 lps22hbStart(&LPS22HBD1, &lps22hbcfg);
110
111 /* Normal main() thread activity, printing MEMS data on the SD2. */
112 while (true) {
113 lps22hbBarometerReadRaw(&LPS22HBD1, &baroraw);
114 chprintf(chp, "LPS22HBD1 Barometer raw data...\r\n");
115 chprintf(chp, "Pres: %d\r\n", baroraw);
116
117 lps22hbThermometerReadRaw(&LPS22HBD1, &thermoraw);
118 chprintf(chp, "LPS22HBD1 Thermometer raw data...\r\n");
119 chprintf(chp, "Temp: %d\r\n", &thermoraw);
120
121 lps22hbBarometerReadCooked(&LPS22HBD1, &barocooked);
122 chprintf(chp, "LPS22HBD1 Barometer cooked data...\r\n");
123 chprintf(chp, "Pres: %.2f\r\n", barocooked);
124
125 lps22hbThermometerReadCooked(&LPS22HBD1, &thermocooked);
126 chprintf(chp, "LPS22HBD1 Thermometer cooked data...\r\n");
127 chprintf(chp, "Temp: %.2f\r\n", thermocooked);
128
129 chThdSleepMilliseconds(100);
130 cls(chp);
131 }
132 lps22hbStop(&LPS22HBD1);
133}