pwm.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * File :_pwm.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-12-04 Haley the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "am_mcu_apollo.h"
  27. #include "board.h"
  28. #ifdef RT_USING_PWM
  29. // LED3 is pin 19
  30. #define AM_BSP_GPIO_PWM_LED 19
  31. #define AM_BSP_GPIO_CFG_PWM_LED AM_HAL_PIN_19_TCTB1
  32. #define AM_BSP_PWM_LED_TIMER 1
  33. #define AM_BSP_PWM_LED_TIMER_SEG AM_HAL_CTIMER_TIMERB
  34. #define AM_BSP_PWM_LED_TIMER_INT AM_HAL_CTIMER_INT_TIMERB1
  35. /**
  36. * @brief LED brightness profile
  37. *
  38. * This function define LED brightness profile
  39. *
  40. * @return None.
  41. */
  42. volatile uint32_t g_ui32Index = 0;
  43. const uint32_t g_pui32Brightness[64] =
  44. {
  45. 1, 1, 1, 2, 3, 4, 6, 8,
  46. 10, 12, 14, 17, 20, 23, 25, 28,
  47. 31, 35, 38, 40, 43, 46, 49, 51,
  48. 53, 55, 57, 59, 60, 61, 62, 62,
  49. 63, 62, 62, 61, 60, 59, 57, 55,
  50. 53, 51, 49, 46, 43, 40, 38, 35,
  51. 32, 28, 25, 23, 20, 17, 14, 12,
  52. 10, 8, 6, 4, 3, 2, 1, 1
  53. };
  54. /**
  55. * @brief Interrupt handler for the Timer
  56. *
  57. * This function is Interrupt handler for the Timer
  58. *
  59. * @return None.
  60. */
  61. void am_ctimer_isr(void)
  62. {
  63. /* Clear the interrupt that got us here */
  64. am_hal_ctimer_int_clear(AM_BSP_PWM_LED_TIMER_INT);
  65. /* Now set new PWM half-period for the LED */
  66. am_hal_ctimer_period_set(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG,
  67. 64, g_pui32Brightness[g_ui32Index]);
  68. /* Set up the LED duty cycle for the next pulse */
  69. g_ui32Index = (g_ui32Index + 1) % 64;
  70. }
  71. /**
  72. * @brief Initialize the PWM
  73. *
  74. * This function initialize the PWM
  75. *
  76. * @return 0.
  77. */
  78. int rt_hw_pwm_init(void)
  79. {
  80. /* init pwm gpio */
  81. am_hal_gpio_pin_config(AM_BSP_GPIO_PWM_LED, AM_BSP_GPIO_CFG_PWM_LED);
  82. /* Configure a timer to drive the LED */
  83. am_hal_ctimer_config_single(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG,
  84. (AM_HAL_CTIMER_FN_PWM_REPEAT |
  85. AM_HAL_CTIMER_XT_2_048KHZ |
  86. AM_HAL_CTIMER_INT_ENABLE |
  87. AM_HAL_CTIMER_PIN_ENABLE));
  88. /* Set up initial timer period */
  89. am_hal_ctimer_period_set(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG,
  90. 64, 32);
  91. /* Enable interrupts for the Timer we are using on this board */
  92. am_hal_ctimer_int_enable(AM_BSP_PWM_LED_TIMER_INT);
  93. am_hal_interrupt_enable(AM_HAL_INTERRUPT_CTIMER);
  94. /* Start the timer */
  95. am_hal_ctimer_start(AM_BSP_PWM_LED_TIMER, AM_BSP_PWM_LED_TIMER_SEG);
  96. rt_kprintf("pwm_init!\n");
  97. return 0;
  98. }
  99. #ifdef RT_USING_COMPONENTS_INIT
  100. INIT_BOARD_EXPORT(rt_hw_pwm_init);
  101. #endif
  102. #endif
  103. /*@}*/