main.c 3.5 KB

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