Skip to content

Commit 63d757d

Browse files
authored
Merge pull request #10 from misonyo/dev
增加iwgd/led/sd sample
2 parents 36f6914 + e47074d commit 63d757d

File tree

3 files changed

+277
-0
lines changed

3 files changed

+277
-0
lines changed

iwdg_sample.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018-09-25 misonyo first edition.
9+
*/
10+
/*
11+
* 程序清单:这是一个独立看门狗设备使用例程
12+
* 例程导出了 iwdg_sample 命令到控制终端
13+
* 命令调用格式:iwdg_sample iwg
14+
* 命令解释:命令第二个参数是要使用的看门狗设备名称,为空则使用例程默认的看门狗设备
15+
* 程序功能:程序通过设备名称查找看门狗设备,然后初始化设备并设置看门狗设备溢出时间。
16+
* 然后设置空闲线程回调函数,在回调函数里会喂狗。
17+
*/
18+
19+
#include <rtthread.h>
20+
#include <rtdevice.h>
21+
22+
#define IWDG_DEVICE_NAME "iwg"
23+
24+
static rt_device_t wdg_dev;
25+
26+
static void idle_hook(void)
27+
{
28+
/* 在空闲线程的回调函数里喂狗 */
29+
rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
30+
}
31+
32+
static int iwdg_sample(int argc,char *argv[])
33+
{
34+
rt_err_t result = RT_EOK;
35+
rt_uint32_t timeout = 1000; /* 超时时间为1000ms*/
36+
char device_name[RT_NAME_MAX];
37+
38+
/* 判断命令行参数是否给定了设备名称 */
39+
if (argc == 2)
40+
{
41+
rt_strncpy(device_name, argv[1], RT_NAME_MAX);
42+
}
43+
else
44+
{
45+
rt_strncpy(device_name, IWDG_DEVICE_NAME, RT_NAME_MAX);
46+
}
47+
/* 根据设备名称查找看门狗设备,获取设备句柄 */
48+
wdg_dev = rt_device_find(device_name);
49+
if (!wdg_dev)
50+
{
51+
rt_kprintf("find %s failed!\n",device_name);
52+
return RT_ERROR;
53+
}
54+
/* 初始化设备 */
55+
result = rt_device_init(wdg_dev);
56+
if (result != RT_EOK)
57+
{
58+
rt_kprintf("initialize %s failed!\n",device_name);
59+
return RT_ERROR;
60+
}
61+
/* 设置看门狗溢出时间 */
62+
result = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void*)timeout);
63+
if (result != RT_EOK)
64+
{
65+
rt_kprintf("set %s timeout failed!\n",device_name);
66+
return RT_ERROR;
67+
}
68+
/* 设置空闲线程回调函数 */
69+
rt_thread_idle_sethook(idle_hook);
70+
71+
return result;
72+
}
73+
/* 导出到 msh 命令列表中 */
74+
MSH_CMD_EXPORT(iwdg_sample, iwdg sample);

led_blink_sample.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018-09-25 misonyo first edition.
9+
*/
10+
/*
11+
* 程序清单:这是一个通过PIN脚控制LED亮灭的使用例程
12+
* 例程导出了 led_sample 命令到控制终端
13+
* 命令调用格式:led_sample 41
14+
* 命令解释:命令第二个参数是要使用的PIN脚编号,为空则使用例程默认的引脚编号。
15+
* 程序功能:程序创建一个led线程,线程每隔1000ms改变PIN脚状态,达到控制led灯
16+
* 亮灭的效果。
17+
*/
18+
19+
#include <rtthread.h>
20+
#include <rtdevice.h>
21+
#include <stdlib.h>
22+
23+
/* PIN脚编号,查看驱动文件drv_gpio.c确定 */
24+
#define LED_PIN_NUM 41
25+
static int pin_num;
26+
27+
static void led_entry(void *parameter)
28+
{
29+
int count = 0;
30+
/* 设置PIN脚模式为输出 */
31+
rt_pin_mode(pin_num, PIN_MODE_OUTPUT);
32+
33+
while (1)
34+
{
35+
count++;
36+
rt_kprintf("thread run count : %d\r\n", count);
37+
/* 拉低PIN脚 */
38+
rt_pin_write(pin_num, PIN_LOW);
39+
rt_kprintf("led on!\r\n");
40+
/* 延时1000ms */
41+
rt_thread_mdelay(1000);
42+
43+
/* 拉高PIN脚 */
44+
rt_pin_write(pin_num, PIN_HIGH);
45+
rt_kprintf("led off!\r\n");
46+
rt_thread_mdelay(1000);
47+
}
48+
}
49+
50+
static void led_sample(int argc,char *argv[])
51+
{
52+
rt_thread_t tid;
53+
54+
/* 判断命令行参数是否给定了PIN脚编号 */
55+
if (argc == 2)
56+
{
57+
pin_num = atoi(argv[1]);
58+
}
59+
else
60+
{
61+
pin_num = LED_PIN_NUM;
62+
}
63+
64+
tid = rt_thread_create("led",
65+
led_entry,
66+
RT_NULL,
67+
512,
68+
RT_THREAD_PRIORITY_MAX/3,
69+
20);
70+
if (tid != RT_NULL)
71+
{
72+
rt_thread_startup(tid);
73+
}
74+
}
75+
/* 导出到 msh 命令列表中 */
76+
MSH_CMD_EXPORT(led_sample, led sample);

