main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /*
  14. Concepts and parts of this file have been contributed by Uladzimir Pylinsky
  15. aka barthess.
  16. */
  17. /*
  18. This demo:
  19. 1) turns LED on when you incline (~45 deg.) or shake the board;
  20. 2) correctly handles absence of slave device on bus.
  21. */
  22. #include <stdlib.h>
  23. #include <math.h>
  24. #include "ch.h"
  25. #include "hal.h"
  26. #include "lis3.h"
  27. #include "fake.h"
  28. /* measured acceleration components {x,y,z} */
  29. static float acc[3];
  30. /*
  31. * Accelerometer polling thread.
  32. */
  33. static THD_WORKING_AREA(PollAccelThreadWA, 256);
  34. static THD_FUNCTION(PollAccelThread, arg) {
  35. (void)arg;
  36. chRegSetThreadName("PollAccel");
  37. while (true) {
  38. osalThreadSleepMilliseconds(32);
  39. lis3GetAcc(acc);
  40. }
  41. }
  42. /*
  43. * Fake polling thread.
  44. */
  45. static THD_WORKING_AREA(PollFakeThreadWA, 256);
  46. static THD_FUNCTION(PollFakeThread, arg) {
  47. (void)arg;
  48. chRegSetThreadName("PollFake");
  49. while (true) {
  50. osalThreadSleepMilliseconds(16);
  51. request_fake();
  52. }
  53. }
  54. /*
  55. * I2C1 config.
  56. */
  57. static const I2CConfig i2cfg1 = {
  58. OPMODE_I2C,
  59. 400000,
  60. FAST_DUTY_CYCLE_2,
  61. };
  62. /*
  63. * Entry point, note, the main() function is already a thread in the system
  64. * on entry.
  65. */
  66. int main(void) {
  67. halInit();
  68. chSysInit();
  69. i2cStart(&I2CD1, &i2cfg1);
  70. lis3Start();
  71. /* Create accelerometer thread. */
  72. chThdCreateStatic(PollAccelThreadWA,
  73. sizeof(PollAccelThreadWA),
  74. NORMALPRIO,
  75. PollAccelThread,
  76. NULL);
  77. /* Create not responding thread. */
  78. chThdCreateStatic(PollFakeThreadWA,
  79. sizeof(PollFakeThreadWA),
  80. NORMALPRIO,
  81. PollFakeThread,
  82. NULL);
  83. /* main loop handles LED */
  84. while (true) {
  85. if (sqrtf(acc[0]*acc[0] + acc[1]*acc[1]) > 0.5)
  86. palClearPad(IOPORT3, GPIOC_LED); /* on */
  87. else
  88. palSetPad(IOPORT3, GPIOC_LED); /* off */
  89. osalThreadSleepMilliseconds(20);
  90. }
  91. return 0;
  92. }