test_gpio.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-30 CDT first version
  9. */
  10. /*
  11. * 程序清单:这是一个 PIN 设备使用例程
  12. * 例程导出了 pin_sample 命令到控制终端
  13. * 命令调用格式:pin_sample
  14. * 程序功能:通过按键控制LED引脚的电平状态
  15. */
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #include "board_config.h"
  19. #if defined(BSP_USING_GPIO)
  20. #include "drv_gpio.h"
  21. /* 1)配置RTT工程
  22. * menuconfig:
  23. * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable TCA9539
  24. */
  25. #if defined(HC32F460)
  26. #define LED1_PIN_NUM GET_PIN(D, 3) /* LED0 */
  27. #define KEY1_PIN_NUM GET_PIN(B, 1) /* K10 */
  28. #elif defined(HC32F4A0) || defined(HC32F4A8)
  29. #define LED1_PIN_NUM GET_PIN(B, 11) /* LED10 */
  30. #define KEY1_PIN_NUM GET_PIN(A, 0) /* K10 */
  31. #elif defined(HC32F448)
  32. #define LED1_PIN_NUM GET_PIN(A, 2) /* LED3 */
  33. #define KEY1_PIN_NUM GET_PIN(B, 6) /* K5 */
  34. #elif defined(HC32F472)
  35. #define LED1_PIN_NUM GET_PIN(C, 9) /* LED5 */
  36. #define KEY1_PIN_NUM GET_PIN(B, 5) /* K10 */
  37. #endif
  38. static uint8_t u8LedState = 1;
  39. void led_control(void *args)
  40. {
  41. u8LedState = !u8LedState;
  42. if (0 == u8LedState)
  43. {
  44. rt_pin_write(LED1_PIN_NUM, PIN_LOW);
  45. }
  46. else
  47. {
  48. rt_pin_write(LED1_PIN_NUM, PIN_HIGH);
  49. }
  50. }
  51. static void pin_sample(void)
  52. {
  53. /* LED引脚为输出模式 */
  54. rt_pin_mode(LED1_PIN_NUM, PIN_MODE_OUTPUT);
  55. /* 默认高电平 */
  56. rt_pin_write(LED1_PIN_NUM, PIN_HIGH);
  57. /* 按键1引脚为输入模式 */
  58. rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
  59. /* 绑定中断,下降沿模式,回调函数名为led_control */
  60. // rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_RISING, led_control, RT_NULL);
  61. // rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, led_control, RT_NULL);
  62. rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_RISING_FALLING, led_control, RT_NULL);
  63. /* 使能中断 */
  64. rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);
  65. }
  66. /* 导出到 msh 命令列表中 */
  67. MSH_CMD_EXPORT(pin_sample, pin sample);
  68. #endif