Skip to content

Commit dfd495d

Browse files
authored
Merge pull request #25 from misonyo/rttdev
[更新]注释更新及移除设置频率代码
2 parents 8b40b59 + dc67980 commit dfd495d

File tree

6 files changed

+146
-26
lines changed

6 files changed

+146
-26
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@
88

99
| 文件 | 说明 |
1010
| ---------------- | ------------------------------- |
11-
| adc_vol_sample.c | 使用 ADC 设备转换电压数据 |
12-
| hwtimer_sample.c | 使用 硬件定时器定时 |
13-
| i2c_aht10_sample.c | 使用 i2c 设备获取 aht10 温湿度传感器数据 |
14-
| iwdg_sample.c | 看门狗设备使用示例 |
15-
| led_blink_sample.c | 使用 pin 设备控制 LED 闪烁 |
16-
| pin_beep_sample.c | 使用 pin 设备控制蜂鸣器 |
17-
| pwm_led_sample.c | 使用 pwm 设备控制 LED 的亮度 |
18-
| rtc_sample.c | 使用 rtc 设备设置年月日时分秒信息 |
19-
| sd_sample.c | 块设备 SD 卡使用示例 |
20-
| spi_w25q_sample.c | 使用 spi 设备读取 W25Q ID |
21-
| uart_sample.c | 使用 serial 设备收发数据 |
11+
| adc_vol_sample.c | 使用 ADC 设备转换电压数据 |
12+
| hwtimer_sample.c | 使用 硬件定时器定时 |
13+
| i2c_aht10_sample.c | 使用 i2c 设备获取 aht10 温湿度传感器数据 |
14+
| iwdg_sample.c | 看门狗设备使用示例 |
15+
| led_blink_sample.c | 使用 pin 设备控制 LED 闪烁 |
16+
| pin_beep_sample.c | 使用 pin 设备控制蜂鸣器 |
17+
| pwm_led_sample.c | 使用 pwm 设备控制 LED 的亮度 |
18+
| rtc_sample.c | 使用 rtc 设备设置年月日时分秒信息 |
19+
| sd_sample.c | 块设备 SD 卡使用示例 |
20+
| spi_w25q_sample.c | 使用 spi 设备读取 W25Q ID |
21+
| uart_sample.c | 使用 serial 设备中断接收及轮询发送模式收发数据 |
22+
| uart_dma_sample.c | 使用 serial 设备 DMA 接收及轮询发送模式收发数据 |
2223

2324
### 1.2 许可证
2425

