pwm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-12-04 Haley the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "am_mcu_apollo.h"
  13. #include "board.h"
  14. #ifdef RT_USING_PWM
  15. // LED3 is pin 19
  16. #define AM_BSP_GPIO_PWM_LED 19
  17. #define AM_BSP_GPIO_CFG_PWM_LED AM_HAL_PIN_19_TCTB1
  18. #define AM_BSP_PWM_LED_TIMER 1
  19. #define AM_BSP_PWM_LED_TIMER_SEG AM_HAL_CTIMER_TIMERB
  20. #define AM_BSP_PWM_LED_TIMER_INT AM_HAL_CTIMER_INT_TIMERB1
  21. /**
  22. * @brief LED brightness profile
  23. *
  24. * This function define LED brightness profile
  25. *
  26. * @return None.
  27. */
  28. volatile uint32_t g_ui32Index = 0;
  29. const uint32_t g_pui32Brightness[64] =
  30. {
  31. 1, 1, 1, 2, 3, 4, 6, 8,
  32. 10, 12, 14, 17, 20, 23, 25, 28,
  33. 31, 35, 38, 40, 43, 46, 49, 51,
  34. 53, 55, 57, 59, 60, 61, 62, 62,
  35. 63, 62, 62, 61, 60, 59, 57, 55,
  36. 53, 51, 49, 46, 43, 40, 38, 35,
  37. 32, 28, 25, 23, 20, 17, 14, 12,
  38. 10, 8, 6, 4, 3, 2, 1, 1
  39. };
  40. /**
  41. * @brief Interrupt handler for the Timer
  42. *
  43. * This function is Interrupt handler for the Timer
  44. *
  45. * @return None.
  46. */
  47. void am_ctimer_isr(void)
  48. {
  49. /* Clear the interrupt that got us here */
  50. am_hal_ctimer_int_clear(AM_BSP_PWM_LED_TIMER_INT);
  51. /* Now set new PWM half-period for the LED */
  52. am_hal_ctimer_period_set(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG,
  53. 64, g_pui32Brightness[g_ui32Index]);
  54. /* Set up the LED duty cycle for the next pulse */
  55. g_ui32Index = (g_ui32Index + 1) % 64;
  56. }
  57. /**
  58. * @brief Initialize the PWM
  59. *
  60. * This function initialize the PWM
  61. *
  62. * @return 0.
  63. */
  64. int rt_hw_pwm_init(void)
  65. {
  66. /* init pwm gpio */
  67. am_hal_gpio_pin_config(AM_BSP_GPIO_PWM_LED, AM_BSP_GPIO_CFG_PWM_LED);
  68. /* Configure a timer to drive the LED */
  69. am_hal_ctimer_config_single(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG,
  70. (AM_HAL_CTIMER_FN_PWM_REPEAT |
  71. AM_HAL_CTIMER_XT_2_048KHZ |
  72. AM_HAL_CTIMER_INT_ENABLE |
  73. AM_HAL_CTIMER_PIN_ENABLE));
  74. /* Set up initial timer period */
  75. am_hal_ctimer_period_set(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG,
  76. 64, 32);
  77. /* Enable interrupts for the Timer we are using on this board */
  78. am_hal_ctimer_int_enable(AM_BSP_PWM_LED_TIMER_INT);
  79. am_hal_interrupt_enable(AM_HAL_INTERRUPT_CTIMER);
  80. /* Start the timer */
  81. am_hal_ctimer_start(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG);
  82. rt_kprintf("pwm_init!\n");
  83. return 0;
  84. }
  85. #ifdef RT_USING_COMPONENTS_INIT
  86. INIT_BOARD_EXPORT(rt_hw_pwm_init);
  87. #endif
  88. #endif
  89. /*@}*/