• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 "string.h"
16 #include "cmsis.h"
17 #ifdef RTOS
18 #include "cmsis_os.h"
19 #endif
20 #include "hal_timer.h"
21 #include "hal_mcu2cp.h"
22 #include "hal_cmu.h"
23 #include "cp_ipc.h"
24 
25 static CP_IPC_MSG_CB ipc_rx_handler[IPC_MSG_TYPE_NUM] = {NULL};
26 
27 static CP_IPC_MSG_CB ipc_tx_handler[IPC_MSG_TYPE_NUM] = {NULL};
28 
29 #ifdef CP_BUILD
mcu2cp_msg_arrived(const unsigned char * data,unsigned int len)30 static uint32_t mcu2cp_msg_arrived(const unsigned char *data, unsigned int len)
31 {
32     CP_IPC_MSG_HDR *msg = (CP_IPC_MSG_HDR *) data;
33 
34     if (msg || msg->id < IPC_MSG_TYPE_NUM) {
35         if (ipc_rx_handler[msg->id])
36             ipc_rx_handler[msg->id]((void *)data);
37     }
38 
39     return len;
40 }
41 
cp2mcu_msg_sent(const unsigned char * data,unsigned int len)42 static void cp2mcu_msg_sent(const unsigned char *data, unsigned int len)
43 {
44     CP_IPC_MSG_HDR *msg = (CP_IPC_MSG_HDR *) data;
45 
46     if (msg || msg->id < IPC_MSG_TYPE_NUM) {
47         if (ipc_tx_handler[msg->id])
48             ipc_tx_handler[msg->id]((void *)data);
49     }
50 }
51 #else
cp2mcu_msg_arrived(const unsigned char * data,unsigned int len)52 static unsigned int cp2mcu_msg_arrived(const unsigned char *data, unsigned int len)
53 {
54     CP_IPC_MSG_HDR *msg = (CP_IPC_MSG_HDR *) data;
55 
56     if (msg || msg->id < IPC_MSG_TYPE_NUM) {
57         if (ipc_rx_handler[msg->id])
58             ipc_rx_handler[msg->id]((void *)data);
59     }
60 
61     return len;
62 }
63 
mcu2cp_msg_sent(const unsigned char * data,unsigned int len)64 static void mcu2cp_msg_sent(const unsigned char *data, unsigned int len)
65 {
66     CP_IPC_MSG_HDR *msg = (CP_IPC_MSG_HDR *) data;
67 
68     if (msg || msg->id < IPC_MSG_TYPE_NUM) {
69         if (ipc_tx_handler[msg->id])
70             ipc_tx_handler[msg->id]((void *)data);
71     }
72 }
73 
cp2mcu_sys_arrived(const unsigned char * data,unsigned int len)74 static unsigned int cp2mcu_sys_arrived(const unsigned char *data, unsigned int len)
75 {
76     return len;
77 }
78 #endif
79 
cp_ipc_init()80 int cp_ipc_init()
81 {
82 #ifdef CP_BUILD
83     hal_mcu2cp_set_send_msg_list_cp();
84 #else
85     /* use cmu dbg reg to store the base address of send msg list when cp run seperate image */
86     hal_cmu_dbg_set_val(0, 0);
87     hal_cmu_dbg_set_val(1, 0);
88     hal_mcu2cp_set_send_msg_list_mcu();
89 #endif
90 
91     return 0;
92 }
93 
cp_ipc_start()94 int cp_ipc_start()
95 {
96 #ifdef CP_BUILD
97     /* wait mcu init send msg addr */
98     while (hal_mcu2cp_get_send_msg_list_mcu() == NULL)
99         osDelay(100);
100 
101     hal_mcu2cp_open_cp(HAL_MCU2CP_ID_0, mcu2cp_msg_arrived, cp2mcu_msg_sent, false);
102     hal_mcu2cp_start_recv_cp(HAL_MCU2CP_ID_0);
103 
104 #ifdef HAL_MCU2CP_ID_1
105     hal_mcu2cp_open_cp(HAL_MCU2CP_ID_1, NULL, NULL, false);
106     hal_mcu2cp_start_recv_cp(HAL_MCU2CP_ID_1);
107 #endif
108 #else
109     /* wait cp to init cmu dbg reg */
110     while (hal_mcu2cp_get_send_msg_list_cp() == NULL)
111         osDelay(100);
112 
113     hal_mcu2cp_open_mcu(HAL_MCU2CP_ID_0, cp2mcu_msg_arrived, mcu2cp_msg_sent, false);
114     hal_mcu2cp_start_recv_mcu(HAL_MCU2CP_ID_0);
115 
116 #ifdef HAL_MCU2CP_ID_1
117     hal_mcu2cp_open_mcu(HAL_MCU2CP_ID_1, cp2mcu_sys_arrived, NULL, false);
118     hal_mcu2cp_start_recv_mcu(HAL_MCU2CP_ID_1);
119 #endif
120 #endif
121 
122     return 0;
123 }
124 
cp_ipc_send(CP_IPC_MSG_HDR * msg)125 int cp_ipc_send(CP_IPC_MSG_HDR *msg)
126 {
127     int ret;
128 
129     if (!msg)
130         return -1;
131 
132 #ifdef CP_BUILD
133     ret = hal_mcu2cp_send_cp(HAL_MCU2CP_ID_0, (const unsigned char *)msg, sizeof(CP_IPC_MSG_HDR));
134 #else
135     ret = hal_mcu2cp_send_mcu(HAL_MCU2CP_ID_0, (const unsigned char *)msg, sizeof(CP_IPC_MSG_HDR));
136 #endif
137 
138     return ret;
139 }
140 
cp_ipc_send_self(CP_IPC_MSG_HDR * msg)141 int cp_ipc_send_self(CP_IPC_MSG_HDR *msg)
142 {
143     int ret = 0;
144 
145     if (!msg)
146         return -1;
147 
148 #ifdef CP_BUILD
149     mcu2cp_msg_arrived((const unsigned char *)msg, 0);
150 #else
151     cp2mcu_msg_arrived((const unsigned char *)msg, 0);
152 #endif
153 
154     return ret;
155 }
156 
cp_ipc_cb_set(CP_IPC_MSG_TYPE type,CP_IPC_MSG_CB func,bool tx)157 int cp_ipc_cb_set(CP_IPC_MSG_TYPE type, CP_IPC_MSG_CB func, bool tx)
158 {
159     if (type >= IPC_MSG_TYPE_NUM)
160         return -1;
161 
162     if (tx) {
163         ipc_tx_handler[type] = func;
164     } else
165         ipc_rx_handler[type] = func;
166 
167     return 0;
168 }
169