key.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "gd32f10x.h"
  2. #include "key.h"
  3. #include "systick.h"
  4. #include "device_message.h"
  5. static uint32_t KEY_PORT[KEYn] = {SYS_RECOVERY_KEY_GPIO_PORT};
  6. static uint32_t KEY_PIN[KEYn] = {SYS_RECOVERY_KEY_PIN};
  7. static rcu_periph_enum KEY_CLK[KEYn] = {SYS_RECOVERY_KEY_GPIO_CLK};
  8. static exti_line_enum KEY_EXTI_LINE[KEYn] = {SYS_RECOVERY_KEY_EXTI_LINE};
  9. static uint8_t KEY_PORT_SOURCE[KEYn] = {SYS_RECOVERY_KEY_EXTI_PORT_SOURCE};
  10. static uint8_t KEY_PIN_SOURCE[KEYn] = {SYS_RECOVERY_KEY_EXTI_PORT_SOURCE};
  11. static uint8_t KEY_IRQn[KEYn] = {SYS_RECOVERY_KEY_EXTI_IRQn};
  12. void gd_eval_key_init(key_typedef_enum key_num, keymode_typedef_enum key_mode)
  13. {
  14. /* enable the key clock */
  15. rcu_periph_clock_enable(KEY_CLK[key_num]);
  16. rcu_periph_clock_enable(RCU_AF);
  17. /* configure button pin as input */
  18. gpio_init(KEY_PORT[key_num], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, KEY_PIN[key_num]);
  19. if (key_mode == KEY_MODE_EXTI) {
  20. /* enable and set key EXTI interrupt to the lowest priority */
  21. nvic_irq_enable(KEY_IRQn[key_num], 2U, 2U);
  22. /* connect key EXTI line to key GPIO pin */
  23. gpio_exti_source_select(KEY_PORT_SOURCE[key_num], KEY_PIN_SOURCE[key_num]);
  24. /* configure key EXTI line */
  25. exti_init(KEY_EXTI_LINE[key_num], EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  26. exti_interrupt_flag_clear(KEY_EXTI_LINE[key_num]);
  27. }
  28. }
  29. void EXTI1_IRQHandler(void){
  30. if (exti_interrupt_flag_get(EXTI_1) != RESET) {
  31. /* 延时1秒钟 */
  32. delay_1ms(1000);
  33. /* 判断按键是否仍然被按下 */
  34. if (gpio_input_bit_get(KEY_PORT[0], KEY_PIN[0]) == RESET) {
  35. /* 执行数据清除操作 */
  36. clear_gateway_config_block();
  37. }
  38. /* 清除EXTI1中断标志 */
  39. exti_interrupt_flag_clear(EXTI_1);
  40. NVIC_SystemReset(); //设备重启
  41. }
  42. }