Skip to content

Commit

Permalink
Merge pull request #4 from slyant/master
Browse files Browse the repository at this point in the history
适配RT-Thread串口驱动框架Serial_v2
  • Loading branch information
MrMichael authored Apr 20, 2022
2 parents b1f192d + 3c87eba commit 65171fd
Show file tree
Hide file tree
Showing 7 changed files with 701 additions and 77 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

1. Release version 1.0.0

2022-04-07:

1. Release version 2.0.0
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ WK2124 软件包为串口设备资源不足的产品提供串口扩展的方法

```shell
wk2124-latest/
├── ChangeLog.md # 修改记录
├── ChangeLog.md # 修改记录
├── LICENSE # 软件包许可证
├── README.md # 软件包使用说明
├── sample # 串口使用程序样例
├── README.md # 软件包使用说明
├── sample # 串口使用程序样例
│   ├── uart_int_sample.c # 中断模式
│   └── uart_sample.c # 轮询模式
├── SConscript # RT-Thread 默认的构建脚本
├── wk2124s.c # wk2124 驱动
├── wk2124s.h # wk2124 驱动头文件
├── wk2124_usart.c # wk2124 spi及uart驱动文件
└── wk2124_usart.h # wk2124 spi及uart驱动头文件
│   └── uart_sample.c # 轮询模式
├── SConscript # RT-Thread 默认的构建脚本
├── wk2124s.c # wk2124 驱动
├── wk2124s.h # wk2124 驱动头文件
├── wk2124_usart.c # wk2124 spi及uart驱动文件
├── wk2124_usart_v2.c # wk2124 spi及uart_v2驱动文件
└── wk2124_usart.h # wk2124 spi及uart驱动头文件
```

### 1.2 许可证
Expand Down Expand Up @@ -108,7 +109,7 @@ uartswk1--4串行字符设备支持轮询和中断接收两种模式(不支持

- 获取软件包时,需要注意正确配置使用的 SPI 设备名称、中断引脚号和芯片晶振频率;
- 推荐使用最新版,V1.0.0波特率计算存在BUG,且wk2124的串口关闭不彻底。

- _v2版本为适配RT-Thread serial_v2版本。


## 5 联系方式
Expand Down
13 changes: 10 additions & 3 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
from building import *

cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
path = [cwd]

group = DefineGroup('wk2124', src, depend = ['PKG_USING_WK2124'], CPPPATH = path)
src = Split('''
wk2124s.c
''')
if GetDepend(['RT_USING_SERIAL']):
if GetDepend(['RT_USING_SERIAL_V1']):
src += Glob('wk2124_usart.c')
if GetDepend(['RT_USING_SERIAL_V2']):
src += Glob('wk2124_usart_v2.c')

group = DefineGroup('wk2124', src, depend = ['RT_USING_SERIAL','PKG_USING_WK2124'], CPPPATH = path)

Return('group')
73 changes: 45 additions & 28 deletions sample/uart_int_sample.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <rtthread.h>
#include <rtdevice.h>

#define SAMPLE_UART_NAME "uartswk1" /* 串口设备名称 */
/* 将子串口1和子串口2连接测试 */
#define WK1_UART_NAME "uartswk1" /* 串口设备名称 */
#define WK2_UART_NAME "uartswk2" /* 串口设备名称 */

/* 用于接收消息的信号量 */
static rt_device_t serial_1, serial_2;
static struct rt_semaphore rx_sem;
static rt_device_t serial;

/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
Expand All @@ -14,52 +16,67 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
return RT_EOK;
}


static int uart_int_sample(int argc, char *argv[])
static int uart_int_sample(void)
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
char str[] = "hello RT-Thread!\r\n";
char ch;

if (argc == 2)
/* 查找串口设备 */
serial_1 = rt_device_find(WK1_UART_NAME);
serial_2 = rt_device_find(WK2_UART_NAME);
if (!serial_1)
{
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
rt_kprintf("find %s failed!\n", WK1_UART_NAME);
return RT_ERROR;
}
else
if (!serial_2)
{
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
}

/* 查找串口设备 */
serial = rt_device_find(uart_name);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
rt_kprintf("find %s failed!\n", WK2_UART_NAME);
return RT_ERROR;
}