sd_sample.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018-09-25 misonyo first edition.
9+
*/
10+
/*
11+
* 程序清单:这是一个通过SD卡设备的使用例程
12+
* 例程导出了 sd_sample 命令到控制终端
13+
* 命令调用格式:sd_sample sd0
14+
* 命令解释:命令第二个参数是要使用的SD设备的名称,为空则使用例程默认的SD设备。
15+
* 程序功能:程序会产生一个块大小的随机数,然后写入SD卡中,然后在读取这部分写入的数据。
16+
* 对比写入和读出的数据是否一致,一致则表示程序运行正确。
17+
*/
18+
19+
#include <rtthread.h>
20+
#include <rtdevice.h>
21+
#include <stdlib.h>
22+
23+
#define SD_DEVICE_NAME "sd0"
24+
25+
void fill_buffer(rt_uint8_t *buff, rt_uint32_t buff_length)
26+
{
27+
rt_uint32_t index;
28+
/* 往缓冲区填充随机数 */
29+
for (index = 0; index < buff_length; index++)
30+
{
31+
buff[index] = ((rt_uint8_t)rand()) & 0xff;
32+
}
33+
}
34+
35+
static rt_err_t sd_sample(int argc, char *argv[])
36+
{
37+
rt_err_t result;
38+
rt_device_t sd_device;
39+
char sd_name[RT_NAME_MAX];
40+
rt_uint8_t* write_buff, *read_buff;
41+
struct rt_device_blk_geometry geo;
42+
rt_uint8_t block_num;
43+
/* 判断命令行参数是否给定了设备名称 */
44+
if (argc == 2)
45+
{
46+
rt_strncpy(sd_name, argv[1], RT_NAME_MAX);
47+
}
48+
else
49+
{
50+
rt_strncpy(sd_name, SD_DEVICE_NAME, RT_NAME_MAX);
51+
}
52+
/* 查找设备获取设备句柄 */
53+
sd_device = rt_device_find(sd_name);
54+
if (sd_device == RT_NULL)
55+
{
56+
rt_kprintf("find device %s failed!\n", sd_name);
57+
return RT_ERROR;
58+
}
59+
/* 打开设备 */
60+
result = rt_device_open(sd_device, RT_DEVICE_OFLAG_RDWR);
61+
if( result != RT_EOK )
62+
{
63+
rt_kprintf("open device %s failed!\n", sd_name);
64+
return result;
65+
}
66+
67+
rt_memset(&geo, 0, sizeof(geo));
68+
/* 获取块设备信息 */
69+
result = rt_device_control(sd_device, RT_DEVICE_CTRL_BLK_GETGEOME, &geo);
70+
if( result != RT_EOK )
71+
{
72+
rt_kprintf("control device %s failed!\n", sd_name);
73+
return result;
74+
}
75+
rt_kprintf("device information:\n");
76+
rt_kprintf("sector size : %d byte\n", geo.bytes_per_sector);
77+
rt_kprintf("sector count : %d \n", geo.sector_count);
78+
rt_kprintf("block size : %d byte\n", geo.block_size);
79+
/* 准备读写缓冲区空间,大小为一个块 */
80+
read_buff = rt_malloc(geo.block_size);
81+
if( read_buff == RT_NULL )
82+
{
83+
rt_kprintf("no memory for read buffer!\n");
84+
return RT_ERROR;
85+
}
86+
write_buff = rt_malloc(geo.block_size);
87+
if( write_buff == RT_NULL )
88+
{
89+
rt_kprintf("no memory for write buffer!\n");
90+
rt_free(read_buff);
91+
return RT_ERROR;
92+
}
93+
94+
/* 填充写数据缓冲区,为写操作做准备 */
95+
fill_buffer(write_buff, geo.block_size);
96+
97+
/* 把写数据缓冲的数据写入SD卡中,大小为一个块,size参数以块为单位 */
98+
block_num = rt_device_write(sd_device, 0, write_buff, 1);
99+
if (1 != block_num)
100+
{
101+
rt_kprintf("write device %s failed!\n",sd_name);
102+
}
103+
104+
/* 从SD卡中读出数据,并保存在读数据缓冲区中 */
105+
block_num = rt_device_read(sd_device, 0, read_buff, 1);
106+
if (1 != block_num)
107+
{
108+
rt_kprintf("read %s device failed!\n",sd_name);
109+
}
110+
111+
/* 比较写数据缓冲区和读数据缓冲区的内容是否完全一致 */
112+
if (rt_memcmp(write_buff, read_buff, geo.block_size) == 0)
113+
{
114+
rt_kprintf("Block test OK!\n");
115+
}
116+
else
117+
{
118+
rt_kprintf("Block test Fail!\n");
119+
}
120+
/* 释放缓冲区空间 */
121+
rt_free(read_buff);
122+
rt_free(write_buff);
123+
124+
return RT_EOK;
125+
}
126+
/* 导出到 msh 命令列表中 */
127+
MSH_CMD_EXPORT(sd_sample, sd device sample);

0 commit comments

Comments
 (0)