main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "lsm6ds0.h"
  17. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  18. /*===========================================================================*/
  19. /* LSM6DS0 related. */
  20. /*===========================================================================*/
  21. /* LSM6DS0 Driver: This object represent an LSM6DS0 instance */
  22. static LSM6DS0Driver LSM6DS0D1;
  23. static int32_t accraw[LSM6DS0_ACC_NUMBER_OF_AXES];
  24. static int32_t gyroraw[LSM6DS0_GYRO_NUMBER_OF_AXES];
  25. static float acccooked[LSM6DS0_ACC_NUMBER_OF_AXES];
  26. static float gyrocooked[LSM6DS0_GYRO_NUMBER_OF_AXES];
  27. static char axisID[LSM6DS0_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 LSM6DS0Config lsm6ds0cfg = {
  35. &I2CD1,
  36. &i2ccfg,
  37. LSM6DS0_SAD_VCC,
  38. NULL,
  39. NULL,
  40. LSM6DS0_ACC_FS_2G,
  41. LSM6DS0_ACC_ODR_50Hz,
  42. #if LSM6DS0_USE_ADVANCED
  43. LSM6DS0_ACC_DEC_X4,
  44. #endif
  45. NULL,
  46. NULL,
  47. LSM6DS0_GYRO_FS_245DPS,
  48. LSM6DS0_GYRO_ODR_119HZ_FC_31,
  49. #if LSM6DS0_USE_ADVANCED
  50. LSM6DS0_GYRO_LP_DISABLED,
  51. LSM6DS0_GYRO_OUT_SEL_0,
  52. LSM6DS0_GYRO_HP_DISABLED,
  53. LSM6DS0_GYRO_HPCF_0,
  54. #endif
  55. #if LSM6DS0_USE_ADVANCED
  56. LSM6DS0_BDU_BLOCKED,
  57. LSM6DS0_END_LITTLE
  58. #endif
  59. };
  60. /*===========================================================================*/
  61. /* Generic code. */
  62. /*===========================================================================*/
  63. static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
  64. /*
  65. * LED blinker thread, times are in milliseconds.
  66. */
  67. static THD_WORKING_AREA(waThread1, 128);
  68. static THD_FUNCTION(Thread1, arg) {
  69. (void)arg;
  70. chRegSetThreadName("blinker");
  71. while (true) {
  72. palToggleLine(LINE_LED_GREEN);
  73. chThdSleepMilliseconds(500);
  74. }
  75. }
  76. /*
  77. * Application entry point.
  78. */
  79. int main(void) {
  80. /*
  81. * System initializations.
  82. * - HAL initialization, this also initializes the configured device drivers
  83. * and performs the board-specific initializations.
  84. * - Kernel initialization, the main() function becomes a thread and the
  85. * RTOS is active.
  86. */
  87. halInit();
  88. chSysInit();
  89. /* Configuring I2C SCK and I2C SDA related GPIOs .*/
  90. palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
  91. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  92. palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
  93. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  94. /* Activates the serial driver 2 using the driver default configuration.*/
  95. sdStart(&SD2, NULL);
  96. /* Creates the blinker thread.*/
  97. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);
  98. /* LSM6DS0 Object Initialization.*/
  99. lsm6ds0ObjectInit(&LSM6DS0D1);
  100. /* Activates the LSM6DS0 driver.*/
  101. lsm6ds0Start(&LSM6DS0D1, &lsm6ds0cfg);
  102. lsm6ds0GyroscopeSampleBias(&LSM6DS0D1);
  103. /* Normal main() thread activity, printing MEMS data on the SDU1.*/
  104. while (true) {
  105. lsm6ds0AccelerometerReadRaw(&LSM6DS0D1, accraw);
  106. chprintf(chp, "LSM6DS0 Accelerometer raw data...\r\n");
  107. for(i = 0; i < LSM6DS0_ACC_NUMBER_OF_AXES; i++) {
  108. chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
  109. }
  110. lsm6ds0GyroscopeReadRaw(&LSM6DS0D1, gyroraw);
  111. chprintf(chp, "LSM6DS0 Gyroscope raw data...\r\n");
  112. for(i = 0; i < LSM6DS0_GYRO_NUMBER_OF_AXES; i++) {
  113. chprintf(chp, "%c-axis: %d\r\n", axisID[i], gyroraw[i]);
  114. }
  115. lsm6ds0AccelerometerReadCooked(&LSM6DS0D1, acccooked);
  116. chprintf(chp, "LSM6DS0 Accelerometer cooked data...\r\n");
  117. for(i = 0; i < LSM6DS0_ACC_NUMBER_OF_AXES; i++) {
  118. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]);
  119. }
  120. lsm6ds0GyroscopeReadCooked(&LSM6DS0D1, gyrocooked);
  121. chprintf(chp, "LSM6DS0 Gyroscope cooked data...\r\n");
  122. for(i = 0; i < LSM6DS0_GYRO_NUMBER_OF_AXES; i++) {
  123. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], gyrocooked[i]);
  124. }
  125. chThdSleepMilliseconds(100);
  126. cls(chp);
  127. }
  128. lsm6ds0Stop(&LSM6DS0D1);
  129. }