main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "l3gd20.h"
  17. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  18. /*===========================================================================*/
  19. /* L3GD20 related. */
  20. /*===========================================================================*/
  21. /* L3GD20 Driver: This object represent an L3GD20 instance.*/
  22. static L3GD20Driver L3GD20D1;
  23. static int32_t gyroraw[L3GD20_GYRO_NUMBER_OF_AXES];
  24. static float gyrocooked[L3GD20_GYRO_NUMBER_OF_AXES];
  25. static char axisID[L3GD20_GYRO_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
  26. static uint32_t i;
  27. static const SPIConfig spicfg = {
  28. FALSE,
  29. NULL,
  30. GPIOE,
  31. GPIOE_L3GD20_CS,
  32. SPI_CR1_BR | SPI_CR1_CPOL | SPI_CR1_CPHA,
  33. 0
  34. };
  35. static L3GD20Config l3gd20cfg = {
  36. &SPID1,
  37. &spicfg,
  38. NULL,
  39. NULL,
  40. L3GD20_FS_250DPS,
  41. L3GD20_ODR_760HZ,
  42. #if L3GD20_USE_ADVANCED
  43. L3GD20_BDU_CONTINUOUS,
  44. L3GD20_END_LITTLE,
  45. L3GD20_BW3,
  46. L3GD20_HPM_REFERENCE,
  47. L3GD20_HPCF_8,
  48. L3GD20_LP2M_ON,
  49. #endif
  50. };
  51. /*===========================================================================*/
  52. /* Generic code. */
  53. /*===========================================================================*/
  54. static BaseSequentialStream* chp = (BaseSequentialStream*)&SD1;
  55. /*
  56. * Red LED blinker thread, times are in milliseconds.
  57. */
  58. static THD_WORKING_AREA(waThread1, 128);
  59. static THD_FUNCTION(Thread1, arg) {
  60. (void)arg;
  61. chRegSetThreadName("blinker");
  62. while (true) {
  63. palToggleLine(LINE_LED3_RED);
  64. chThdSleepMilliseconds(250);
  65. }
  66. }
  67. /*
  68. * Application entry point.
  69. */
  70. int main(void) {
  71. /*
  72. * System initializations.
  73. * - HAL initialization, this also initializes the configured device drivers
  74. * and performs the board-specific initializations.
  75. * - Kernel initialization, the main() function becomes a thread and the
  76. * RTOS is active.
  77. */
  78. halInit();
  79. chSysInit();
  80. /* Activates the serial driver 1 using the driver default configuration.*/
  81. sdStart(&SD1, NULL);
  82. /* Creates the blinker thread.*/
  83. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  84. /* L3GD20 Object Initialization.*/
  85. l3gd20ObjectInit(&L3GD20D1);
  86. /* Activates the L3GD20 driver.*/
  87. l3gd20Start(&L3GD20D1, &l3gd20cfg);
  88. /* Normal main() thread activity, printing MEMS data on the SD1.*/
  89. while (true) {
  90. l3gd20GyroscopeReadRaw(&L3GD20D1, gyroraw);
  91. chprintf(chp, "L3GD20 Gyroscope raw data...\r\n");
  92. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  93. chprintf(chp, "%c-axis: %d\r\n", axisID[i], gyroraw[i]);
  94. }
  95. l3gd20GyroscopeReadCooked(&L3GD20D1, gyrocooked);
  96. chprintf(chp, "L3GD20 Gyroscope cooked data...\r\n");
  97. for(i = 0; i < L3GD20_GYRO_NUMBER_OF_AXES; i++) {
  98. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], gyrocooked[i]);
  99. }
  100. chThdSleepMilliseconds(100);
  101. cls(chp);
  102. }
  103. l3gd20Stop(&L3GD20D1);
  104. }