1 /*
2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., 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 /*chipsea_ohos proguard begin*/
16 #include "cs_proguard.h"
17 /*chipsea_ohos proguard end*/
18 #include "al_rtos.h"
19 #include "app_bt.h"
20 #include "cs_adp_api.h"
21 #include "bt_task_msg.h"
22 #ifdef CFG_BLE_STACK
23 #include "csble.h"
24 #endif
25 #include "bt_cs8800_drvif.h"
26 #include "sysctrl_api.h"
27 #include "bt_task.h"
28 #include "bt_common_config.h"
29 #include "bt_hci.h"
30
31 /// Handle of the BT task
32 static rtos_task_handle bt_task_handle;
33 /// Indicates whether BT task is suspended or not
34 static bool bt_task_suspended;
35 static rtos_queue app_bt_task_queue;
36
37 #ifdef CFG_BLE_STACK
38 #define CS_BT_STACK_SIZE 1024*4
39 #else
40 #define CS_BT_STACK_SIZE TASK_STACK_SIZE_BT_TASK
41 #endif
42 static cs_host_cfg_t cfg = {
43 #if APP_SUPPORT_OTA_BOX
44 .support_sniff = false,
45 #else
46 .support_sniff = true,
47 #endif
48 #if APP_SUPPORT_MULTIPLE_PHONE == 0
49 .multiple_phone = false,
50 #else
51 .multiple_phone = true,
52 #endif
53 #if (AON_SUPPORT)
54 .support_aon = true,
55 #ifdef APP_SUPPORT_DEEPSLEEP
56 .support_sleep = true,
57 #else
58 .support_sleep = false,
59 #endif
60 #else
61 .support_aon = false,
62 .support_sleep = false,
63 #endif
64 #ifdef CFG_BTDM_RAM_VER
65 .rom_version = 0,
66 #else
67 .rom_version = CFG_ROM_VER,
68 #endif
69 };
70
bt_task_change_default_param(void)71 void bt_task_change_default_param(void)
72 {
73 bt_common_change_fw_load_in_ram();
74 }
75
bt_task_suspend(void)76 uint32_t bt_task_suspend(void)
77 {
78 uint32_t retvalue = 0;
79 GLOBAL_INT_DISABLE();
80 bt_task_suspended = true;
81 GLOBAL_INT_RESTORE();
82
83 // wait for notification
84 retvalue = rtos_task_wait_notification(-1);
85 return retvalue;
86 }
87
88
bt_task_resume(bool isr,uint32_t value)89 void bt_task_resume(bool isr,uint32_t value)
90 {
91 /*
92 if (!bt_task_suspended) {
93 return;
94 }*/
95 bt_task_suspended = false;
96
97 // Notify the bt task
98 rtos_task_notify(bt_task_handle, value, isr);
99 }
100
101
102 /**
103 * @brief Main loop for the BT Task
104 */
105 //extern void host_aon_sram_restore_all(void);
RTOS_TASK_FCT(bt_task)106 static RTOS_TASK_FCT(bt_task)
107 {
108 BOOL ret = TRUE;
109 #if APP_SUPPORT_AES == 1
110 uint32_t pwrmd = pwrctrl_pwrmd_cpusys_sw_record_getf();
111 if(pwrmd < CPU_SYS_POWER_DOWN) {
112 // clk en
113 cpusysctrl_hclkme_set(CSC_HCLKME_DMA_EN_BIT);
114 bt_drv_generate_secret_key();
115 bt_drv_start_generate_key();
116 app_bt_handler_register(HANDLER_REG_2, bt_drv_pool_key_complete);
117 }
118 #endif
119 #ifdef CFG_BTDM_RAM_VER
120 bt_task_change_default_param();
121 #endif
122 #if (CFG_ROM_VER == 255)
123 uint8_t chip_id = ChipIdGet(0);
124 if (chip_id == 0x03) {
125 cfg.rom_version = 2;
126 } else if (chip_id == 0x07) {
127 cfg.rom_version = 3;
128 }
129 #endif
130 cs_stack_init(cfg);
131 app_bt_init();
132 ret = cs_adp_stack_config();
133 if(ret == TRUE){
134 GLOBAL_INT_DISABLE();
135 cs_bt_start();
136 GLOBAL_INT_RESTORE();
137 bt_task_set_default_priority();
138 }
139 bt_task_queue_notify(false);
140 for (;;)
141 {
142 uint32_t msg;
143 // Suspend the BT task while waiting for new events
144 rtos_queue_read(app_bt_task_queue,(void *)&msg, -1, 0);
145 //host_aon_sram_restore_all();//temp add for test ,it will be deleted in real deelsleep mode.
146 // schedule all pending events
147 cs_stack_loop();
148 handler_reg_list_poll();
149 #ifdef CFG_BLE_STACK
150 cs_ble_schedule();
151 #endif
152 }
153 }
154
155
bt_task_queue_notify(bool isr)156 void bt_task_queue_notify(bool isr)
157 {
158 bool ret = 0;
159 uint32_t msg = 0xff;
160 if(rtos_queue_cnt(app_bt_task_queue) < 5){
161 ret = rtos_queue_write(app_bt_task_queue,&msg , 0, isr);
162 if(isr){
163 ret += 0;
164 //TRACE(" notify %d ",ret);
165 }
166 }
167 }
168
169
bt_task_init(void)170 int bt_task_init(void)
171 {
172 #if PLF_CONSOLE
173 app_console_command_add();
174 #endif
175 if (rtos_queue_create(sizeof(uint32_t),10,&app_bt_task_queue)){
176 TRACE("BT_TASK: Failed to Create app_bt_queue\n");
177 return -1;
178 }
179
180 bt_task_suspended = false;
181 // Create the BT task
182 if (rtos_task_create(bt_task, "BT_TASK", BT_TASK,
183 CS_BT_STACK_SIZE, NULL, TASK_PRIORITY_MAX, &bt_task_handle)) {
184 rtos_queue_delete(app_bt_task_queue);
185 return -1;
186 }
187 #if APP_SUPPORT_HFG == 1 && PLF_USB_BT == 1
188 int res = 0;
189 if (0 != usb_bt_init()) {
190 TRACE("usb_bt_init fail\n");
191 res = -1;
192 goto exit;
193 }
194
195 if (0 != usb_bt_dongle_init()) {
196 TRACE("usb_bt_dongle_init fail\n");
197 res = -2;
198 goto exit;
199 }
200 exit:
201 if (res == 0) {
202 TRACE("usb_bt_test success\n");
203 } else {
204 TRACE("usb_bt_test fail\n");
205 usb_bt_dongle_deinit();
206 usb_bt_deinit();
207 }
208 #endif
209 sysctrl_clock_cfg(CLK_CFG_D52S52P26F26);
210 SystemCoreClockUpdate();
211 return 0;
212 }
213
bt_task_set_max_priority(void)214 void bt_task_set_max_priority(void)
215 {
216 rtos_task_set_priority(bt_task_handle, TASK_PRIORITY_MAX);
217 }
218
bt_task_set_default_priority(void)219 void bt_task_set_default_priority(void)
220 {
221 rtos_task_set_priority(bt_task_handle, TASK_PRIORITY_BT_TASK);
222 }
223
224