gd32f30x_ctc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*!
  2. \file gd32f30x_ctc.h
  3. \brief definitions for the CTC
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #ifndef GD32F30X_CTC_H
  33. #define GD32F30X_CTC_H
  34. #include "gd32f30x.h"
  35. /* CTC definitions */
  36. #define CTC CTC_BASE /*!< CTC base address */
  37. /* registers definitions */
  38. #define CTC_CTL0 REG32((CTC) + 0x00000000U) /*!< CTC control register 0 */
  39. #define CTC_CTL1 REG32((CTC) + 0x00000004U) /*!< CTC control register 1 */
  40. #define CTC_STAT REG32((CTC) + 0x00000008U) /*!< CTC status register */
  41. #define CTC_INTC REG32((CTC) + 0x0000000CU) /*!< CTC interrupt clear register */
  42. /* bits definitions */
  43. /* CTC_CTL0 */
  44. #define CTC_CTL0_CKOKIE BIT(0) /*!< clock trim OK(CKOKIF) interrupt enable */
  45. #define CTC_CTL0_CKWARNIE BIT(1) /*!< clock trim warning(CKWARNIF) interrupt enable */
  46. #define CTC_CTL0_ERRIE BIT(2) /*!< error(ERRIF) interrupt enable */
  47. #define CTC_CTL0_EREFIE BIT(3) /*!< EREFIF interrupt enable */
  48. #define CTC_CTL0_CNTEN BIT(5) /*!< CTC counter enable */
  49. #define CTC_CTL0_AUTOTRIM BIT(6) /*!< hardware automatically trim mode */
  50. #define CTC_CTL0_SWREFPUL BIT(7) /*!< software reference source sync pulse */
  51. #define CTC_CTL0_TRIMVALUE BITS(8,13) /*!< IRC48M trim value */
  52. /* CTC_CTL1 */
  53. #define CTC_CTL1_RLVALUE BITS(0,15) /*!< CTC counter reload value */
  54. #define CTC_CTL1_CKLIM BITS(16,23) /*!< clock trim base limit value */
  55. #define CTC_CTL1_REFPSC BITS(24,26) /*!< reference signal source prescaler */
  56. #define CTC_CTL1_REFSEL BITS(28,29) /*!< reference signal source selection */
  57. #define CTC_CTL1_REFPOL BIT(31) /*!< reference signal source polarity */
  58. /* CTC_STAT */
  59. #define CTC_STAT_CKOKIF BIT(0) /*!< clock trim OK interrupt flag */
  60. #define CTC_STAT_CKWARNIF BIT(1) /*!< clock trim warning interrupt flag */
  61. #define CTC_STAT_ERRIF BIT(2) /*!< error interrupt flag */
  62. #define CTC_STAT_EREFIF BIT(3) /*!< expect reference interrupt flag */
  63. #define CTC_STAT_CKERR BIT(8) /*!< clock trim error bit */
  64. #define CTC_STAT_REFMISS BIT(9) /*!< reference sync pulse miss */
  65. #define CTC_STAT_TRIMERR BIT(10) /*!< trim value error bit */
  66. #define CTC_STAT_REFDIR BIT(15) /*!< CTC trim counter direction when reference sync pulse occurred */
  67. #define CTC_STAT_REFCAP BITS(16,31) /*!< CTC counter capture when reference sync pulse occurred */
  68. /* CTC_INTC */
  69. #define CTC_INTC_CKOKIC BIT(0) /*!< CKOKIF interrupt clear bit */
  70. #define CTC_INTC_CKWARNIC BIT(1) /*!< CKWARNIF interrupt clear bit */
  71. #define CTC_INTC_ERRIC BIT(2) /*!< ERRIF interrupt clear bit */
  72. #define CTC_INTC_EREFIC BIT(3) /*!< EREFIF interrupt clear bit */
  73. /* constants definitions */
  74. /* hardware automatically trim mode definitions */
  75. #define CTC_HARDWARE_TRIM_MODE_ENABLE CTC_CTL0_AUTOTRIM /*!< hardware automatically trim mode enable*/
  76. #define CTC_HARDWARE_TRIM_MODE_DISABLE ((uint32_t)0x00000000U) /*!< hardware automatically trim mode disable*/
  77. /* reference signal source polarity definitions */
  78. #define CTC_REFSOURCE_POLARITY_FALLING CTC_CTL1_REFPOL /*!< reference signal source polarity is falling edge*/
  79. #define CTC_REFSOURCE_POLARITY_RISING ((uint32_t)0x00000000U) /*!< reference signal source polarity is rising edge*/
  80. /* reference signal source selection definitions */
  81. #define CTL1_REFSEL(regval) (BITS(28,29) & ((uint32_t)(regval) << 28))
  82. #define CTC_REFSOURCE_GPIO CTL1_REFSEL(0) /*!< GPIO is selected */
  83. #define CTC_REFSOURCE_LXTAL CTL1_REFSEL(1) /*!< LXTAL is selected */
  84. /* reference signal source prescaler definitions */
  85. #define CTL1_REFPSC(regval) (BITS(24,26) & ((uint32_t)(regval) << 24))
  86. #define CTC_REFSOURCE_PSC_OFF CTL1_REFPSC(0) /*!< reference signal not divided */
  87. #define CTC_REFSOURCE_PSC_DIV2 CTL1_REFPSC(1) /*!< reference signal divided by 2 */
  88. #define CTC_REFSOURCE_PSC_DIV4 CTL1_REFPSC(2) /*!< reference signal divided by 4 */
  89. #define CTC_REFSOURCE_PSC_DIV8 CTL1_REFPSC(3) /*!< reference signal divided by 8 */
  90. #define CTC_REFSOURCE_PSC_DIV16 CTL1_REFPSC(4) /*!< reference signal divided by 16 */
  91. #define CTC_REFSOURCE_PSC_DIV32 CTL1_REFPSC(5) /*!< reference signal divided by 32 */
  92. #define CTC_REFSOURCE_PSC_DIV64 CTL1_REFPSC(6) /*!< reference signal divided by 64 */
  93. #define CTC_REFSOURCE_PSC_DIV128 CTL1_REFPSC(7) /*!< reference signal divided by 128 */
  94. /* CTC interrupt enable definitions */
  95. #define CTC_INT_CKOK CTC_CTL0_CKOKIE /*!< clock trim OK interrupt enable */
  96. #define CTC_INT_CKWARN CTC_CTL0_CKWARNIE /*!< clock trim warning interrupt enable */
  97. #define CTC_INT_ERR CTC_CTL0_ERRIE /*!< error interrupt enable */
  98. #define CTC_INT_EREF CTC_CTL0_EREFIE /*!< expect reference interrupt enable */
  99. /* CTC interrupt source definitions */
  100. #define CTC_INT_FLAG_CKOK CTC_STAT_CKOKIF /*!< clock trim OK interrupt flag */
  101. #define CTC_INT_FLAG_CKWARN CTC_STAT_CKWARNIF /*!< clock trim warning interrupt flag */
  102. #define CTC_INT_FLAG_ERR CTC_STAT_ERRIF /*!< error interrupt flag */
  103. #define CTC_INT_FLAG_EREF CTC_STAT_EREFIF /*!< expect reference interrupt flag */
  104. #define CTC_INT_FLAG_CKERR CTC_STAT_CKERR /*!< clock trim error bit */
  105. #define CTC_INT_FLAG_REFMISS CTC_STAT_REFMISS /*!< reference sync pulse miss */
  106. #define CTC_INT_FLAG_TRIMERR CTC_STAT_TRIMERR /*!< trim value error */
  107. /* CTC flag definitions */
  108. #define CTC_FLAG_CKOK CTC_STAT_CKOKIF /*!< clock trim OK flag */
  109. #define CTC_FLAG_CKWARN CTC_STAT_CKWARNIF /*!< clock trim warning flag */
  110. #define CTC_FLAG_ERR CTC_STAT_ERRIF /*!< error flag */
  111. #define CTC_FLAG_EREF CTC_STAT_EREFIF /*!< expect reference flag */
  112. #define CTC_FLAG_CKERR CTC_STAT_CKERR /*!< clock trim error bit */
  113. #define CTC_FLAG_REFMISS CTC_STAT_REFMISS /*!< reference sync pulse miss */
  114. #define CTC_FLAG_TRIMERR CTC_STAT_TRIMERR /*!< trim value error bit */
  115. /* function declarations */
  116. /* reset ctc clock trim controller */
  117. void ctc_deinit(void);
  118. /* enable CTC trim counter */
  119. void ctc_counter_enable(void);
  120. /* disable CTC trim counter */
  121. void ctc_counter_disable(void);
  122. /* configure the IRC48M trim value */
  123. void ctc_irc48m_trim_value_config(uint8_t trim_value);
  124. /* generate software reference source sync pulse */
  125. void ctc_software_refsource_pulse_generate(void);
  126. /* configure hardware automatically trim mode */
  127. void ctc_hardware_trim_mode_config(uint32_t hardmode);
  128. /* configure reference signal source polarity */
  129. void ctc_refsource_polarity_config(uint32_t polarity);
  130. /* select reference signal source */
  131. void ctc_refsource_signal_select(uint32_t refs);
  132. /* configure reference signal source prescaler */
  133. void ctc_refsource_prescaler_config(uint32_t prescaler);
  134. /* configure clock trim base limit value */
  135. void ctc_clock_limit_value_config(uint8_t limit_value);
  136. /* configure CTC counter reload value */
  137. void ctc_counter_reload_value_config(uint16_t reload_value);
  138. /* read CTC counter capture value when reference sync pulse occurred */
  139. uint16_t ctc_counter_capture_value_read(void);
  140. /* read CTC trim counter direction when reference sync pulse occurred */
  141. FlagStatus ctc_counter_direction_read(void);
  142. /* read CTC counter reload value */
  143. uint16_t ctc_counter_reload_value_read(void);
  144. /* read the IRC48M trim value */
  145. uint8_t ctc_irc48m_trim_value_read(void);
  146. /* interrupt & flag functions */
  147. /* get CTC flag */
  148. FlagStatus ctc_flag_get(uint32_t flag);
  149. /* clear CTC flag */
  150. void ctc_flag_clear(uint32_t flag);
  151. /* enable the CTC interrupt */
  152. void ctc_interrupt_enable(uint32_t interrupt);
  153. /* disable the CTC interrupt */
  154. void ctc_interrupt_disable(uint32_t interrupt);
  155. /* get CTC interrupt flag */
  156. FlagStatus ctc_interrupt_flag_get(uint32_t int_flag);
  157. /* clear CTC interrupt flag */
  158. void ctc_interrupt_flag_clear(uint32_t int_flag);
  159. #endif /* GD32F30X_CTC_H */