diff options
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F4xx/I2C-LSM6DS0/main.c')
-rw-r--r-- | lib/chibios/testex/STM32/STM32F4xx/I2C-LSM6DS0/main.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F4xx/I2C-LSM6DS0/main.c b/lib/chibios/testex/STM32/STM32F4xx/I2C-LSM6DS0/main.c new file mode 100644 index 000000000..42b841660 --- /dev/null +++ b/lib/chibios/testex/STM32/STM32F4xx/I2C-LSM6DS0/main.c | |||
@@ -0,0 +1,158 @@ | |||
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 gyroliance 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 "lsm6ds0.h" | ||
22 | |||
23 | #define cls(chp) chprintf(chp, "\033[2J\033[1;1H") | ||
24 | |||
25 | /*===========================================================================*/ | ||
26 | /* LSM6DS0 related. */ | ||
27 | /*===========================================================================*/ | ||
28 | |||
29 | /* LSM6DS0 Driver: This object represent an LSM6DS0 instance */ | ||
30 | static LSM6DS0Driver LSM6DS0D1; | ||
31 | |||
32 | static int32_t accraw[LSM6DS0_ACC_NUMBER_OF_AXES]; | ||
33 | static int32_t gyroraw[LSM6DS0_GYRO_NUMBER_OF_AXES]; | ||
34 | |||
35 | static float acccooked[LSM6DS0_ACC_NUMBER_OF_AXES]; | ||
36 | static float gyrocooked[LSM6DS0_GYRO_NUMBER_OF_AXES]; | ||
37 | |||
38 | static char axisID[LSM6DS0_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; | ||
39 | static uint32_t i; | ||
40 | |||
41 | static const I2CConfig i2ccfg = { | ||
42 | OPMODE_I2C, | ||
43 | 400000, | ||
44 | FAST_DUTY_CYCLE_2, | ||
45 | }; | ||
46 | |||
47 | static const LSM6DS0Config lsm6ds0cfg = { | ||
48 | &I2CD1, | ||
49 | &i2ccfg, | ||
50 | LSM6DS0_SAD_VCC, | ||
51 | NULL, | ||
52 | NULL, | ||
53 | LSM6DS0_ACC_FS_2G, | ||
54 | LSM6DS0_ACC_ODR_50Hz, | ||
55 | #if LSM6DS0_USE_ADVANCED | ||
56 | LSM6DS0_ACC_DEC_X4, | ||
57 | #endif | ||
58 | NULL, | ||
59 | NULL, | ||
60 | LSM6DS0_GYRO_FS_245DPS, | ||
61 | LSM6DS0_GYRO_ODR_119HZ_FC_31, | ||
62 | #if LSM6DS0_USE_ADVANCED | ||
63 | LSM6DS0_GYRO_LP_DISABLED, | ||
64 | LSM6DS0_GYRO_OUT_SEL_0, | ||
65 | LSM6DS0_GYRO_HP_DISABLED, | ||
66 | LSM6DS0_GYRO_HPCF_0, | ||
67 | #endif | ||
68 | #if LSM6DS0_USE_ADVANCED | ||
69 | LSM6DS0_BDU_BLOCKED, | ||
70 | LSM6DS0_END_LITTLE | ||
71 | #endif | ||
72 | }; | ||
73 | |||
74 | /*===========================================================================*/ | ||
75 | /* Generic code. */ | ||
76 | /*===========================================================================*/ | ||
77 | |||
78 | static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2; | ||
79 | /* | ||
80 | * LED blinker thread, times are in milliseconds. | ||
81 | */ | ||
82 | static THD_WORKING_AREA(waThread1, 128); | ||
83 | static THD_FUNCTION(Thread1, arg) { | ||
84 | |||
85 | (void)arg; | ||
86 | chRegSetThreadName("blinker"); | ||
87 | while (true) { | ||
88 | palToggleLine(LINE_LED_GREEN); | ||
89 | chThdSleepMilliseconds(500); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | |||
94 | /* | ||
95 | * Application entry point. | ||
96 | */ | ||
97 | int main(void) { | ||
98 | |||
99 | /* | ||
100 | * System initializations. | ||
101 | * - HAL initialization, this also initializes the configured device drivers | ||
102 | * and performs the board-specific initializations. | ||
103 | * - Kernel initialization, the main() function becomes a thread and the | ||
104 | * RTOS is active. | ||
105 | */ | ||
106 | halInit(); | ||
107 | chSysInit(); | ||
108 | |||
109 | /* Configuring I2C SCK and I2C SDA related GPIOs .*/ | ||
110 | palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) | | ||
111 | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN); | ||
112 | palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) | | ||
113 | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN); | ||
114 | |||
115 | /* Activates the serial driver 2 using the driver default configuration.*/ | ||
116 | sdStart(&SD2, NULL); | ||
117 | |||
118 | /* Creates the blinker thread.*/ | ||
119 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL); | ||
120 | |||
121 | /* LSM6DS0 Object Initialization.*/ | ||
122 | lsm6ds0ObjectInit(&LSM6DS0D1); | ||
123 | |||
124 | /* Activates the LSM6DS0 driver.*/ | ||
125 | lsm6ds0Start(&LSM6DS0D1, &lsm6ds0cfg); | ||
126 | |||
127 | lsm6ds0GyroscopeSampleBias(&LSM6DS0D1); | ||
128 | |||
129 | /* Normal main() thread activity, printing MEMS data on the SDU1.*/ | ||
130 | while (true) { | ||
131 | lsm6ds0AccelerometerReadRaw(&LSM6DS0D1, accraw); | ||
132 | chprintf(chp, "LSM6DS0 Accelerometer raw data...\r\n"); | ||
133 | for(i = 0; i < LSM6DS0_ACC_NUMBER_OF_AXES; i++) { | ||
134 | chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]); | ||
135 | } | ||
136 | |||
137 | lsm6ds0GyroscopeReadRaw(&LSM6DS0D1, gyroraw); | ||
138 | chprintf(chp, "LSM6DS0 Gyroscope raw data...\r\n"); | ||
139 | for(i = 0; i < LSM6DS0_GYRO_NUMBER_OF_AXES; i++) { | ||
140 | chprintf(chp, "%c-axis: %d\r\n", axisID[i], gyroraw[i]); | ||
141 | } | ||
142 | |||
143 | lsm6ds0AccelerometerReadCooked(&LSM6DS0D1, acccooked); | ||
144 | chprintf(chp, "LSM6DS0 Accelerometer cooked data...\r\n"); | ||
145 | for(i = 0; i < LSM6DS0_ACC_NUMBER_OF_AXES; i++) { | ||
146 | chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]); | ||
147 | } | ||
148 | |||
149 | lsm6ds0GyroscopeReadCooked(&LSM6DS0D1, gyrocooked); | ||
150 | chprintf(chp, "LSM6DS0 Gyroscope cooked data...\r\n"); | ||
151 | for(i = 0; i < LSM6DS0_GYRO_NUMBER_OF_AXES; i++) { | ||
152 | chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], gyrocooked[i]); | ||
153 | } | ||
154 | chThdSleepMilliseconds(100); | ||
155 | cls(chp); | ||
156 | } | ||
157 | lsm6ds0Stop(&LSM6DS0D1); | ||
158 | } | ||