diff options
Diffstat (limited to 'lib/chibios/testex/STM32/STM32F3xx/SPI-L3GD20/main.c')
-rw-r--r-- | lib/chibios/testex/STM32/STM32F3xx/SPI-L3GD20/main.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/lib/chibios/testex/STM32/STM32F3xx/SPI-L3GD20/main.c b/lib/chibios/testex/STM32/STM32F3xx/SPI-L3GD20/main.c new file mode 100644 index 000000000..e1b7c61b0 --- /dev/null +++ b/lib/chibios/testex/STM32/STM32F3xx/SPI-L3GD20/main.c | |||
@@ -0,0 +1,128 @@ | |||
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 "l3gd20.h" | ||
22 | |||
23 | #define cls(chp) chprintf(chp, "\033[2J\033[1;1H") | ||
24 | |||
25 | /*===========================================================================*/ | ||
26 | /* L3GD20 related. */ | ||
27 | /*===========================================================================*/ | ||
28 | |||
29 | /* L3GD20 Driver: This object represent an L3GD20 instance.*/ | ||
30 | static L3GD20Driver L3GD20D1; | ||
31 | |||
32 | static int32_t gyroraw[L3GD20_GYRO_NUMBER_OF_AXES]; | ||
33 | static float gyrocooked[L3GD20_GYRO_NUMBER_OF_AXES]; | ||
34 | |||
35 | static char axisID[L3GD20_GYRO_NUMBER_OF_AXES] = {'X', 'Y', 'Z'}; | ||
36 | static uint32_t i; | ||
37 | |||
38 | static const SPIConfig spicfg = { | ||
39 | FALSE, | ||
40 | NULL, | ||
41 | GPIOE, | ||
42 | GPIOE_L3GD20_CS, | ||
43 | SPI_CR1_BR | SPI_CR1_CPOL | SPI_CR1_CPHA, | ||
44 | 0 | ||
45 | }; | ||
46 | |||
47 | static L3GD20Config l3gd20cfg = { | ||
48 | &SPID1, | ||
49 | &spicfg, | ||
50 | NULL, | ||
51 | NULL, | ||
52 | L3GD20_FS_250DPS, | ||
53 | L3GD20_ODR_760HZ, | ||
54 | #if L3GD20_USE_ADVANCED | ||
55 | L3GD20_BDU_CONTINUOUS, | ||
56 | L3GD20_END_LITTLE, | ||
57 | L3GD20_BW3, | ||
58 | L3GD20_HPM_REFERENCE, | ||
59 | L3GD20_HPCF_8, | ||
60 | L3GD20_LP2M_ON, | ||
61 | #endif | ||
62 | }; | ||
63 | |||
64 | /*===========================================================================*/ | ||
65 | /* Generic code. */ | ||
66 | /*===========================================================================*/ | ||
67 | |||
68 | static BaseSequentialStream* chp = (BaseSequentialStream*)&SD1; | ||
69 | /* | ||
70 | * Red LED blinker thread, times are in milliseconds. | ||
71 | */ | ||
72 | static THD_WORKING_AREA(waThread1, 128); | ||
73 | static THD_FUNCTION(Thread1, arg) { | ||
74 | |||
75 | (void)arg; | ||
76 | chRegSetThreadName("blinker"); | ||
77 | while (true) { | ||
78 | palToggleLine(LINE_LED3_RED); | ||
79 | chThdSleepMilliseconds(250); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | /* | ||
84 | * Application entry point. | ||
85 | */ | ||
86 | int 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 | /* Activates the serial driver 1 using the driver default configuration.*/ | ||
99 | sdStart(&SD1, NULL); | ||
100 | |||
101 | /* Creates the blinker thread.*/ | ||
102 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); | ||
103 | |||
104 | /* L3GD20 Object Initialization.*/ | ||
105 | l3gd20ObjectInit(&L3GD20D1); | ||
106 | |||
107 | /* Activates the L3GD20 driver.*/ | ||
108 | l3gd20Start(&L3GD20D1, &l3gd20cfg); | ||
109 | |||
110 | /* Normal main() thread activity, printing MEMS data on the SD1.*/ | ||
111 | while (true) { | ||
112 | l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw); | ||
113 | chprintf(chp, "L3GD20 Gyroscope raw data...\r\n"); | ||
114 | for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) { | ||
115 | chprintf(chp, "%c-axis: %d\r\n", axisID[i], gyroraw[i]); | ||
116 | } | ||
117 | |||
118 | l3gd20GyroscopeReadCooked(&L3GD20D1, gyrocooked); | ||
119 | chprintf(chp, "L3GD20 Gyroscope cooked data...\r\n"); | ||
120 | for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) { | ||
121 | chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], gyrocooked[i]); | ||
122 | } | ||
123 | |||
124 | chThdSleepMilliseconds(100); | ||
125 | cls(chp); | ||
126 | } | ||
127 | l3gd20Stop(&L3GD20D1); | ||
128 | } | ||