summaryrefslogtreecommitdiff
path: root/bsp/essemi/es32f0654/drivers/bsp_driver_example/led_blink_sample.c
blob: 42c956a0602719b829d754037e11d491632c4bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (c) 2006-2018, 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>

/* PIN脚编号,查看驱动文件drv_gpio.c确定 */
#define LED_PIN_NUM    40   /*PB9*/
static int pin_num;

static void led_entry(void *parameter)
{
    int count = 0;
    /* 设置PIN脚模式为输出 */
    rt_pin_mode(pin_num, PIN_MODE_OUTPUT);

    while (1)
    {
        count++;
        rt_kprintf("thread run count : %d\r\n", count);
        /* 拉低PIN脚 */
        rt_pin_write(pin_num, PIN_LOW);
        rt_kprintf("led on!\r\n");
        /* 延时1000ms */
        rt_thread_mdelay(1000);

        /* 拉高PIN脚 */
        rt_pin_write(pin_num, PIN_HIGH);
        rt_kprintf("led off!\r\n");
        rt_thread_mdelay(1000);
    }
}

static 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,
                           512,
                           RT_THREAD_PRIORITY_MAX / 3,
                           20);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
    else
    {
        ret = RT_ERROR;
    }

    return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(led_sample, led sample);