接口/总线/驱动
目标
测试 ARTPI 上和 pin 有关的这些接口:
rt_pin_get() 获取引脚编号
rt_pin_mode() 设置引脚模式
rt_pin_write() 设置引脚电平
rt_pin_read() 读取引脚电平
rt_pin_attach_irq() 绑定引脚中断回调函数
rt_pin_irq_enable() 使能引脚中断
rt_pin_detach_irq() 脱离引脚中断回调函数
接线
根据 ARTPI 引脚图:
我们选择 PC6、PC7 两个引脚,PC7 输出,PC6 输入下拉。用一根杜邦线将两个引脚连接起来。
PC7 每隔半秒钟切换一次高低电平,PC6 设置成上升沿中断。同时每隔一秒切换一次中断使能状况。这样每过2秒钟就会触发执行中断回调函数。
代码
// main.c
#include
#include
#include "drv_common.h"
//#define PC7_PIN GET_PIN(C, 7)
//#define PC6_PIN GET_PIN(C, 6)
// 中断回调函数
void pin_isr(void *args)
{
rt_uint32_t pin = (rt_uint32_t)args; // 获取引脚编号
rt_kprintf("pin %d level is highn", pin); // 打印引脚电平状态
}
int main(void)
{
rt_uint32_t count = 1;
int pc6 = rt_pin_get("PC.6");
int pc7 = rt_pin_get("PC.7");
rt_kprintf("pc6 is %dn", pc6);
rt_kprintf("pc7 is %dn", pc7);
rt_pin_mode(pc7, PIN_MODE_OUTPUT);
rt_pin_mode(pc6, PIN_MODE_INPUT_PULLDOWN);
rt_pin_attach_irq(pc6, PIN_IRQ_MODE_RISING, pin_isr, (void )pc6);
rt_pin_irq_enable(pc6, PIN_IRQ_ENABLE);
while (count ++) {
if (count % 2) rt_pin_irq_enable(pc6, PIN_IRQ_DISABLE);
else rt_pin_irq_enable(pc6, PIN_IRQ_ENABLE);
rt_thread_mdelay(500);
rt_pin_write(pc7, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(pc7, PIN_LOW);
}
return RT_EOK;
}
#include "stm32h7xx.h"
static int vtor_config(void)
{
/ Vector Table Relocation in Internal QSPI_FLASH */
SCB->VTOR = QSPI_BASE;
return 0;
}
INIT_BOARD_EXPORT(vtor_config);
运行结果
| /
RT - Thread Operating System
/ | 4.1.0 build Jun 9 2023 14:46:16
2006 - 2022 Copyright by RT-Thread team
pc6 is 38
pc7 is 39
msh />pin 38 level is high
pin 38 level is high
pin 38 level is high
pin 38 level is high
pin 38 level is high
pin 38 level is high
pin 38 level is high
(每隔2秒输出一次 pin 38 level is high)
至此,RT-Thread 中 PIN 的所有接口都已经测试完毕。
utest 自动测试
在 application 目录下新建一个 pin_tc.c 文件,内容如下:
/*
Copyright (c) 2006-2023, RT-Thread Development Team
SPDX-License-Identifier: Apache-2.0
Change Logs:
Date Author Notes
2023-06-09 IcyFeather the first version
*/
#include
#include
#include "drv_common.h"
#include "utest.h"
// 中断回调函数
void pin_isr(void *args)
{
rt_uint32_t pin = (rt_uint32_t)args; // 获取引脚编号
uassert_int_equal(pin, 38);
uassert_int_equal(rt_pin_read(38), PIN_HIGH);
}
static void test_rt_pin(void)
{
int pc6 = rt_pin_get("PC.6");
int pc7 = rt_pin_get("PC.7");
uassert_int_equal(pc6, 38);
uassert_int_equal(pc7, 39);
rt_pin_mode(pc7, PIN_MODE_OUTPUT);
rt_pin_mode(pc6, PIN_MODE_INPUT_PULLDOWN);
rt_pin_attach_irq(pc6, PIN_IRQ_MODE_RISING, pin_isr, (void *)pc6);
rt_pin_irq_enable(pc6, PIN_IRQ_ENABLE);
rt_pin_write(pc7, PIN_HIGH);
uassert_int_equal(rt_pin_read(pc6), PIN_HIGH);
rt_pin_write(pc7, PIN_LOW);
uassert_int_equal(rt_pin_read(pc6), PIN_LOW);
}
static rt_err_t utest_tc_init(void)
{
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(test_rt_pin);
}
UTEST_TC_EXPORT(testcase, "pin_tc", utest_tc_init, utest_tc_cleanup, 10);
编译下载,在 finsh 中运行 utest_run:
最后显示 PASSED 即为测试通过。
全部0条评论
快来发表一下你的评论吧 !