aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/testex/STM32/STM32F4xx/I2C-LIS3MDL/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F4xx/I2C-LIS3MDL/main.c')
-rw-r--r--lib/chibios/testex/STM32/STM32F4xx/I2C-LIS3MDL/main.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F4xx/I2C-LIS3MDL/main.c b/lib/chibios/testex/STM32/STM32F4xx/I2C-LIS3MDL/main.c
new file mode 100644
index 000000000..8f0f0373a
--- /dev/null
+++ b/lib/chibios/testex/STM32/STM32F4xx/I2C-LIS3MDL/main.c
@@ -0,0 +1,131 @@
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#include "ch.h"
17#include "hal.h"
18
19#include "chprintf.h"
20#include "lis3mdl.h"
21
22#define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
23
24/*===========================================================================*/
25/* LIS3MDL related. */
26/*===========================================================================*/
27
28/* LIS3MDL Driver: This object represent an LIS3MDL instance.*/
29static LIS3MDLDriver LIS3MDLD1;
30
31static int32_t compraw[LIS3MDL_COMP_NUMBER_OF_AXES];
32
33static float compcooked[LIS3MDL_COMP_NUMBER_OF_AXES];
34
35static char axisID[LIS3MDL_COMP_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
36static uint32_t i;
37
38static const I2CConfig i2ccfg = {
39 OPMODE_I2C,
40 400000,
41 FAST_DUTY_CYCLE_2,
42};
43
44static LIS3MDLConfig lis3mdlcfg = {
45 &I2CD1,
46 &i2ccfg,
47 LIS3MDL_SAD_VCC,
48 NULL,
49 NULL,
50 LIS3MDL_COMP_FS_4GA,
51 LIS3MDL_COMP_ODR_40HZ,
52#if LIS3MDL_USE_ADVANCED
53 LIS3MDL_COMP_LP_ENABLED,
54 LIS3MDL_COMP_MD_CONTINUOUS,
55 LIS3MDL_COMP_OMXY_LP,
56 LIS3MDL_COMP_OMZ_LP,
57 LIS3MDL_BDU_CONTINUOUS,
58 LIS3MDL_END_LITTLE
59#endif
60};
61
62/*===========================================================================*/
63/* Generic code. */
64/*===========================================================================*/
65
66static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
67/*
68 * LED blinker thread, times are in milliseconds.
69 */
70static THD_WORKING_AREA(waThread1, 128);
71static THD_FUNCTION(Thread1, arg) {
72
73 (void)arg;
74 chRegSetThreadName("blinker");
75 while (true) {
76 palToggleLine(LINE_LED_GREEN);
77 chThdSleepMilliseconds(500);
78 }
79}
80
81/*
82 * Application entry point.
83 */
84int main(void) {
85
86 /*
87 * System initializations.
88 * - HAL initialization, this also initializes the configured device drivers
89 * and performs the board-specific initializations.
90 * - Kernel initialization, the main() function becomes a thread and the
91 * RTOS is active.
92 */
93 halInit();
94 chSysInit();
95
96 /* Configuring I2C SCK and I2C SDA related GPIOs .*/
97 palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
98 PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
99 palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
100 PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
101
102 /* Activates the serial driver 1 using the driver default configuration.*/
103 sdStart(&SD2, NULL);
104
105 /* Creates the blinker thread.*/
106 chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
107
108 /* LIS3MDL Object Initialization.*/
109 lis3mdlObjectInit(&LIS3MDLD1);
110
111 /* Activates the LIS3MDL driver.*/
112 lis3mdlStart(&LIS3MDLD1, &lis3mdlcfg);
113
114 /* Normal main() thread activity, printing MEMS data on the SD2. */
115 while (true) {
116 lis3mdlCompassReadRaw(&LIS3MDLD1, compraw);
117 chprintf(chp, "LIS3MDL Compass raw data...\r\n");
118 for(i = 0; i < LIS3MDL_COMP_NUMBER_OF_AXES; i++) {
119 chprintf(chp, "%c-axis: %d\r\n", axisID[i], compraw[i]);
120 }
121
122 lis3mdlCompassReadCooked(&LIS3MDLD1, compcooked);
123 chprintf(chp, "LIS3MDL Compass cooked data...\r\n");
124 for(i = 0; i < LIS3MDL_COMP_NUMBER_OF_AXES; i++) {
125 chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], compcooked[i]);
126 }
127 chThdSleepMilliseconds(100);
128 cls(chp);
129 }
130 lis3mdlStop(&LIS3MDLD1);
131}