main.c 3.9 KB

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