main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "usbcfg.h"
  16. #include "chprintf.h"
  17. #include "lsm303dlhc.h"
  18. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  19. /*===========================================================================*/
  20. /* LSM303DLHC related. */
  21. /*===========================================================================*/
  22. /* LSM303DLHC Driver: This object represent an LSM303DLHC instance */
  23. static LSM303DLHCDriver LSM303DLHCD1;
  24. static int32_t accraw[LSM303DLHC_ACC_NUMBER_OF_AXES];
  25. static int32_t compraw[LSM303DLHC_COMP_NUMBER_OF_AXES];
  26. static float acccooked[LSM303DLHC_ACC_NUMBER_OF_AXES];
  27. static float compcooked[LSM303DLHC_COMP_NUMBER_OF_AXES];
  28. static char axisID[LSM303DLHC_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
  29. static uint32_t i;
  30. static const I2CConfig i2ccfg = {
  31. OPMODE_I2C,
  32. 400000,
  33. FAST_DUTY_CYCLE_2,
  34. };
  35. static const LSM303DLHCConfig lsm303dlhccfg = {
  36. &I2CD1,
  37. &i2ccfg,
  38. NULL,
  39. NULL,
  40. LSM303DLHC_ACC_FS_4G,
  41. LSM303DLHC_ACC_ODR_100Hz,
  42. #if LSM303DLHC_USE_ADVANCED
  43. LSM303DLHC_ACC_LP_DISABLED,
  44. LSM303DLHC_ACC_HR_DISABLED,
  45. LSM303DLHC_ACC_BDU_BLOCK,
  46. LSM303DLHC_ACC_END_LITTLE,
  47. #endif
  48. NULL,
  49. NULL,
  50. LSM303DLHC_COMP_FS_1P3GA,
  51. LSM303DLHC_COMP_ODR_30HZ,
  52. #if LSM303DLHC_USE_ADVANCED
  53. LSM303DLHC_COMP_MD_BLOCK
  54. #endif
  55. };
  56. /*===========================================================================*/
  57. /* Generic code. */
  58. /*===========================================================================*/
  59. static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1;
  60. /*
  61. * LED blinker thread, times are in milliseconds.
  62. */
  63. static THD_WORKING_AREA(waThread1, 128);
  64. static THD_FUNCTION(Thread1, arg) {
  65. (void)arg;
  66. chRegSetThreadName("blinker");
  67. while (true) {
  68. systime_t time;
  69. time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
  70. palToggleLine(LINE_LED5);
  71. chThdSleepMilliseconds(time);
  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. /* Initializes a serial-over-USB CDC driver.*/
  88. sduObjectInit(&SDU1);
  89. sduStart(&SDU1, &serusbcfg);
  90. /*
  91. * Activates the USB driver and then the USB bus pull-up on D+.
  92. * Note, a delay is inserted in order to not have to disconnect the cable
  93. * after a reset.
  94. */
  95. usbDisconnectBus(serusbcfg.usbp);
  96. chThdSleepMilliseconds(1500);
  97. usbStart(serusbcfg.usbp, &usbcfg);
  98. usbConnectBus(serusbcfg.usbp);
  99. /* Creates the blinker thread.*/
  100. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 1, Thread1, NULL);
  101. /* LSM303DLHC Object Initialization.*/
  102. lsm303dlhcObjectInit(&LSM303DLHCD1);
  103. /* Activates the LSM303DLHC driver.*/
  104. lsm303dlhcStart(&LSM303DLHCD1, &lsm303dlhccfg);
  105. /* Normal main() thread activity, printing MEMS data on the SDU1.*/
  106. while (true) {
  107. lsm303dlhcAccelerometerReadRaw(&LSM303DLHCD1, accraw);
  108. chprintf(chp, "LSM303DLHC Accelerometer raw data...\r\n");
  109. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  110. chprintf(chp, "%c-axis: %d\r\n", axisID[i], accraw[i]);
  111. }
  112. lsm303dlhcCompassReadRaw(&LSM303DLHCD1, compraw);
  113. chprintf(chp, "LSM303DLHC Compass raw data...\r\n");
  114. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  115. chprintf(chp, "%c-axis: %d\r\n", axisID[i], compraw[i]);
  116. }
  117. lsm303dlhcAccelerometerReadCooked(&LSM303DLHCD1, acccooked);
  118. chprintf(chp, "LSM303DLHC Accelerometer cooked data...\r\n");
  119. for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++) {
  120. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], acccooked[i]);
  121. }
  122. lsm303dlhcCompassReadCooked(&LSM303DLHCD1, compcooked);
  123. chprintf(chp, "LSM303DLHC Compass cooked data...\r\n");
  124. for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++) {
  125. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], compcooked[i]);
  126. }
  127. chThdSleepMilliseconds(100);
  128. cls(chp);
  129. }
  130. lsm303dlhcStop(&LSM303DLHCD1);
  131. }