main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 gyroliance 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 "lsm6dsl.h"
  17. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  18. /*===========================================================================*/
  19. /* LSM6DSL related. */
  20. /*===========================================================================*/
  21. /* LSM6DSL Driver: This object represent an LSM6DSL instance */
  22. static LSM6DSLDriver LSM6DSLD1;
  23. static int32_t accraw[LSM6DSL_ACC_NUMBER_OF_AXES];
  24. static int32_t gyroraw[LSM6DSL_GYRO_NUMBER_OF_AXES];
  25. static float acccooked[LSM6DSL_ACC_NUMBER_OF_AXES];
  26. static float gyrocooked[LSM6DSL_GYRO_NUMBER_OF_AXES];
  27. static char axisID[LSM6DSL_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
  28. static uint32_t i;
  29. static const I2CConfig i2ccfg = {
  30. OPMODE_I2C,
  31. 400000,
  32. FAST_DUTY_CYCLE_2,
  33. };
  34. static const LSM6DSLConfig lsm6dslcfg = {
  35. &I2CD1,
  36. &i2ccfg,
  37. LSM6DSL_SAD_VCC,
  38. NULL,
  39. NULL,
  40. LSM6DSL_ACC_FS_2G,
  41. LSM6DSL_GYRO_ODR_52Hz,
  42. #if LSM6DSL_USE_ADVANCED
  43. LSM6DSL_ACC_LP_ENABLED,
  44. #endif
  45. NULL,
  46. NULL,
  47. LSM6DSL_GYRO_FS_250DPS,
  48. LSM6DSL_GYRO_ODR_104Hz,
  49. #if LSM6DSL_USE_ADVANCED
  50. LSM6DSL_GYRO_LP_ENABLED,
  51. LSM6DSL_GYRO_LPF_FTYPE1,
  52. #endif
  53. #if LSM6DSL_USE_ADVANCED
  54. LSM6DSL_BDU_BLOCKED,
  55. LSM6DSL_END_LITTLE
  56. #endif
  57. };
  58. /*===========================================================================*/
  59. /* Generic code. */
  60. /*===========================================================================*/
  61. static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
  62. /*
  63. * LED blinker thread, times are in milliseconds.
  64. */
  65. static THD_WORKING_AREA(waThread1, 128);
  66. static THD_FUNCTION(Thread1, arg) {
  67. (void)arg;
  68. chRegSetThreadName("blinker");
  69. while (true) {
  70. palToggleLine(LINE_LED_GREEN);
  71. chThdSleepMilliseconds(500);
  72. }
  73. }
  74. /*
  75. * Application entry point.
  76. */
  77. int main(void) {
  78. /*
  79. * System initializations.
  80. * - HAL initialization, this also initializes the configured device drivers
  81. * and performs the board-specific initializations.
  82. * - Kernel initialization, the main() function becomes a thread and the
  83. * RTOS is active.
  84. */
  85. halInit();
  86. chSysInit();
  87. /* Configuring I2C SCK and I2C SDA related GPIOs .*/
  88. palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
  89. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  90. palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
  91. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  92. /* Activates the serial driver 2 using the driver default configuration.*/
  93. sdStart(&SD2, NULL);
  94. /* Creates the blinker thread.*/
  95. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);
  96. /* LSM6DSL Object Initialization.*/
  97. lsm6dslObjectInit(&LSM6DSLD1);
  98. /* Activates the LSM6DSL driver.*/
  99. lsm6dslStart(&LSM6DSLD1, &lsm6dslcfg);
  100. lsm6dslGyroscopeSampleBias(&LSM6DSLD1);
  101. /* Normal main() thread activity, printing MEMS data on the SDU1.*/
  102. while (true) {
  103. lsm6dslAccelerometerReadRaw(&LSM6DSLD1, accraw);
  104. chprintf(chp, "LSM6DSL Accelerometer raw data...\r\n");
  105. for(i = 0; i < LSM6DSL_ACC_NUMBER_OF_AXES; i++) {
  106. chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
  107. }
  108. lsm6dslGyroscopeReadRaw(&LSM6DSLD1, gyroraw);
  109. chprintf(chp, "LSM6DSL Gyroscope raw data...\r\n");
  110. for(i = 0; i < LSM6DSL_GYRO_NUMBER_OF_AXES; i++) {
  111. chprintf(chp, "%c-axis: %d\r\n", axisID[i], gyroraw[i]);
  112. }
  113. lsm6dslAccelerometerReadCooked(&LSM6DSLD1, acccooked);
  114. chprintf(chp, "LSM6DSL Accelerometer cooked data...\r\n");
  115. for(i = 0; i < LSM6DSL_ACC_NUMBER_OF_AXES; i++) {
  116. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]);
  117. }
  118. lsm6dslGyroscopeReadCooked(&LSM6DSLD1, gyrocooked);
  119. chprintf(chp, "LSM6DSL Gyroscope cooked data...\r\n");
  120. for(i = 0; i < LSM6DSL_GYRO_NUMBER_OF_AXES; i++) {
  121. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], gyrocooked[i]);
  122. }
  123. chThdSleepMilliseconds(100);
  124. cls(chp);
  125. }
  126. lsm6dslStop(&LSM6DSLD1);
  127. }