Key.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "Key.h"
  2. #include "SysTick.h"
  3. #include "gd32e230x_conf.h"
  4. #define KEY_NAME_MAX 5
  5. #define KEY_A_RCU RCU_GPIOA
  6. #define KEY_A_IO GPIOA
  7. #define KEY_B_RCU RCU_GPIOB
  8. #define KEY_B_IO GPIOB
  9. #define KEY_F_RCU RCU_GPIOF
  10. #define KEY_F_IO GPIOF
  11. #define KEY_ON 0
  12. #define KEY_OFF 1
  13. #define KEY1 GPIO_PIN_7
  14. #define KEY2 GPIO_PIN_6
  15. #define KEY3 GPIO_PIN_12
  16. #define KEY4 GPIO_PIN_8
  17. #if ((RelaySlaveChaNum <=4) && (!SUPPORT_TWIN_FIRE))
  18. #define KEY5 GPIO_PIN_15
  19. #else
  20. #define KEY5 GPIO_PIN_10
  21. #endif
  22. #if (RelaySlaveChaNum == 6)
  23. #define DERECTION_GPIO GPIOB
  24. #define DERECTION_IO_1 GPIO_PIN_8
  25. #define DERECTION_IO_2 GPIO_PIN_9
  26. #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOB
  27. #define DERECTION_IO_1_PIN DERECTION_IO_8
  28. #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN8
  29. #define DERECTION_IO_1_EXIT EXTI_8
  30. #define DERECTION_IO_2_RCU EXTI_SOURCE_GPIOB
  31. #define DERECTION_IO_2_PIN DERECTION_IO_9
  32. #define DERECTION_IO_2_SOURCE EXTI_SOURCE_PIN9
  33. #define DERECTION_IO_2_EXIT EXTI_9
  34. #elif((RelaySlaveChaNum <= 4) && (!SUPPORT_TWIN_FIRE))
  35. #define DERECTION_GPIO GPIOB
  36. #define DERECTION_IO_1 GPIO_PIN_10
  37. #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOB
  38. #define DERECTION_IO_1_PIN DERECTION_IO_1
  39. #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN10
  40. #define DERECTION_IO_1_EXIT EXTI_10
  41. #elif ((RelaySlaveChaNum == 8) && (SUPPORT_2_DETECTION))
  42. #define DERECTION_GPIO GPIOB
  43. #define DERECTION_IO_1 GPIO_PIN_1
  44. #define DERECTION_IO_2 GPIO_PIN_2
  45. #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOB
  46. #define DERECTION_IO_1_PIN DERECTION_IO_1
  47. #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN1
  48. #define DERECTION_IO_1_EXIT EXTI_1
  49. #define DERECTION_IO_2_RCU EXTI_SOURCE_GPIOB
  50. #define DERECTION_IO_2_PIN DERECTION_IO_2
  51. #define DERECTION_IO_2_SOURCE EXTI_SOURCE_PIN2
  52. #define DERECTION_IO_2_EXIT EXTI_2
  53. #else
  54. #define DERECTION_GPIO GPIOA
  55. #define DERECTION_IO_1 GPIO_PIN_1
  56. #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOA
  57. #define DERECTION_IO_1_PIN DERECTION_IO_1
  58. #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN1
  59. #define DERECTION_IO_1_EXIT EXTI_1
  60. #endif
  61. //#if SUPPORT_DETECTION
  62. uint8_t key_detection_1 = 0;
  63. uint8_t key_detection_2 = 0;
  64. //#endif
  65. extern uint16_t detection_count_1;
  66. extern uint16_t detection_count_2;
  67. static unsigned char s_arrKeyDownLevel[KEY_NAME_MAX]; //使用前要在InitKey函数中进行初始化
  68. static void ConfigKeyGPIO(void); //配置按键的GPIO
  69. static void ConfigDetection(void);
  70. static void ConfigKeyGPIO(void)
  71. {
  72. //使能RCU相关时钟
  73. rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA的时钟
  74. rcu_periph_clock_enable(RCU_GPIOF); //使能GPIOF的时钟
  75. rcu_periph_clock_enable(RCU_GPIOB);
  76. gpio_mode_set(KEY_F_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY1); //配置PF7为上拉输入K1
  77. gpio_mode_set(KEY_F_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY2); //配置PF6为上拉输入K2
  78. gpio_mode_set(KEY_A_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY3); //配置PA12为上拉输入K3
  79. gpio_mode_set(KEY_A_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY4); //配置PA12为上拉输入K3
  80. #if SUPPORT_4_KEY
  81. #else
  82. gpio_mode_set(KEY_B_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY5);
  83. #endif
  84. }
  85. static void ConfigDetection(void)
  86. {
  87. #if SUPPORT_DETECTION
  88. #if SUPPORT_FALLING_DETECTED
  89. rcu_periph_clock_enable(RCU_CFGCMP);
  90. #endif
  91. #if (RelaySlaveChaNum == 6)
  92. rcu_periph_clock_enable(RCU_GPIOB);
  93. #elif((RelaySlaveChaNum <= 4) && (!SUPPORT_TWIN_FIRE))
  94. rcu_periph_clock_enable(RCU_GPIOB);
  95. #elif ((RelaySlaveChaNum == 8) && (SUPPORT_2_DETECTION))
  96. rcu_periph_clock_enable(RCU_GPIOB);
  97. #else
  98. rcu_periph_clock_enable(RCU_GPIOA);
  99. #endif
  100. //config io
  101. #if (RelaySlaveChaNum == 6 || ((RelaySlaveChaNum == 8) && SUPPORT_2_DETECTION))
  102. gpio_mode_set(DERECTION_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_2);
  103. #endif
  104. gpio_mode_set(DERECTION_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_1);
  105. //config interrupt
  106. #if SUPPORT_FALLING_DETECTED
  107. #if (RelaySlaveChaNum == 6)
  108. nvic_irq_enable(EXTI4_15_IRQn,2);
  109. syscfg_exti_line_config(DERECTION_IO_2_RCU,DERECTION_IO_2_SOURCE);
  110. exti_init(DERECTION_IO_2_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  111. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  112. #elif((RelaySlaveChaNum <= 4) && (!SUPPORT_TWIN_FIRE))
  113. nvic_irq_enable(EXTI4_15_IRQn,2);
  114. #elif ((RelaySlaveChaNum == 8) && (SUPPORT_2_DETECTION))
  115. nvic_irq_enable(EXTI0_1_IRQn,2);
  116. nvic_irq_enable(EXTI2_3_IRQn,2);
  117. syscfg_exti_line_config(DERECTION_IO_2_RCU,DERECTION_IO_2_SOURCE);
  118. exti_init(DERECTION_IO_2_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  119. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  120. #else
  121. nvic_irq_enable(EXTI0_1_IRQn,2);
  122. #endif
  123. syscfg_exti_line_config(DERECTION_IO_1_RCU,DERECTION_IO_1_SOURCE);
  124. exti_init(DERECTION_IO_1_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  125. exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
  126. #endif
  127. #endif
  128. }
  129. void InitKey(void)
  130. {
  131. ConfigKeyGPIO(); //配置按键的GPIO
  132. ConfigDetection();
  133. }
  134. unsigned char ScanKey(unsigned int gpio_periph,unsigned int pin)
  135. {
  136. if(gpio_input_bit_get(gpio_periph,pin)==0)
  137. return KEY_ON;
  138. else
  139. return KEY_OFF;
  140. }
  141. unsigned char ReadKeyValue(void)
  142. {
  143. static unsigned char SlaveAddress;
  144. s_arrKeyDownLevel[0]=ScanKey(KEY_F_IO,KEY1);
  145. s_arrKeyDownLevel[1]=ScanKey(KEY_F_IO,KEY2);
  146. s_arrKeyDownLevel[2]=ScanKey(KEY_A_IO,KEY3);
  147. s_arrKeyDownLevel[3]=ScanKey(KEY_A_IO,KEY4);
  148. #if SUPPORT_4_KEY
  149. #else
  150. s_arrKeyDownLevel[4]=ScanKey(KEY_B_IO,KEY5);
  151. #endif
  152. #if SUPPORT_4_KEY
  153. SlaveAddress=(~((s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[1]<<1)|s_arrKeyDownLevel[0]))&0x0f;
  154. #else
  155. SlaveAddress=(~((s_arrKeyDownLevel[4]<<4)|(s_arrKeyDownLevel[3]<<3)|(s_arrKeyDownLevel[2]<<2)|(s_arrKeyDownLevel[1]<<1)|s_arrKeyDownLevel[0]))&0x1f;
  156. #endif
  157. return SlaveAddress;
  158. }
  159. void EXTI0_1_IRQHandler(void)
  160. {
  161. if(RESET != exti_interrupt_flag_get(DERECTION_IO_1_EXIT))
  162. {
  163. key_detection_1 = 1;
  164. detection_count_1 = 0;
  165. exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
  166. }
  167. }
  168. void EXTI2_3_IRQHandler(void)
  169. {
  170. #if ((RelaySlaveChaNum == 8) && (SUPPORT_2_DETECTION))
  171. if(RESET != exti_interrupt_flag_get(DERECTION_IO_2_EXIT))
  172. {
  173. key_detection_2 = 1;
  174. detection_count_2 = 0;
  175. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  176. }
  177. #endif
  178. }
  179. void EXTI4_15_IRQHandler(void){
  180. if(RESET != exti_interrupt_flag_get(DERECTION_IO_1_EXIT))
  181. {
  182. key_detection_1 = 1;
  183. detection_count_1 = 0;
  184. exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
  185. }
  186. #if (RelaySlaveChaNum == 6)
  187. if(RESET != exti_interrupt_flag_get(DERECTION_IO_2_EXIT))
  188. {
  189. key_detection_2 = 1;
  190. detection_count_2 = 0;
  191. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  192. }
  193. #endif
  194. }
  195. uint8_t get_detection(void)
  196. {
  197. uint8_t value = 0;
  198. #if SUPPORT_DETECTION
  199. #if SUPPORT_FALLING_DETECTED
  200. if(key_detection_1 > 0)
  201. {
  202. value |= (1 << 0);
  203. }
  204. if(key_detection_2 > 0)
  205. {
  206. value |= (1 << 1);
  207. }
  208. #else
  209. {
  210. #if (RelaySlaveChaNum == 6 || ((RelaySlaveChaNum == 8) && SUPPORT_2_DETECTION))
  211. uint8_t val1 = 0;
  212. val1 = gpio_input_bit_get(DERECTION_GPIO,DERECTION_IO_2);
  213. if(val1 == 0)
  214. {
  215. value |= (1 << 1);
  216. }
  217. #endif
  218. uint8_t val2 = 0;
  219. val2 = gpio_input_bit_get(DERECTION_GPIO,DERECTION_IO_1);
  220. if(val2 == 0)
  221. {
  222. value |= (1 << 0);
  223. }
  224. }
  225. #endif
  226. #endif
  227. return value;
  228. }