Skip to content

Commit 41ff3e5

Browse files
committed
update samples
1 parent d6f6309 commit 41ff3e5

File tree

6 files changed

+51
-58
lines changed

6 files changed

+51
-58
lines changed

adc_vol_sample.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@
88
* 2018-11-29 misonyo first implementation.
99
*/
1010
/*
11-
* 程序清单:这是一个 ADC 设备使用例程
11+
* 程序清单: ADC 设备使用例程
1212
* 例程导出了 adc_sample 命令到控制终端
1313
* 命令调用格式:adc_sample
1414
* 程序功能:通过 ADC 设备采样电压值并转换为数值。
15+
* 示例代码参考电压为3.3V,分辨率为12位。
1516
*/
1617

1718
#include <rtthread.h>
1819
#include <rtdevice.h>
1920

20-
#define ADC_DEV_NAME "adc1"
21-
#define ADC_DEV_CHANNEL 5
21+
#define ADC_DEV_NAME "adc1" /* ADC 设备名称 */
22+
#define ADC_DEV_CHANNEL 5 /* ADC 通道 */
2223

2324
static int adc_vol_sample(int argc, char *argv[])
2425
{
@@ -37,15 +38,15 @@ static int adc_vol_sample(int argc, char *argv[])
3738
/* 使能设备 */
3839
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
3940

40-
/* 读取数据 */
41+
/* 读取采样值 */
4142
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
4243
rt_kprintf("the value is :%d \n", value);
4344

4445
/* 转换为对应电压值,3.3V对应12位最大值4096,数据精度乘以100保留2位小数 */
4546
vol = value * 330 / 4096;
4647
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
4748

48-
/* 关闭设备 */
49+
/* 关闭通道 */
4950
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
5051

5152
return ret;

hwtimer_sample.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* 程序清单:这是一个 hwtimer 设备使用例程
1212
* 例程导出了 hwtimer_sample 命令到控制终端
1313
* 命令调用格式:hwtimer_sample
14-
* 程序功能:硬件定时器周期性的打印当前tick值,2次tick值之差也就是定时时间
14+
* 程序功能:硬件定时器超时回调函数周期性的打印当前tick值,2次tick值之差换算为时间等同于定时时间值
1515
*/
1616

1717
#include <rtthread.h>
1818
#include <rtdevice.h>
1919

20-
#define HWTIMER_DEV_NAME "timer0"
20+
#define HWTIMER_DEV_NAME "timer0" /* 定时器名称 */
2121

2222
/* 定时器超时回调函数 */
2323
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
@@ -31,8 +31,8 @@ static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
3131
static int hwtimer_sample(int argc, char *argv[])
3232
{
3333
rt_err_t ret = RT_EOK;
34-
rt_hwtimerval_t timeout_s;
35-
rt_device_t hw_dev = RT_NULL;
34+
rt_hwtimerval_t timeout_s; /* 定时器超时值 */
35+
rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
3636
rt_hwtimer_mode_t mode; /* 定时器模式 */
3737
rt_uint32_t freq = 10000; /* 计数频率 */
3838

@@ -44,7 +44,7 @@ static int hwtimer_sample(int argc, char *argv[])
4444
return RT_ERROR;
4545
}
4646

47-
/* 打开设备 */
47+
/* 以读写方式打开设备 */
4848
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
4949
if (ret != RT_EOK)
5050
{

i2c_aht10_sample.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#define AHT10_NORMAL_CMD 0xA8 /* 一般命令 */
2525
#define AHT10_GET_DATA 0xAC /* 获取数据命令 */
2626

27-
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
28-
static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */
27+
static struct rt_i2c_bus_device *i2c_bus = RT_NULL; /* I2C总线设备句柄 */
28+
static rt_bool_t initialized = RT_FALSE; /* 传感器初始化状态 */
2929

3030
/* 写传感器寄存器 */
3131
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
@@ -44,9 +44,13 @@ static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint
4444

4545
/* 调用I2C设备接口传输数据 */
4646
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
47+
{
4748
return RT_EOK;
49+
}
4850
else
51+
{
4952
return -RT_ERROR;
53+
}
5054
}
5155

5256
/* 读传感器寄存器数据 */
@@ -61,9 +65,13 @@ static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint
6165

6266
/* 调用I2C设备接口传输数据 */
6367
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
68+
{
6469
return RT_EOK;
70+
}
6571
else
72+
{
6673
return -RT_ERROR;
74+
}
6775
}
6876

6977
static void read_temp_humi(float *cur_temp, float *cur_humi)

