ch56x_gpio.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_GPIO_H__
  11. #define __CH56X_GPIO_H__
  12. #include "soc.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /* Fixed linear mapping : 32 pins per port, for ports A,B,C,D...
  17. */
  18. #define GET_PIN(port,pin) (GPIO_P##port##_PIN_START + pin)
  19. #ifdef SOC_SERIES_CH569
  20. #define GPIO_INT_PINS { \
  21. GET_PIN(A,2), GET_PIN(A,3), GET_PIN(A,4), GET_PIN(B,3), \
  22. GET_PIN(B,4), GET_PIN(B,11), GET_PIN(B,12), GET_PIN(B,15) \
  23. }
  24. #else
  25. #define GPIO_INT_PINS { \
  26. GET_PIN(A,3), GET_PIN(A,4), GET_PIN(A,6), GET_PIN(A,10), \
  27. GET_PIN(A,11), GET_PIN(A,12), GET_PIN(B,4), GET_PIN(B,10), \
  28. }
  29. #endif
  30. /*
  31. * R8_GPIO_INT_FLAG / R8_GPIO_INT_STATUS (CH567,CH568):
  32. * write 1 to specific bit to clear int flag
  33. *
  34. * R8_GPIO_INT_ENABLE:
  35. * To use EXTIx function, pin should be set as input.
  36. * For wakeup function, also set RB_SLP_GPIO_WAKE.
  37. *
  38. * R8_GPIO_INT_MODE:
  39. * R8_GPIO_INT_POLAR:
  40. */
  41. #if defined(SOC_SERIES_CH569)
  42. union _gpio_interrupt
  43. {
  44. uint8_t reg;
  45. struct
  46. {
  47. uint8_t pa2 : 1;
  48. uint8_t pa3 : 1;
  49. uint8_t pa4 : 1;
  50. uint8_t pb3 : 1;
  51. uint8_t pb4 : 1;
  52. uint8_t pb11 : 1;
  53. uint8_t pb12 : 1;
  54. uint8_t pb15 : 1;
  55. };
  56. };
  57. #else
  58. union _gpio_interrupt
  59. {
  60. uint8_t reg;
  61. struct
  62. {
  63. uint8_t pa3 : 1;
  64. uint8_t pa4 : 1;
  65. uint8_t pa6 : 1;
  66. uint8_t pa10 : 1;
  67. uint8_t pa11 : 1;
  68. uint8_t pa12 : 1;
  69. uint8_t pb4 : 1;
  70. uint8_t pb10 : 1;
  71. };
  72. };
  73. #endif
  74. #define GPIO_IE_DISABLE 0
  75. #define GPIO_IE_ENABLE 1
  76. #define GPIO_IM_LEVEL 0
  77. #define GPIO_IM_EDGE 1
  78. #define GPIO_IP_LOW_FALLING 0
  79. #define GPIO_IP_HIGH_RISING 1
  80. /*
  81. * R8_PIN_ALTERNATE (CH569) : reset value is 0x01
  82. * R8_PORT_PIN (CH567/CH568) : reset value is 0x00
  83. */
  84. union _gpio_pin_alternate
  85. {
  86. uint8_t reg;
  87. struct
  88. {
  89. uint8_t pin_mii : 1; // RW, ETH uses RMII/RGMII (CH565W/CH569W)
  90. uint8_t pin_tmr1 : 1; // RW, TMR1/PWM5/CAP1 remapping
  91. uint8_t pin_tmr2 : 1; // RW, TMR2/PWM6/CAP2 remapping
  92. uint8_t resv_3 : 1;
  93. uint8_t pin_uart0 : 1; // RW, RXD0/TXD0 remapping
  94. uint8_t pin_uart1 : 1; // RW, CH567 only
  95. uint8_t resv_6 : 2;
  96. };
  97. };
  98. #define RB_PIN_MII 0x01
  99. #define RB_PIN_TMR1 0x02
  100. #define RB_PIN_TMR2 0x04
  101. #define RB_PIN_UART0 0x10
  102. #define RB_PIN_UART1 0x20
  103. #ifdef SOC_SERIES_CH569
  104. #define GPIO_ALT_RMII 0
  105. #define GPIO_ALT_RGMII 1
  106. #define GPIO_ALT_TMR1_PB15 0
  107. #define GPIO_ALT_TMR1_PB0 1
  108. #define GPIO_ALT_TMR2_PA4 0
  109. #define GPIO_ALT_TMR2_PB3 1
  110. #define GPIO_ALT_UART0_PB5_6 0
  111. #define GPIO_ALT_UART0_PA5_6 1
  112. #else
  113. #define GPIO_ALT_TMR1_PA10 0
  114. #define GPIO_ALT_TMR1_PB2 1
  115. #define GPIO_ALT_TMR2_PA11 0
  116. #define GPIO_ALT_TMR2_PB11 1
  117. #define GPIO_ALT_UART0_PB4_7 0
  118. #define GPIO_ALT_UART0_PA15_14 1
  119. #define GPIO_ALT_UART1_PA8_9 0
  120. #define GPIO_ALT_UART1_PB8_9 1
  121. #endif
  122. struct gpio_px_regs
  123. {
  124. uint32_t DIR; // reset value for pins is 0, input pins
  125. uint32_t PIN; // RO
  126. uint32_t OUT; // reset value is 0
  127. uint32_t CLR; // reset value is 0
  128. uint32_t PU; // reset value is 0
  129. uint32_t PD; // reset value is 0
  130. uint32_t DRV; // reset value for pins is 0, 8mA
  131. uint32_t SMT; // reset value for pins is 1, enable schmitt trigger
  132. } __packed;
  133. CHECK_STRUCT_SIZE(struct gpio_px_regs, 0x20);
  134. #define GPIO_PX_DIR_IN 0
  135. #define GPIO_PX_DIR_OUT 1
  136. #define GPIO_PX_PU_DISABLE 0
  137. #define GPIO_PX_PU_ENABLE 1
  138. #define GPIO_PX_PD_DISABLE 0 // for DIR_IN
  139. #define GPIO_PX_PD_ENABLE 1 // for DIR_IN
  140. #define GPIO_PX_PD_PUSH_PULL 0 // for DIR_OUT
  141. #define GPIO_PX_PD_OPEN_DRAIN 1 // for DIR_OUT
  142. #define GPIO_PX_DRV_8mA 0
  143. #define GPIO_PX_DRV_16mA 1
  144. #define GPIO_PX_SMT_DISABLE 0
  145. #define GPIO_PX_SMT_SLOW 1 // for DIR_OUT
  146. #define GPIO_PX_SMT_ENABLE 1 // for DIR_IN
  147. /*
  148. * 0x12 R8_PIN_ALTERNATE: GPIO multi-use remapping register
  149. * 0x1c R8_GPIO_INT_FLAG: GPIO interrupt flag register
  150. * 0x1d R8_GPIO_INT_ENABLE: GPIO interrupt enable register
  151. * 0x1e R8_GPIO_INT_MODE: GPIO interrupt mode register
  152. * 0x1f R8_GPIO_INT_POLAR: GPIO interrupt polarity register
  153. *
  154. * 0x40 R32_PA_DIR: PA pin direction control
  155. * 0x44 R32_PA_PIN: PA pin input status
  156. * 0x48 R32_PA_OUT: PA pin output register
  157. * 0x4c R32_PA_CLR: PA pin output clear
  158. * 0x50 R32_PA_PU: PA pin pull-up resistor enable register
  159. * 0x54 R32_PA_PD: PA pin open drain output / input pull-down control
  160. * 0x58 R32_PA_DRV: PA pin output driving capability register
  161. * 0x5c R32_PA_SMT: PA pin slow output / schmitt trigger input control
  162. *
  163. * 0x60 R32_PB_DIR:
  164. * 0x64 R32_PB_PIN:
  165. * 0x68 R32_PB_OUT:
  166. * 0x6c R32_PB_CLR:
  167. * 0x70 R32_PB_PU:
  168. * 0x74 R32_PB_PD:
  169. * 0x78 R32_PB_DRV:
  170. * 0x7c R32_PB_SMT:
  171. *
  172. * CAVEAT: gcc (as of 8.2.0) tends to read 32-bit word for bit field test.
  173. * Be careful for those with side effect for read.
  174. */
  175. struct gpio_registers
  176. {
  177. uint32_t resv_00[4];
  178. uint8_t resv_10[2];
  179. union _gpio_pin_alternate PIN_ALTERNATE;
  180. uint8_t resv_13;
  181. uint32_t resv_14[2];
  182. union _gpio_interrupt INT_FLAG;
  183. union _gpio_interrupt INT_ENABLE;
  184. union _gpio_interrupt INT_MODE;
  185. union _gpio_interrupt INT_POLAR;
  186. uint32_t resv_20[8];
  187. struct gpio_px_regs PA;
  188. struct gpio_px_regs PB;
  189. } __packed;
  190. CHECK_STRUCT_SIZE(struct gpio_registers, 0x80);
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif