main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /*===========================================================================*/
  16. /* PWM driver related. */
  17. /*===========================================================================*/
  18. static void pwmpcb(PWMDriver *pwmp) {
  19. (void)pwmp;
  20. }
  21. static void pwmc1cb(PWMDriver *pwmp) {
  22. (void)pwmp;
  23. }
  24. static PWMConfig pwmcfg = {
  25. 10000, /* 10kHz PWM clock frequency. */
  26. 10000, /* Initial PWM period 1S. */
  27. pwmpcb,
  28. {
  29. {PWM_OUTPUT_ACTIVE_HIGH, pwmc1cb},
  30. {PWM_OUTPUT_DISABLED, NULL},
  31. {PWM_OUTPUT_DISABLED, NULL},
  32. {PWM_OUTPUT_DISABLED, NULL}
  33. },
  34. 0,
  35. 0
  36. };
  37. /*===========================================================================*/
  38. /* ICU driver related. */
  39. /*===========================================================================*/
  40. icucnt_t last_width, last_period;
  41. static void icuwidthcb(ICUDriver *icup) {
  42. palSetLine(LINE_ARD_D13);
  43. last_width = icuGetWidthX(icup);
  44. }
  45. static void icuperiodcb(ICUDriver *icup) {
  46. palClearLine(LINE_ARD_D13);
  47. last_period = icuGetPeriodX(icup);
  48. }
  49. static void icuovfcb(ICUDriver *icup) {
  50. (void)icup;
  51. }
  52. static ICUConfig icucfg = {
  53. ICU_INPUT_ACTIVE_HIGH,
  54. 10000, /* 10kHz ICU clock frequency. */
  55. icuwidthcb,
  56. icuperiodcb,
  57. icuovfcb,
  58. ICU_CHANNEL_1,
  59. 0
  60. };
  61. /*===========================================================================*/
  62. /* Application code. */
  63. /*===========================================================================*/
  64. /*
  65. * Application entry point.
  66. */
  67. int main(void) {
  68. /*
  69. * System initializations.
  70. * - HAL initialization, this also initializes the configured device drivers
  71. * and performs the board-specific initializations.
  72. * - Kernel initialization, the main() function becomes a thread and the
  73. * RTOS is active.
  74. */
  75. halInit();
  76. chSysInit();
  77. /*
  78. * Starting PWM driver 1 and enabling the notifications.
  79. * GPIOA8 is programmed as PWM output (channel 1 of TIM1).
  80. */
  81. pwmStart(&PWMD1, &pwmcfg);
  82. pwmEnablePeriodicNotification(&PWMD1);
  83. palSetLineMode(LINE_ARD_D5, PAL_MODE_ALTERNATE(1));
  84. /*
  85. * Starting ICU driver 2.
  86. * GPIOA15 is programmed as ICU input (channel 1 of TIM2).
  87. */
  88. icuStart(&ICUD2, &icucfg);
  89. palSetLineMode(LINE_ARD_D9, PAL_MODE_ALTERNATE(1));
  90. /*
  91. * GPIOI1 is programmed as output (board LED).
  92. */
  93. palClearLine(LINE_ARD_D13);
  94. palSetLineMode(LINE_ARD_D13, PAL_MODE_OUTPUT_PUSHPULL);
  95. chThdSleepMilliseconds(1000);
  96. /*
  97. * Starting ICU capture and enabling the notifications.
  98. */
  99. icuStartCapture(&ICUD2);
  100. icuEnableNotifications(&ICUD2);
  101. /*
  102. * Normal main() thread activity, various PWM patterns are generated
  103. * cyclically, if the ICU input is connected to the PWM output the
  104. * board LED mirrors the PWM output.
  105. */
  106. while (true) {
  107. /*
  108. * Starts the PWM channel 0 using 75% duty cycle.
  109. */
  110. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
  111. pwmEnableChannelNotification(&PWMD1, 0);
  112. chThdSleepMilliseconds(5000);
  113. /*
  114. * Changes the PWM channel 0 to 50% duty cycle.
  115. */
  116. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  117. chThdSleepMilliseconds(5000);
  118. /*
  119. * Changes the PWM channel 0 to 25% duty cycle.
  120. */
  121. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  122. chThdSleepMilliseconds(5000);
  123. /*
  124. * Changes PWM period to half second the duty cycle becomes 50%
  125. * implicitly.
  126. */
  127. pwmChangePeriod(&PWMD1, 5000);
  128. chThdSleepMilliseconds(5000);
  129. /*
  130. * Disables channel 0.
  131. */
  132. pwmDisableChannel(&PWMD1, 0);
  133. }
  134. }