ch56x_timer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-07-15 Emuzit first version
  9. */
  10. #ifndef __CH56X_TIMER_H__
  11. #define __CH56X_TIMER_H__
  12. #include "soc.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. union _timer_ctrl_mod
  17. {
  18. uint8_t reg;
  19. struct
  20. {
  21. uint8_t mode_in : 1; // B.0 : RW, timer mode setting
  22. uint8_t all_clear : 1; // B.1 : RW, clear FIFO/count/int-flag
  23. uint8_t count_en : 1; // B.2 : RW, enable timer module
  24. uint8_t out_en : 1; // B.3 : RW, timer output enable
  25. uint8_t out_polar : 1; // B.4 : RW, output polarity for PWM mode
  26. uint8_t resv_5 : 1;
  27. uint8_t pwm_repeat : 2; // B.7-6 : RW, PWM repeat count, 1/4/8/16
  28. };
  29. struct
  30. {
  31. uint8_t stuff_0 : 6;
  32. uint8_t cap_edge : 2; // B.7-6 : RW, capture edge mode
  33. };
  34. };
  35. #define RB_TMR_MODE_IN 0x01
  36. #define RB_TMR_ALL_CLEAR 0x02
  37. #define RB_TMR_COUNT_EN 0x04
  38. #define RB_TMR_OUT_EN 0x08
  39. #define RB_TMR_OUT_POLAR 0x10
  40. #define RB_TMR_CAP_COUNT 0x10
  41. #define RB_TMR_PWM_REPEAT 0xc0
  42. #define RB_TMR_CAP_EDGE 0xc0
  43. #define TMR_MODE_TIMER_PWM 0
  44. #define TMR_MODE_CAP_COUNT 1
  45. #define TMR_PWM_REPEAT_1 0
  46. #define TMR_PWM_REPEAT_4 1
  47. #define TMR_PWM_REPEAT_8 2
  48. #define TMR_PWM_REPEAT_16 3
  49. #define TMR_CAP_EDGE_NONE 0
  50. #define TMR_CAP_EDGE_BOTH 1
  51. #define TMR_CAP_EDGE_F2F 2
  52. #define TMR_CAP_EDGE_R2R 3
  53. union _timer_ctrl_dma
  54. {
  55. uint8_t reg;
  56. struct
  57. {
  58. uint8_t dma_enable : 1; // B.0 : RW, enable DMA
  59. uint8_t resv_1 : 1;
  60. uint8_t dma_loop : 1; // B.2 : RW, enable DMA address looping
  61. uint8_t resv_3 : 5;
  62. };
  63. };
  64. #define RB_TMR_DMA_ENABLE 0x01
  65. #define RB_TMR_DMA_LOOP 0x04
  66. union _timer_interrupt
  67. {
  68. uint8_t reg;
  69. struct
  70. {
  71. uint8_t cyc_end : 1; // B.0
  72. uint8_t data_act : 1; // B.1
  73. uint8_t fifo_hf : 1; // B.2
  74. uint8_t dma_end : 1; // B.3
  75. uint8_t fifo_ov : 1; // B.4
  76. uint8_t resv_5 : 3;
  77. };
  78. };
  79. #define RB_TMR_IX_MASK 0x1f
  80. #define RB_TMR_IE_CYC_END 0x01 // RW, enable interrupt for timer capture count timeout or PWM cycle end
  81. #define RB_TMR_IE_DATA_ACT 0x02 // RW, enable interrupt for timer capture input action or PWM trigger
  82. #define RB_TMR_IE_FIFO_HF 0x04 // RW, enable interrupt for timer FIFO half (capture fifo >=4 or PWM fifo <=3)
  83. #define RB_TMR_IE_DMA_END 0x08 // RW, enable interrupt for timer1/2 DMA completion
  84. #define RB_TMR_IE_FIFO_OV 0x10 // RW, enable interrupt for timer FIFO overflow
  85. #define RB_TMR_IF_CYC_END 0x01 // RW1, interrupt flag for timer capture count timeout or PWM cycle end
  86. #define RB_TMR_IF_DATA_ACT 0x02 // RW1, interrupt flag for timer capture input action or PWM trigger
  87. #define RB_TMR_IF_FIFO_HF 0x04 // RW1, interrupt flag for timer FIFO half (capture fifo >=4 or PWM fifo <=3)
  88. #define RB_TMR_IF_DMA_END 0x08 // RW1, interrupt flag for timer1/2 DMA completion
  89. #define RB_TMR_IF_FIFO_OV 0x10 // RW1, interrupt flag for timer FIFO overflow
  90. /*
  91. * 0x00 R8_TMRx_CTRL_MOD: mode setting register
  92. * 0x01 R8_TMRx_CTRL_DMA: DMA control register
  93. * 0x02 R8_TMRx_INTER_EN: interrupt enable register
  94. * 0x06 R8_TMRx_INT_FLAG: interrupt flag register
  95. * 0x07 R8_TMRx_FIFO_COUNT: RO, FIFO count register
  96. * 0x08 R32_TMRx_COUNT: RO, timer current count register
  97. * 0x0c R32_TMRx_CNT_END: RW, timer count end register
  98. * 0x10 R32_TMRx_FIFO: RO/WO, FIFO data register, LSB 26 bits
  99. * 0x14 R32_TMRx_DMA_NOW: RW, DMA buffer current address, LSB 18 bits
  100. * 0x18 R32_TMRx_DMA_BEG: RW, DMA buffer begin address, LSB 18 bits
  101. * 0x1c R32_TMRx_DMA_END: RW, DMA buffer end address (exclusive), LSB 18 bits
  102. *
  103. * Note: DMA related registers (0x10,0x14,0x18,0x1c) are TMR1/2 only
  104. *
  105. * CAVEAT: gcc (as of 8.2.0) tends to read 32-bit word for bit field test.
  106. * Be careful for those with side effect for read.
  107. */
  108. struct timer_registers
  109. {
  110. union _timer_ctrl_mod CTRL_MOD;
  111. union _timer_ctrl_dma CTRL_DMA;
  112. union _timer_interrupt INTER_EN;
  113. uint8_t resv_3[3];
  114. union _timer_interrupt INT_FLAG;
  115. uint8_t FIFO_COUNT;
  116. uint32_t COUNT;
  117. uint32_t CNT_END;
  118. uint32_t FIFO;
  119. uint32_t DMA_NOW;
  120. uint32_t DMA_BEG;
  121. uint32_t DMA_END;
  122. } __packed;
  123. CHECK_STRUCT_SIZE(struct timer_registers, 0x20);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif