aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c')
-rw-r--r--lib/chibios/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c159
1 files changed, 159 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c b/lib/chibios/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c
new file mode 100644
index 000000000..97f1f7696
--- /dev/null
+++ b/lib/chibios/testex/STM32/STM32F4xx/I2C-LSM303DLHC/main.c
@@ -0,0 +1,159 @@
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 "usbcfg.h"
21#include "chprintf.h"
22#include "lsm303dlhc.h"
23
24#define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
25
26/*===========================================================================*/
27/* LSM303DLHC related. */
28/*===========================================================================*/
29
30/* LSM303DLHC Driver: This object represent an LSM303DLHC instance */
31static LSM303DLHCDriver LSM303DLHCD1;
32
33static int32_t accraw[LSM303DLHC_ACC_NUMBER_OF_AXES];
34static int32_t compraw[LSM303DLHC_COMP_NUMBER_OF_AXES];
35
36static float acccooked[LSM303DLHC_ACC_NUMBER_OF_AXES];
37static float compcooked[LSM303DLHC_COMP_NUMBER_OF_AXES];
38
39static char axisID[LSM303DLHC_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
40static uint32_t i;
41
42static const I2CConfig i2ccfg = {
43 OPMODE_I2C,
44 400000,
45 FAST_DUTY_CYCLE_2,
46};
47
48static const LSM303DLHCConfig lsm303dlhccfg = {
49 &I2CD1,
50 &i2ccfg,
51 NULL,
52 NULL,
53 LSM303DLHC_ACC_FS_4G,
54 LSM303DLHC_ACC_ODR_100Hz,
55#if LSM303DLHC_USE_ADVANCED
56 LSM303DLHC_ACC_LP_DISABLED,
57 LSM303DLHC_ACC_HR_DISABLED,
58 LSM303DLHC_ACC_BDU_BLOCK,
59 LSM303DLHC_ACC_END_LITTLE,
60#endif
61 NULL,
62 NULL,
63 LSM303DLHC_COMP_FS_1P3GA,
64 LSM303DLHC_COMP_ODR_30HZ,
65#if LSM303DLHC_USE_ADVANCED
66 LSM303DLHC_COMP_MD_BLOCK
67#endif
68};
69
70/*===========================================================================*/
71/* Generic code. */
72/*===========================================================================*/
73
74static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1;
75/*
76 * LED blinker thread, times are in milliseconds.
77 */
78static THD_WORKING_AREA(waThread1, 128);
79static THD_FUNCTION(Thread1, arg) {
80
81 (void)arg;
82 chRegSetThreadName("blinker");
83 while (true) {
84 systime_t time;
85
86 time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
87 palToggleLine(LINE_LED5);
88 chThdSleepMilliseconds(time);
89 }
90}
91
92/*
93 * Application entry point.
94 */
95int main(void) {
96
97 /*
98 * System initializations.
99 * - HAL initialization, this also initializes the configured device drivers
100 * and performs the board-specific initializations.
101 * - Kernel initialization, the main() function becomes a thread and the
102 * RTOS is active.
103 */
104 halInit();
105 chSysInit();
106
107 /* Initializes a serial-over-USB CDC driver.*/
108 sduObjectInit(&SDU1);
109 sduStart(&SDU1, &serusbcfg);
110
111 /*
112 * Activates the USB driver and then the USB bus pull-up on D+.
113 * Note, a delay is inserted in order to not have to disconnect the cable
114 * after a reset.
115 */
116 usbDisconnectBus(serusbcfg.usbp);
117 chThdSleepMilliseconds(1500);
118 usbStart(serusbcfg.usbp, &usbcfg);
119 usbConnectBus(serusbcfg.usbp);
120
121 /* Creates the blinker thread.*/
122 chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);
123
124 /* LSM303DLHC Object Initialization.*/
125 lsm303dlhcObjectInit(&LSM303DLHCD1);
126
127 /* Activates the LSM303DLHC driver.*/
128 lsm303dlhcStart(&LSM303DLHCD1, &lsm303dlhccfg);
129
130 /* Normal main() thread activity, printing MEMS data on the SDU1.*/
131 while (true) {
132 lsm303dlhcAccelerometerReadRaw(&LSM303DLHCD1, accraw);
133 chprintf(chp, "LSM303DLHC Accelerometer raw data...\r\n");
134 for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
135 chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
136 }
137
138 lsm303dlhcCompassReadRaw(&LSM303DLHCD1, compraw);
139 chprintf(chp, "LSM303DLHC Compass raw data...\r\n");
140 for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
141 chprintf(chp, "%c-axis: %d\r\n", axisID[i], compraw[i]);
142 }
143
144 lsm303dlhcAccelerometerReadCooked(&LSM303DLHCD1, acccooked);
145 chprintf(chp, "LSM303DLHC Accelerometer cooked data...\r\n");
146 for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
147 chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]);
148 }
149
150 lsm303dlhcCompassReadCooked(&LSM303DLHCD1, compcooked);
151 chprintf(chp, "LSM303DLHC Compass cooked data...\r\n");
152 for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
153 chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], compcooked[i]);
154 }
155 chThdSleepMilliseconds(100);
156 cls(chp);
157 }
158 lsm303dlhcStop(&LSM303DLHCD1);
159}