/* 初始化信号量 */
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
/* 以读写及中断接收方式打开串口设备 */
rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
/* 修改串口1配置 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_4800;
config.rx_bufsz = 1024;
config.tx_bufsz = 0; //必须重新配置为0:阻塞发送,非阻塞接收
rt_device_control(serial_1, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_control(serial_2, RT_DEVICE_CTRL_CONFIG, &config);
/* 设置接收回调函数 */
rt_device_set_rx_indicate(serial, uart_input);
rt_device_set_rx_indicate(serial_2, uart_input);
/* 打开串口 */
rt_device_open(serial_1, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
rt_device_open(serial_2, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
/* 发送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
rt_device_write(serial_1, 0, str, (sizeof(str) - 1));

while (1) {
while (1)
{
/* 阻塞等待串口接收中断 */
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
if (rt_device_read(serial, -1, &ch, 1)) {
rt_kprintf("%s: %c\n", uart_name, ch);
/* 发送已接收的数据 */
rt_device_write(serial, 0, &ch, 1);
ret = rt_sem_take(&rx_sem, rt_tick_from_millisecond(50));
if (ret == RT_EOK)
{
while (rt_device_read(serial_2, -1, &ch, 1))
{
rt_kprintf("%s: %c\n", WK2_UART_NAME, ch);
}
}
else
{
break;
}
}
/* 关闭串口 */
rt_device_close(serial_1);
rt_device_close(serial_2);
/* 脱离信号量 */
rt_sem_detach(&rx_sem);

return ret;
}

/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(uart_int_sample, uart device interrupt sample);
MSH_CMD_EXPORT(uart_int_sample, uart device interrupt sample);
72 changes: 37 additions & 35 deletions sample/uart_sample.c
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
#include <rtthread.h>
#include <rtdevice.h>

#define SAMPLE_UART_NAME "uartswk2" /* 串口设备名称 */
/* 将子串口1和子串口2连接测试 */
#define WK1_UART_NAME "uartswk1" /* 串口设备名称 */
#define WK2_UART_NAME "uartswk2" /* 串口设备名称 */

/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;
static rt_device_t serial_1, serial_2;

/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
rt_sem_release(&rx_sem);
return RT_EOK;
}


static int uart_sample(int argc, char *argv[])
static int uart_sample(void)
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
char str[] = "hello RT-Thread!\r\n";
char ch;

if (argc == 2)
{
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
}
else
/* 查找串口设备 */
serial_1 = rt_device_find(WK1_UART_NAME);
serial_2 = rt_device_find(WK2_UART_NAME);
if (!serial_1)
{
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
rt_kprintf("find %s failed!\n", WK1_UART_NAME);
return RT_ERROR;
}

/* 查找串口设备 */
serial = rt_device_find(uart_name);
if (!serial)
if (!serial_2)
{
rt_kprintf("find %s failed!\n", uart_name);
rt_kprintf("find %s failed!\n", WK2_UART_NAME);
return RT_ERROR;
}

/* 以读写及中断接收方式打开串口设备 */
rt_device_open(serial, RT_DEVICE_OFLAG_RDWR);
/* 修改串口配置 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_4800;
config.rx_bufsz = 1024;
config.tx_bufsz = 0; //必须重新配置为0:阻塞发送,非阻塞接收
rt_device_control(serial_1, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_control(serial_2, RT_DEVICE_CTRL_CONFIG, &config);
/* 打开串口 */
rt_device_open(serial_1, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
rt_device_open(serial_2, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
/* 发送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
rt_device_write(serial_1, 0, str, (sizeof(str) - 1));

while (1) {
while (1)
{
rt_thread_mdelay(100);
if (rt_device_read(serial, -1, &ch, 1)) {
rt_kprintf("%s: %c\n", uart_name, ch);
/* 发送已接收的数据 */
rt_device_write(serial, 0, &ch, 1);
if (rt_device_read(serial_2, -1, &ch, 1))
{
rt_kprintf("%s: %c\n", WK2_UART_NAME, ch);
}
else
{
break;
}
}
/* 关闭串口 */
rt_device_close(serial_1);
rt_device_close(serial_2);

return ret;
}
Expand Down
5 changes: 4 additions & 1 deletion wk2124_usart.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include <drv_log.h>
// #define DRV_DEBUG


#ifdef RT_USING_SERIAL
#ifdef RT_USING_SERIAL_V1
#ifdef PKG_USING_WK2124

//WK2124晶振频率,单位Hz
Expand Down Expand Up @@ -506,3 +507,5 @@ int wk2124_device_init(void)
INIT_ENV_EXPORT(wk2124_device_init);

#endif /* PKG_USING_WK2124 */
#endif /* RT_USING_SERIAL_V2 */
#endif /* RT_USING_SERIAL */
Loading

0 comments on commit 65171fd

Please sign in to comment.