1 /* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at [email protected] 34 * 35 */ 36 37 /* 38 * btstack_chipset_cc256x.c 39 * 40 * Adapter to use cc256x-based chipsets with BTstack 41 * 42 * Handles init script (a.k.a. Service Patch) 43 * Allows for non-standard UART baud rate 44 * Allows to configure transmit power 45 * Allows to activate eHCILL deep sleep mode 46 * 47 * Issues with mspgcc LTS: 48 * - 20 bit support is not there yet -> .text cannot get bigger than 48 kb 49 * - arrays cannot have more than 32k entries 50 * 51 * workarounds: 52 * - store init script in .fartext and use assembly code to read from there 53 * - split into two arrays 54 * 55 * Issues with AVR 56 * - Harvard architecture doesn't allow to store init script directly -> use avr-libc helpers 57 * 58 * Documentation for TI VS CC256x commands: http://processors.wiki.ti.com/index.php/CC256x_VS_HCI_Commands 59 * 60 */ 61 62 #include "btstack_config.h" 63 #include "btstack_chipset_cc256x.h" 64 65 #include <stddef.h> /* NULL */ 66 #include <stdio.h> 67 #include <string.h> /* memcpy */ 68 69 #if defined(__GNUC__) && defined(__MSP430X__) && (__MSP430X__ > 0) 70 #include "hal_compat.h" 71 #endif 72 73 #ifdef __AVR__ 74 #include <avr/pgmspace.h> 75 #endif 76 77 #include "btstack_control.h" 78 79 80 // actual init script provided by seperate .c file 81 extern const uint8_t cc256x_init_script[]; 82 extern const uint32_t cc256x_init_script_size; 83 84 // init script 85 static uint32_t init_script_offset = 0; 86 static int16_t init_power_in_dB = 13; // 13 dBm 87 88 // support for SCO over HCI 89 #ifdef ENABLE_SCO_OVER_HCI 90 static int init_send_route_sco_over_hci = 0; 91 // Follow recommendation from https://e2e.ti.com/support/wireless_connectivity/bluetooth_cc256x/f/660/t/397004 92 // route SCO over HCI (connection type=1, tx buffer size = 120, tx buffer max latency= 720, accept packets with CRC Error 93 static const uint8_t hci_route_sco_over_hci[] = { 94 0x10, 0xfe, 0x05, 0x01, 0x78, 0xd0, 0x02, 0x01, 95 }; 96 #endif 97 98 static void chipset_init(const void * config){ 99 init_script_offset = 0; 100 #ifdef ENABLE_SCO_OVER_HCI 101 init_send_route_sco_over_hci = 1; 102 #endif 103 } 104 105 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 106 hci_cmd_buffer[0] = 0x36; 107 hci_cmd_buffer[1] = 0xFF; 108 hci_cmd_buffer[2] = 0x04; 109 hci_cmd_buffer[3] = baudrate & 0xff; 110 hci_cmd_buffer[4] = (baudrate >> 8) & 0xff; 111 hci_cmd_buffer[5] = (baudrate >> 16) & 0xff; 112 hci_cmd_buffer[6] = 0; 113 } 114 115 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 116 hci_cmd_buffer[0] = 0x06; 117 hci_cmd_buffer[1] = 0xFC; 118 hci_cmd_buffer[2] = 0x06; 119 reverse_bd_addr(addr, &hci_cmd_buffer[3]); 120 } 121 122 // Output Power control from: http://e2e.ti.com/support/low_power_rf/f/660/p/134853/484767.aspx 123 #define NUM_POWER_LEVELS 16 124 #define DB_MIN_LEVEL -35 125 #define DB_PER_LEVEL 5 126 #define DB_DYNAMIC_RANGE 30 127 128 static int get_max_power_for_modulation_type(int type){ 129 // a) limit max output power 130 int power_db; 131 switch (type){ 132 case 0: // GFSK 133 power_db = 12; 134 break; 135 default: // EDRx 136 power_db = 10; 137 break; 138 } 139 if (power_db > init_power_in_dB) { 140 power_db = init_power_in_dB; 141 } 142 return power_db; 143 } 144 145 static int get_highest_level_for_given_power(int power_db, int recommended_db){ 146 int i = NUM_POWER_LEVELS-1; 147 while (i) { 148 if (power_db <= recommended_db) { 149 return i; 150 } 151 power_db -= DB_PER_LEVEL; 152 i--; 153 } 154 return 0; 155 } 156 157 static void update_set_power_vector(uint8_t *hci_cmd_buffer){ 158 int i; 159 int modulation_type = hci_cmd_buffer[3]; 160 int power_db = get_max_power_for_modulation_type(modulation_type); 161 int dynamic_range = 0; 162 163 // f) don't touch level 0 164 for ( i = (NUM_POWER_LEVELS-1) ; i >= 1 ; i--){ 165 166 #ifdef ENABLE_BLE 167 // level 1 is BLE transmit power for GFSK 168 if (i == 1 && modulation_type == 0) { 169 hci_cmd_buffer[4+1] = 2 * get_max_power_for_modulation_type(modulation_type); 170 // as level 0 isn't set, we're done 171 continue; 172 } 173 #endif 174 hci_cmd_buffer[4+i] = 2 * power_db; 175 176 if (dynamic_range + DB_PER_LEVEL > DB_DYNAMIC_RANGE) continue; // e) 177 178 power_db -= DB_PER_LEVEL; // d) 179 dynamic_range += DB_PER_LEVEL; 180 181 if (power_db > DB_MIN_LEVEL) continue; 182 183 power_db = DB_MIN_LEVEL; // b) 184 } 185 } 186 187 // max permitted power for class 2 devices: 4 dBm 188 static void update_set_class2_single_power(uint8_t * hci_cmd_buffer){ 189 const int max_power_class_2 = 4; 190 int i = 0; 191 for (i=0;i<3;i++){ 192 hci_cmd_buffer[3+i] = get_highest_level_for_given_power(get_max_power_for_modulation_type(i), max_power_class_2); 193 } 194 } 195 196 // eHCILL activate from http://e2e.ti.com/support/low_power_rf/f/660/p/134855/484776.aspx 197 static void update_sleep_mode_configurations(uint8_t * hci_cmd_buffer){ 198 #ifdef ENABLE_EHCILL 199 hci_cmd_buffer[4] = 1; 200 #else 201 hci_cmd_buffer[4] = 0; 202 #endif 203 } 204 205 static void update_init_script_command(uint8_t *hci_cmd_buffer){ 206 207 uint16_t opcode = hci_cmd_buffer[0] | (hci_cmd_buffer[1] << 8); 208 209 switch (opcode){ 210 case 0xFD87: 211 update_set_class2_single_power(hci_cmd_buffer); 212 break; 213 case 0xFD82: 214 update_set_power_vector(hci_cmd_buffer); 215 break; 216 case 0xFD0C: 217 update_sleep_mode_configurations(hci_cmd_buffer); 218 break; 219 default: 220 break; 221 } 222 } 223 224 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 225 if (init_script_offset >= cc256x_init_script_size) { 226 227 #ifdef ENABLE_SCO_OVER_HCI 228 // append send route SCO over HCI if requested 229 if (init_send_route_sco_over_hci){ 230 init_send_route_sco_over_hci = 0; 231 memcpy(hci_cmd_buffer, hci_route_sco_over_hci, sizeof(hci_route_sco_over_hci)); 232 return BTSTACK_CHIPSET_VALID_COMMAND; 233 } 234 #endif 235 236 return BTSTACK_CHIPSET_DONE; 237 } 238 239 // extracted init script has 0x01 cmd packet type, but BTstack expects them without 240 init_script_offset++; 241 242 #if defined(__GNUC__) && defined(__MSP430X__) && (__MSP430X__ > 0) 243 244 // workaround: use FlashReadBlock with 32-bit integer and assume init script starts at 0x10000 245 uint32_t init_script_addr = 0x10000; 246 FlashReadBlock(&hci_cmd_buffer[0], init_script_addr + init_script_offset, 3); // cmd header 247 init_script_offset += 3; 248 int payload_len = hci_cmd_buffer[2]; 249 FlashReadBlock(&hci_cmd_buffer[3], init_script_addr + init_script_offset, payload_len); // cmd payload 250 251 #elif defined (__AVR__) 252 253 // workaround: use memcpy_P to access init script in lower 64 kB of flash 254 memcpy_P(&hci_cmd_buffer[0], &cc256x_init_script[init_script_offset], 3); 255 init_script_offset += 3; 256 int payload_len = hci_cmd_buffer[2]; 257 memcpy_P(&hci_cmd_buffer[3], &cc256x_init_script[init_script_offset], payload_len); 258 259 #else 260 261 // use memcpy with pointer 262 uint8_t * init_script_ptr = (uint8_t*) &cc256x_init_script[0]; 263 memcpy(&hci_cmd_buffer[0], init_script_ptr + init_script_offset, 3); // cmd header 264 init_script_offset += 3; 265 int payload_len = hci_cmd_buffer[2]; 266 memcpy(&hci_cmd_buffer[3], init_script_ptr + init_script_offset, payload_len); // cmd payload 267 268 #endif 269 270 init_script_offset += payload_len; 271 272 // control power commands and ehcill 273 update_init_script_command(hci_cmd_buffer); 274 275 return BTSTACK_CHIPSET_VALID_COMMAND; 276 } 277 278 279 // MARK: public API 280 void btstack_chipset_cc256x_set_power(int16_t power_in_dB){ 281 init_power_in_dB = power_in_dB; 282 } 283 284 static const btstack_chipset_t btstack_chipset_cc256x = { 285 "CC256x", 286 chipset_init, 287 chipset_next_command, 288 chipset_set_baudrate_command, 289 chipset_set_bd_addr_command, 290 }; 291 292 const btstack_chipset_t * btstack_chipset_cc256x_instance(void){ 293 return &btstack_chipset_cc256x; 294 } 295 296