iwdg_sample.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
#include <rtthread.h>
2020
#include <rtdevice.h>
2121

22-
#define IWDG_DEVICE_NAME "iwg"
22+
#define IWDG_DEVICE_NAME "iwg" /* 看门狗设备名称 */
2323

24-
static rt_device_t wdg_dev;
24+
static rt_device_t wdg_dev; /* 看门狗设备句柄 */
2525

2626
static void idle_hook(void)
2727
{
2828
/* 在空闲线程的回调函数里喂狗 */
2929
rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
30+
rt_kprintf("feed the dog!\n ");
3031
}
3132

3233
static int iwdg_sample(int argc, char *argv[])
@@ -59,7 +60,7 @@ static int iwdg_sample(int argc, char *argv[])
5960
return RT_ERROR;
6061
}
6162
/* 设置看门狗溢出时间 */
62-
ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)timeout);
63+
ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
6364
if (ret != RT_EOK)
6465
{
6566
rt_kprintf("set %s timeout failed!\n", device_name);

pwm_led_sample.c

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,38 @@
1717
#include <rtthread.h>
1818
#include <rtdevice.h>
1919

20-
/* LED PIN脚编号,查看驱动文件drv_gpio.c确定 */
21-
#define LED_PIN_NUM 57
22-
#define PWM_DEV_NAME "pwm3"
23-
#define PWM_DEV_CHANNEL 4
20+
#define LED_PIN_NUM 57 /* LED PIN脚编号,查看驱动文件drv_gpio.c确定 */
21+
#define PWM_DEV_NAME "pwm3" /* PWM设备名称 */
22+
#define PWM_DEV_CHANNEL 4 /* PWM通道 */
2423

25-
struct rt_device_pwm *pwm_dev;
24+
struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
2625

27-
static void pwm_led_entry(void *parameter)
26+
static int pwm_led_sample(int argc, char *argv[])
2827
{
2928
rt_uint32_t period, pulse, dir;
3029

3130
period = 500000; /* 周期为0.5ms,单位为纳秒ns */
3231
dir = 1; /* PWM脉冲宽度值的增减方向 */
3332
pulse = 0; /* PWM脉冲宽度值,单位为纳秒ns */
3433

34+
/* 设置LED引脚脚模式为输出 */
35+
rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT);
36+
/* 拉高LED引脚 */
37+
rt_pin_write(LED_PIN_NUM, PIN_HIGH);
38+
39+
/* 查找设备 */
40+
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
41+
if (pwm_dev == RT_NULL)
42+
{
43+
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
44+
return RT_ERROR;
45+
}
46+
47+
/* 设置PWM周期和脉冲宽度默认值 */
48+
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
49+
/* 使能设备 */
50+
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
51+
3552
while (1)
3653
{
3754
rt_thread_mdelay(50);
@@ -56,39 +73,5 @@ static void pwm_led_entry(void *parameter)
5673
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
5774
}
5875
}
59-
60-
static int pwm_led_sample(int argc, char *argv[])
61-
{
62-
rt_thread_t tid;
63-
64-
/* 设置LED引脚脚模式为输出 */
65-
rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT);
66-
/* 拉高LED引脚 */
67-
rt_pin_write(LED_PIN_NUM, PIN_HIGH);
68-
69-
/* 查找设备 */
70-
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
71-
if (pwm_dev == RT_NULL)
72-
{
73-
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
74-
return RT_ERROR;
75-
}
76-
77-
/* 使能设备 */
78-
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
79-
80-
tid = rt_thread_create("pwm",
81-
pwm_led_entry,
82-
RT_NULL,
83-
512,
84-
RT_THREAD_PRIORITY_MAX / 3,
85-
20);
86-
if (tid != RT_NULL)
87-
{
88-
rt_thread_startup(tid);
89-
}
90-
91-
return RT_EOK;
92-
}
9376
/* 导出到 msh 命令列表中 */
94-
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);
77+
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);

uart_sample.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <rtthread.h>
1919

20-
#define SAMPLE_UART_NAME "uart2"
20+
#define SAMPLE_UART_NAME "uart2" /* 串口设备名称 */
2121

2222
/* 用于接收消息的信号量 */
2323
static struct rt_semaphore rx_sem;
@@ -65,7 +65,7 @@ static int uart_sample(int argc, char *argv[])
6565
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
6666
}
6767

68-
/* 查找系统中的串口设备 */
68+
/* 查找串口设备 */
6969
serial = rt_device_find(uart_name);
7070
if (!serial)
7171
{

0 commit comments

Comments
 (0)