main.c 3.9 KB

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