2023-04-27 09:29:44 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* Change Logs:
|
|
|
|
|
* Date Author Notes
|
|
|
|
|
* 2018-09-25 misonyo first edition.
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* 程序清单:这是一个通过PIN脚控制LED亮灭的使用例程
|
|
|
|
|
* 例程导出了 led_sample 命令到控制终端
|
|
|
|
|
* 命令调用格式:led_sample 41
|
|
|
|
|
* 命令解释:命令第二个参数是要使用的PIN脚编号,为空则使用例程默认的引脚编号。
|
|
|
|
|
* 程序功能:程序创建一个led线程,线程每隔1000ms改变PIN脚状态,达到控制led灯
|
|
|
|
|
* 亮灭的效果。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <board.h>
|
|
|
|
|
|
|
|
|
|
#define LOG_TAG "heartbeat" // 该 模 块 对 应 的 标 签。 不 定 义 时, 默 认:NO_TAG
|
|
|
|
|
#define LOG_LVL LOG_LVL_DBG // 该 模 块 对 应 的 日 志 输 出 级 别。 不 定 义 时, 默 认: 调 试级 别
|
|
|
|
|
#include <ulog.h> // 必 须 在 LOG_TAG 与 LOG_LVL 下 面
|
|
|
|
|
|
|
|
|
|
/* PIN脚编号,查看驱动文件drv_gpio.c确定 */
|
|
|
|
|
#define LED_PIN_NUM GET_PIN(E,3)
|
|
|
|
|
static int pin_num;
|
|
|
|
|
|
|
|
|
|
static void led_entry(void *parameter)
|
|
|
|
|
{
|
|
|
|
|
/* 设置PIN脚模式为输出 */
|
|
|
|
|
rt_pin_mode(pin_num, PIN_MODE_OUTPUT);
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
/* 拉低PIN脚 */
|
2023-08-15 09:10:18 +00:00
|
|
|
|
rt_pin_write(LED_HEART, PIN_LOW);
|
|
|
|
|
rt_pin_write(LED_HEART_DEBUG, PIN_HIGH);
|
2023-04-27 09:29:44 +00:00
|
|
|
|
/* 延时1ms,省电 */
|
2023-08-15 09:10:18 +00:00
|
|
|
|
rt_thread_mdelay(10); //去掉延时,共用print替换
|
2023-04-27 09:29:44 +00:00
|
|
|
|
// rt_kprintf("Heartbeat.\n");
|
|
|
|
|
|
|
|
|
|
/* 拉高PIN脚 */
|
2023-08-15 09:10:18 +00:00
|
|
|
|
rt_pin_write(LED_HEART, PIN_HIGH);
|
|
|
|
|
rt_pin_write(LED_HEART_DEBUG, PIN_LOW);
|
2023-04-27 09:29:44 +00:00
|
|
|
|
rt_thread_mdelay(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int led_sample(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
rt_thread_t tid;
|
|
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
|
|
|
|
|
|
|
/* 判断命令行参数是否给定了PIN脚编号 */
|
|
|
|
|
if (argc == 2)
|
|
|
|
|
{
|
|
|
|
|
pin_num = atoi(argv[1]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pin_num = LED_PIN_NUM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tid = rt_thread_create("led",
|
|
|
|
|
led_entry,
|
|
|
|
|
RT_NULL,
|
2023-08-15 09:10:18 +00:00
|
|
|
|
256,
|
|
|
|
|
30,
|
2023-04-27 09:29:44 +00:00
|
|
|
|
20);
|
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret = RT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
/* 导出到 msh 命令列表中 */
|
2023-05-04 08:35:14 +00:00
|
|
|
|
//MSH_CMD_EXPORT(led_sample, led sample);
|
2023-04-27 09:29:44 +00:00
|
|
|
|
/* 导出到自动初始化 */
|
2023-08-15 09:10:18 +00:00
|
|
|
|
INIT_COMPONENT_EXPORT(led_sample);
|
2023-04-27 09:29:44 +00:00
|
|
|
|
|