drv_pwm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-13 JasonHu the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <sunxi_hal_pwm.h>
  13. #define PWM_CHANNEL_MAX (8) /* 0-7*/
  14. #if defined(RT_USING_PWM) && defined(BSP_USING_PWM)
  15. struct rt_hal_pwm
  16. {
  17. struct rt_device_pwm parent;
  18. rt_uint32_t period[PWM_CHANNEL_MAX];
  19. rt_uint32_t pulse[PWM_CHANNEL_MAX];
  20. rt_uint32_t status;
  21. };
  22. static struct rt_hal_pwm _hal_pwm_device;
  23. static struct rt_pwm_configuration configuration = {1, 100000, 100000, RT_TRUE};
  24. static rt_err_t set(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  25. {
  26. struct rt_hal_pwm *hal_pwm_device = (struct rt_hal_pwm *)device;
  27. LOG_D("drv_pwm.c set channel %d: period: %d, pulse: %d\n", configuration->channel, configuration->period, configuration->pulse);
  28. hal_pwm_device->period[configuration->channel] = configuration->period;
  29. hal_pwm_device->pulse[configuration->channel] = configuration->pulse;
  30. return RT_EOK;
  31. }
  32. static rt_err_t get(struct rt_device_pwm *device, struct rt_pwm_configuration *configuration)
  33. {
  34. struct rt_hal_pwm *hal_pwm_device = (struct rt_hal_pwm *)device;
  35. configuration->period = hal_pwm_device->period[configuration->channel];
  36. configuration->pulse = hal_pwm_device->pulse[configuration->channel];
  37. LOG_D("drv_pwm.c get channel %d: period: %d, pulse: %d\n", configuration->channel, configuration->period, configuration->pulse);
  38. return RT_EOK;
  39. }
  40. static rt_err_t control(struct rt_device_pwm *device, int cmd, void *arg)
  41. {
  42. rt_err_t result = RT_EOK;
  43. struct rt_pwm_configuration * configuration = (struct rt_pwm_configuration *)arg;
  44. struct rt_hal_pwm *hal_pwm_device = (struct rt_hal_pwm *)device;
  45. LOG_D("drv_pwm.c control cmd: %d. \n", cmd);
  46. if (configuration->channel > (PWM_CHANNEL_MAX - 1))
  47. {
  48. LOG_E("drv_pwm.c control channel: %d not support! \n", configuration->channel);
  49. return -RT_EIO;
  50. }
  51. LOG_D("PWM: channel:%d", configuration->channel);
  52. if (cmd == PWM_CMD_ENABLE)
  53. {
  54. LOG_D("PWM_CMD_ENABLE");
  55. struct pwm_config pwm_cfg;
  56. pwm_cfg.polarity = PWM_POLARITY_NORMAL;
  57. pwm_cfg.duty_ns = hal_pwm_device->pulse[configuration->channel];
  58. pwm_cfg.period_ns = hal_pwm_device->period[configuration->channel];
  59. hal_pwm_device->status = 1;
  60. if (hal_pwm_control(configuration->channel, &pwm_cfg) != 0)
  61. {
  62. result = -RT_EIO;
  63. }
  64. }
  65. else if (cmd == PWM_CMD_DISABLE)
  66. {
  67. LOG_D("PWM_CMD_DISABLE");
  68. struct pwm_config pwm_cfg;
  69. pwm_cfg.polarity = PWM_POLARITY_NORMAL;
  70. pwm_cfg.duty_ns = 0; /* set as 0 for no duty */
  71. pwm_cfg.period_ns = hal_pwm_device->period[configuration->channel];
  72. hal_pwm_device->status = 0;
  73. if (hal_pwm_control(configuration->channel, &pwm_cfg) != 0)
  74. {
  75. result = -RT_EIO;
  76. }
  77. else
  78. {
  79. hal_pwm_disable_controller(configuration->channel);
  80. }
  81. }
  82. else if (cmd == PWM_CMD_SET)
  83. {
  84. LOG_D("PWM_CMD_SET");
  85. result = set(device, (struct rt_pwm_configuration *)arg);
  86. if(hal_pwm_device->status)
  87. {
  88. struct pwm_config pwm_cfg;
  89. pwm_cfg.polarity = PWM_POLARITY_NORMAL;
  90. pwm_cfg.duty_ns = hal_pwm_device->pulse[configuration->channel];
  91. pwm_cfg.period_ns = hal_pwm_device->period[configuration->channel];
  92. if (hal_pwm_control(configuration->channel, &pwm_cfg) != 0)
  93. {
  94. result = -RT_EIO;
  95. }
  96. }
  97. }
  98. else if (cmd == PWM_CMD_GET)
  99. {
  100. LOG_D("PWM_CMD_GET");
  101. result = get(device, (struct rt_pwm_configuration *)arg);
  102. }
  103. return result;
  104. }
  105. static const struct rt_pwm_ops pwm_ops =
  106. {
  107. control,
  108. };
  109. int rt_hw_pwm_init(void)
  110. {
  111. /* add pwm initial. */
  112. if (hal_pwm_init() != 0)
  113. {
  114. LOG_E("init pwm failed!");
  115. return -1;
  116. }
  117. _hal_pwm_device.status = 0;
  118. return rt_device_pwm_register(&_hal_pwm_device.parent, "pwm", &pwm_ops, RT_NULL);
  119. }
  120. INIT_DEVICE_EXPORT(rt_hw_pwm_init);
  121. #endif /* RT_USING_PWM && BSP_USING_PWM */