key.c 1.7 KB

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