main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #define LED 7
  17. static icucnt_t width = 0;
  18. static icucnt_t period = 0;
  19. static thread_t *thread1 = NULL;
  20. static thread_t *thread2 = NULL;
  21. static thread_t *thread_main = NULL;
  22. static const uint32_t ICU_CLK = F_CPU / 1024;
  23. void width_cb(ICUDriver *icup) {
  24. width = icup->width;
  25. }
  26. void period_cb(ICUDriver *icup) {
  27. period = icup->period;
  28. chSysLockFromISR();
  29. chEvtSignalI(thread1, (eventmask_t) 1);
  30. chSysUnlockFromISR();
  31. }
  32. void overflow_cb(ICUDriver *icup) {
  33. chSysLockFromISR();
  34. chEvtSignalI(thread2, (eventmask_t) 1);
  35. chSysUnlockFromISR();
  36. }
  37. void output_single_cycle(const uint16_t low, const uint16_t high) {
  38. palClearPad(IOPORT2, LED);
  39. palClearPad(IOPORT4, 4);
  40. chThdSleepMilliseconds(low);
  41. palSetPad(IOPORT2, LED);
  42. palSetPad(IOPORT4, 4);
  43. chThdSleepMilliseconds(high);
  44. palClearPad(IOPORT2, LED);
  45. palClearPad(IOPORT4, 4);
  46. }
  47. static THD_WORKING_AREA(waThread1, 64);
  48. static THD_FUNCTION(Thread1, arg) {
  49. BaseSequentialStream *serp = (BaseSequentialStream *) &SD1;
  50. thread1 = chThdGetSelfX();
  51. while (TRUE) {
  52. chEvtWaitAny((eventmask_t) 1);
  53. chprintf(serp, "WIDTH[%lu ms, %u ticks] PERIOD[%lu ms, %u ticks]\r\n",
  54. ((uint32_t) width * 1000) / ICU_CLK,
  55. width,
  56. ((uint32_t) period * 1000) / ICU_CLK,
  57. period);
  58. chEvtSignal(thread_main, (eventmask_t) 1);
  59. }
  60. }
  61. static THD_WORKING_AREA(waThread2, 64);
  62. static THD_FUNCTION(Thread2, arg) {
  63. BaseSequentialStream *serp = (BaseSequentialStream *) &SD1;
  64. thread2 = chThdGetSelfX();
  65. while (TRUE) {
  66. chEvtWaitAny((eventmask_t) 1);
  67. chprintf(serp, "OVERFLOW\r\n");
  68. chEvtSignal(thread_main, (eventmask_t) 2);
  69. }
  70. }
  71. int main(void) {
  72. halInit();
  73. static ICUConfig icu3cfg = {
  74. ICU_INPUT_ACTIVE_HIGH,
  75. 0, /* bogus frequency */
  76. width_cb,
  77. period_cb,
  78. overflow_cb,
  79. };
  80. palSetPadMode(IOPORT2, LED, PAL_MODE_OUTPUT_PUSHPULL);
  81. palSetPadMode(IOPORT4, 4, PAL_MODE_OUTPUT_PUSHPULL);
  82. palClearPad(IOPORT2, LED);
  83. palClearPad(IOPORT4, 4);
  84. sdStart(&SD1, NULL);
  85. icuStart(&ICUD3, &icu3cfg);
  86. chSysInit();
  87. thread_main = chThdGetSelfX();
  88. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  89. chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);
  90. while (1) {
  91. BaseSequentialStream *serp = (BaseSequentialStream *) &SD1;
  92. chprintf(serp, "Testing 50 duty cycle\r\n");
  93. icuStartCapture(&ICUD3);
  94. output_single_cycle(500, 500);
  95. icuStopCapture(&ICUD3);
  96. chEvtWaitAny((eventmask_t) 3);
  97. chprintf(serp, "Testing 25 duty cycle\r\n");
  98. icuStartCapture(&ICUD3);
  99. output_single_cycle(250, 750);
  100. icuStopCapture(&ICUD3);
  101. chEvtWaitAny((eventmask_t) 3);
  102. chprintf(serp, "Testing 75 duty cycle\r\n");
  103. icuStartCapture(&ICUD3);
  104. output_single_cycle(750, 250);
  105. icuStopCapture(&ICUD3);
  106. chEvtWaitAny((eventmask_t) 3);
  107. chprintf(serp, "Testing overflow\r\n");
  108. icuStartCapture(&ICUD3);
  109. chEvtWaitAny((eventmask_t) 3);
  110. icuStopCapture(&ICUD3);
  111. }
  112. }