Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
rt-thread
提交
4919d29d
R
rt-thread
项目概览
BaiXuePrincess
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
4919d29d
编写于
8月 19, 2013
作者:
G
Grissiom
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ringbuffer: add put_force and putchar_force API
Add the APIs that will discard the old data when rb is full.
上级
981d929b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
124 addition
and
7 deletion
+124
-7
components/drivers/include/rtdevice.h
components/drivers/include/rtdevice.h
+31
-7
components/drivers/src/ringbuffer.c
components/drivers/src/ringbuffer.c
+93
-0
未找到文件。
components/drivers/include/rtdevice.h
浏览文件 @
4919d29d
...
...
@@ -134,37 +134,61 @@ void rt_ringbuffer_init(struct rt_ringbuffer *rb,
rt_size_t
rt_ringbuffer_put
(
struct
rt_ringbuffer
*
rb
,
const
rt_uint8_t
*
ptr
,
rt_uint16_t
length
);
rt_size_t
rt_ringbuffer_put_force
(
struct
rt_ringbuffer
*
rb
,
const
rt_uint8_t
*
ptr
,
rt_uint16_t
length
);
rt_size_t
rt_ringbuffer_putchar
(
struct
rt_ringbuffer
*
rb
,
const
rt_uint8_t
ch
);
rt_size_t
rt_ringbuffer_putchar_force
(
struct
rt_ringbuffer
*
rb
,
const
rt_uint8_t
ch
);
rt_size_t
rt_ringbuffer_get
(
struct
rt_ringbuffer
*
rb
,
rt_uint8_t
*
ptr
,
rt_uint16_t
length
);
rt_size_t
rt_ringbuffer_getchar
(
struct
rt_ringbuffer
*
rb
,
rt_uint8_t
*
ch
);
enum
rt_ringbuffer_state
{
RT_RINGBUFFER_EMPTY
,
RT_RINGBUFFER_FULL
,
/* half full is neither full nor empty */
RT_RINGBUFFER_HALFFULL
,
};
rt_inline
rt_uint16_t
rt_ringbuffer_get_size
(
struct
rt_ringbuffer
*
rb
)
{
RT_ASSERT
(
rb
!=
RT_NULL
);
return
rb
->
buffer_size
;
}
/** return the size of data in rb */
rt_
inline
rt_uint16_t
rt_ringbuffer_data_len
(
struct
rt_ringbuffer
*
rb
)
rt_inline
enum
rt_ringbuffer_state
rt_
ringbuffer_status
(
struct
rt_ringbuffer
*
rb
)
{
if
(
rb
->
read_index
==
rb
->
write_index
)
{
if
(
rb
->
read_mirror
==
rb
->
write_mirror
)
/* we are in the same side, the ringbuffer is empty. */
return
0
;
return
RT_RINGBUFFER_EMPTY
;
else
return
rb
->
buffer_size
;
return
RT_RINGBUFFER_FULL
;
}
else
return
RT_RINGBUFFER_HALFFULL
;
}
/** return the size of data in rb */
rt_inline
rt_uint16_t
rt_ringbuffer_data_len
(
struct
rt_ringbuffer
*
rb
)
{
switch
(
rt_ringbuffer_status
(
rb
))
{
case
RT_RINGBUFFER_EMPTY
:
return
0
;
case
RT_RINGBUFFER_FULL
:
return
rb
->
buffer_size
;
case
RT_RINGBUFFER_HALFFULL
:
default:
if
(
rb
->
write_index
>
rb
->
read_index
)
return
rb
->
write_index
-
rb
->
read_index
;
else
return
rb
->
buffer_size
-
(
rb
->
read_index
-
rb
->
write_index
);
}
}
;
}
/** return the size of empty space in rb */
...
...
components/drivers/src/ringbuffer.c
浏览文件 @
4919d29d
...
...
@@ -44,6 +44,9 @@ void rt_ringbuffer_init(struct rt_ringbuffer *rb,
}
RTM_EXPORT
(
rt_ringbuffer_init
);
/**
* put a block of data into ring buffer
*/
rt_size_t
rt_ringbuffer_put
(
struct
rt_ringbuffer
*
rb
,
const
rt_uint8_t
*
ptr
,
rt_uint16_t
length
)
...
...
@@ -88,6 +91,59 @@ rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
}
RTM_EXPORT
(
rt_ringbuffer_put
);
/**
* put a block of data into ring buffer
*
* When the buffer is full, it will discard the old data.
*/
rt_size_t
rt_ringbuffer_put_force
(
struct
rt_ringbuffer
*
rb
,
const
rt_uint8_t
*
ptr
,
rt_uint16_t
length
)
{
enum
rt_ringbuffer_state
old_state
;
RT_ASSERT
(
rb
!=
RT_NULL
);
old_state
=
rt_ringbuffer_status
(
rb
);
if
(
length
>
rb
->
buffer_size
)
length
=
rb
->
buffer_size
;
if
(
rb
->
buffer_size
-
rb
->
write_index
>
length
)
{
/* read_index - write_index = empty space */
memcpy
(
&
rb
->
buffer_ptr
[
rb
->
write_index
],
ptr
,
length
);
/* this should not cause overflow because there is enough space for
* length of data in current mirror */
rb
->
write_index
+=
length
;
if
(
old_state
==
RT_RINGBUFFER_FULL
)
rb
->
read_index
=
rb
->
write_index
;
return
length
;
}
memcpy
(
&
rb
->
buffer_ptr
[
rb
->
write_index
],
&
ptr
[
0
],
rb
->
buffer_size
-
rb
->
write_index
);
memcpy
(
&
rb
->
buffer_ptr
[
0
],
&
ptr
[
rb
->
buffer_size
-
rb
->
write_index
],
length
-
(
rb
->
buffer_size
-
rb
->
write_index
));
/* we are going into the other side of the mirror */
rb
->
write_mirror
=
~
rb
->
write_mirror
;
rb
->
write_index
=
length
-
(
rb
->
buffer_size
-
rb
->
write_index
);
if
(
old_state
==
RT_RINGBUFFER_FULL
)
{
rb
->
read_mirror
=
~
rb
->
read_mirror
;
rb
->
read_index
=
rb
->
write_index
;
}
return
length
;
}
RTM_EXPORT
(
rt_ringbuffer_put_force
);
/**
* get data from ring buffer
*/
...
...
@@ -163,6 +219,43 @@ rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
}
RTM_EXPORT
(
rt_ringbuffer_putchar
);
/**
* put a character into ring buffer
*
* When the buffer is full, it will discard one old data.
*/
rt_size_t
rt_ringbuffer_putchar_force
(
struct
rt_ringbuffer
*
rb
,
const
rt_uint8_t
ch
)
{
enum
rt_ringbuffer_state
old_state
;
RT_ASSERT
(
rb
!=
RT_NULL
);
old_state
=
rt_ringbuffer_status
(
rb
);
rb
->
buffer_ptr
[
rb
->
write_index
]
=
ch
;
/* flip mirror */
if
(
rb
->
write_index
==
rb
->
buffer_size
-
1
)
{
rb
->
write_mirror
=
~
rb
->
write_mirror
;
rb
->
write_index
=
0
;
if
(
old_state
==
RT_RINGBUFFER_FULL
)
{
rb
->
read_mirror
=
~
rb
->
read_mirror
;
rb
->
read_index
=
rb
->
write_index
;
}
}
else
{
rb
->
write_index
++
;
if
(
old_state
==
RT_RINGBUFFER_FULL
)
rb
->
read_index
=
rb
->
write_index
;
}
return
1
;
}
RTM_EXPORT
(
rt_ringbuffer_putchar_force
);
/**
* get a character from a ringbuffer
*/
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录