main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. #include "chprintf.h"
  16. #include "hts221.h"
  17. #include "lps22hb.h"
  18. #include "lsm303agr.h"
  19. #include "lsm6dsl.h"
  20. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  21. #define MAX_AXIS_NUMBER 3U
  22. /* Array for data storage. */
  23. static float cooked[MAX_AXIS_NUMBER];
  24. /* Axis identifiers. */
  25. static char axis_id[MAX_AXIS_NUMBER] = {'X', 'Y', 'Z'};
  26. /* Generic I2C configuration for every MEMS. */
  27. static const I2CConfig i2ccfg = {
  28. OPMODE_I2C,
  29. 400000,
  30. FAST_DUTY_CYCLE_2,
  31. };
  32. static uint32_t i;
  33. static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
  34. /*===========================================================================*/
  35. /* HTS221 related. */
  36. /*===========================================================================*/
  37. /* HTS221 Driver: This object represent an HTS221 instance */
  38. static HTS221Driver HTS221D1;
  39. static const HTS221Config hts221cfg = {
  40. &I2CD1,
  41. &i2ccfg,
  42. NULL,
  43. NULL,
  44. NULL,
  45. NULL,
  46. HTS221_ODR_7HZ
  47. };
  48. /*===========================================================================*/
  49. /* LPS22HB related. */
  50. /*===========================================================================*/
  51. /* LPS22HB Driver: This object represent an LPS22HB instance */
  52. static LPS22HBDriver LPS22HBD1;
  53. static const LPS22HBConfig lps22hbcfg = {
  54. &I2CD1,
  55. &i2ccfg,
  56. LPS22HB_SAD_VCC,
  57. NULL,
  58. NULL,
  59. NULL,
  60. NULL,
  61. LPS22HB_ODR_10HZ,
  62. };
  63. /*===========================================================================*/
  64. /* LSM303AGR related. */
  65. /*===========================================================================*/
  66. /* LSM303AGR Driver: This object represent an LSM303AGR instance */
  67. static LSM303AGRDriver LSM303AGRD1;
  68. static const LSM303AGRConfig lsm303agrcfg = {
  69. &I2CD1,
  70. &i2ccfg,
  71. NULL,
  72. NULL,
  73. LSM303AGR_ACC_FS_4G,
  74. LSM303AGR_ACC_ODR_100Hz,
  75. NULL,
  76. NULL,
  77. LSM303AGR_COMP_ODR_50HZ,
  78. };
  79. /*===========================================================================*/
  80. /* LSM6DSL related. */
  81. /*===========================================================================*/
  82. /* LSM6DSL Driver: This object represent an LSM6DSL instance */
  83. static LSM6DSLDriver LSM6DSLD1;
  84. static const LSM6DSLConfig lsm6dslcfg = {
  85. &I2CD1,
  86. &i2ccfg,
  87. LSM6DSL_SAD_VCC,
  88. NULL,
  89. NULL,
  90. LSM6DSL_ACC_FS_2G,
  91. LSM6DSL_GYRO_ODR_52Hz,
  92. NULL,
  93. NULL,
  94. LSM6DSL_GYRO_FS_250DPS,
  95. LSM6DSL_GYRO_ODR_104Hz
  96. };
  97. /*===========================================================================*/
  98. /* Generic code. */
  99. /*===========================================================================*/
  100. /*
  101. * Green LED blinker thread, times are in milliseconds.
  102. */
  103. static THD_WORKING_AREA(waThread1, 128);
  104. static THD_FUNCTION(Thread1, arg) {
  105. (void)arg;
  106. chRegSetThreadName("blinker");
  107. while (true) {
  108. palClearPad(GPIOA, GPIOA_LED_GREEN);
  109. chThdSleepMilliseconds(500);
  110. palSetPad(GPIOA, GPIOA_LED_GREEN);
  111. chThdSleepMilliseconds(500);
  112. }
  113. }
  114. /*
  115. * Application entry point.
  116. */
  117. int main(void) {
  118. /*
  119. * System initializations.
  120. * - HAL initialization, this also initializes the configured device drivers
  121. * and performs the board-specific initializations.
  122. * - Kernel initialization, the main() function becomes a thread and the
  123. * RTOS is active.
  124. */
  125. halInit();
  126. chSysInit();
  127. /* Configuring I2C SCK and I2C SDA related GPIOs .*/
  128. palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
  129. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  130. palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
  131. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  132. /*
  133. * Activates the serial driver 2 using the driver default configuration.
  134. */
  135. sdStart(&SD2, NULL);
  136. /*
  137. * Creates the blinker thread.
  138. */
  139. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);
  140. /* MEMS Driver Objects Initialization.*/
  141. hts221ObjectInit(&HTS221D1);
  142. lps22hbObjectInit(&LPS22HBD1);
  143. lsm303agrObjectInit(&LSM303AGRD1);
  144. lsm6dslObjectInit(&LSM6DSLD1);
  145. /* Activates all the MEMS related drivers.*/
  146. hts221Start(&HTS221D1, &hts221cfg);
  147. lps22hbStart(&LPS22HBD1, &lps22hbcfg);
  148. lsm303agrStart(&LSM303AGRD1, &lsm303agrcfg);
  149. lsm6dslStart(&LSM6DSLD1, &lsm6dslcfg);
  150. /*
  151. * Normal main() thread activity, in this demo it does nothing except
  152. * sleeping in a loop and check the button state.
  153. */
  154. while (true) {
  155. hts221HygrometerReadCooked(&HTS221D1, cooked);
  156. chprintf(chp, "HTS221D1 Hygrometer cooked data...\r\n");
  157. chprintf(chp, "Hum: %.2f\r\n", *cooked);
  158. hts221ThermometerReadCooked(&HTS221D1, cooked);
  159. chprintf(chp, "HTS221D1 Thermometer cooked data...\r\n");
  160. chprintf(chp, "Temp: %.2f\r\n", *cooked);
  161. lps22hbBarometerReadCooked(&LPS22HBD1, cooked);
  162. chprintf(chp, "LPS22HBD1 Barometer cooked data...\r\n");
  163. chprintf(chp, "Pres: %.2f\r\n", *cooked);
  164. lps22hbThermometerReadCooked(&LPS22HBD1, cooked);
  165. chprintf(chp, "LPS22HBD1 Thermometer cooked data...\r\n");
  166. chprintf(chp, "Temp: %.2f\r\n", *cooked);
  167. lsm303agrAccelerometerReadCooked(&LSM303AGRD1, cooked);
  168. chprintf(chp, "LSM303AGR Accelerometer cooked data...\r\n");
  169. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  170. chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]);
  171. }
  172. lsm303agrCompassReadCooked(&LSM303AGRD1, cooked);
  173. chprintf(chp, "LSM303AGR Compass cooked data...\r\n");
  174. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) {
  175. chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]);
  176. }
  177. lsm6dslAccelerometerReadCooked(&LSM6DSLD1, cooked);
  178. chprintf(chp, "LSM6DSL Accelerometer cooked data...\r\n");
  179. for(i = 0; i < LSM6DSL_ACC_NUMBER_OF_AXES; i++) {
  180. chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]);
  181. }
  182. lsm6dslGyroscopeReadCooked(&LSM6DSLD1, cooked);
  183. chprintf(chp, "LSM6DSL Gyroscope cooked data...\r\n");
  184. for(i = 0; i < LSM6DSL_GYRO_NUMBER_OF_AXES; i++) {
  185. chprintf(chp, "%c-axis: %.3f\r\n", axis_id[i], cooked[i]);
  186. }
  187. chThdSleepMilliseconds(500);
  188. cls(chp);
  189. }
  190. }