main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "lsm303agr.h"
  17. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  18. /*===========================================================================*/
  19. /* LSM303AGR related. */
  20. /*===========================================================================*/
  21. /* LSM303AGR Driver: This object represent an LSM303AGR instance */
  22. static LSM303AGRDriver LSM303AGRD1;
  23. static int32_t accraw[LSM303AGR_ACC_NUMBER_OF_AXES];
  24. static int32_t compraw[LSM303AGR_COMP_NUMBER_OF_AXES];
  25. static float acccooked[LSM303AGR_ACC_NUMBER_OF_AXES];
  26. static float compcooked[LSM303AGR_COMP_NUMBER_OF_AXES];
  27. static char axisID[LSM303AGR_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 LSM303AGRConfig lsm303agrcfg = {
  35. &I2CD1,
  36. &i2ccfg,
  37. NULL,
  38. NULL,
  39. LSM303AGR_ACC_FS_4G,
  40. LSM303AGR_ACC_ODR_100Hz,
  41. #if LSM303AGR_USE_ADVANCED
  42. LSM303AGR_ACC_MODE_LPOW,
  43. LSM303AGR_ACC_BDU_BLOCK,
  44. LSM303AGR_ACC_END_LITTLE,
  45. #endif
  46. NULL,
  47. NULL,
  48. LSM303AGR_COMP_ODR_50HZ,
  49. #if LSM303AGR_USE_ADVANCED
  50. LSM303AGR_COMP_MODE_NORM,
  51. LSM303AGR_COMP_LPOW_EN
  52. #endif
  53. };
  54. /*===========================================================================*/
  55. /* Generic code. */
  56. /*===========================================================================*/
  57. static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
  58. /*
  59. * LED blinker thread, times are in milliseconds.
  60. */
  61. static THD_WORKING_AREA(waThread1, 128);
  62. static THD_FUNCTION(Thread1, arg) {
  63. (void)arg;
  64. chRegSetThreadName("blinker");
  65. while (true) {
  66. palToggleLine(LINE_LED_GREEN);
  67. chThdSleepMilliseconds(500);
  68. }
  69. }
  70. /*
  71. * Application entry point.
  72. */
  73. int main(void) {
  74. /*
  75. * System initializations.
  76. * - HAL initialization, this also initializes the configured device drivers
  77. * and performs the board-specific initializations.
  78. * - Kernel initialization, the main() function becomes a thread and the
  79. * RTOS is active.
  80. */
  81. halInit();
  82. chSysInit();
  83. /* Configuring I2C SCK and I2C SDA related GPIOs .*/
  84. palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
  85. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  86. palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
  87. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  88. /* Activates the serial driver 2 using the driver default configuration.*/
  89. sdStart(&SD2, NULL);
  90. /* Creates the blinker thread.*/
  91. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  92. /* LSM303AGR Object Initialization.*/
  93. lsm303agrObjectInit(&LSM303AGRD1);
  94. /* Activates the LSM303AGR driver.*/
  95. lsm303agrStart(&LSM303AGRD1, &lsm303agrcfg);
  96. /* Normal main() thread activity, printing MEMS data on the SD2. */
  97. while (true) {
  98. lsm303agrAccelerometerReadRaw(&LSM303AGRD1, accraw);
  99. chprintf(chp, "LSM303AGR Accelerometer raw data...\r\n");
  100. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  101. chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
  102. }
  103. lsm303agrCompassReadRaw(&LSM303AGRD1, compraw);
  104. chprintf(chp, "LSM303AGR Compass raw data...\r\n");
  105. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) {
  106. chprintf(chp, "%c-axis: %d\r\n", axisID[i], compraw[i]);
  107. }
  108. lsm303agrAccelerometerReadCooked(&LSM303AGRD1, acccooked);
  109. chprintf(chp, "LSM303AGR Accelerometer cooked data...\r\n");
  110. for(i = 0; i < LSM303AGR_ACC_NUMBER_OF_AXES; i++) {
  111. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]);
  112. }
  113. lsm303agrCompassReadCooked(&LSM303AGRD1, compcooked);
  114. chprintf(chp, "LSM303AGR Compass cooked data...\r\n");
  115. for(i = 0; i < LSM303AGR_COMP_NUMBER_OF_AXES; i++) {
  116. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], compcooked[i]);
  117. }
  118. chThdSleepMilliseconds(100);
  119. cls(chp);
  120. }
  121. lsm303agrStop(&LSM303AGRD1);
  122. }