main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "l3gd20.h"
  18. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  19. /*===========================================================================*/
  20. /* L3GD20 related. */
  21. /*===========================================================================*/
  22. /* L3GD20 Driver: This object represent an L3GD20 instance.*/
  23. static L3GD20Driver L3GD20D1;
  24. static int32_t gyroraw[L3GD20_GYRO_NUMBER_OF_AXES];
  25. static float gyrocooked[L3GD20_GYRO_NUMBER_OF_AXES];
  26. static char axisID[L3GD20_GYRO_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
  27. static uint32_t i;
  28. static const SPIConfig spicfg = {
  29. FALSE,
  30. NULL,
  31. GPIOE,
  32. GPIOE_L3GD20_CS,
  33. SPI_CR1_BR_0 | SPI_CR1_CPOL | SPI_CR1_CPHA,
  34. 0
  35. };
  36. static L3GD20Config l3gd20cfg = {
  37. &SPID1,
  38. &spicfg,
  39. NULL,
  40. NULL,
  41. L3GD20_FS_250DPS,
  42. L3GD20_ODR_760HZ,
  43. #if L3GD20_USE_ADVANCED
  44. L3GD20_BDU_CONTINUOUS,
  45. L3GD20_END_LITTLE,
  46. L3GD20_BW3,
  47. L3GD20_HPM_REFERENCE,
  48. L3GD20_HPCF_8,
  49. L3GD20_LP2M_ON,
  50. #endif
  51. };
  52. /*===========================================================================*/
  53. /* Generic code. */
  54. /*===========================================================================*/
  55. static BaseSequentialStream* chp = (BaseSequentialStream*)&SDU1;
  56. /*
  57. * LED blinker thread, times are in milliseconds.
  58. */
  59. static THD_WORKING_AREA(waThread1, 128);
  60. static THD_FUNCTION(Thread1, arg) {
  61. (void)arg;
  62. chRegSetThreadName("blinker");
  63. while (true) {
  64. systime_t time;
  65. time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
  66. palClearLine(LINE_LED5);
  67. chThdSleepMilliseconds(time);
  68. palSetLine(LINE_LED5);
  69. chThdSleepMilliseconds(time);
  70. }
  71. }
  72. /*
  73. * Application entry point.
  74. */
  75. int main(void) {
  76. /*
  77. * System initializations.
  78. * - HAL initialization, this also initializes the configured device drivers
  79. * and performs the board-specific initializations.
  80. * - Kernel initialization, the main() function becomes a thread and the
  81. * RTOS is active.
  82. */
  83. halInit();
  84. chSysInit();
  85. /* Initializes a serial-over-USB CDC driver.*/
  86. sduObjectInit(&SDU1);
  87. sduStart(&SDU1, &serusbcfg);
  88. /*
  89. * Activates the USB driver and then the USB bus pull-up on D+.
  90. * Note, a delay is inserted in order to not have to disconnect the cable
  91. * after a reset.
  92. */
  93. usbDisconnectBus(serusbcfg.usbp);
  94. chThdSleepMilliseconds(1500);
  95. usbStart(serusbcfg.usbp, &usbcfg);
  96. usbConnectBus(serusbcfg.usbp);
  97. /* Creates the blinker thread.*/
  98. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  99. /* L3GD20 Object Initialization.*/
  100. l3gd20ObjectInit(&L3GD20D1);
  101. /* Activates the L3GD20 driver.*/
  102. l3gd20Start(&L3GD20D1, &l3gd20cfg);
  103. /* Normal main() thread activity, printing MEMS data on the SDU1.*/
  104. while (true) {
  105. l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw);
  106. chprintf(chp, "L3GD20 Gyroscope raw data...\r\n");
  107. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  108. chprintf(chp, "%c-axis: %d\r\n", axisID[i], gyroraw[i]);
  109. }
  110. l3gd20GyroscopeReadCooked(&L3GD20D1, gyrocooked);
  111. chprintf(chp, "L3GD20 Gyroscope cooked data...\r\n");
  112. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  113. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], gyrocooked[i]);
  114. }
  115. chThdSleepMilliseconds(100);
  116. cls(chp);
  117. }
  118. l3gd20Stop(&L3GD20D1);
  119. }