1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <os/os.h>
16 #include <components/log.h>
17
18 #include "media_core.h"
19 #include "media_evt.h"
20 #include "camera_act.h"
21 #include "mailbox_act.h"
22 #include "aud_act.h"
23 #include "comm_act.h"
24 #include "lcd_act.h"
25 #include "transfer_act.h"
26 #include "storage_act.h"
27 #include "mailbox_channel.h"
28 #include "frame_buffer.h"
29 #include "media_app.h"
30 #include "mlist.h"
31
32
33 #define MJ_TAG "media0"
34
35 #define LOGI(...) BK_LOGI(MJ_TAG, ##__VA_ARGS__)
36 #define LOGW(...) BK_LOGW(MJ_TAG, ##__VA_ARGS__)
37 #define LOGE(...) BK_LOGE(MJ_TAG, ##__VA_ARGS__)
38 #define LOGD(...) BK_LOGD(MJ_TAG, ##__VA_ARGS__)
39
40 static beken_thread_t media_major_th_hd = NULL;
41 static beken_queue_t media_major_msg_queue = NULL;
42
43 #if 0
44 static mlist_t *mailbox_free_list = NULL;
45 static mlist_t *mailbox_msg_list = NULL;
46 bool mailbox_busy = false;
47 #define MAX_MAILBOX_QUEUE = 10;
48 #endif
49
50 extern uint32_t platform_is_in_interrupt_context(void);
51
52
53 #ifdef CONFIG_DUAL_CORE
media_mailbox_send_msg(uint32_t cmd,uint32_t param1,uint32_t param2)54 bk_err_t media_mailbox_send_msg(uint32_t cmd, uint32_t param1, uint32_t param2)
55 {
56 mb_chnl_cmd_t mb_cmd;
57
58 mb_cmd.hdr.cmd = 1;
59 mb_cmd.param1 = cmd;
60 mb_cmd.param2 = param1;
61 mb_cmd.param3 = param2;
62 return mb_chnl_write(MB_CHNL_MEDIA, &mb_cmd);
63 }
64
media_major_mailbox_rx_isr(void * param,mb_chnl_cmd_t * cmd_buf)65 static void media_major_mailbox_rx_isr(void *param, mb_chnl_cmd_t *cmd_buf)
66 {
67 LOGD("%s, %08X\n", __func__, cmd_buf->param1);
68
69 switch (cmd_buf->param1 >> MEDIA_EVT_BIT)
70 {
71 #ifdef CONFIG_CAMERA
72 case MAILBOX_EVT:
73 {
74 media_msg_t msg;
75 msg.event = cmd_buf->param1;
76 msg.param = cmd_buf->param2;
77
78 if (media_major_msg_queue)
79 {
80 bk_err_t ret;
81
82 ret = rtos_push_to_queue(&media_major_msg_queue, &msg, BEKEN_NO_WAIT);
83
84 if (BK_OK != ret)
85 {
86 LOGE("%s failed\n", __func__);
87 }
88 }
89
90 }
91 break;
92 #endif
93 default:
94 break;
95 }
96 }
97
media_major_mailbox_tx_isr(void * param)98 static void media_major_mailbox_tx_isr(void *param)
99 {
100 LOGD("%s\n", __func__);
101
102 }
103
media_major_mailbox_tx_cmpl_isr(void * param,mb_chnl_ack_t * ack_buf)104 static void media_major_mailbox_tx_cmpl_isr(void *param, mb_chnl_ack_t *ack_buf)
105 {
106 LOGD("%s\n", __func__);
107
108 }
109 #endif
110
media_send_msg(media_msg_t * msg)111 bk_err_t media_send_msg(media_msg_t *msg)
112 {
113 bk_err_t ret = BK_OK;
114
115 #ifdef CONFIG_DUAL_CORE
116
117 if (msg->event >> MEDIA_EVT_BIT == MAILBOX_EVT)
118 {
119 LOGE("%s failed, error event\n", __func__);
120 return BK_FAIL;
121 }
122
123 if (msg->event >> MEDIA_EVT_BIT == MAILBOX_CMD)
124 {
125 #if 0
126 if (platform_is_in_interrupt_context())
127 {
128 //LOGW("%s should not call form isr\n", __func__);
129 }
130 #endif
131
132 ret = media_mailbox_send_msg(msg->event, msg->param, 0);
133 return ret;
134 }
135 #endif
136
137 if (media_major_msg_queue)
138 {
139 ret = rtos_push_to_queue(&media_major_msg_queue, msg, BEKEN_NO_WAIT);
140
141 if (BK_OK != ret)
142 {
143 LOGE("%s failed\n", __func__);
144 return BK_FAIL;
145 }
146
147 return ret;
148 }
149 return ret;
150 }
151
152
153
media_major_message_handle(void)154 static void media_major_message_handle(void)
155 {
156 bk_err_t ret = BK_OK;
157 media_msg_t msg;
158
159 frame_buffer_init();
160
161 #if (defined(CONFIG_CAMERA) || defined(CONFIG_USB_UVC))
162 camera_init();
163 #endif
164
165 #ifdef CONFIG_WIFI_TRANSFER
166 transfer_init();
167 #endif
168
169 #ifdef CONFIG_LCD
170 lcd_init();
171 #endif
172
173 #ifdef CONFIG_CAMERA
174 storage_init();
175 #endif
176
177 while (1)
178 {
179
180 ret = rtos_pop_from_queue(&media_major_msg_queue, &msg, BEKEN_WAIT_FOREVER);
181
182 if (kNoErr == ret)
183 {
184 switch (msg.event >> MEDIA_EVT_BIT)
185 {
186 case COM_EVENT:
187 comm_event_handle(msg.event, msg.param);
188 break;
189
190 #if (defined(CONFIG_CAMERA) || defined(CONFIG_USB_UVC))
191 case CAM_EVENT:
192 camera_event_handle(msg.event, msg.param);
193 break;
194 #endif
195
196 #ifdef CONFIG_AUDIO
197 case AUD_EVENT:
198 audio_event_handle(msg.event, msg.param);
199 break;
200 #endif
201
202 #ifdef CONFIG_LCD
203 case LCD_EVENT:
204 lcd_event_handle(msg.event, msg.param);
205 break;
206 #endif
207
208 #ifdef CONFIG_WIFI_TRANSFER
209 case TRS_EVENT:
210 transfer_event_handle(msg.event, msg.param);
211 break;
212 #endif
213
214 #ifdef CONFIG_CAMERA
215 case STORAGE_EVENT:
216 storage_event_handle(msg.event, msg.param);
217 break;
218 #endif
219
220 #ifdef CONFIG_DUAL_CORE
221 case MAILBOX_EVT:
222 mailbox_evt_handle(msg.event, msg.param);
223 break;
224 #endif
225 case QUEUE_EVENT:
226
227 break;
228
229 case EXIT_EVENT:
230 goto exit;
231 break;
232
233 default:
234 break;
235 }
236 }
237 }
238
239 exit:
240
241 frame_buffer_deinit();
242
243 /* delate msg queue */
244 ret = rtos_deinit_queue(&media_major_msg_queue);
245
246 if (ret != kNoErr)
247 {
248 LOGE("delate message queue fail\n");
249 }
250
251 media_major_msg_queue = NULL;
252
253 LOGE("delate message queue complete\n");
254
255 /* delate task */
256 rtos_delete_thread(NULL);
257
258 media_major_th_hd = NULL;
259
260 LOGE("delate task complete\n");
261 }
262
263
media_major_init(void)264 bk_err_t media_major_init(void)
265 {
266 bk_err_t ret = BK_OK;
267
268 if (media_major_msg_queue != NULL)
269 {
270 ret = kNoErr;
271 LOGE("%s, media_major_msg_queue allready init, exit!\n");
272 goto error;
273 }
274
275 if (media_major_th_hd != NULL)
276 {
277 ret = kNoErr;
278 LOGE("%s, media_major_th_hd allready init, exit!\n");
279 goto error;
280 }
281
282 ret = rtos_init_queue(&media_major_msg_queue,
283 "media_major_queue",
284 sizeof(media_msg_t),
285 MEDIA_MAJOR_MSG_QUEUE_SIZE);
286
287 if (ret != kNoErr)
288 {
289 LOGE("%s, ceate media major message queue failed\n");
290 goto error;
291 }
292
293 ret = rtos_create_thread(&media_major_th_hd,
294 BEKEN_DEFAULT_WORKER_PRIORITY,
295 "media_major_thread",
296 (beken_thread_function_t)media_major_message_handle,
297 4096,
298 NULL);
299
300 if (ret != kNoErr)
301 {
302 LOGE("create media major thread fail\n");
303 goto error;
304 }
305
306 media_app_init();
307
308 #ifdef CONFIG_DUAL_CORE
309
310 #if 0
311 if (mailbox_free_list)
312 {
313 mailbox_free_list = mlist_new(NULL);
314
315 for (int i = 0; i < MAX_MAILBOX_QUEUE; i ++)
316 {
317 media_msg_t *data = (media_msg_t *)osi_malloc(sizeof(media_msg_t));
318
319 mlist_append(mailbox_free_list, data);
320 }
321 }
322
323 if (mailbox_msg_list)
324 {
325 mailbox_msg_list = mlist_new(NULL);
326 }
327 #endif
328
329 mb_chnl_open(MB_CHNL_MEDIA, NULL);
330 mb_chnl_ctrl(MB_CHNL_MEDIA, MB_CHNL_SET_RX_ISR, media_major_mailbox_rx_isr);
331 mb_chnl_ctrl(MB_CHNL_MEDIA, MB_CHNL_SET_TX_ISR, media_major_mailbox_tx_isr);
332 mb_chnl_ctrl(MB_CHNL_MEDIA, MB_CHNL_SET_TX_CMPL_ISR, media_major_mailbox_tx_cmpl_isr);
333 #endif
334
335 LOGI("media major thread startup complete\n");
336
337 return kNoErr;
338 error:
339
340 if (media_major_msg_queue)
341 {
342 rtos_deinit_queue(&media_major_msg_queue);
343 media_major_msg_queue = NULL;
344 }
345
346 return ret;
347 }
348
get_cpu_id(void)349 media_cpu_t get_cpu_id(void)
350 {
351 return MAJOR_CPU;
352 }
353