@@ -37,7 +38,6 @@ RT-Thread online packages
3738
miscellaneous packages --->
3839
samples: kernel and components samples --->
3940
a peripheral_samples package for rt-thread --->
40-
4141
```
4242

4343
然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。

SConscript

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ src = []
44
cwd = GetCurrentDir()
55
include_path = [cwd]
66

7-
# add kernel samples.
8-
97
if GetDepend('PERIPHERAL_SAMPLES_USING_ADC'):
108
src += ['adc_vol_sample.c']
119

@@ -36,6 +34,9 @@ if GetDepend('PERIPHERAL_SAMPLES_USING_SD'):
3634
if GetDepend('PERIPHERAL_SAMPLES_USING_SERIAL'):
3735
src += Glob('uart_sample.c')
3836

37+
if GetDepend('PERIPHERAL_SAMPLES_USING_SERIAL_DMA'):
38+
src += Glob('uart_dma_sample.c')
39+
3940
if GetDepend('PERIPHERAL_SAMPLES_USING_SPI'):
4041
src += Glob('spi_w25q_sample.c')
4142

hwtimer_sample.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
/* 定时器超时回调函数 */
2323
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
2424
{
25-
rt_kprintf("this is hwtimer timeout callback fucntion!\n");
2625
rt_kprintf("tick is :%d !\n", rt_tick_get());
2726

2827
return 0;
@@ -34,7 +33,6 @@ static int hwtimer_sample(int argc, char *argv[])
3433
rt_hwtimerval_t timeout_s; /* 定时器超时值 */
3534
rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
3635
rt_hwtimer_mode_t mode; /* 定时器模式 */
37-
rt_uint32_t freq = 10000; /* 计数频率 */
3836

3937
/* 查找定时器设备 */
4038
hw_dev = rt_device_find(HWTIMER_DEV_NAME);
@@ -55,14 +53,6 @@ static int hwtimer_sample(int argc, char *argv[])
5553
/* 设置超时回调函数 */
5654
rt_device_set_rx_indicate(hw_dev, timeout_cb);
5755

58-
/* 设置计数频率(默认1Mhz或支持的最小计数频率) */
59-
ret = rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
60-
if (ret != RT_EOK)
61-
{
62-
rt_kprintf("set frequency failed! ret is :%d\n", ret);
63-
return ret;
64-
}
65-
6656
/* 设置模式为周期性定时器 */
6757
mode = HWTIMER_MODE_PERIOD;
6858
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);

i2c_aht10_sample.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static void read_temp_humi(float *cur_temp, float *cur_humi)
7979
rt_uint8_t temp[6];
8080

8181
write_reg(i2c_bus, AHT10_GET_DATA, 0); /* 发送命令 */
82+
rt_thread_mdelay(400);
8283
read_regs(i2c_bus, 6, temp); /* 获取传感器数据 */
8384

8485
/* 湿度数据转换 */

pin_beep_sample.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ static void pin_beep_sample(void)
5151

5252
/* 按键0引脚为输入模式 */
5353
rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP);
54-
/* 绑定中断,上升沿模式,回调函数名为beep_on */
54+
/* 绑定中断,下降沿模式,回调函数名为beep_on */
5555
rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_on, RT_NULL);
5656
/* 使能中断 */
5757
rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE);
5858

5959
/* 按键1引脚为输入模式 */
6060
rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
61-
/* 绑定中断,上升沿模式,回调函数名为beep_off */
61+
/* 绑定中断,下降沿模式,回调函数名为beep_off */
6262
rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_off, RT_NULL);
6363
/* 使能中断 */
6464
rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);

uart_dma_sample.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
* 2019-04-16 misonyo first implementation.
9+
*/
10+
/*
11+
* 程序清单:这是一个串口设备 DMA 接收使用例程
12+
* 例程导出了 uart_dma_sample 命令到控制终端
13+
* 命令调用格式:uart_dma_sample uart3
14+
* 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
15+
* 程序功能:通过串口输出字符串"hello RT-Thread!",并通过串口输出接收到的数据,然后打印接收到的数据。
16+
*/
17+
18+
#include <rtthread.h>
19+
20+
#define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */
21+
22+
/* 串口接收消息结构*/
23+
struct rx_msg
24+
{
25+
rt_device_t dev;
26+
rt_size_t size;
27+
};
28+
/* 串口设备句柄 */
29+
static rt_device_t serial;
30+
/* 消息队列控制块 */
31+
static struct rt_messagequeue rx_mq;
32+
33+
/* 接收数据回调函数 */
34+
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
35+
{
36+
struct rx_msg msg;
37+
rt_err_t result;
38+
msg.dev = dev;
39+
msg.size = size;
40+
41+
result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
42+
if ( result == -RT_EFULL)
43+
{
44+
/* 消息队列满 */
45+
rt_kprintf("message queue full!\n");
46+
}
47+
return result;
48+
}
49+
50+
static void serial_thread_entry(void *parameter)
51+
{
52+
struct rx_msg msg;
53+
rt_err_t result;
54+
rt_uint32_t rx_length;
55+
static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
56+
57+
while (1)
58+
{
59+
rt_memset(&msg, 0, sizeof(msg));
60+
/* 从消息队列中读取消息*/
61+
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
62+
if (result == RT_EOK)
63+
{
64+
/* 从串口读取数据*/
65+
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
66+
rx_buffer[rx_length] = '\0';
67+
/* 通过串口设备 serial 输出读取到的消息 */
68+
rt_device_write(serial, 0, rx_buffer, rx_length);
69+
/* 打印数据 */
70+
rt_kprintf("%s\n",rx_buffer);
71+
}
72+
}
73+
}
74+
75+
static int uart_dma_sample(int argc, char *argv[])
76+
{
77+
rt_err_t ret = RT_EOK;
78+
char uart_name[RT_NAME_MAX];
79+
static char msg_pool[256];
80+
char str[] = "hello RT-Thread!\r\n";
81+
82+
if (argc == 2)
83+
{
84+
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
85+
}
86+
else
87+
{
88+
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
89+
}
90+
91+
/* 查找串口设备 */
92+
serial = rt_device_find(uart_name);
93+
if (!serial)
94+
{
95+
rt_kprintf("find %s failed!\n", uart_name);
96+
return RT_ERROR;
97+
}
98+
99+
/* 初始化消息队列 */
100+
rt_mq_init(&rx_mq, "rx_mq",
101+
msg_pool, /* 存放消息的缓冲区 */
102+
sizeof(struct rx_msg), /* 一条消息的最大长度 */
103+
sizeof(msg_pool), /* 存放消息的缓冲区大小 */
104+
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
105+
106+
/* 以 DMA 接收及轮询发送方式打开串口设备 */
107+
rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX);
108+
/* 设置接收回调函数 */
109+
rt_device_set_rx_indicate(serial, uart_input);
110+
/* 发送字符串 */
111+
rt_device_write(serial, 0, str, (sizeof(str) - 1));
112+
113+
/* 创建 serial 线程 */
114+
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
115+
/* 创建成功则启动线程 */
116+
if (thread != RT_NULL)
117+
{
118+
rt_thread_startup(thread);
119+
}
120+
else
121+
{
122+
ret = RT_ERROR;
123+
}
124+
125+
return ret;
126+
}
127+
/* 导出到 msh 命令列表中 */
128+
MSH_CMD_EXPORT(uart_dma_sample, uart device dma sample);

0 commit comments

Comments
 (0)