1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18
19 #include "../../stack/ble/ble.h"
20 #include "../../stack/ble/ble_format.h"
21 #include "b91_bt_buffer.h"
22 #include "b91_bt_init.h"
23
24 #if CONFIG_B91_BLE_CTRL_EXT_ADV
25
26 /** Number of Supported Advertising Sets, no exceed "ADV_SETS_NUMBER_MAX" */
27 #define APP_ADV_SETS_NUMBER CONFIG_B91_BLE_CTRL_EXT_ADV_SETS_NUM
28
29 /** Maximum Advertising Data Length, (if legacy ADV, max length 31 bytes is enough) */
30 #define APP_MAX_LENGTH_ADV_DATA CONFIG_B91_BLE_CTRL_EXT_ADV_DATA_LEN_MAX
31
32 /** Maximum Scan Response Data Length, (if legacy ADV, max length 31 bytes is enough) */
33 #define APP_MAX_LENGTH_SCAN_RESPONSE_DATA CONFIG_B91_BLE_CTRL_EXT_ADV_SCAN_DATA_LEN_MAX
34
35 _attribute_ble_data_retention_ u8 app_advSet_buffer[ADV_SET_PARAM_LENGTH * APP_ADV_SETS_NUMBER];
36 _attribute_ble_data_retention_ u8 app_advData_buffer[APP_MAX_LENGTH_ADV_DATA * APP_ADV_SETS_NUMBER];
37 _attribute_ble_data_retention_ u8 app_scanRspData_buffer[APP_MAX_LENGTH_SCAN_RESPONSE_DATA * APP_ADV_SETS_NUMBER];
38
39 #endif /* CONFIG_B91_BLE_CTRL_EXT_ADV */
40
41 #if CONFIG_B91_BLE_CTRL_PER_ADV
42
43 /** Number of Supported Periodic Advertising Sets, no exceed "PERIODIC_ADV_NUMBER_MAX" */
44 #define APP_PER_ADV_SETS_NUMBER CONFIG_B91_BLE_CTRL_PER_ADV_SETS_NUM
45
46 /** Maximum Periodic Advertising Data Length */
47 #define APP_MAX_LENGTH_PER_ADV_DATA CONFIG_B91_BLE_CTRL_PER_ADV_DATA_LEN_MAX
48
49 _attribute_ble_data_retention_ u8 app_perdAdvSet_buffer[PERD_ADV_PARAM_LENGTH * APP_PER_ADV_SETS_NUMBER];
50 _attribute_ble_data_retention_ u8 app_perdAdvData_buffer[APP_MAX_LENGTH_PER_ADV_DATA * APP_PER_ADV_SETS_NUMBER];
51
52 #endif /* CONFIG_B91_BLE_CTRL_PER_ADV */
53
54
55 /**
56 * @brief This function is used to initialize the MAC address
57 * @param[in] flash_addr - flash address for MAC address
58 * @param[in] mac_public - public address
59 * @param[in] mac_random_static - random static MAC address
60 * @return none
61 */
62 _attribute_no_inline_
b91_bt_blc_mac_init(int flash_addr,u8 * mac_public,u8 * mac_random_static)63 static void b91_bt_blc_mac_init(int flash_addr, u8 *mac_public, u8 *mac_random_static)
64 {
65 if(flash_addr == 0) {
66 return;
67 }
68
69 u8 mac_read[8];
70 flash_read_page(flash_addr, 8, mac_read);
71
72 u8 value_rand[5];
73 generateRandomNum(5, value_rand);
74
75 u8 ff_six_byte[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
76 if (memcmp(mac_read, ff_six_byte, 6)) {
77 memcpy(mac_public, mac_read, 6); /* copy public address from flash */
78 }
79 else { /* no public address in flash */
80 mac_public[0] = value_rand[0];
81 mac_public[1] = value_rand[1];
82 mac_public[2] = value_rand[2];
83 mac_public[3] = 0x38; /* company id: 0xA4C138 */
84 mac_public[4] = 0xC1;
85 mac_public[5] = 0xA4;
86
87 flash_write_page (flash_addr, 6, mac_public);
88 }
89
90 mac_random_static[0] = mac_public[0];
91 mac_random_static[1] = mac_public[1];
92 mac_random_static[2] = mac_public[2];
93 mac_random_static[5] = 0xC0; /* for random static */
94
95 u16 high_2_byte = (mac_read[6] | mac_read[7]<<8);
96 if (high_2_byte != 0xFFFF) {
97 memcpy( (u8 *)(mac_random_static + 3), (u8 *)(mac_read + 6), 2);
98 }
99 else {
100 mac_random_static[3] = value_rand[3];
101 mac_random_static[4] = value_rand[4];
102
103 flash_write_page (flash_addr + 6, 2, (u8 *)(mac_random_static + 3) );
104 }
105 }
106
107 /**
108 * @brief Telink B91 BLE Controller initialization
109 * @param[in] prx - HCI RX callback
110 * @param[in] ptx -HCI TX callback
111 * @return Status - 0: command succeeded; -1: command failed
112 */
b91_bt_blc_init(void * prx,void * ptx)113 int b91_bt_blc_init(void *prx, void *ptx)
114 {
115 /* random number generator must be initiated here(in the beginning of user_init_nromal).
116 * When deepSleep retention wakeUp, no need initialize again */
117 random_generator_init();
118
119 /* for 512K Flash, mac_address equals to 0x76000
120 * for 1M Flash, mac_address equals to 0xFF000 */
121 u8 mac_public[BLE_ADDR_LEN];
122 u8 mac_random_static[BLE_ADDR_LEN];
123 b91_bt_blc_mac_init(CONFIG_B91_BLE_CTRL_MAC_FLASH_ADDR, mac_public, mac_random_static);
124
125 blc_ll_initBasicMCU();
126
127 #if defined(CONFIG_B91_BLE_CTRL_MAC_PUBLIC)
128 blc_ll_initStandby_module(mac_public);
129 #else
130 blc_ll_initStandby_module(mac_random_static);
131 #endif
132
133 blc_ll_initLegacyAdvertising_module(); //adv module: mandatory for BLE slave,
134
135 blc_ll_initLegacyScanning_module(); //scan module: mandatory for BLE master
136
137 blc_ll_initInitiating_module(); //initiate module: mandatory for BLE master
138
139 blc_ll_initAclConnection_module();
140 blc_ll_initAclMasterRole_module();
141 blc_ll_initAclSlaveRole_module();
142
143 #if CONFIG_B91_BLE_CTRL_EXT_ADV
144 blc_ll_initExtendedAdvertising_module();
145 blc_ll_initExtendedAdvSetBuffer(app_advSet_buffer, APP_ADV_SETS_NUMBER);
146 blc_ll_initExtAdvDataBuffer(app_advData_buffer, APP_MAX_LENGTH_ADV_DATA);
147 blc_ll_initExtScanRspDataBuffer(app_scanRspData_buffer, APP_MAX_LENGTH_SCAN_RESPONSE_DATA);
148 #endif
149
150 #if CONFIG_B91_BLE_CTRL_PER_ADV
151 blc_ll_initPeriodicAdvertising_module();
152 blc_ll_initPeriodicAdvParamBuffer(app_perdAdvSet_buffer, APP_PER_ADV_SETS_NUMBER);
153 blc_ll_initPeriodicAdvDataBuffer(app_perdAdvData_buffer, APP_MAX_LENGTH_PER_ADV_DATA);
154 #endif
155
156 #if CONFIG_B91_BLE_CTRL_EXT_SCAN
157 blc_ll_initExtendedScanning_module();
158 #endif
159
160 #if CONFIG_B91_BLE_CTRL_PER_ADV_SYNC
161 blc_ll_initPeriodicAdvertisingSynchronization_module();
162 #endif
163
164 blc_ll_setAclConnMaxOctetsNumber(ACL_CONN_MAX_RX_OCTETS, ACL_MASTER_MAX_TX_OCTETS, ACL_SLAVE_MAX_TX_OCTETS);
165
166 /* all ACL connection share same RX FIFO */
167 if(blc_ll_initAclConnRxFifo(app_acl_rxfifo, ACL_RX_FIFO_SIZE, ACL_RX_FIFO_NUM) != BLE_SUCCESS) {
168 return INIT_FAILED;
169 }
170
171 /* ACL Master TX FIFO */
172 if(blc_ll_initAclConnMasterTxFifo(app_acl_mstTxfifo, ACL_MASTER_TX_FIFO_SIZE,
173 ACL_MASTER_TX_FIFO_NUM, CONFIG_B91_BLE_CTRL_MASTER_MAX_NUM) != BLE_SUCCESS)
174 {
175 return INIT_FAILED;
176 }
177
178 /* ACL Slave TX FIFO */
179 if(blc_ll_initAclConnSlaveTxFifo(app_acl_slvTxfifo, ACL_SLAVE_TX_FIFO_SIZE,
180 ACL_SLAVE_TX_FIFO_NUM, CONFIG_B91_BLE_CTRL_SLAVE_MAX_NUM) != BLE_SUCCESS) {
181 return INIT_FAILED;
182 }
183
184 blc_ll_ConfigLegacyAdvEnable_by_API_only(LEG_ADV_STRATEGY_2);
185 blc_ll_ConfigLegacyScanEnable_by_API_only(LEG_SCAN_STRATEGY_2);
186
187 blc_ll_setMaxConnectionNumber(CONFIG_B91_BLE_CTRL_MASTER_MAX_NUM, CONFIG_B91_BLE_CTRL_SLAVE_MAX_NUM);
188 blc_ll_setAclMasterConnectionInterval(CONFIG_B91_BLE_CTRL_CONNECTION_INTERVAL_IDX);
189 blc_ll_setCreateConnectionTimeout(CONFIG_B91_BLE_CTRL_CONNECTION_TIMEOUT_MS);
190
191 rf_set_power_level_index(CONFIG_B91_BLE_CTRL_RF_POWER);
192
193 blc_ll_initChannelSelectionAlgorithm_2_feature();
194 blc_ll_init2MPhyCodedPhy_feature();
195
196 /* HCI RX FIFO */
197 if(blc_ll_initHciRxFifo(app_hci_rxfifo, HCI_RX_FIFO_SIZE, HCI_RX_FIFO_NUM) != BLE_SUCCESS) {
198 return INIT_FAILED;
199 }
200
201 /* HCI TX FIFO */
202 if(blc_ll_initHciTxFifo(app_hci_txfifo, HCI_TX_FIFO_SIZE, HCI_TX_FIFO_NUM) != BLE_SUCCESS) {
203 return INIT_FAILED;
204 }
205
206 /* HCI RX ACL FIFO */
207 if(blc_ll_initHciAclDataFifo(app_hci_rxAclfifo, HCI_RX_ACL_FIFO_SIZE, HCI_RX_ACL_FIFO_NUM) != BLE_SUCCESS) {
208 return INIT_FAILED;
209 }
210
211 /* HCI Data && Event */
212 blc_hci_registerControllerDataHandler (blc_hci_sendACLData2Host);
213 blc_hci_registerControllerEventHandler(blc_hci_send_data); //controller hci event to host all processed in this func
214
215 /* bluetooth event */
216 blc_hci_setEventMask_cmd (HCI_EVT_MASK_DISCONNECTION_COMPLETE);
217
218 /* bluetooth low energy(LE) event, all enable */
219 blc_hci_le_setEventMask_cmd(0xFFFFFFFF);
220 blc_hci_le_setEventMask_2_cmd(0x7FFFFFFF);
221
222 u8 check_status = blc_controller_check_appBufferInitialization();
223 if(check_status != BLE_SUCCESS) {
224 return INIT_FAILED;
225 }
226
227 /* HCI configuration */
228 blc_register_hci_handler(prx, ptx);
229
230 return INIT_OK;
231 }
232