main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "lis3mdl.h"
  17. #define cls(chp) chprintf(chp, "\033[2J\033[1;1H")
  18. /*===========================================================================*/
  19. /* LIS3MDL related. */
  20. /*===========================================================================*/
  21. /* LIS3MDL Driver: This object represent an LIS3MDL instance.*/
  22. static LIS3MDLDriver LIS3MDLD1;
  23. static int32_t compraw[LIS3MDL_COMP_NUMBER_OF_AXES];
  24. static float compcooked[LIS3MDL_COMP_NUMBER_OF_AXES];
  25. static char axisID[LIS3MDL_COMP_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
  26. static uint32_t i;
  27. static const I2CConfig i2ccfg = {
  28. OPMODE_I2C,
  29. 400000,
  30. FAST_DUTY_CYCLE_2,
  31. };
  32. static LIS3MDLConfig lis3mdlcfg = {
  33. &I2CD1,
  34. &i2ccfg,
  35. LIS3MDL_SAD_VCC,
  36. NULL,
  37. NULL,
  38. LIS3MDL_COMP_FS_4GA,
  39. LIS3MDL_COMP_ODR_40HZ,
  40. #if LIS3MDL_USE_ADVANCED
  41. LIS3MDL_COMP_LP_ENABLED,
  42. LIS3MDL_COMP_MD_CONTINUOUS,
  43. LIS3MDL_COMP_OMXY_LP,
  44. LIS3MDL_COMP_OMZ_LP,
  45. LIS3MDL_BDU_CONTINUOUS,
  46. LIS3MDL_END_LITTLE
  47. #endif
  48. };
  49. /*===========================================================================*/
  50. /* Generic code. */
  51. /*===========================================================================*/
  52. static BaseSequentialStream* chp = (BaseSequentialStream*)&SD2;
  53. /*
  54. * LED blinker thread, times are in milliseconds.
  55. */
  56. static THD_WORKING_AREA(waThread1, 128);
  57. static THD_FUNCTION(Thread1, arg) {
  58. (void)arg;
  59. chRegSetThreadName("blinker");
  60. while (true) {
  61. palToggleLine(LINE_LED_GREEN);
  62. chThdSleepMilliseconds(500);
  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. /* Configuring I2C SCK and I2C SDA related GPIOs .*/
  79. palSetLineMode(LINE_ARD_D15, PAL_MODE_ALTERNATE(4) |
  80. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  81. palSetLineMode(LINE_ARD_D14, PAL_MODE_ALTERNATE(4) |
  82. PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN);
  83. /* Activates the serial driver 1 using the driver default configuration.*/
  84. sdStart(&SD2, NULL);
  85. /* Creates the blinker thread.*/
  86. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  87. /* LIS3MDL Object Initialization.*/
  88. lis3mdlObjectInit(&LIS3MDLD1);
  89. /* Activates the LIS3MDL driver.*/
  90. lis3mdlStart(&LIS3MDLD1, &lis3mdlcfg);
  91. /* Normal main() thread activity, printing MEMS data on the SD2. */
  92. while (true) {
  93. lis3mdlCompassReadRaw(&LIS3MDLD1, compraw);
  94. chprintf(chp, "LIS3MDL Compass raw data...\r\n");
  95. for(i = 0; i < LIS3MDL_COMP_NUMBER_OF_AXES; i++) {
  96. chprintf(chp, "%c-axis: %d\r\n", axisID[i], compraw[i]);
  97. }
  98. lis3mdlCompassReadCooked(&LIS3MDLD1, compcooked);
  99. chprintf(chp, "LIS3MDL Compass cooked data...\r\n");
  100. for(i = 0; i < LIS3MDL_COMP_NUMBER_OF_AXES; i++) {
  101. chprintf(chp, "%c-axis: %.3f\r\n", axisID[i], compcooked[i]);
  102. }
  103. chThdSleepMilliseconds(100);
  104. cls(chp);
  105. }
  106. lis3mdlStop(&LIS3MDLD1);
  107. }