• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 #include <hi_types_base.h>
19 #include <los_memory.h>
20 #include <los_event.h>
21 #include <los_hwi.h>
22 #include <hi_event.h>
23 #include <hi_stdlib.h>
24 
25 #define HI_EVENT_SUPPORT_BITS 0X00FFFFFF /* LITEOS本身限制EVENT不支持25bit,VXWORKS仅支持低24bit。 */
26 #define RSV_MAX_SIZE 3
27 
28 typedef struct {
29     hi_bool use;
30     hi_u8 rsv[RSV_MAX_SIZE];
31     EVENT_CB_S os_event;
32 } hi_event_item;
33 
34 typedef struct {
35     hi_u8 init;          /* 模块是否初始化 */
36     hi_u8 max_event_cnt; /* 模块最多支持多少个event */
37     hi_u8 used_count;
38     hi_u8 pad;
39     hi_event_item *item; /* 指向item数组 */
40 } hi_event_ctrl;
41 
42 hi_event_ctrl g_event_ctrl = {
43     0,
44 };
45 
46 extern hi_u32 ms2systick(HI_IN hi_u32 ms, HI_IN hi_bool include0);
47 
osa_event_get_usage(hi_void)48 hi_u8 osa_event_get_usage(hi_void)
49 {
50     return g_event_ctrl.used_count;
51 }
52 
hi_event_init(hi_u8 max_event_cnt,hi_pvoid event_space)53 hi_u32 hi_event_init(hi_u8 max_event_cnt, hi_pvoid event_space)
54 {
55     if (!g_event_ctrl.init) {
56         if (max_event_cnt == 0) {
57             return HI_ERR_EVENT_INVALID_PARAM;
58         }
59 
60         if (event_space) {
61             g_event_ctrl.item = (hi_event_item *)event_space;
62         } else {
63             g_event_ctrl.item = (hi_event_item *)LOS_MemAlloc(m_aucSysMem0, sizeof(hi_event_item) * max_event_cnt);
64         }
65 
66         if (g_event_ctrl.item == HI_NULL) {
67             return HI_ERR_EVENT_NOT_ENOUGH_MEMORY;
68         }
69 
70         memset_s(g_event_ctrl.item, sizeof(hi_event_item) * max_event_cnt, 0, sizeof(hi_event_item) * max_event_cnt);
71         g_event_ctrl.max_event_cnt = max_event_cnt;
72         g_event_ctrl.init = HI_TRUE;
73 
74         return HI_ERR_SUCCESS;
75     }
76 
77     return HI_ERR_EVENT_RE_INIT;
78 }
79 
hi_event_create(HI_OUT hi_u32 * id)80 hi_u32 hi_event_create(HI_OUT hi_u32 *id)
81 {
82     hi_u32 ret;
83     hi_u32 i = 0;
84     hi_u32 int_save = 0;
85     hi_event_item *item = HI_NULL;
86     hi_u32 temp_event_id = HI_INVALID_EVENT_ID;
87 
88     if (!g_event_ctrl.init) {
89         return HI_ERR_EVENT_NOT_INIT;
90     }
91 
92     if ((HI_NULL == id)) {
93         return HI_ERR_EVENT_INVALID_PARAM;
94     }
95 
96     int_save = LOS_IntLock();
97 
98     for (i = 0; i < g_event_ctrl.max_event_cnt; i++) {
99         item = &g_event_ctrl.item[i];
100 
101         if (HI_FALSE == item->use) {
102             item->use = HI_TRUE;
103             temp_event_id = i;
104             break;
105         }
106     }
107 
108     LOS_IntRestore(int_save);
109 
110     if (temp_event_id == HI_INVALID_EVENT_ID) {
111         return HI_ERR_EVENT_CREATE_NO_HADNLE;
112     }
113 
114     if (item != HI_NULL) {
115         ret = LOS_EventInit(&(item->os_event));
116 
117         if (ret != LOS_OK) {
118             return HI_ERR_EVENT_CREATE_SYS_FAIL;
119         }
120     }
121 
122     *id = temp_event_id;
123     g_event_ctrl.used_count++;
124     return HI_ERR_SUCCESS;
125 }
126 
hi_event_send(hi_u32 id,hi_u32 event_bits)127 hi_u32 hi_event_send(hi_u32 id, hi_u32 event_bits)
128 {
129     hi_u32 ret;
130     hi_event_item *item = HI_NULL;
131 
132     if (hi_unlikely(!g_event_ctrl.init)) {
133         return HI_ERR_EVENT_NOT_INIT;
134     }
135 
136     if (hi_unlikely(id >= g_event_ctrl.max_event_cnt)) {
137         return HI_ERR_EVENT_INVALID_PARAM;
138     }
139 
140     if (hi_unlikely(event_bits & (~HI_EVENT_SUPPORT_BITS))) {
141         return HI_ERR_EVENT_INVALID_PARAM;
142     }
143 
144     item = &g_event_ctrl.item[id];
145 
146     if (hi_unlikely(item->use == HI_FALSE)) {
147         return HI_ERR_EVENT_INVALID_PARAM;
148     }
149 
150     event_bits = (event_bits & HI_EVENT_SUPPORT_BITS);
151     ret = LOS_EventWrite(&(item->os_event), event_bits);
152     if (hi_unlikely(ret != LOS_OK)) {
153         return HI_ERR_EVENT_SEND_FAIL;
154     }
155 
156     return HI_ERR_SUCCESS;
157 }
158 
hi_event_wait(hi_u32 id,hi_u32 mask,HI_OUT hi_u32 * event_bits,hi_u32 timeout,hi_u32 flag)159 hi_u32 hi_event_wait(hi_u32 id, hi_u32 mask, HI_OUT hi_u32 *event_bits, hi_u32 timeout, hi_u32 flag)
160 {
161     hi_event_item *item = HI_NULL;
162     hi_u32 tick = HI_SYS_WAIT_FOREVER;
163     hi_u32 tmp_bits = 0;
164 
165     if (hi_unlikely(!g_event_ctrl.init)) {
166         return HI_ERR_EVENT_NOT_INIT;
167     }
168 
169     if (hi_unlikely((id >= g_event_ctrl.max_event_cnt) || (event_bits == HI_NULL))) {
170         return HI_ERR_EVENT_INVALID_PARAM;
171     }
172 
173     item = &g_event_ctrl.item[id];
174 
175     if (hi_unlikely(item->use == HI_FALSE)) {
176         return HI_ERR_EVENT_INVALID_PARAM;
177     }
178 
179     if (hi_unlikely(mask & (~HI_EVENT_SUPPORT_BITS))) {
180         return HI_ERR_EVENT_INVALID_PARAM;
181     }
182 
183     if (hi_unlikely(HI_SYS_WAIT_FOREVER != timeout)) {
184         tick = ms2systick(timeout, HI_TRUE);
185     }
186 
187     tmp_bits = LOS_EventRead(&(item->os_event), mask, flag, tick);
188 
189     if (hi_unlikely(tmp_bits == LOS_ERRNO_EVENT_READ_TIMEOUT)) {
190         return HI_ERR_EVENT_WAIT_TIME_OUT;
191     }
192 
193     if (hi_unlikely(!tmp_bits || (tmp_bits & LOS_ERRTYPE_ERROR))) {
194         return HI_ERR_EVENT_WAIT_FAIL;
195     } else {
196         *event_bits = tmp_bits;
197         return HI_ERR_SUCCESS;
198     }
199 }
200 
hi_event_clear(hi_u32 id,hi_u32 event_bits)201 hi_u32 hi_event_clear(hi_u32 id, hi_u32 event_bits)
202 {
203     hi_u32 ret;
204     hi_event_item *item = HI_NULL;
205 
206     if (hi_unlikely(!g_event_ctrl.init)) {
207         return HI_ERR_EVENT_NOT_INIT;
208     }
209 
210     if (hi_unlikely(id >= g_event_ctrl.max_event_cnt)) {
211         return HI_ERR_EVENT_INVALID_PARAM;
212     }
213 
214     item = &g_event_ctrl.item[id];
215 
216     if (hi_unlikely(item->use == HI_FALSE)) {
217         return HI_ERR_EVENT_INVALID_PARAM;
218     }
219 
220     if (hi_unlikely(event_bits & (~HI_EVENT_SUPPORT_BITS))) {
221         return HI_ERR_EVENT_INVALID_PARAM;
222     }
223 
224     event_bits = (event_bits & HI_EVENT_SUPPORT_BITS);
225 
226     ret = LOS_EventClear(&(item->os_event), ~event_bits);
227     if (hi_unlikely(ret != LOS_OK)) {
228         return HI_ERR_EVENT_CLEAR_FAIL;
229     }
230 
231     return HI_ERR_SUCCESS;
232 }
233 
hi_event_delete(hi_u32 id)234 hi_u32 hi_event_delete(hi_u32 id)
235 {
236     hi_u32 ret;
237     hi_event_item *item = HI_NULL;
238 
239     if (!g_event_ctrl.init) {
240         return HI_ERR_EVENT_NOT_INIT;
241     }
242 
243     if (id >= g_event_ctrl.max_event_cnt) {
244         return HI_ERR_EVENT_INVALID_PARAM;
245     }
246 
247     item = &g_event_ctrl.item[id];
248 
249     if (item->use == HI_FALSE) {
250         return HI_ERR_EVENT_INVALID_PARAM;
251     }
252 
253     ret = LOS_EventDestroy(&(item->os_event));
254     if (ret != LOS_OK) {
255         return HI_ERR_EVENT_DELETE_FAIL;
256     }
257 
258     memset_s(item, sizeof(hi_event_item), 0x0, sizeof(hi_event_item));
259     g_event_ctrl.used_count--;
260     return HI_ERR_SUCCESS;
261 }
262