-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathlist_xzy.c
237 lines (193 loc) · 6.56 KB
/
list_xzy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
*循环链表
*/
#include "list_xzy.h"
#include "string.h"
#if 1
/*
*名称:链表初始化
*/
ENUM_LIST_XZY_ERR list_xzy_init(volatile STRUCT_LIST_XZY *p_info,unsigned char *buff,unsigned int size_buff,unsigned int unit_size,ENUM_LIST_XZY_RW_STEP flag_cover)
{
if(p_info==NULL || buff==NULL)return enum_list_xzy_err_param_NULL;
if(unit_size < 1)return enum_list_xzy_err_param_wrong;
if(size_buff < unit_size)return enum_list_xzy_err_param_small;
p_info->flag_on_off = enum_rw_step_idle;
p_info->r.flag_on_off = enum_rw_step_idle;
p_info->w.flag_on_off = enum_rw_step_idle;
p_info->cache_address = buff;
p_info->cache_size = size_buff;
p_info->cache_size_value = size_buff / unit_size * unit_size;
p_info->unit_size = unit_size;
p_info->flag_cover = flag_cover;
p_info->flag_on_off = enum_rw_step_work;
return enum_list_xzy_err_ok;
}
/*
*链表开始
*/
ENUM_LIST_XZY_ERR list_xzy_start(volatile STRUCT_LIST_XZY *p_info)
{
if(p_info==NULL)return enum_list_xzy_err_param_NULL;
if(p_info->flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
p_info->flag_on_off = enum_rw_step_idle;
p_info->r.flag_on_off = enum_rw_step_idle;
p_info->w.flag_on_off = enum_rw_step_idle;
p_info->count_w_cover = 0;
p_info->r.index = 0;
p_info->r.step = enum_rw_step_idle;
p_info->r.cir_times = 0;
p_info->w.index = 0;
p_info->w.step = enum_rw_step_idle;
p_info->w.cir_times = 0;
p_info->r.flag_on_off = enum_rw_step_work;
p_info->w.flag_on_off = enum_rw_step_work;
p_info->flag_on_off = enum_rw_step_work;
return enum_list_xzy_err_ok;
}
/*
*链表结束
*/
ENUM_LIST_XZY_ERR list_xzy_end(volatile STRUCT_LIST_XZY *p_info)
{
if(p_info==NULL)return enum_list_xzy_err_param_NULL;
if(p_info->flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
p_info->flag_on_off = enum_rw_step_idle;
p_info->r.flag_on_off = enum_rw_step_idle;
p_info->w.flag_on_off = enum_rw_step_idle;
return enum_list_xzy_err_ok;
}
/*
链表读
*/
ENUM_LIST_XZY_ERR list_xzy_read(volatile STRUCT_LIST_XZY *p_info,unsigned char *p_data,unsigned int size_data,ENUM_LIST_XZY_RW_STEP flag_updata)
{
STRUCT_LIST_XZY tmp_info = {0};
STRUCT_LIST_XZY tmp_info_1 = {0};
if(p_info==NULL)return enum_list_xzy_err_param_NULL;
if(p_info->flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
if(p_data == NULL)return enum_list_xzy_err_param_NULL;
if(size_data < p_info->unit_size)return enum_list_xzy_err_param_small;
if(p_info->r.flag_on_off != enum_rw_step_work)return enum_list_xzy_err_read_off;
if(p_info->r.step == enum_rw_step_work)return enum_list_xzy_err_read_busy;
tmp_info = *p_info;
p_info->r.step = enum_rw_step_work;
if(p_info->flag_cover == enum_rw_step_work)
{
if(p_info->r.cir_times == p_info->w.cir_times)
;
else if((p_info->r.cir_times+1) == p_info->w.cir_times && \
p_info->r.index >= p_info->w.index
)
;
else
{
p_info->r.cir_times = p_info->w.cir_times - 1;
p_info->r.index = p_info->w.index;
}
}
if(p_info->r.cir_times == p_info->w.cir_times && \
p_info->r.index == p_info->w.index
)
return enum_list_xzy_err_idle;
memcpy(p_data,p_info->cache_address + p_info->r.index,p_info->unit_size);
tmp_info_1 = *p_info;
if(p_info->flag_cover == enum_rw_step_work)
{
if(tmp_info.w.cir_times == tmp_info_1.w.cir_times && \
tmp_info.w.index == tmp_info_1.w.index \
)
;
if(p_info->r.index == tmp_info_1.w.index && \
tmp_info_1.w.step == enum_rw_step_work)
{
return enum_list_xzy_err_recover;
}
else if(p_info->r.cir_times == tmp_info.w.cir_times && \
p_info->r.index < tmp_info.w.index && \
p_info->r.cir_times == tmp_info_1.w.cir_times && \
p_info->r.index < tmp_info_1.w.index
)
;
else if(p_info->r.cir_times == tmp_info.w.cir_times && \
p_info->r.index < tmp_info.w.index && \
(p_info->r.cir_times+1) == tmp_info_1.w.cir_times && \
p_info->r.index >= tmp_info_1.w.index
)
;
else if((p_info->r.cir_times+1) == tmp_info.w.cir_times && \
p_info->r.index >= tmp_info.w.index && \
(p_info->r.cir_times+1) == tmp_info_1.w.cir_times && \
p_info->r.index >= tmp_info_1.w.index
)
;
else
{
return enum_list_xzy_err_recover;
}
}
if(flag_updata == enum_rw_step_work)
{
p_info->r.index += p_info->unit_size;
p_info->r.cir_times += p_info->r.index / p_info->cache_size_value;
p_info->r.index %= p_info->cache_size_value;
}
return enum_list_xzy_err_ok;
}
/*
链表缓存数据的可读次数
*/
ENUM_LIST_XZY_ERR list_xzy_get_num(volatile STRUCT_LIST_XZY *p_info,unsigned int *item_num)
{
STRUCT_LIST_XZY tmp_info = {0};
if(p_info==NULL)return enum_list_xzy_err_param_NULL;
if(p_info->flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
if(p_info->r.flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
if(p_info->w.flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
if(item_num==NULL)return enum_list_xzy_err_param_NULL;
tmp_info = *p_info;
if(tmp_info.r.cir_times == tmp_info.w.cir_times)
{
if(tmp_info.r.index == tmp_info.w.index)
*item_num = 0;
else
*item_num = (tmp_info.w.index - tmp_info.r.index) / tmp_info.unit_size;
}
else if((tmp_info.r.cir_times+1) == tmp_info.w.cir_times)
{
if(tmp_info.r.index >= p_info->w.index)
*item_num = (tmp_info.cache_size_value - (tmp_info.r.index - p_info->w.index)) / tmp_info.unit_size;
else
*item_num = tmp_info.cache_size_value / tmp_info.unit_size;
}
else
*item_num = tmp_info.cache_size_value / tmp_info.unit_size;
return enum_list_xzy_err_ok;
}
/*
链表写
*/
ENUM_LIST_XZY_ERR list_xzy_write(volatile STRUCT_LIST_XZY *p_info,unsigned char *p_data,unsigned int write_bytes)
{
if(p_info==NULL)return enum_list_xzy_err_param_NULL;
if(write_bytes > p_info->unit_size)return enum_list_xzy_err_param_wrong;
if(p_info->flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
if(p_info->w.flag_on_off != enum_rw_step_work)return enum_list_xzy_err_param_wrong;
if(p_info->w.step != enum_rw_step_work)return enum_list_xzy_err_write_busy;
p_info->w.step = enum_rw_step_work;
if(p_info->flag_cover == enum_rw_step_idle)
{
if((p_info->r.cir_times + 1) == p_info->w.cir_times && \
p_info->w.index == p_info->r.index \
)
return enum_list_xzy_err_recover;
}
memset(p_info->cache_address + p_info->w.index,0,p_info->unit_size);
memcpy(p_info->cache_address + p_info->w.index,p_data,write_bytes);
p_info->w.index += p_info->unit_size;
p_info->w.cir_times += p_info->w.index / p_info->cache_size_value;
p_info->w.index %= p_info->cache_size_value;
p_info->w.step = enum_rw_step_idle;
return enum_list_xzy_err_ok;
}
#endif