• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "aud_act.h"
22 #include "comm_act.h"
23 #include "lcd_act.h"
24 #include "mailbox_channel.h"
25 #include "mailbox_act.h"
26 
27 
28 #define TAG "media1"
29 
30 #define LOGI(...) BK_LOGI(TAG, ##__VA_ARGS__)
31 #define LOGW(...) BK_LOGW(TAG, ##__VA_ARGS__)
32 #define LOGE(...) BK_LOGE(TAG, ##__VA_ARGS__)
33 #define LOGD(...) BK_LOGD(TAG, ##__VA_ARGS__)
34 
35 static beken_thread_t media_minor_th_hd = NULL;
36 static beken_queue_t media_minor_msg_queue = NULL;
37 
media_minor_mailbox_rx_isr(void * param,mb_chnl_cmd_t * cmd_buf)38 static void media_minor_mailbox_rx_isr(void *param, mb_chnl_cmd_t *cmd_buf)
39 {
40 	LOGD("%s, %08X\n", __func__, cmd_buf->param1);
41 	bk_err_t ret;
42 
43 	switch (cmd_buf->param1 >> MEDIA_EVT_BIT)
44 	{
45 #ifdef CONFIG_CAMERA
46 		case MAILBOX_CMD:
47 		{
48 			media_msg_t msg;
49 			msg.event = cmd_buf->param1;
50 			msg.param = cmd_buf->param2;
51 
52 			if (media_minor_msg_queue)
53 			{
54 				ret = rtos_push_to_queue(&media_minor_msg_queue, &msg, BEKEN_NO_WAIT);
55 
56 				if (BK_OK != ret)
57 				{
58 					LOGE("%s failed\n", __func__);
59 				}
60 			}
61 
62 		}
63 		break;
64 #endif
65 		default:
66 			break;
67 	}
68 }
69 
70 
media_minor_mailbox_tx_isr(void * param)71 static void media_minor_mailbox_tx_isr(void *param)
72 {
73 	LOGD("%s\n", __func__);
74 
75 }
76 
media_minor_mailbox_tx_cmpl_isr(void * param,mb_chnl_ack_t * ack_buf)77 static void media_minor_mailbox_tx_cmpl_isr(void *param, mb_chnl_ack_t *ack_buf)
78 {
79 	LOGD("%s\n", __func__);
80 
81 }
82 
83 
media_send_msg(media_msg_t * msg)84 bk_err_t media_send_msg(media_msg_t *msg)
85 {
86 	bk_err_t ret;
87 
88 	if (msg->event >> MEDIA_EVT_BIT == MAILBOX_CMD)
89 	{
90 		LOGE("%s failed, error event\n", __func__);
91 		return BK_FAIL;
92 	}
93 
94 	if (msg->event >> MEDIA_EVT_BIT == MAILBOX_EVT)
95 	{
96 		ret = media_mailbox_send_msg(msg->event, msg->param, 0);
97 		return ret;
98 	}
99 
100 	if (media_minor_msg_queue)
101 	{
102 		ret = rtos_push_to_queue(&media_minor_msg_queue, msg, BEKEN_NO_WAIT);
103 
104 		if (kNoErr != ret)
105 		{
106 			LOGE("%s failed\n", __func__);
107 			return kOverrunErr;
108 		}
109 
110 		return ret;
111 	}
112 	return kNoResourcesErr;
113 }
114 
115 
116 
media_minor_message_handle(void)117 static void media_minor_message_handle(void)
118 {
119 	bk_err_t ret = BK_OK;
120 	media_msg_t msg;
121 
122 	while (1)
123 	{
124 		ret = rtos_pop_from_queue(&media_minor_msg_queue, &msg, BEKEN_WAIT_FOREVER);
125 
126 		if (kNoErr == ret)
127 		{
128 			switch (msg.event >> MEDIA_EVT_BIT)
129 			{
130 #if 0
131 				case COM_EVENT:
132 					comm_event_handle(msg.event, msg.param);
133 					break;
134 #ifdef CONFIG_CAMERA
135 				case DVP_EVENT:
136 					dvp_camera_event_handle(msg.event, msg.param);
137 					break;
138 #endif
139 
140 #ifdef CONFIG_AUDIO
141 				case AUD_EVENT:
142 					audio_event_handle(msg.event, msg.param);
143 					break;
144 #endif
145 
146 #ifdef CONFIG_LCD
147 				case LCD_EVENT:
148 					lcd_event_handle(msg.event, msg.param);
149 					break;
150 #endif
151 
152 #if CONFIG_USB_UVC
153 				case UVC_EVENT:
154 					uvc_camera_event_handle(msg.event, msg.param);
155 					break;
156 #endif
157 #endif
158 
159 #ifdef CONFIG_DUAL_CORE
160 				case MAILBOX_CMD:
161 					mailbox_cmd_handle(msg.event, msg.param);
162 					break;
163 #endif
164 
165 				case EXIT_EVENT:
166 					goto exit;
167 					break;
168 
169 				default:
170 					break;
171 			}
172 		}
173 	}
174 
175 exit:
176 
177 	/* delate msg queue */
178 	ret = rtos_deinit_queue(&media_minor_msg_queue);
179 
180 	if (ret != kNoErr)
181 	{
182 		LOGE("delate message queue fail\n");
183 	}
184 
185 	media_minor_msg_queue = NULL;
186 
187 	LOGE("delate message queue complete\n");
188 
189 	/* delate task */
190 	rtos_delete_thread(NULL);
191 
192 	media_minor_th_hd = NULL;
193 
194 	LOGE("delate task complete\n");
195 }
196 
197 
media_minor_init(void)198 bk_err_t media_minor_init(void)
199 {
200 	bk_err_t ret = BK_OK;
201 
202 	if (media_minor_msg_queue != NULL)
203 	{
204 		ret = kNoErr;
205 		LOGE("%s, media_minor_msg_queue allready init, exit!\n");
206 		goto error;
207 	}
208 
209 	if (media_minor_th_hd != NULL)
210 	{
211 		ret = kNoErr;
212 		LOGE("%s, media_minor_th_hd allready init, exit!\n");
213 		goto error;
214 	}
215 
216 	ret = rtos_init_queue(&media_minor_msg_queue,
217 	                      "media_minor_queue",
218 	                      sizeof(media_msg_t),
219 	                      MEDIA_MINOR_MSG_QUEUE_SIZE);
220 
221 	if (ret != kNoErr)
222 	{
223 		LOGE("%s, ceate media minor message queue failed\n");
224 		goto error;
225 	}
226 
227 	ret = rtos_create_thread(&media_minor_th_hd,
228 	                         BEKEN_DEFAULT_WORKER_PRIORITY,
229 	                         "media_minor_thread",
230 	                         (beken_thread_function_t)media_minor_message_handle,
231 	                         4096,
232 	                         NULL);
233 
234 	if (ret != kNoErr)
235 	{
236 		LOGE("create media minor thread fail\n");
237 		goto error;
238 	}
239 
240 	mb_chnl_open(MB_CHNL_MEDIA, NULL);
241 	mb_chnl_ctrl(MB_CHNL_MEDIA, MB_CHNL_SET_RX_ISR, media_minor_mailbox_rx_isr);
242 	mb_chnl_ctrl(MB_CHNL_MEDIA, MB_CHNL_SET_TX_ISR, media_minor_mailbox_tx_isr);
243 	mb_chnl_ctrl(MB_CHNL_MEDIA, MB_CHNL_SET_TX_CMPL_ISR, media_minor_mailbox_tx_cmpl_isr);
244 
245 	LOGI("media minor thread startup complete\n");
246 
247 	return kNoErr;
248 error:
249 
250 	if (media_minor_msg_queue)
251 	{
252 		rtos_deinit_queue(&media_minor_msg_queue);
253 		media_minor_msg_queue = NULL;
254 	}
255 
256 	return ret;
257 }
258 
media_mailbox_send_msg(uint32_t cmd,uint32_t param1,uint32_t param2)259 bk_err_t media_mailbox_send_msg(uint32_t cmd, uint32_t param1, uint32_t param2)
260 {
261 	mb_chnl_cmd_t mb_cmd;
262 
263 	mb_cmd.hdr.cmd = 1;
264 	mb_cmd.param1 = cmd;
265 	mb_cmd.param2 = param1;
266 	mb_cmd.param3 = param2;
267 	return mb_chnl_write(MB_CHNL_MEDIA, &mb_cmd);
268 }
269 
get_cpu_id(void)270 media_cpu_t get_cpu_id(void)
271 {
272 	return MINOR_CPU;
273 }
274