main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "lsm303dlhc.h"
  17. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  18. /*===========================================================================*/
  19. /* LSM303DLHC related. */
  20. /*===========================================================================*/
  21. /* LSM303DLHC Driver: This object represent an LSM303DLHC instance */
  22. static LSM303DLHCDriver LSM303DLHCD1;
  23. static int32_t accraw[LSM303DLHC_ACC_NUMBER_OF_AXES];
  24. static int32_t compraw[LSM303DLHC_COMP_NUMBER_OF_AXES];
  25. static float acccooked[LSM303DLHC_ACC_NUMBER_OF_AXES];
  26. static float compcooked[LSM303DLHC_COMP_NUMBER_OF_AXES];
  27. static char axisID[LSM303DLHC_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
  28. static uint32_t i;
  29. static const I2CConfig i2ccfg = {
  30. STM32_TIMINGR_PRESC(15U) |
  31. STM32_TIMINGR_SCLDEL(4U) | STM32_TIMINGR_SDADEL(2U) |
  32. STM32_TIMINGR_SCLH(15U) | STM32_TIMINGR_SCLL(21U),
  33. 0,
  34. 0
  35. };
  36. static const LSM303DLHCConfig lsm303dlhccfg = {
  37. &I2CD1,
  38. &i2ccfg,
  39. NULL,
  40. NULL,
  41. LSM303DLHC_ACC_FS_4G,
  42. LSM303DLHC_ACC_ODR_100Hz,
  43. #if LSM303DLHC_USE_ADVANCED
  44. LSM303DLHC_ACC_LP_DISABLED,
  45. LSM303DLHC_ACC_HR_DISABLED,
  46. LSM303DLHC_ACC_BDU_BLOCK,
  47. LSM303DLHC_ACC_END_LITTLE,
  48. #endif
  49. NULL,
  50. NULL,
  51. LSM303DLHC_COMP_FS_1P3GA,
  52. LSM303DLHC_COMP_ODR_30HZ,
  53. #if LSM303DLHC_USE_ADVANCED
  54. LSM303DLHC_COMP_MD_BLOCK
  55. #endif
  56. };
  57. /*===========================================================================*/
  58. /* Generic code. */
  59. /*===========================================================================*/
  60. static BaseSequentialStream* chp = (BaseSequentialStream*)&SD1;
  61. /*
  62. * Red LED blinker thread, times are in milliseconds.
  63. */
  64. static THD_WORKING_AREA(waThread1, 128);
  65. static THD_FUNCTION(Thread1, arg) {
  66. (void)arg;
  67. chRegSetThreadName("blinker");
  68. while (true) {
  69. palToggleLine(LINE_LED3_RED);
  70. chThdSleepMilliseconds(250);
  71. }
  72. }
  73. /*
  74. * Application entry point.
  75. */
  76. int main(void) {
  77. /*
  78. * System initializations.
  79. * - HAL initialization, this also initializes the configured device drivers
  80. * and performs the board-specific initializations.
  81. * - Kernel initialization, the main() function becomes a thread and the
  82. * RTOS is active.
  83. */
  84. halInit();
  85. chSysInit();
  86. /* Activates the serial driver 1 using the driver default configuration.*/
  87. sdStart(&SD1, NULL);
  88. /* Creates the blinker thread.*/
  89. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  90. /* LSM303DLHC Object Initialization.*/
  91. lsm303dlhcObjectInit(&LSM303DLHCD1);
  92. /* Activates the LSM303DLHC driver.*/
  93. lsm303dlhcStart(&LSM303DLHCD1, &lsm303dlhccfg);
  94. /* Normal main() thread activity, printing MEMS data on the SD1. */
  95. while (true) {
  96. lsm303dlhcAccelerometerReadRaw(&LSM303DLHCD1, accraw);
  97. chprintf(chp, "LSM303DLHC Accelerometer raw data...\r\n");
  98. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  99. chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
  100. }
  101. lsm303dlhcCompassReadRaw(&LSM303DLHCD1, compraw);
  102. chprintf(chp, "LSM303DLHC Compass raw data...\r\n");
  103. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  104. chprintf(chp, "%c-axis: %d\r\n", axisID[i], compraw[i]);
  105. }
  106. lsm303dlhcAccelerometerReadCooked(&LSM303DLHCD1, acccooked);
  107. chprintf(chp, "LSM303DLHC Accelerometer cooked data...\r\n");
  108. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  109. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]);
  110. }
  111. lsm303dlhcCompassReadCooked(&LSM303DLHCD1, compcooked);
  112. chprintf(chp, "LSM303DLHC Compass cooked data...\r\n");
  113. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  114. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], compcooked[i]);
  115. }
  116. chThdSleepMilliseconds(100);
  117. cls(chp);
  118. }
  119. lsm303dlhcStop(&LSM303DLHCD1);
  120. }