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