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 #include "blt_common.h"
19 #include "drivers.h"
20 #include "stack/ble/ble.h"
21 #include "tl_common.h"
22
23 const u8 vendor_OtaUUID[16] = WRAPPING_BRACES(TELINK_SPP_DATA_OTA);
24
25 #if (MCU_CORE_TYPE == MCU_CORE_9518)
26 /* default flash is 1M
27 * for 1M Flash, flash_sector_mac_address equals to 0xFF000
28 * for 2M Flash, flash_sector_mac_address equals to 0x1FF000 */
29 _attribute_ble_data_retention_ u32 flash_sector_mac_address = CFG_ADR_MAC_1M_FLASH;
30 _attribute_ble_data_retention_ u32 flash_sector_calibration = CFG_ADR_CALIBRATION_1M_FLASH;
31 #else
32 /* default flash is 512K
33 * for 512K Flash, flash_sector_mac_address equals to 0x76000
34 * for 1M Flash, flash_sector_mac_address equals to 0xFF000
35 * for 2M Flash, flash_sector_mac_address equals to 0x1FF000 */
36 _attribute_ble_data_retention_ u32 flash_sector_mac_address = CFG_ADR_MAC_512K_FLASH;
37 _attribute_ble_data_retention_ u32 flash_sector_calibration = CFG_ADR_CALIBRATION_512K_FLASH;
38 #endif
39
40 /**
41 * @brief This function can automatically recognize the flash size,
42 * and the system selects different customized sector according
43 * to different sizes.
44 * @param[in] none
45 * @return none
46 */
blc_readFlashSize_autoConfigCustomFlashSector(void)47 _attribute_no_inline_ void blc_readFlashSize_autoConfigCustomFlashSector(void)
48 {
49 u8 temp_buf[4];
50 flash_read_mid(temp_buf);
51 u8 flash_cap = temp_buf[2];
52
53 if (flash_cap == FLASH_CAPACITY_512K) {
54 flash_sector_mac_address = CFG_ADR_MAC_512K_FLASH;
55 flash_sector_calibration = CFG_ADR_CALIBRATION_512K_FLASH;
56 } else if (flash_cap == FLASH_CAPACITY_1M) {
57 flash_sector_mac_address = CFG_ADR_MAC_1M_FLASH;
58 flash_sector_calibration = CFG_ADR_CALIBRATION_1M_FLASH;
59 } else if (flash_cap == FLASH_CAPACITY_2M) {
60 flash_sector_mac_address = CFG_ADR_MAC_2M_FLASH;
61 flash_sector_calibration = CFG_ADR_CALIBRATION_2M_FLASH;
62 } else {
63 // This SDK do not support flash size other than 512K/1M/2M
64 // If code stop here, please check your Flash
65 while (1) {
66 }
67 }
68
69 flash_set_capacity(flash_cap);
70 }
71
72 /*
73 * Kite: VVWWXX38C1A4YYZZ
74 * Vulture: VVWWXXD119C4YYZZ
75 * Eagle: VVWWXX
76 * public_mac:
77 * Kite : VVWWXX 38C1A4
78 * Vulture : VVWWXX D119C4
79 * Eagle : VVWWXX
80 * random_static_mac: VVWWXXYYZZ C0
81 */
82 /**
83 * @brief This function is used to initialize the MAC address
84 * @param[in] flash_addr - flash address for MAC address
85 * @param[in] mac_public - public address
86 * @param[in] mac_random_static - random static MAC address
87 * @return none
88 */
blc_initMacAddress(int flash_addr,u8 * mac_public,u8 * mac_random_static)89 _attribute_no_inline_ void blc_initMacAddress(int flash_addr, u8 *mac_public, u8 *mac_random_static)
90 {
91 if (flash_sector_mac_address == 0) {
92 return;
93 }
94
95 u8 mac_read[8];
96 flash_read_page(flash_addr, 8, mac_read);
97
98 u8 value_rand[5];
99 generateRandomNum(5, value_rand);
100
101 u8 ff_six_byte[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
102 if (memcmp(mac_read, ff_six_byte, 6)) {
103 memcpy(mac_public, mac_read, 6); // copy public address from flash
104 } else { // no public address on flash
105 mac_public[0] = value_rand[0];
106 mac_public[1] = value_rand[1];
107 mac_public[2] = value_rand[2];
108 mac_public[3] = 0x38; // company id: 0xA4C138
109 mac_public[4] = 0xC1;
110 mac_public[5] = 0xA4;
111
112 flash_write_page(flash_addr, 6, mac_public);
113 }
114
115 mac_random_static[0] = mac_public[0];
116 mac_random_static[1] = mac_public[1];
117 mac_random_static[2] = mac_public[2];
118 mac_random_static[5] = 0xC0; // for random static
119
120 u16 high_2_byte = (mac_read[6] | (mac_read[7] << 8));
121 if (high_2_byte != 0xFFFF) {
122 memcpy((u8 *)(mac_random_static + 3), (u8 *)(mac_read + 6), 2);
123 } else {
124 mac_random_static[3] = value_rand[3];
125 mac_random_static[4] = value_rand[4];
126
127 flash_write_page(flash_addr + 6, 2, (u8 *)(mac_random_static + 3));
128 }
129 }
130