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