fw_gpio.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ___FW_GPIO_H___
  15. #define ___FW_GPIO_H___
  16. #include "fw_conf.h"
  17. #include "fw_types.h"
  18. typedef enum
  19. {
  20. GPIO_Mode_InOut_QBD = 0x00, /* quasi-bidirectional(be compatible with classical 8051 MCUs) */
  21. GPIO_Mode_Output_PP = 0x01, /* push-pull output */
  22. GPIO_Mode_Input_HIP = 0x02, /* high-impedance input */
  23. GPIO_Mode_InOut_OD = 0x03, /* open drain output */
  24. } GPIO_Mode_t;
  25. typedef enum
  26. {
  27. GPIO_Port_0 = 0U,
  28. GPIO_Port_1 = 1U,
  29. GPIO_Port_2 = 2U,
  30. GPIO_Port_3 = 3U,
  31. GPIO_Port_4 = 4U,
  32. GPIO_Port_5 = 5U,
  33. #if (__CONF_MCU_TYPE == 1 ) || (__CONF_MCU_TYPE == 3 )
  34. GPIO_Port_6 = 6U,
  35. GPIO_Port_7 = 7U,
  36. #endif
  37. } GPIO_Port_t;
  38. typedef enum
  39. {
  40. GPIO_Pin_0 = B00000001,
  41. GPIO_Pin_1 = B00000010,
  42. GPIO_Pin_2 = B00000100,
  43. GPIO_Pin_3 = B00001000,
  44. GPIO_Pin_4 = B00010000,
  45. GPIO_Pin_5 = B00100000,
  46. GPIO_Pin_6 = B01000000,
  47. GPIO_Pin_7 = B10000000,
  48. GPIO_Pin_All = B11111111,
  49. } GPIO_Pin_t;
  50. typedef enum
  51. {
  52. GPIO_SchmittTrigger_ON = 0x00,
  53. GPIO_SchmittTrigger_OFF = 0x01,
  54. } GPIO_SchmittTrigger_t;
  55. typedef enum
  56. {
  57. GPIO_SwitchSpeed_High = 0x00,
  58. GPIO_SwitchSpeed_Low = 0x01,
  59. } GPIO_SwitchSpeed_t;
  60. typedef enum
  61. {
  62. GPIO_DriveCapability_High = 0x00,
  63. GPIO_DriveCapability_Normal = 0x01,
  64. } GPIO_DriveCapability_t;
  65. #define GPIO_P0_SetMode(__PINS__, __MODE__) do { \
  66. P0M0 = P0M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  67. P0M1 = P0M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  68. } while(0)
  69. #define GPIO_P1_SetMode(__PINS__, __MODE__) do { \
  70. P1M0 = P1M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  71. P1M1 = P1M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  72. } while(0)
  73. #define GPIO_P2_SetMode(__PINS__, __MODE__) do { \
  74. P2M0 = P2M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  75. P2M1 = P2M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  76. } while(0)
  77. #define GPIO_P3_SetMode(__PINS__, __MODE__) do { \
  78. P3M0 = P3M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  79. P3M1 = P3M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  80. } while(0)
  81. #define GPIO_P4_SetMode(__PINS__, __MODE__) do { \
  82. P4M0 = P4M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  83. P4M1 = P4M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  84. } while(0)
  85. #define GPIO_P5_SetMode(__PINS__, __MODE__) do { \
  86. P5M0 = P5M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  87. P5M1 = P5M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  88. } while(0)
  89. #if (__CONF_MCU_TYPE == 1 ) || (__CONF_MCU_TYPE == 3 )
  90. #define GPIO_P6_SetMode(__PINS__, __MODE__) do { \
  91. P6M0 = P6M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  92. P6M1 = P6M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  93. } while(0)
  94. #define GPIO_P7_SetMode(__PINS__, __MODE__) do { \
  95. P7M0 = P7M0 & ~(__PINS__) | (((__MODE__) & 0x01)? (__PINS__) : 0x00); \
  96. P7M1 = P7M1 & ~(__PINS__) | (((__MODE__) & 0x02)? (__PINS__) : 0x00); \
  97. } while(0)
  98. #endif
  99. #define GPIO_SetPullUp(__PORT__, __PINS__, __STATE__) do { \
  100. SFRX_ON(); \
  101. SFRX(PxPU + (__PORT__)) = SFRX(PxPU + (__PORT__)) \
  102. & ~(__PINS__) | (((__STATE__) & 0x01)? (__PINS__) : 0x00); \
  103. SFRX_OFF(); \
  104. } while(0)
  105. #define GPIO_SetSchmittTrigger(__PORT__, __PINS__, __STATE__) do { \
  106. SFRX_ON(); \
  107. SFRX(PxNCS + (__PORT__)) = SFRX(PxNCS + (__PORT__)) \
  108. & ~(__PINS__) | (((__STATE__) & 0x01)? (__PINS__) : 0x00); \
  109. SFRX_OFF(); \
  110. } while(0)
  111. #define GPIO_SetSwitchSpeed(__PORT__, __PINS__, __STATE__) do { \
  112. SFRX_ON(); \
  113. SFRX(PxSR + (__PORT__)) = SFRX(PxSR + (__PORT__)) \
  114. & ~(__PINS__) | (((__STATE__) & 0x01)? (__PINS__) : 0x00); \
  115. SFRX_OFF(); \
  116. } while(0)
  117. #define GPIO_SetDriveCapability(__PORT__, __PINS__, __STATE__) do { \
  118. SFRX_ON(); \
  119. SFRX(PxDR + (__PORT__)) = SFRX(PxDR + (__PORT__)) \
  120. & ~(__PINS__) | (((__STATE__) & 0x01)? (__PINS__) : 0x00); \
  121. SFRX_OFF(); \
  122. } while(0)
  123. #define GPIO_SetDigitalInput(__PORT__, __PINS__, __STATE__) do { \
  124. SFRX_ON(); \
  125. SFRX(PxIE + (__PORT__)) = SFRX(PxIE + (__PORT__)) \
  126. & ~(__PINS__) | (((__STATE__) & 0x01)? (__PINS__) : 0x00); \
  127. SFRX_OFF(); \
  128. } while(0)
  129. #endif