ch56x_pwm.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * 2022-08-04 Emuzit first version
  9. */
  10. #ifndef __CH56X_PWM_H__
  11. #define __CH56X_PWM_H__
  12. #include "soc.h"
  13. #include "ch56x_gpio.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define PWM_DEVICE_NAME "pwmx"
  18. #define PWM_CHANNELS 4
  19. #define PWM0_PIN GET_PIN(B, 15)
  20. #define PWM1_PIN GET_PIN(A, 4)
  21. #define PWM2_PIN GET_PIN(B, 1)
  22. #define PWM3_PIN GET_PIN(B, 2)
  23. union _pwm_ctrl_mod
  24. {
  25. uint8_t reg;
  26. struct
  27. {
  28. uint8_t pwm0_out_en : 1; // RW, PWM output enable
  29. uint8_t pwm1_out_en : 1;
  30. uint8_t pwm2_out_en : 1;
  31. uint8_t pwm3_out_en : 1;
  32. uint8_t pwm0_polar : 1; // RW, PWM output polarity
  33. uint8_t pwm1_polar : 1;
  34. uint8_t pwm2_polar : 1;
  35. uint8_t pwm3_polar : 1;
  36. };
  37. };
  38. #define RB_PWM0_OUT_EN 0x01
  39. #define RB_PWM1_OUT_EN 0x02
  40. #define RB_PWM2_OUT_EN 0x04
  41. #define RB_PWM3_OUT_EN 0x08
  42. #define RB_PWM0_POLAR 0x10
  43. #define RB_PWM1_POLAR 0x20
  44. #define RB_PWM2_POLAR 0x40
  45. #define RB_PWM3_POLAR 0x80
  46. #define PWM_OUT_EN_MASK 0x0f
  47. union _pwm_ctrl_cfg
  48. {
  49. uint8_t reg;
  50. struct
  51. {
  52. uint8_t cycle_sel : 1; // RW, PWM cycle select, 0/1 for 256/255
  53. uint8_t resv_1 : 7;
  54. };
  55. };
  56. #define RB_PWM_CYCLE_SEL 0x01
  57. #define PWM_CYCLE_SEL_256 0
  58. #define PWM_CYCLE_SEL_255 1
  59. /*
  60. * 0x00 R8_PWM_CTRL_MOD: PWM control register
  61. * 0x01 R8_PWM_CTRL_CFG: PWM control configuration register
  62. * 0x02 R8_PWM_CLOCK_DIV: PWM clock divisor register
  63. * 0x04 R8_PWM0_DATA: PWM0 data holding register
  64. * 0x05 R8_PWM1_DATA: PWM1 data holding register
  65. * 0x06 R8_PWM2_DATA: PWM2 data holding register
  66. * 0x07 R8_PWM3_DATA: PWM3 data holding register
  67. */
  68. struct pwm_registers
  69. {
  70. union _pwm_ctrl_mod CTRL_MOD;
  71. union _pwm_ctrl_cfg CTRL_CFG;
  72. uint8_t CLOCK_DIV;
  73. uint8_t resv_3;
  74. union
  75. {
  76. uint32_t R32_PWM_DATA;
  77. uint8_t PWM_DATA[4];
  78. struct
  79. {
  80. uint8_t PWM0_DATA;
  81. uint8_t PWM1_DATA;
  82. uint8_t PWM2_DATA;
  83. uint8_t PWM3_DATA;
  84. };
  85. };
  86. };
  87. CHECK_STRUCT_SIZE(struct pwm_registers, 8);
  88. #ifdef __cplusplus
  89. }
  90. #endif
  91. #endif