main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. static void pwmpcb(PWMDriver *pwmp) {
  16. (void)pwmp;
  17. palClearPad(GPIOC, GPIOC_LED4);
  18. }
  19. static void pwmc1cb(PWMDriver *pwmp) {
  20. (void)pwmp;
  21. palSetPad(GPIOC, GPIOC_LED4);
  22. }
  23. static PWMConfig pwmcfg = {
  24. 10000, /* 10kHz PWM clock frequency. */
  25. 10000, /* Initial PWM period 1S. */
  26. pwmpcb,
  27. {
  28. {PWM_OUTPUT_ACTIVE_HIGH, pwmc1cb},
  29. {PWM_OUTPUT_DISABLED, NULL},
  30. {PWM_OUTPUT_DISABLED, NULL},
  31. {PWM_OUTPUT_DISABLED, NULL}
  32. },
  33. 0,
  34. 0
  35. };
  36. icucnt_t last_width, last_period;
  37. static void icuwidthcb(ICUDriver *icup) {
  38. palSetPad(GPIOC, GPIOC_LED3);
  39. last_width = icuGetWidthX(icup);
  40. }
  41. static void icuperiodcb(ICUDriver *icup) {
  42. palClearPad(GPIOC, GPIOC_LED3);
  43. last_period = icuGetPeriodX(icup);
  44. }
  45. static void icuoverflowcb(ICUDriver *icup) {
  46. (void)icup;
  47. }
  48. static ICUConfig icucfg = {
  49. ICU_INPUT_ACTIVE_HIGH,
  50. 10000, /* 10kHz ICU clock frequency. */
  51. icuwidthcb,
  52. icuperiodcb,
  53. icuoverflowcb,
  54. ICU_CHANNEL_1,
  55. 0
  56. };
  57. /*
  58. * Application entry point.
  59. */
  60. int main(void) {
  61. /*
  62. * System initializations.
  63. * - HAL initialization, this also initializes the configured device drivers
  64. * and performs the board-specific initializations.
  65. * - Kernel initialization, the main() function becomes a thread and the
  66. * RTOS is active.
  67. */
  68. halInit();
  69. chSysInit();
  70. /*
  71. * Initializes the PWM driver 2 and ICU driver 3.
  72. * GPIOA6 is the ICU input (CH1).
  73. * GPIOA8 is the PWM output (CH1).
  74. * The two pins have to be externally connected together.
  75. */
  76. pwmStart(&PWMD1, &pwmcfg);
  77. pwmEnablePeriodicNotification(&PWMD1);
  78. palSetPadMode(GPIOA, 8, PAL_MODE_ALTERNATE(2));
  79. icuStart(&ICUD3, &icucfg);
  80. palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(1));
  81. icuStartCapture(&ICUD3);
  82. icuEnableNotifications(&ICUD3);
  83. chThdSleepMilliseconds(2000);
  84. /*
  85. * Starts the PWM channel 0 using 75% duty cycle.
  86. */
  87. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
  88. pwmEnableChannelNotification(&PWMD1, 0);
  89. chThdSleepMilliseconds(5000);
  90. /*
  91. * Changes the PWM channel 0 to 50% duty cycle.
  92. */
  93. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  94. chThdSleepMilliseconds(5000);
  95. /*
  96. * Changes the PWM channel 0 to 25% duty cycle.
  97. */
  98. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
  99. chThdSleepMilliseconds(5000);
  100. /*
  101. * Changes PWM period to half second the duty cycle becomes 50%
  102. * implicitly.
  103. */
  104. pwmChangePeriod(&PWMD1, 5000);
  105. chThdSleepMilliseconds(5000);
  106. /*
  107. * Disables channel 0 and stops the drivers.
  108. */
  109. pwmDisableChannel(&PWMD1, 0);
  110. pwmStop(&PWMD1);
  111. icuStopCapture(&ICUD3);
  112. icuStop(&ICUD3);
  113. palClearPad(GPIOC, GPIOC_LED3);
  114. palClearPad(GPIOC, GPIOC_LED4);
  115. /*
  116. * Normal main() thread activity, in this demo it does nothing.
  117. */
  118. while (true) {
  119. chThdSleepMilliseconds(500);
  120. }
  121. return 0;
  122. }