• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef BLT_COMMON_H_
19 #define BLT_COMMON_H_
20 
21 #include "drivers.h"
22 #include "vendor/common/user_config.h"
23 
24 /**
25  * @brief	512 K Flash MAC address and calibration data area
26  */
27 #if (MCU_CORE_TYPE == MCU_CORE_9518)
28 #define CFG_ADR_MAC_512K_FLASH         0x7F000
29 #define CFG_ADR_CALIBRATION_512K_FLASH 0x7E000
30 #else
31 #define CFG_ADR_MAC_512K_FLASH         0x76000
32 #define CFG_ADR_CALIBRATION_512K_FLASH 0x77000
33 #endif
34 
35 /**
36  * @brief	1 M Flash MAC address and calibration data area
37  */
38 #define CFG_ADR_MAC_1M_FLASH         0xFF000
39 #define CFG_ADR_CALIBRATION_1M_FLASH 0xFE000
40 
41 /**
42  * @brief	2 M Flash MAC address and calibration data area
43  */
44 #define CFG_ADR_MAC_2M_FLASH         0x1FF000
45 #define CFG_ADR_CALIBRATION_2M_FLASH 0x1FE000
46 
47 /**
48  * @brief	Flash size type supported by this SDK
49  */
50 #define FLASH_SIZE_512K 0x80000
51 #define FLASH_SIZE_1M   0x100000
52 #define FLASH_SIZE_2M   0x200000
53 
54 /**
55  * @brief	Flash size default configuration(user can change in app_config.h)
56  */
57 #if (MCU_CORE_TYPE == MCU_CORE_9518)
58 #ifndef FLASH_SIZE_CONFIG
59 #define FLASH_SIZE_CONFIG FLASH_SIZE_1M
60 #endif
61 #else
62 #ifndef FLASH_SIZE_CONFIG
63 #define FLASH_SIZE_CONFIG FLASH_SIZE_512K
64 #endif
65 #endif
66 
67 /**
68  * @brief	Flash using area default Configuration, user can change some of them in app_config.h
69  * 			CFG_ADR_MAC:  		  BLE MAC address stored in flash, can not change this value
70  * 			CFG_ADR_CALIBRATION:  some calibration data stored in flash, can not change this value
71  * 			FLASH_ADR_SMP_PAIRING & FLASH_SMP_PAIRING_MAX_SIZE:
72  * 									If Slave or Master SMP enable, use 16K flash for SMP pairing information storage.
73  * 									First 8K is for normal use,
74  *                                  second 8K is a backup to guarantee SMP information never lose.
75  * 									use API blc_smp_configPairingSecurityInfoStorageAddressAndSize
76  *                                  (FLASH_ADR_SMP_PAIRING, FLASH_SMP_PAIRING_MAX_SIZE) to set the two value.
77  * 			FLASH_ADR_CUSTOM_PAIRING & FLASH_CUSTOM_PAIRING_MAX_SIZE:
78  * 									If master role is used but master SMP not used,
79  *                                  use this flash area to store bonding slave information for custom pair.
80  * 			FLASH_SDP_ATT_ADRRESS & FLASH_SDP_ATT_MAX_SIZE
81  * 									If master role use service discovery,
82  *                                  use this flash area to store some critical information of peer GATT server.
83 
84  */
85 #if (FLASH_SIZE_CONFIG == FLASH_SIZE_512K)
86 
87 /* MAC and calibration data area */
88 #define CFG_ADR_MAC         CFG_ADR_MAC_512K_FLASH          // can not change this value
89 #define CFG_ADR_CALIBRATION CFG_ADR_CALIBRATION_512K_FLASH  // can not change this value
90 
91 #if (MCU_CORE_TYPE == MCU_CORE_9518)
92 
93 #else
94 /* SMP paring and key information area */
95 #ifndef FLASH_ADR_SMP_PAIRING
96 #define FLASH_ADR_SMP_PAIRING 0x78000
97 #endif
98 
99 #ifndef FLASH_SMP_PAIRING_MAX_SIZE
100 #define FLASH_SMP_PAIRING_MAX_SIZE (2 * 4096)  // normal 8K + backup 8K = 16K
101 #endif
102 
103 /* bonding slave information for custom pair area */
104 #ifndef FLASH_ADR_CUSTOM_PAIRING
105 #define FLASH_ADR_CUSTOM_PAIRING 0x7C000
106 #endif
107 
108 #ifndef FLASH_CUSTOM_PAIRING_MAX_SIZE
109 #define FLASH_CUSTOM_PAIRING_MAX_SIZE 4096
110 #endif
111 
112 /* bonding slave GATT service critical information area */
113 #ifndef FLASH_SDP_ATT_ADRRESS
114 #define FLASH_SDP_ATT_ADRRESS 0x7D000  // for master: store peer slave device's ATT handle
115 #endif
116 
117 #ifndef FLASH_SDP_ATT_MAX_SIZE
118 #define FLASH_SDP_ATT_MAX_SIZE (2 * 4096)  // 8K flash for ATT HANLDE storage
119 #endif
120 #endif
121 
122 #elif (FLASH_SIZE_CONFIG == FLASH_SIZE_1M)
123 
124 /* MAC and calibration data area */
125 #define CFG_ADR_MAC         CFG_ADR_MAC_1M_FLASH          // can not change this value
126 #define CFG_ADR_CALIBRATION CFG_ADR_CALIBRATION_1M_FLASH  // can not change this value
127 
128 /* SMP paring and key information area */
129 #ifndef FLASH_ADR_SMP_PAIRING
130 #define FLASH_ADR_SMP_PAIRING 0xFA000
131 #endif
132 
133 #ifndef FLASH_SMP_PAIRING_MAX_SIZE
134 #define FLASH_SMP_PAIRING_MAX_SIZE (2 * 4096)  // normal 8K + backup 8K = 16K
135 #endif
136 
137 /* bonding slave information for custom pair area */
138 #ifndef FLASH_ADR_CUSTOM_PAIRING
139 #define FLASH_ADR_CUSTOM_PAIRING 0xF8000
140 #endif
141 
142 #ifndef FLASH_CUSTOM_PAIRING_MAX_SIZE
143 #define FLASH_CUSTOM_PAIRING_MAX_SIZE 4096
144 #endif
145 
146 /* bonding slave GATT service critical information area */
147 #ifndef FLASH_SDP_ATT_ADRRESS
148 #define FLASH_SDP_ATT_ADRRESS 0xF6000  // for master: store peer slave device's ATT handle
149 #endif
150 
151 #ifndef FLASH_SDP_ATT_MAX_SIZE
152 #define FLASH_SDP_ATT_MAX_SIZE (2 * 4096)  // 8K flash for ATT HANLDE storage
153 #endif
154 
155 #elif (FLASH_SIZE_CONFIG == FLASH_SIZE_2M)
156 /* MAC and calibration data area */
157 #define CFG_ADR_MAC         CFG_ADR_MAC_2M_FLASH          // can not change this value
158 #define CFG_ADR_CALIBRATION CFG_ADR_CALIBRATION_2M_FLASH  // can not change this value
159 
160 /* SMP paring and key information area */
161 #ifndef FLASH_ADR_SMP_PAIRING
162 #define FLASH_ADR_SMP_PAIRING 0x1FA000
163 #endif
164 
165 #ifndef FLASH_SMP_PAIRING_MAX_SIZE
166 #define FLASH_SMP_PAIRING_MAX_SIZE (2 * 4096) / / normal 8K + backup 8K = 16K
167 #endif
168 
169 /* bonding slave information for custom pair area */
170 #ifndef FLASH_ADR_CUSTOM_PAIRING
171 #define FLASH_ADR_CUSTOM_PAIRING 0x1F8000
172 #endif
173 
174 #ifndef FLASH_CUSTOM_PAIRING_MAX_SIZE
175 #define FLASH_CUSTOM_PAIRING_MAX_SIZE 4096
176 #endif
177 
178 /* bonding slave GATT service critical information area */
179 #ifndef FLASH_SDP_ATT_ADRRESS
180 #define FLASH_SDP_ATT_ADRRESS 0x1F6000  // for master: store peer slave device's ATT handle
181 #endif
182 
183 #ifndef FLASH_SDP_ATT_MAX_SIZE
184 #define FLASH_SDP_ATT_MAX_SIZE (2 * 4096)  // 8K flash for ATT HANLDE storage
185 #endif
186 #endif
187 
188 /** Calibration Information FLash Address Offset of  CFG_ADR_CALIBRATION_xx_FLASH ***/
189 #define CALIB_OFFSET_CAP_INFO 0x00
190 #define CALIB_OFFSET_TP_INFO  0x40
191 
192 #define CALIB_OFFSET_ADC_VREF 0xC0
193 
194 #define CALIB_OFFSET_FIRMWARE_SIGNKEY 0x180
195 
196 extern const u8 vendor_OtaUUID[];
197 extern u32 flash_sector_mac_address;
198 extern u32 flash_sector_calibration;
199 
200 /**
201  * @brief		This function is used to enable the external crystal capacitor
202  * @param[in]	en - enable the external crystal capacitor
203  * @return      none
204  */
blc_app_setExternalCrystalCapEnable(u8 en)205 static inline void blc_app_setExternalCrystalCapEnable(u8 en)
206 {
207     blt_miscParam.ext_cap_en = en;
208     analog_write(0x8a, analog_read(0x8a) | 0x80);  // disable internal cap
209 }
210 
211 /**
212  * @brief		This function is used to load customized parameters from flash sector for application
213  * @param[in]	none
214  * @return      none
215  */
blc_app_loadCustomizedParameters(void)216 static inline void blc_app_loadCustomizedParameters(void)
217 {
218     if (!blt_miscParam.ext_cap_en) {
219         // customize freq_offset adjust cap value, if not customized, default ana_81 is 0xd0
220         // for 512K Flash, flash_sector_calibration equals to 0x77000
221         // for 1M  Flash, flash_sector_calibration equals to 0xFE000
222         if (flash_sector_calibration) {
223             u8 cap_frqoft;
224             flash_read_page(flash_sector_calibration + CALIB_OFFSET_CAP_INFO, 1, &cap_frqoft);
225 
226             if (cap_frqoft != 0xff) {
227                 analog_write(0x8A, (analog_read(0x8A) & 0xc0) | (cap_frqoft & 0x3f));
228             }
229         }
230     }
231 }
232 
233 /**
234  * @brief		This function can automatically recognize the flash size,
235  * 				and the system selects different customized sector according
236  * 				to different sizes.
237  * @param[in]	none
238  * @return      none
239  */
240 void blc_readFlashSize_autoConfigCustomFlashSector(void);
241 
242 /**
243  * @brief		This function is used to initialize the MAC address
244  * @param[in]	flash_addr - flash address for MAC address
245  * @param[in]	mac_public - public address
246  * @param[in]	mac_random_static - random static MAC address
247  * @return      none
248  */
249 void blc_initMacAddress(int flash_addr, u8 *mac_public, u8 *mac_random_static);
250 
251 #endif /* BLT_COMMON_H_ */
252