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