Skip to content

Commit 43d8621

Browse files
committed
add pwm device sample and astyle all files
1 parent 308137c commit 43d8621

File tree

8 files changed

+405
-324
lines changed

8 files changed

+405
-324
lines changed

i2c_aht10_sample.c

Lines changed: 142 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,142 @@
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-08-15 misonyo first implementation.
9-
*/
10-
/*
11-
* 程序清单:这是一个 I2C 设备使用例程
12-
* 例程导出了 i2c_aht10_sample 命令到控制终端
13-
* 命令调用格式:i2c_aht10_sample i2c1
14-
* 命令解释:命令第二个参数是要使用的I2C总线设备名称,为空则使用默认的I2C总线设备
15-
* 程序功能:通过 I2C 设备读取温湿度传感器 aht10 的温湿度数据并打印
16-
*/
17-
18-
#include <rtthread.h>
19-
#include <rtdevice.h>
20-
21-
#define AHT10_I2C_BUS_NAME "i2c1" /* 传感器连接的I2C总线设备名称 */
22-
#define AHT10_ADDR 0x38
23-
#define AHT10_CALIBRATION_CMD 0xE1 /* 校准命令 */
24-
#define AHT10_NORMAL_CMD 0xA8 /* 一般命令 */
25-
#define AHT10_GET_DATA 0xAC /* 获取数据命令 */
26-
27-
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
28-
static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */
29-
30-
/* 写传感器寄存器 */
31-
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
32-
{
33-
rt_uint8_t buf[3];
34-
struct rt_i2c_msg msgs;
35-
36-
buf[0] = reg; //cmd
37-
buf[1] = data[0];
38-
buf[2] = data[1];
39-
40-
msgs.addr = AHT10_ADDR;
41-
msgs.flags = RT_I2C_WR;
42-
msgs.buf = buf;
43-
msgs.len = 3;
44-
45-
/* 调用I2C设备接口发送数据 */
46-
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
47-
return RT_EOK;
48-
else
49-
return -RT_ERROR;
50-
}
51-
52-
/* 读传感器寄存器数据 */
53-
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
54-
{
55-
struct rt_i2c_msg msgs;
56-
57-
msgs.addr = AHT10_ADDR;
58-
msgs.flags = RT_I2C_RD;
59-
msgs.buf = buf;
60-
msgs.len = len;
61-
62-
/* 调用I2C设备接口接收数据 */
63-
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
64-
return RT_EOK;
65-
else
66-
return -RT_ERROR;
67-
}
68-
69-
static void read_temp_humi(float *cur_temp,float *cur_humi)
70-
{
71-
rt_uint8_t temp[6];
72-
73-
write_reg(i2c_bus, AHT10_GET_DATA, 0); /* 发送命令 */
74-
read_regs(i2c_bus, 6, temp); /* 获取传感器数据 */
75-
76-
/* 湿度数据转换 */
77-
*cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
78-
/* 温度数据转换 */
79-
*cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
80-
}
81-
82-
static void aht10_init(const char *name)
83-
{
84-
rt_uint8_t temp[2] = {0, 0};
85-
86-
/* 查找I2C总线设备,获取I2C总线设备句柄 */
87-
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
88-
89-
if (i2c_bus == RT_NULL)
90-
{
91-
rt_kprintf("can't find %s device!\n", name);
92-
}
93-
else
94-
{
95-
write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
96-
rt_thread_mdelay(400);
97-
98-
temp[0] = 0x08;
99-
temp[1] = 0x00;
100-
write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp);
101-
rt_thread_mdelay(400);
102-
initialized = RT_TRUE;
103-
}
104-
}
105-
106-
static void i2c_aht10_sample(int argc,char *argv[])
107-
{
108-
float humidity, temperature;
109-
char name[RT_NAME_MAX];
110-
111-
humidity = 0.0;
112-
temperature = 0.0;
113-
114-
if (argc == 2)
115-
{
116-
rt_strncpy(name, argv[1], RT_NAME_MAX);
117-
}
118-
else
119-
{
120-
rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
121-
}
122-
123-
if (!initialized)
124-
{
125-
/* 传感器初始化 */
126-
aht10_init(name);
127-
}
128-
if (initialized)
129-
{
130-
/* 读取温湿度数据 */
131-
read_temp_humi(&temperature, &humidity);
132-
133-
rt_kprintf("read aht10 sensor humidity : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
134-
rt_kprintf("read aht10 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);
135-
}
136-
else
137-
{
138-
rt_kprintf("initialize sensor failed!\n");
139-
}
140-
}
141-
/* 导出到 msh 命令列表中 */
142-
MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);
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-08-15 misonyo first implementation.
9+
*/
10+
/*
11+
* 程序清单:这是一个 I2C 设备使用例程
12+
* 例程导出了 i2c_aht10_sample 命令到控制终端
13+
* 命令调用格式:i2c_aht10_sample i2c1
14+
* 命令解释:命令第二个参数是要使用的I2C总线设备名称,为空则使用默认的I2C总线设备
15+
* 程序功能:通过 I2C 设备读取温湿度传感器 aht10 的温湿度数据并打印
16+
*/
17+
18+
#include <rtthread.h>
19+
#include <rtdevice.h>
20+
21+
#define AHT10_I2C_BUS_NAME "i2c1" /* 传感器连接的I2C总线设备名称 */
22+
#define AHT10_ADDR 0x38
23+
#define AHT10_CALIBRATION_CMD 0xE1 /* 校准命令 */
24+
#define AHT10_NORMAL_CMD 0xA8 /* 一般命令 */
25+
#define AHT10_GET_DATA 0xAC /* 获取数据命令 */
26+
27+
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
28+
static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */
29+
30+
/* 写传感器寄存器 */
31+
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
32+
{
33+
rt_uint8_t buf[3];
34+
struct rt_i2c_msg msgs;
35+
36+
buf[0] = reg; //cmd
37+
buf[1] = data[0];
38+
buf[2] = data[1];
39+
40+
msgs.addr = AHT10_ADDR;
41+
msgs.flags = RT_I2C_WR;
42+
msgs.buf = buf;
43+
msgs.len = 3;
44+
45+
/* 调用I2C设备接口发送数据 */
46+
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
47+
return RT_EOK;
48+
else
49+
return -RT_ERROR;
50+
}
51+
52+
/* 读传感器寄存器数据 */
53+
static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
54+
{
55+
struct rt_i2c_msg msgs;
56+
57+
msgs.addr = AHT10_ADDR;
58+
msgs.flags = RT_I2C_RD;
59+
msgs.buf = buf;
60+
msgs.len = len;
61+
62+
/* 调用I2C设备接口接收数据 */
63+
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
64+
return RT_EOK;
65+
else
66+
return -RT_ERROR;
67+
}
68+
69+
static void read_temp_humi(float *cur_temp, float *cur_humi)
70+
{
71+
rt_uint8_t temp[6];
72+
73+
write_reg(i2c_bus, AHT10_GET_DATA, 0); /* 发送命令 */
74+
read_regs(i2c_bus, 6, temp); /* 获取传感器数据 */
75+
76+
/* 湿度数据转换 */
77+
*cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
78+
/* 温度数据转换 */
79+
*cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
80+
}
81+
82+
static void aht10_init(const char *name)
83+
{
84+
rt_uint8_t temp[2] = {0, 0};
85+
86+
/* 查找I2C总线设备,获取I2C总线设备句柄 */
87+
i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
88+
89+
if (i2c_bus == RT_NULL)
90+
{
91+
rt_kprintf("can't find %s device!\n", name);
92+
}
93+
else
94+
{
95+
write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
96+
rt_thread_mdelay(400);
97+
98+
temp[0] = 0x08;
99+
temp[1] = 0x00;
100+
write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp);
101+
rt_thread_mdelay(400);
102+
initialized = RT_TRUE;
103+
}
104+
}
105+
106+
static void i2c_aht10_sample(int argc, char *argv[])
107+
{
108+
float humidity, temperature;
109+
char name[RT_NAME_MAX];
110+
111+
humidity = 0.0;
112+
temperature = 0.0;
113+
114+
if (argc == 2)
115+
{
116+
rt_strncpy(name, argv[1], RT_NAME_MAX);
117+
}
118+
else
119+
{
120+
rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
121+
}
122+
123+
if (!initialized)
124+
{
125+
/* 传感器初始化 */
126+
aht10_init(name);
127+
}
128+
if (initialized)
129+
{
130+
/* 读取温湿度数据 */
131+
read_temp_humi(&temperature, &humidity);
132+
133+
rt_kprintf("read aht10 sensor humidity : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
134+
rt_kprintf("read aht10 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);
135+
}
136+
else
137+
{
138+
rt_kprintf("initialize sensor failed!\n");
139+
}
140+
}
141+
/* 导出到 msh 命令列表中 */
142+
MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);

iwdg_sample.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Date Author Notes
88
* 2018-09-25 misonyo first edition.
99
*/
10-
/*
10+
/*
1111
* 程序清单:这是一个独立看门狗设备使用例程
1212
* 例程导出了 iwdg_sample 命令到控制终端
1313
* 命令调用格式:iwdg_sample iwg
@@ -29,7 +29,7 @@ static void idle_hook(void)
2929
rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
3030
}
3131

32-
static int iwdg_sample(int argc,char *argv[])
32+
static int iwdg_sample(int argc, char *argv[])
3333
{
3434
rt_err_t result = RT_EOK;
3535
rt_uint32_t timeout = 1000; /* 超时时间为1000ms*/
@@ -48,21 +48,21 @@ static int iwdg_sample(int argc,char *argv[])
4848
wdg_dev = rt_device_find(device_name);
4949
if (!wdg_dev)
5050
{
51-
rt_kprintf("find %s failed!\n",device_name);
51+
rt_kprintf("find %s failed!\n", device_name);
5252
return RT_ERROR;
5353
}
5454
/* 初始化设备 */
5555
result = rt_device_init(wdg_dev);
5656
if (result != RT_EOK)
5757
{
58-
rt_kprintf("initialize %s failed!\n",device_name);
58+
rt_kprintf("initialize %s failed!\n", device_name);
5959
return RT_ERROR;
6060
}
6161
/* 设置看门狗溢出时间 */
62-
result = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void*)timeout);
62+
result = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)timeout);
6363
if (result != RT_EOK)
6464
{
65-
rt_kprintf("set %s timeout failed!\n",device_name);
65+
rt_kprintf("set %s timeout failed!\n", device_name);
6666
return RT_ERROR;
6767
}
6868
/* 设置空闲线程回调函数 */

led_blink_sample.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Date Author Notes
88
* 2018-09-25 misonyo first edition.
99
*/
10-
/*
10+
/*
1111
* 程序清单:这是一个通过PIN脚控制LED亮灭的使用例程
1212
* 例程导出了 led_sample 命令到控制终端
1313
* 命令调用格式:led_sample 41
@@ -47,7 +47,7 @@ static void led_entry(void *parameter)
4747
}
4848
}
4949

50-
static void led_sample(int argc,char *argv[])
50+
static void led_sample(int argc, char *argv[])
5151
{
5252
rt_thread_t tid;
5353

@@ -65,7 +65,7 @@ static void led_sample(int argc,char *argv[])
6565
led_entry,
6666
RT_NULL,
6767
512,
68-
RT_THREAD_PRIORITY_MAX/3,
68+
RT_THREAD_PRIORITY_MAX / 3,
6969
20);
7070
if (tid != RT_NULL)
7171
{

0 commit comments

Comments
 (0)