rt_drv_pwm.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-07 aozima the first version
  9. * 2022-09-24 yuqi add phase and dead time configuration
  10. */
  11. #ifndef __DRV_PWM_H_INCLUDE__
  12. #define __DRV_PWM_H_INCLUDE__
  13. #include <rtthread.h>
  14. #define PWM_CMD_ENABLE (RT_DEVICE_CTRL_BASE(PWM) + 0)
  15. #define PWM_CMD_DISABLE (RT_DEVICE_CTRL_BASE(PWM) + 1)
  16. #define PWM_CMD_SET (RT_DEVICE_CTRL_BASE(PWM) + 2)
  17. #define PWM_CMD_GET (RT_DEVICE_CTRL_BASE(PWM) + 3)
  18. #define PWMN_CMD_ENABLE (RT_DEVICE_CTRL_BASE(PWM) + 4)
  19. #define PWMN_CMD_DISABLE (RT_DEVICE_CTRL_BASE(PWM) + 5)
  20. #define PWM_CMD_SET_PERIOD (RT_DEVICE_CTRL_BASE(PWM) + 6)
  21. #define PWM_CMD_SET_PULSE (RT_DEVICE_CTRL_BASE(PWM) + 7)
  22. #define PWM_CMD_SET_DEAD_TIME (RT_DEVICE_CTRL_BASE(PWM) + 8)
  23. #define PWM_CMD_SET_PHASE (RT_DEVICE_CTRL_BASE(PWM) + 9)
  24. #define PWM_CMD_ENABLE_IRQ (RT_DEVICE_CTRL_BASE(PWM) + 10)
  25. #define PWM_CMD_DISABLE_IRQ (RT_DEVICE_CTRL_BASE(PWM) + 11)
  26. struct rt_pwm_configuration
  27. {
  28. rt_uint32_t channel; /* 0 ~ n or 0 ~ -n, which depends on specific MCU requirements */
  29. rt_uint32_t period; /* unit:ns 1ns~4.29s:1Ghz~0.23hz */
  30. rt_uint32_t pulse; /* unit:ns (pulse<=period) */
  31. rt_uint32_t dead_time; /* unit:ns */
  32. rt_uint32_t phase; /*unit: degree, 0~360, which is the phase of pwm output, */
  33. /*
  34. * RT_TRUE : The channel of pwm is complememtary.
  35. * RT_FALSE : The channel of pwm is nomal.
  36. */
  37. rt_bool_t complementary;
  38. };
  39. struct rt_device_pwm;
  40. struct rt_pwm_ops
  41. {
  42. rt_err_t (*control)(struct rt_device_pwm *device, int cmd, void *arg);
  43. };
  44. struct rt_device_pwm
  45. {
  46. struct rt_device parent;
  47. const struct rt_pwm_ops *ops;
  48. };
  49. rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data);
  50. rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel);
  51. rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel);
  52. rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse);
  53. rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period);
  54. rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse);
  55. rt_err_t rt_pwm_set_dead_time(struct rt_device_pwm *device, int channel, rt_uint32_t dead_time);
  56. rt_err_t rt_pwm_set_phase(struct rt_device_pwm *device, int channel, rt_uint32_t phase);
  57. #endif /* __DRV_PWM_H_INCLUDE__ */