WS63 SDK 文档 7021f4f@fbb_ws63
ws63 和 ws63e 解决方案的 SDK 文档
载入中...
搜索中...
未找到
circ_buf.h
浏览该文件的文档.
1/*
2 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2019-2021. All rights reserved.
3 * Description: audio circle buffer function
4 */
5
6#ifndef __CIRC_BUF_H__
7#define __CIRC_BUF_H__
8
9#include "securec.h"
10#include "td_type.h"
11
12#ifdef __cplusplus
13#if __cplusplus
14extern "C" {
15#endif
16#endif /* __cplusplus */
17
18typedef struct {
19 volatile td_u32 *write; /* circle buffer write pointer */
20 volatile td_u32 *read; /* circle buffer read pointer */
21 td_u8 *data; /* circle buffer data virtual address */
22 td_u32 size; /* circle buffer size */
23} circ_buf;
24
25static inline td_u32 circ_buf_min(td_u32 x, td_u32 y)
26{
27 return (x <= y) ? x : y;
28}
29
30static inline td_u32 saturate_add(td_u32 x, td_u32 y, td_u32 saturation)
31{
32 /* add x and y, saturate to saturation */
33 td_u32 x_temp = x + y;
34
35 return (x_temp >= saturation) ? (x_temp - saturation) : x_temp;
36}
37
38static inline td_void circ_buf_init(circ_buf *cb, td_u32 *write, td_u32 *read, td_u8 *data, td_u32 len)
39{
40 cb->write = write;
41 cb->read = read;
42 cb->data = data;
43 cb->size = len;
44
45 *cb->write = 0;
46 *cb->read = 0;
47}
48
49static inline td_void circ_buf_flush(circ_buf *cb)
50{
51 *cb->read = 0;
52 *cb->write = 0;
53}
54
55static inline td_u32 circ_buf_data_size(td_u32 write, td_u32 read, td_u32 size)
56{
57 return (write >= read) ? (write - read) : (size - read + write);
58}
59
60static inline td_u32 circ_buf_free_size(td_u32 write, td_u32 read, td_u32 size)
61{
62 return size - circ_buf_data_size(write, read, size);
63}
64
65static inline td_u32 cb_read_data(const circ_buf *cb, td_u8 *to, td_u32 len, td_u32 off)
66{
67 if ((len == 0) || (memcpy_s(to, len * sizeof(td_u8), cb->data + off, len) != EOK)) {
68 return 0;
69 }
70
71 return len;
72}
73
74/* read data from circle buffer */
75static inline td_u32 circ_buf_read_data(const circ_buf *cb, td_u8 *to, td_u32 len, td_u32 off)
76{
77 td_u32 l = circ_buf_min(len, cb->size - off);
78
79 if (to == TD_NULL) {
80 /* dump data in circle buffer */
81 return len;
82 }
83
84 return cb_read_data(cb, to, l, off) + cb_read_data(cb, to + l, len - l, 0);
85}
86
87static inline td_u32 cb_write_zero(circ_buf *cb, const td_u8 *from, td_u32 len, td_u32 off)
88{
89 if ((len == 0) || (memset_s(cb->data + off, len, 0, len) != EOK)) {
90 return 0;
91 }
92 (td_void)from;
93 return len;
94}
95
96static inline td_u32 cb_write_data(circ_buf *cb, const td_u8 *from, td_u32 len, td_u32 off)
97{
98 if ((len == 0) || (memcpy_s(cb->data + off, cb->size - off, from, len) != EOK)) {
99 return 0;
100 }
101
102 return len;
103}
104
105/*
106 * circ_buf_write_data - puts some data from kernel space into the circle buffer
107 * @cb: address of the circle buffer to be used
108 * @from: pointer to the data to be added
109 * @len: the length of the data to be added
110 * @off: data write offset in circle buffer
111 *
112 * Note: Check free space in circle buffer before calling this function
113 */
114static inline td_u32 circ_buf_write_data(circ_buf *cb, const td_u8 *from, td_u32 len, td_u32 off)
115{
116 td_u32 l = circ_buf_min(len, cb->size - off);
117
118 /* generate mute data if input buffer is TD_NULL */
119 td_u32 (*action)(circ_buf *, const td_u8 *, td_u32, td_u32) =
120 (from == TD_NULL) ? cb_write_zero : cb_write_data;
121
122 return action(cb, from, l, off) + action(cb, from + l, len - l, 0);
123}
124
125static inline td_u32 circ_buf_query_busy(const circ_buf *cb)
126{
127 return circ_buf_data_size(*cb->write, *cb->read, cb->size);
128}
129
130static inline td_u32 circ_buf_query_free(const circ_buf *cb)
131{
132 return circ_buf_free_size(*cb->write, *cb->read, cb->size);
133}
134
135/* read data from circle buffer */
136static inline td_u32 circ_buf_read(circ_buf *cb, td_u8 *to, td_u32 len)
137{
138 td_u32 read = *cb->read;
139
140 if ((len == 0) || (circ_buf_data_size(*cb->write, read, cb->size) < len) ||
141 (circ_buf_read_data(cb, to, len, read) != len)) {
142 return 0;
143 }
144
145 *cb->read = saturate_add(read, len, cb->size);
146 return len;
147}
148
149/* write data into circle buffer */
150static inline td_u32 circ_buf_write(circ_buf *cb, const td_u8 *from, td_u32 len)
151{
152 td_u32 write = *cb->write;
153
154 if ((len == 0) || (circ_buf_write_data(cb, from, len, write) != len)) {
155 return 0;
156 }
157
158 *cb->write = saturate_add(write, len, cb->size);
159 return len;
160}
161
162static inline td_u32 circ_buf_update_read_pos(circ_buf *cb, td_u32 len)
163{
164 *cb->read = saturate_add(*cb->read, len, cb->size);
165 return len;
166}
167
168static inline td_u32 circ_buf_update_write_pos(circ_buf *cb, td_u32 len)
169{
170 *cb->write = saturate_add(*cb->write, len, cb->size);
171 return len;
172}
173
174static inline td_u32 circ_buf_cast_read(const circ_buf *cb, td_u32 *data_offset, td_u32 len)
175{
176 td_u32 read = *cb->read;
177
178 if (circ_buf_data_size(*cb->write, read, cb->size) < len) {
179 *data_offset = 0;
180 return 0;
181 }
182
183 *data_offset = read;
184 return len;
185}
186
187static inline td_u32 circ_buf_cast_relese(circ_buf *cb, td_u32 len)
188{
189 td_u32 read = *cb->read;
190 td_u32 data_size = circ_buf_data_size(*cb->write, read, cb->size);
191 td_u32 len_temp;
192
193 len_temp = circ_buf_min(data_size, len);
194 *cb->read = saturate_add(read, len_temp, cb->size);
195
196 return len_temp;
197}
198
199/* peak data from circle buffer */
200static inline td_u32 circ_buf_peak(circ_buf *cb, td_u8 *to, td_u32 len)
201{
202 td_u32 read = *cb->read;
203
204 if ((len == 0) || (circ_buf_data_size(*cb->write, read, cb->size) < len) ||
205 (circ_buf_read_data(cb, to, len, read) != len)) {
206 return 0;
207 }
208
209 return len;
210}
211
212/* poke/release data from circle buffer */
213static inline td_void circ_buf_poke(circ_buf *cb, td_u32 len)
214{
215 td_u32 read = *cb->read;
216
217 *cb->read = saturate_add(read, len, cb->size);
218}
219
220#ifdef __cplusplus
221#if __cplusplus
222}
223#endif
224#endif /* __cplusplus */
225
226#endif /* __CIRC_BUF_H__ */
errno_t memset_s(void *dest, size_t destMax, int c, size_t count)
errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count)
#define EOK
Definition securec.h:57
Definition circ_buf.h:18
td_u8 * data
Definition circ_buf.h:21
volatile td_u32 * read
Definition circ_buf.h:20
td_u32 size
Definition circ_buf.h:22
volatile td_u32 * write
Definition circ_buf.h:19
unsigned char td_u8
Definition td_type.h:36
void td_void
Definition td_type.h:49
unsigned int td_u32
Definition td_type.h:38
#define TD_NULL
Definition td_type.h:30
Definition hal_uart_v151_regs_def.h:38