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