main.c 3.3 KB

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