Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
天中雨水
rt-thread
提交
1c522be5
R
rt-thread
项目概览
天中雨水
/
rt-thread
该项目与 Fork 源项目分叉
Fork自
RT-Thread / rt-thread
通知
2
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1c522be5
编写于
2月 07, 2013
作者:
P
prife
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rewrite serial/uart code in simulator to support multi-uarts
上级
dc089a64
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
87 addition
and
80 deletion
+87
-80
bsp/simulator/drivers/board.c
bsp/simulator/drivers/board.c
+3
-3
bsp/simulator/drivers/serial.c
bsp/simulator/drivers/serial.c
+57
-16
bsp/simulator/drivers/serial.h
bsp/simulator/drivers/serial.h
+11
-1
bsp/simulator/drivers/usart_sim.c
bsp/simulator/drivers/usart_sim.c
+16
-60
未找到文件。
bsp/simulator/drivers/board.c
浏览文件 @
1c522be5
...
...
@@ -62,8 +62,6 @@ _CRTIMP void __cdecl abort(void);
#endif
#endif
#if defined(RT_USING_FINSH)
#include <finsh.h>
void
rt_hw_exit
(
void
)
{
rt_kprintf
(
"RT-Thread, bye
\n
"
);
...
...
@@ -81,6 +79,9 @@ void rt_hw_exit(void)
#endif
exit
(
0
);
}
#if defined(RT_USING_FINSH)
#include <finsh.h>
FINSH_FUNCTION_EXPORT_ALIAS
(
rt_hw_exit
,
exit
,
exit
rt
-
thread
);
#endif
/* RT_USING_FINSH */
...
...
@@ -97,7 +98,6 @@ void rt_hw_board_init()
//#endif
#if defined(RT_USING_CONSOLE)
rt_hw_serial_init
();
rt_console_set_device
(
RT_CONSOLE_DEVICE_NAME
);
#endif
}
...
...
bsp/simulator/drivers/serial.c
浏览文件 @
1c522be5
...
...
@@ -7,20 +7,59 @@
*/
#include <rthw.h>
#include <rtthread.h>
#define _DEBUG_SERIAL 0
#include "serial.h"
#include <stdio.h>
struct
rt_device
serial_device
;
//extern struct serial_int_rx serial_rx;
struct
serial_int_rx
serial_rx
;
#if 0
static FILE *fp = RT_NULL;
#endif
/*@{*/
int
seial_save_byte
(
unsigned
char
ch
,
struct
serial_device
*
serial
)
{
/* save on rx buffer */
rt_base_t
level
;
struct
rt_device
*
dev
=
RT_DEVICE
(
serial
);
/* disable interrupt */
//暂时关闭中断,因为要操作uart数据结构
level
=
rt_hw_interrupt_disable
();
/* save character */
serial
->
serial_rx
.
rx_buffer
[
serial
->
serial_rx
.
save_index
]
=
ch
;
serial
->
serial_rx
.
save_index
++
;
//下面的代码检查save_index是否已经到到缓冲区尾部,如果是则回转到头部,称为一个环形缓冲区
if
(
serial
->
serial_rx
.
save_index
>=
SERIAL_RX_BUFFER_SIZE
)
serial
->
serial_rx
.
save_index
=
0
;
//这种情况表示反转后的save_index追上了read_index,则增大read_index,丢弃一个旧的数据
/* if the next position is read index, discard this 'read char' */
if
(
serial
->
serial_rx
.
save_index
==
serial
->
serial_rx
.
read_index
)
{
serial
->
serial_rx
.
read_index
++
;
if
(
serial
->
serial_rx
.
read_index
>=
SERIAL_RX_BUFFER_SIZE
)
serial
->
serial_rx
.
read_index
=
0
;
}
/* enable interrupt */
//uart数据结构已经操作完成,重新使能中断
rt_hw_interrupt_enable
(
level
);
/* invoke callback */
if
(
dev
->
rx_indicate
!=
RT_NULL
)
{
rt_size_t
rx_length
;
/* get rx length */
rx_length
=
serial
->
serial_rx
.
read_index
>
serial
->
serial_rx
.
save_index
?
SERIAL_RX_BUFFER_SIZE
-
serial
->
serial_rx
.
read_index
+
serial
->
serial_rx
.
save_index
:
serial
->
serial_rx
.
save_index
-
serial
->
serial_rx
.
read_index
;
dev
->
rx_indicate
(
dev
,
rx_length
);
}
return
0
;
}
/* RT-Thread Device Interface */
/**
...
...
@@ -28,14 +67,15 @@ static FILE *fp = RT_NULL;
*/
static
rt_err_t
rt_serial_init
(
rt_device_t
dev
)
{
struct
serial_device
*
serial
=
SERIAL_DEVICE
(
dev
);
if
(
!
(
dev
->
flag
&
RT_DEVICE_FLAG_ACTIVATED
))
{
if
(
dev
->
flag
&
RT_DEVICE_FLAG_INT_RX
)
{
rt_memset
(
serial_rx
.
rx_buffer
,
0
,
sizeof
(
serial_rx
.
rx_buffer
));
serial_rx
.
read_index
=
0
;
serial_rx
.
save_index
=
0
;
rt_memset
(
serial
->
serial
_rx
.
rx_buffer
,
0
,
sizeof
(
serial
->
serial
_rx
.
rx_buffer
));
serial
->
serial
_rx
.
read_index
=
0
;
serial
->
serial
_rx
.
save_index
=
0
;
}
dev
->
flag
|=
RT_DEVICE_FLAG_ACTIVATED
;
...
...
@@ -62,6 +102,7 @@ static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_
{
rt_uint8_t
*
ptr
;
rt_err_t
err_code
;
struct
serial_device
*
serial
=
SERIAL_DEVICE
(
dev
);
ptr
=
buffer
;
err_code
=
RT_EOK
;
...
...
@@ -76,16 +117,16 @@ static rt_size_t rt_serial_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_
/* disable interrupt */
level
=
rt_hw_interrupt_disable
();
if
(
serial
_rx
.
read_index
!=
serial_rx
.
save_index
)
if
(
serial
->
serial_rx
.
read_index
!=
serial
->
serial_rx
.
save_index
)
{
/* read a character */
*
ptr
++
=
serial
_rx
.
rx_buffer
[
serial_rx
.
read_index
];
*
ptr
++
=
serial
->
serial_rx
.
rx_buffer
[
serial
->
serial_rx
.
read_index
];
size
--
;
/* move to next position */
serial_rx
.
read_index
++
;
if
(
serial_rx
.
read_index
>=
SERIAL_RX_BUFFER_SIZE
)
serial_rx
.
read_index
=
0
;
serial
->
serial
_rx
.
read_index
++
;
if
(
serial
->
serial
_rx
.
read_index
>=
SERIAL_RX_BUFFER_SIZE
)
serial
->
serial
_rx
.
read_index
=
0
;
}
else
{
...
...
@@ -149,7 +190,7 @@ static rt_err_t rt_serial_control(rt_device_t dev, rt_uint8_t cmd, void *args)
/*
* serial register
*/
static
rt_err_t
rt_hw_serial_register
(
rt_device_t
device
,
const
char
*
name
,
rt_uint32_t
flag
)
rt_err_t
rt_hw_serial_register
(
rt_device_t
device
,
const
char
*
name
,
rt_uint32_t
flag
)
{
RT_ASSERT
(
device
!=
RT_NULL
);
#if _DEBUG_SERIAL==1
...
...
@@ -170,8 +211,8 @@ static rt_err_t rt_hw_serial_register(rt_device_t device, const char *name, rt_u
return
rt_device_register
(
device
,
name
,
(
rt_uint16_t
)(
RT_DEVICE_FLAG_RDWR
|
flag
));
}
rt_err_t
rt_hw_serial_init
(
void
)
rt_err_t
rt_hw_serial_init
(
struct
serial_device
*
serial
,
char
*
name
)
{
return
rt_hw_serial_register
(
&
serial_device
,
RT_CONSOLE_DEVICE_NAME
,
return
rt_hw_serial_register
(
RT_DEVICE
(
serial
),
name
,
RT_DEVICE_FLAG_RDWR
|
RT_DEVICE_FLAG_INT_RX
|
RT_DEVICE_FLAG_STREAM
);
}
bsp/simulator/drivers/serial.h
浏览文件 @
1c522be5
...
...
@@ -11,6 +11,7 @@
#ifndef __RT_HW_SERIAL_H__
#define __RT_HW_SERIAL_H__
#include <rtdevice.h>
#define SERIAL_RX_BUFFER_SIZE 80
struct
serial_int_rx
{
...
...
@@ -18,5 +19,14 @@ struct serial_int_rx
rt_uint32_t
read_index
,
save_index
;
};
rt_err_t
rt_hw_serial_init
(
void
);
struct
serial_device
{
struct
rt_device
dev
;
struct
serial_int_rx
serial_rx
;
};
#define SERIAL_DEVICE(dev) ((struct serial_device *)(dev))
int
seial_save_byte
(
unsigned
char
ch
,
struct
serial_device
*
serial
);
rt_err_t
rt_hw_serial_init
(
struct
serial_device
*
serial
,
char
*
name
);
#endif
bsp/simulator/drivers/usart_sim.c
浏览文件 @
1c522be5
...
...
@@ -10,9 +10,9 @@
#include <stdio.h>
#include "serial.h"
struct
serial_int_rx
serial_rx
;
extern
struct
rt_device
serial_device
;
struct
serial_device
serial1
;
#define SAVEKEY(key) seial_save_byte(key, &serial1)
#ifdef _WIN32
/*
* Handler for OSKey Thread
...
...
@@ -23,6 +23,7 @@ static DWORD OSKey_ThreadID;
static
DWORD
WINAPI
ThreadforKeyGet
(
LPVOID
lpParam
);
void
rt_hw_usart_init
(
void
)
{
rt_hw_serial_init
(
&
serial1
,
RT_CONSOLE_DEVICE_NAME
);
/*
* create serial thread that receive key input from keyboard
*/
...
...
@@ -60,7 +61,6 @@ void rt_hw_usart_init(void)
#include <termios.h>
/* for tcxxxattr, ECHO, etc */
#include <unistd.h>
/* for STDIN_FILENO */
static
void
*
ThreadforKeyGet
(
void
*
lpParam
);
static
pthread_t
OSKey_Thread
;
void
rt_hw_usart_init
(
void
)
...
...
@@ -74,57 +74,7 @@ void rt_hw_usart_init(void)
}
}
#endif
/*
* 方向键(←): 0xe04b
* 方向键(↑): 0xe048
* 方向键(→): 0xe04d
* 方向键(↓): 0xe050
*/
static
int
savekey
(
unsigned
char
key
)
{
/* save on rx buffer */
{
rt_base_t
level
;
/* disable interrupt */
//暂时关闭中断,因为要操作uart数据结构
level
=
rt_hw_interrupt_disable
();
/* save character */
serial_rx
.
rx_buffer
[
serial_rx
.
save_index
]
=
key
;
serial_rx
.
save_index
++
;
//下面的代码检查save_index是否已经到到缓冲区尾部,如果是则回转到头部,称为一个环形缓冲区
if
(
serial_rx
.
save_index
>=
SERIAL_RX_BUFFER_SIZE
)
serial_rx
.
save_index
=
0
;
//这种情况表示反转后的save_index追上了read_index,则增大read_index,丢弃一个旧的数据
/* if the next position is read index, discard this 'read char' */
if
(
serial_rx
.
save_index
==
serial_rx
.
read_index
)
{
serial_rx
.
read_index
++
;
if
(
serial_rx
.
read_index
>=
SERIAL_RX_BUFFER_SIZE
)
serial_rx
.
read_index
=
0
;
}
/* enable interrupt */
//uart数据结构已经操作完成,重新使能中断
rt_hw_interrupt_enable
(
level
);
}
/* invoke callback */
if
(
serial_device
.
rx_indicate
!=
RT_NULL
)
{
rt_size_t
rx_length
;
/* get rx length */
rx_length
=
serial_rx
.
read_index
>
serial_rx
.
save_index
?
SERIAL_RX_BUFFER_SIZE
-
serial_rx
.
read_index
+
serial_rx
.
save_index
:
serial_rx
.
save_index
-
serial_rx
.
read_index
;
serial_device
.
rx_indicate
(
&
serial_device
,
rx_length
);
}
return
0
;
}
#ifdef _WIN32
static
DWORD
WINAPI
ThreadforKeyGet
(
LPVOID
lpParam
)
#else
...
...
@@ -153,6 +103,12 @@ void restore_stty(void)
static
void
*
ThreadforKeyGet
(
void
*
lpParam
)
#endif
/* not _WIN32*/
{
/*
* 方向键(←): 0xe04b
* 方向键(↑): 0xe048
* 方向键(→): 0xe04d
* 方向键(↓): 0xe050
*/
unsigned
char
key
;
#ifndef _WIN32
...
...
@@ -173,20 +129,20 @@ static void * ThreadforKeyGet(void * lpParam)
if
(
key
==
0x48
)
//up key , 0x1b 0x5b 0x41
{
savekey
(
0x1b
);
savekey
(
0x5b
);
savekey
(
0x41
);
SAVEKEY
(
0x1b
);
SAVEKEY
(
0x5b
);
SAVEKEY
(
0x41
);
}
else
if
(
key
==
0x50
)
//0x1b 0x5b 0x42
{
savekey
(
0x1b
);
savekey
(
0x5b
);
savekey
(
0x42
);
SAVEKEY
(
0x1b
);
SAVEKEY
(
0x5b
);
SAVEKEY
(
0x42
);
}
continue
;
}
#endif
savekey
(
key
);
SAVEKEY
(
key
);
}
}
/*** ThreadforKeyGet ***/
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录