aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c')
-rw-r--r--lib/chibios/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c b/lib/chibios/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c
new file mode 100644
index 000000000..7c0d1aa43
--- /dev/null
+++ b/lib/chibios/testex/STM32/STM32F4xx/SPI-LIS302DL/main.c
@@ -0,0 +1,138 @@
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 "lis302dl.h"
23
24#define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
25
26/*===========================================================================*/
27/* LIS302DL related. */
28/*===========================================================================*/
29
30/* LIS302DL Driver: This object represent an LIS302DL instance */
31static LIS302DLDriver LIS302DLD1;
32
33static int32_t accraw[LIS302DL_ACC_NUMBER_OF_AXES];
34
35static float acccooked[LIS302DL_ACC_NUMBER_OF_AXES];
36
37static char axisID[LIS302DL_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
38static uint32_t i;
39
40static const SPIConfig spicfg = {
41 FALSE,
42 NULL,
43 GPIOE,
44 GPIOE_CS_SPI,
45 SPI_CR1_BR_0 | SPI_CR1_CPOL | SPI_CR1_CPHA,
46 0
47};
48
49static LIS302DLConfig lis302dlcfg = {
50 &SPID1,
51 &spicfg,
52 NULL,
53 NULL,
54 LIS302DL_ACC_FS_2G,
55 LIS302DL_ACC_ODR_100HZ,
56#if LIS302DL_USE_ADVANCED
57 LIS302DL_ACC_HP_1,
58#endif
59};
60
61/*===========================================================================*/
62/* Generic code. */
63/*===========================================================================*/
64
65static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1;
66/*
67 * LED blinker thread, times are in milliseconds.
68 */
69static THD_WORKING_AREA(waThread1, 128);
70static THD_FUNCTION(Thread1, arg) {
71
72 (void)arg;
73 chRegSetThreadName("blinker");
74 while (true) {
75 systime_t time;
76
77 time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
78 palToggleLine(LINE_LED6);
79 chThdSleepMilliseconds(time);
80 }
81}
82
83/*
84 * Application entry point.
85 */
86int main(void) {
87
88 /*
89 * System initializations.
90 * - HAL initialization, this also initializes the configured device drivers
91 * and performs the board-specific initializations.
92 * - Kernel initialization, the main() function becomes a thread and the
93 * RTOS is active.
94 */
95 halInit();
96 chSysInit();
97
98 /* Initializes a serial-over-USB CDC driver.*/
99 sduObjectInit(&SDU1);
100 sduStart(&SDU1, &serusbcfg);
101
102 /*
103 * Activates the USB driver and then the USB bus pull-up on D+.
104 * Note, a delay is inserted in order to not have to disconnect the cable
105 * after a reset.
106 */
107 usbDisconnectBus(serusbcfg.usbp);
108 chThdSleepMilliseconds(1500);
109 usbStart(serusbcfg.usbp, &usbcfg);
110 usbConnectBus(serusbcfg.usbp);
111
112 /* Creates the blinker thread.*/
113 chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);
114
115 /* LIS302DL Object Initialization.*/
116 lis302dlObjectInit(&LIS302DLD1);
117
118 /* Activates the LIS302DL driver.*/
119 lis302dlStart(&LIS302DLD1, &lis302dlcfg);
120
121 /* Normal main() thread activity, printing MEMS data on the SDU1.*/
122 while (true) {
123 lis302dlAccelerometerReadRaw(&LIS302DLD1, accraw);
124 chprintf(chp, "LIS302DL Accelerometer raw data...\r\n");
125 for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) {
126 chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
127 }
128
129 lis302dlAccelerometerReadCooked(&LIS302DLD1, acccooked);
130 chprintf(chp, "LIS302DL Accelerometer cooked data...\r\n");
131 for(i = 0; i < LIS302DL_ACC_NUMBER_OF_AXES; i++) {
132 chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]);
133 }
134 chThdSleepMilliseconds(100);
135 cls(chp);
136 }
137 lis302dlStop(&LIS302DLD1);
138}