diff options
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F4xx/I2C-IKS01A2/main.c')
-rw-r--r-- | lib/chibios/testex/STM32/STM32F4xx/I2C-IKS01A2/main.c | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F4xx/I2C-IKS01A2/main.c b/lib/chibios/testex/STM32/STM32F4xx/I2C-IKS01A2/main.c new file mode 100644 index 000000000..61178a154 --- /dev/null +++ b/lib/chibios/testex/STM32/STM32F4xx/I2C-IKS01A2/main.c | |||
@@ -0,0 +1,230 @@ | |||
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 | #include "lps22hb.h" | ||
23 | #include "lsm303agr.h" | ||
24 | #include "lsm6dsl.h" | ||
25 | |||
26 | #define cls(chp) chprintf(chp, "\033[2J\033[1;1H") | ||
27 | #define MAX_AXIS_NUMBER 3U | ||
28 | |||
29 | /* Array for data storage. */ | ||
30 | static float cooked[MAX_AXIS_NUMBER]; | ||
31 | /* Axis identifiers. */ | ||
32 | static char axis_id[MAX_AXIS_NUMBER] = {'X', 'Y', 'Z'}; | ||
33 | |||
34 | /* Generic I2C configuration for every MEMS. */ | ||
35 | static const I2CConfig i2ccfg = { | ||
36 | OPMODE_I2C, | ||
37 | 400000, | ||
38 | FAST_DUTY_CYCLE_2, | ||
39 | }; | ||
40 | |||
41 | static uint32_t i; | ||
42 | static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2; | ||
43 | |||
44 | /*===========================================================================*/ | ||
45 | /* HTS221 related. */ | ||
46 | /*===========================================================================*/ | ||
47 | |||
48 | /* HTS221 Driver: This object represent an HTS221 instance */ | ||
49 | static HTS221Driver HTS221D1; | ||
50 | |||
51 | static const HTS221Config hts221cfg = { | ||
52 | &I2CD1, | ||
53 | &i2ccfg, | ||
54 | NULL, | ||
55 | NULL, | ||
56 | NULL, | ||
57 | NULL, | ||
58 | HTS221_ODR_7HZ | ||
59 | }; | ||
60 | |||
61 | /*===========================================================================*/ | ||
62 | /* LPS22HB related. */ | ||
63 | /*===========================================================================*/ | ||
64 | |||
65 | /* LPS22HB Driver: This object represent an LPS22HB instance */ | ||
66 | static LPS22HBDriver LPS22HBD1; | ||
67 | |||
68 | static const LPS22HBConfig lps22hbcfg = { | ||
69 | &I2CD1, | ||
70 | &i2ccfg, | ||
71 | LPS22HB_SAD_VCC, | ||
72 | NULL, | ||
73 | NULL, | ||
74 | NULL, | ||
75 | NULL, | ||
76 | LPS22HB_ODR_10HZ, | ||
77 | }; | ||
78 | |||
79 | /*===========================================================================*/ | ||
80 | /* LSM303AGR related. */ | ||
81 | /*===========================================================================*/ | ||
82 | |||
83 | /* LSM303AGR Driver: This object represent an LSM303AGR instance */ | ||
84 | static LSM303AGRDriver LSM303AGRD1; | ||
85 | |||
86 | static const LSM303AGRConfig lsm303agrcfg = { | ||
87 | &I2CD1, | ||
88 | &i2ccfg, | ||
89 | NULL, | ||
90 | NULL, | ||
91 | LSM303AGR_ACC_FS_4G, | ||
92 | LSM303AGR_ACC_ODR_100Hz, | ||
93 | NULL, | ||
94 | NULL, | ||
95 | LSM303AGR_COMP_ODR_50HZ, | ||
96 | }; | ||
97 | |||
98 | /*===========================================================================*/ | ||
99 | /* LSM6DSL related. */ | ||
100 | /*===========================================================================*/ | ||
101 | |||
102 | /* LSM6DSL Driver: This object represent an LSM6DSL instance */ | ||
103 | static LSM6DSLDriver LSM6DSLD1; | ||
104 | |||
105 | static const LSM6DSLConfig lsm6dslcfg = { | ||
106 | &I2CD1, | ||
107 | &i2ccfg, | ||
108 | LSM6DSL_SAD_VCC, | ||
109 | NULL, | ||
110 | NULL, | ||
111 | LSM6DSL_ACC_FS_2G, | ||
112 | LSM6DSL_ACC_ODR_52Hz, | ||
113 | NULL, | ||
114 | NULL, | ||
115 | LSM6DSL_GYRO_FS_250DPS, | ||
116 | LSM6DSL_GYRO_ODR_104Hz | ||
117 | }; | ||
118 | |||
119 | /*===========================================================================*/ | ||
120 | /* Generic code. */ | ||
121 | /*===========================================================================*/ | ||
122 | |||
123 | /* | ||
124 | * Green LED blinker thread, times are in milliseconds. | ||
125 | */ | ||
126 | static THD_WORKING_AREA(waThread1, 128); | ||
127 | static THD_FUNCTION(Thread1, arg) { | ||
128 | |||
129 | (void)arg; | ||
130 | chRegSetThreadName("blinker"); | ||
131 | while (true) { | ||
132 | palClearPad(GPIOA, GPIOA_LED_GREEN); | ||
133 | chThdSleepMilliseconds(500); | ||
134 | palSetPad(GPIOA, GPIOA_LED_GREEN); | ||
135 | chThdSleepMilliseconds(500); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | /* | ||
140 | * Application entry point. | ||
141 | */ | ||
142 | int main(void) { | ||
143 | |||
144 | /* | ||
145 | * System initializations. | ||
146 | * - HAL initialization, this also initializes the configured device drivers | ||
147 | * and performs the board-specific initializations. | ||
148 | * - Kernel initialization, the main() function becomes a thread and the | ||
149 | * RTOS is active. | ||
150 | */ | ||
151 | halInit(); | ||
152 | chSysInit(); | ||
153 | |||
154 | /* Configuring I2C SCK and I2C SDA related GPIOs .*/ | ||
155 | palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) | | ||
156 | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN); | ||
157 | palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) | | ||
158 | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN); | ||
159 | |||
160 | /* | ||
161 | * Activates the serial driver 2 using the driver default configuration. | ||
162 | */ | ||
163 | sdStart(&SD2, NULL); | ||
164 | |||
165 | /* | ||
166 | * Creates the blinker thread. | ||
167 | */ | ||
168 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL); | ||
169 | |||
170 | /* MEMS Driver Objects Initialization.*/ | ||
171 | hts221ObjectInit(&HTS221D1); | ||
172 | lps22hbObjectInit(&LPS22HBD1); | ||
173 | lsm303agrObjectInit(&LSM303AGRD1); | ||
174 | lsm6dslObjectInit(&LSM6DSLD1); | ||
175 | |||
176 | /* Activates all the MEMS related drivers.*/ | ||
177 | hts221Start(&HTS221D1, &hts221cfg); | ||
178 | lps22hbStart(&LPS22HBD1, &lps22hbcfg); | ||
179 | lsm303agrStart(&LSM303AGRD1, &lsm303agrcfg); | ||
180 | lsm6dslStart(&LSM6DSLD1, &lsm6dslcfg); | ||
181 | |||
182 | /* | ||
183 | * Normal main() thread activity, in this demo it does nothing except | ||
184 | * sleeping in a loop and check the button state. | ||
185 | */ | ||
186 | while (true) { | ||
187 | hts221HygrometerReadCooked(&HTS221D1, cooked); | ||
188 | chprintf(chp, "HTS221D1 Hygrometer cooked data...\r\n"); | ||
189 | chprintf(chp, "Hum: %.2f\r\n", *cooked); | ||
190 | |||
191 | hts221ThermometerReadCooked(&HTS221D1, cooked); | ||
192 | chprintf(chp, "HTS221D1 Thermometer cooked data...\r\n"); | ||
193 | chprintf(chp, "Temp: %.2f\r\n", *cooked); | ||
194 | |||
195 | lps22hbBarometerReadCooked(&LPS22HBD1, cooked); | ||
196 | chprintf(chp, "LPS22HBD1 Barometer cooked data...\r\n"); | ||
197 | chprintf(chp, "Pres: %.2f\r\n", *cooked); | ||
198 | |||
199 | lps22hbThermometerReadCooked(&LPS22HBD1, cooked); | ||
200 | chprintf(chp, "LPS22HBD1 Thermometer cooked data...\r\n"); | ||
201 | chprintf(chp, "Temp: %.2f\r\n", *cooked); | ||
202 | |||
203 | lsm303agrAccelerometerReadCooked(&LSM303AGRD1, cooked); | ||
204 | chprintf(chp, "LSM303AGR Accelerometer cooked data...\r\n"); | ||
205 | for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) { | ||
206 | chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]); | ||
207 | } | ||
208 | |||
209 | lsm303agrCompassReadCooked(&LSM303AGRD1, cooked); | ||
210 | chprintf(chp, "LSM303AGR Compass cooked data...\r\n"); | ||
211 | for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) { | ||
212 | chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]); | ||
213 | } | ||
214 | |||
215 | lsm6dslAccelerometerReadCooked(&LSM6DSLD1, cooked); | ||
216 | chprintf(chp, "LSM6DSL Accelerometer cooked data...\r\n"); | ||
217 | for(i = 0; i < LSM6DSL_ACC_NUMBER_OF_AXES; i++) { | ||
218 | chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]); | ||
219 | } | ||
220 | |||
221 | lsm6dslGyroscopeReadCooked(&LSM6DSLD1, cooked); | ||
222 | chprintf(chp, "LSM6DSL Gyroscope cooked data...\r\n"); | ||
223 | for(i = 0; i < LSM6DSL_GYRO_NUMBER_OF_AXES; i++) { | ||
224 | chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]); | ||
225 | } | ||
226 | |||
227 | chThdSleepMilliseconds(500); | ||
228 | cls(chp); | ||
229 | } | ||
230 | } | ||