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 #define BTSTACK_FILE__ "btstack_chipset_cc256x.c" 38 39 /* 40 * btstack_chipset_cc256x.c 41 * 42 * Adapter to use cc256x-based chipsets with BTstack 43 * 44 * Handles init script (a.k.a. Service Patch) 45 * Allows for non-standard UART baud rate 46 * Allows to configure transmit power 47 * Allows to activate eHCILL deep sleep mode 48 * 49 * Issues with mspgcc LTS: 50 * - 20 bit support is not there yet -> .text cannot get bigger than 48 kb 51 * - arrays cannot have more than 32k entries 52 * 53 * workarounds: 54 * - store init script in .fartext and use assembly code to read from there 55 * - split into two arrays 56 * 57 * Issues with AVR 58 * - Harvard architecture doesn't allow to store init script directly -> use avr-libc helpers 59 * 60 * Documentation for TI VS CC256x commands: http://processors.wiki.ti.com/index.php/CC256x_VS_HCI_Commands 61 * 62 */ 63 64 #include "btstack_config.h" 65 #include "btstack_chipset_cc256x.h" 66 #include "btstack_debug.h" 67 #include "hci.h" 68 69 #include <stddef.h> /* NULL */ 70 #include <stdio.h> 71 #include <string.h> /* memcpy */ 72 73 // assert outgoing and incoming hci packet buffers can hold max hci command resp. event packet 74 #if HCI_OUTGOING_PACKET_BUFFER_SIZE < (HCI_CMD_HEADER_SIZE + 255) 75 #error "HCI_OUTGOING_PACKET_BUFFER_SIZE to small. Outgoing HCI packet buffer to small for largest HCI Command packet. Please set HCI_ACL_PAYLOAD_SIZE to 258 or higher." 76 #endif 77 #if HCI_INCOMING_PACKET_BUFFER_SIZE < (HCI_EVENT_HEADER_SIZE_HEADER_SIZE + 255) 78 #error "HCI_INCOMING_PACKET_BUFFER_SIZE to small. Incoming HCI packet buffer to small for largest HCI Event packet. Please set HCI_ACL_PAYLOAD_SIZE to 257 or higher." 79 #endif 80 81 #if defined(__GNUC__) && defined(__MSP430X__) && (__MSP430X__ > 0) 82 #include "hal_compat.h" 83 #endif 84 85 #ifdef __AVR__ 86 #include <avr/pgmspace.h> 87 #endif 88 89 #include "btstack_control.h" 90 91 92 // default init script provided by separate .c file 93 extern const uint8_t cc256x_init_script[]; 94 extern const uint32_t cc256x_init_script_size; 95 96 // custom init script set by btstack_chipset_cc256x_set_init_script 97 // used to select init scripts before each power up 98 static const uint8_t * custom_init_script; 99 static uint32_t custom_init_script_size; 100 101 // init script to use: either cc256x_init_script or custom_init_script 102 static const uint8_t * init_script; 103 static uint32_t init_script_size; 104 105 // power in db - set by btstack_chipset_cc256x_set_power 106 static int16_t init_power_in_dB = 13; // 13 dBm 107 108 // upload position 109 static uint32_t init_script_offset = 0; 110 111 // support for SCO over HCI 112 #ifdef ENABLE_SCO_OVER_HCI 113 static int init_send_route_sco_over_hci = 0; 114 static const uint8_t hci_route_sco_over_hci[] = { 115 #if 1 116 // Follow recommendation from https://e2e.ti.com/support/wireless_connectivity/bluetooth_cc256x/f/660/t/397004 117 // route SCO over HCI (connection type=1, tx buffer size = 120, tx buffer max latency= 720, accept packets with CRC Error 118 0x10, 0xfe, 0x05, 0x01, 0x78, 0xd0, 0x02, 0x01, 119 #else 120 // Configure SCO via I2S interface - 256 kbps 121 // Send_HCI_VS_Write_CODEC_Config 0xFD06, 122 0x06, 0xfd, 123 // len 124 34, 125 //3072, - clock rate 3072000 hz 126 0x00, 0x01, 127 // 0x00 - clock direction: output = master 128 0x00, 129 // 8000, framesync frequency in hz 130 0x40, 0x1f, 0x00, 0x00, 131 // 0x0001, framesync duty cycle 132 0x01, 0x00, 133 // 1, framesync edge 134 1, 135 // 0x00, framesync polarity 136 0x00, 137 // 0x00, RESERVED 138 0x00, 139 // 16, channel 1 out size 140 8, 0, 141 // 0x0001, channel 1 out offset 142 0x01, 0x00, 143 // 1, channel 1 out edge 144 1, 145 // 16, channel 1 in size 146 8, 0, 147 // 0x0001, channel 1 in offset 148 0x01, 0x00, 149 // 0, channel 1 in edge 150 0, 151 // 0x00, RESERVED 152 0x00, 153 // 16, channel 2 out size 154 8, 0, 155 // 17, channel 2 out offset 156 9, 0, 157 // 0x01, channel 2 out edge 158 0x01, 159 // 16, channel 2 in size 160 8, 0, 161 // 17, channel 2 in offset 162 9, 0, 163 // 0x00, channel 2 in edge 164 0x00, 165 // 0x0001, RESERVED 166 0x00 167 #endif 168 }; 169 #endif 170 171 static void chipset_init(const void * config){ 172 init_script_offset = 0; 173 #if defined(__GNUC__) && defined(__MSP430X__) && (__MSP430X__ > 0) 174 // On MSP430, custom init script is not supported 175 init_script_size = cc256x_init_script_size; 176 #else 177 if (custom_init_script){ 178 log_info("cc256x: using custom init script"); 179 init_script = custom_init_script; 180 init_script_size = custom_init_script_size; 181 } else { 182 log_info("cc256x: using default init script"); 183 init_script = cc256x_init_script; 184 init_script_size = cc256x_init_script_size; 185 } 186 #endif 187 #ifdef ENABLE_SCO_OVER_HCI 188 init_send_route_sco_over_hci = 1; 189 #endif 190 } 191 192 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 193 hci_cmd_buffer[0] = 0x36; 194 hci_cmd_buffer[1] = 0xFF; 195 hci_cmd_buffer[2] = 0x04; 196 hci_cmd_buffer[3] = baudrate & 0xff; 197 hci_cmd_buffer[4] = (baudrate >> 8) & 0xff; 198 hci_cmd_buffer[5] = (baudrate >> 16) & 0xff; 199 hci_cmd_buffer[6] = 0; 200 } 201 202 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 203 hci_cmd_buffer[0] = 0x06; 204 hci_cmd_buffer[1] = 0xFC; 205 hci_cmd_buffer[2] = 0x06; 206 reverse_bd_addr(addr, &hci_cmd_buffer[3]); 207 } 208 209 // Output Power control from: http://e2e.ti.com/support/low_power_rf/f/660/p/134853/484767.aspx 210 #define NUM_POWER_LEVELS 16 211 #define DB_MIN_LEVEL -35 212 #define DB_PER_LEVEL 5 213 #define DB_DYNAMIC_RANGE 30 214 215 static int get_max_power_for_modulation_type(int type){ 216 // a) limit max output power 217 int power_db; 218 switch (type){ 219 case 0: // GFSK 220 power_db = 12; 221 break; 222 default: // EDRx 223 power_db = 10; 224 break; 225 } 226 if (power_db > init_power_in_dB) { 227 power_db = init_power_in_dB; 228 } 229 return power_db; 230 } 231 232 static int get_highest_level_for_given_power(int power_db, int recommended_db){ 233 int i = NUM_POWER_LEVELS-1; 234 while (i) { 235 if (power_db <= recommended_db) { 236 return i; 237 } 238 power_db -= DB_PER_LEVEL; 239 i--; 240 } 241 return 0; 242 } 243 244 static void update_set_power_vector(uint8_t *hci_cmd_buffer){ 245 int i; 246 int modulation_type = hci_cmd_buffer[3]; 247 int power_db = get_max_power_for_modulation_type(modulation_type); 248 int dynamic_range = 0; 249 250 // f) don't touch level 0 251 for ( i = (NUM_POWER_LEVELS-1) ; i >= 1 ; i--){ 252 253 #ifdef ENABLE_BLE 254 // level 1 is BLE transmit power for GFSK 255 if (i == 1 && modulation_type == 0) { 256 hci_cmd_buffer[4+1] = 2 * get_max_power_for_modulation_type(modulation_type); 257 // as level 0 isn't set, we're done 258 continue; 259 } 260 #endif 261 hci_cmd_buffer[4+i] = 2 * power_db; 262 263 if (dynamic_range + DB_PER_LEVEL > DB_DYNAMIC_RANGE) continue; // e) 264 265 power_db -= DB_PER_LEVEL; // d) 266 dynamic_range += DB_PER_LEVEL; 267 268 if (power_db > DB_MIN_LEVEL) continue; 269 270 power_db = DB_MIN_LEVEL; // b) 271 } 272 } 273 274 // max permitted power for class 2 devices: 4 dBm 275 static void update_set_class2_single_power(uint8_t * hci_cmd_buffer){ 276 const int max_power_class_2 = 4; 277 int i = 0; 278 for (i=0;i<3;i++){ 279 hci_cmd_buffer[3+i] = get_highest_level_for_given_power(get_max_power_for_modulation_type(i), max_power_class_2); 280 } 281 } 282 283 // eHCILL activate from http://e2e.ti.com/support/low_power_rf/f/660/p/134855/484776.aspx 284 static void update_sleep_mode_configurations(uint8_t * hci_cmd_buffer){ 285 #ifdef ENABLE_EHCILL 286 hci_cmd_buffer[4] = 1; 287 #else 288 hci_cmd_buffer[4] = 0; 289 #endif 290 } 291 292 static void update_init_script_command(uint8_t *hci_cmd_buffer){ 293 294 uint16_t opcode = hci_cmd_buffer[0] | (hci_cmd_buffer[1] << 8); 295 296 switch (opcode){ 297 case 0xFD87: 298 update_set_class2_single_power(hci_cmd_buffer); 299 break; 300 case 0xFD82: 301 update_set_power_vector(hci_cmd_buffer); 302 break; 303 case 0xFD0C: 304 update_sleep_mode_configurations(hci_cmd_buffer); 305 break; 306 default: 307 break; 308 } 309 } 310 311 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 312 if (init_script_offset >= init_script_size) { 313 314 #ifdef ENABLE_SCO_OVER_HCI 315 // append send route SCO over HCI if requested 316 if (init_send_route_sco_over_hci){ 317 init_send_route_sco_over_hci = 0; 318 memcpy(hci_cmd_buffer, hci_route_sco_over_hci, sizeof(hci_route_sco_over_hci)); 319 return BTSTACK_CHIPSET_VALID_COMMAND; 320 } 321 #endif 322 323 return BTSTACK_CHIPSET_DONE; 324 } 325 326 // extracted init script has 0x01 cmd packet type, but BTstack expects them without 327 init_script_offset++; 328 329 #if defined(__GNUC__) && defined(__MSP430X__) && (__MSP430X__ > 0) 330 331 // workaround: use FlashReadBlock with 32-bit integer and assume init script starts at 0x10000 332 uint32_t init_script_addr = 0x10000; 333 FlashReadBlock(&hci_cmd_buffer[0], init_script_addr + init_script_offset, 3); // cmd header 334 init_script_offset += 3; 335 int payload_len = hci_cmd_buffer[2]; 336 FlashReadBlock(&hci_cmd_buffer[3], init_script_addr + init_script_offset, payload_len); // cmd payload 337 338 #elif defined (__AVR__) 339 340 // workaround: use memcpy_P to access init script in lower 64 kB of flash 341 memcpy_P(&hci_cmd_buffer[0], &init_script[init_script_offset], 3); 342 init_script_offset += 3; 343 int payload_len = hci_cmd_buffer[2]; 344 memcpy_P(&hci_cmd_buffer[3], &init_script[init_script_offset], payload_len); 345 346 #else 347 348 // use memcpy with pointer 349 uint8_t * init_script_ptr = (uint8_t*) &init_script[0]; 350 memcpy(&hci_cmd_buffer[0], init_script_ptr + init_script_offset, 3); // cmd header 351 init_script_offset += 3; 352 int payload_len = hci_cmd_buffer[2]; 353 memcpy(&hci_cmd_buffer[3], init_script_ptr + init_script_offset, payload_len); // cmd payload 354 355 #endif 356 357 init_script_offset += payload_len; 358 359 // control power commands and ehcill 360 update_init_script_command(hci_cmd_buffer); 361 362 return BTSTACK_CHIPSET_VALID_COMMAND; 363 } 364 365 366 // MARK: public API 367 void btstack_chipset_cc256x_set_power(int16_t power_in_dB){ 368 init_power_in_dB = power_in_dB; 369 } 370 371 void btstack_chipset_cc256x_set_init_script(uint8_t * data, uint32_t size){ 372 custom_init_script = data; 373 custom_init_script_size = size; 374 } 375 376 static const btstack_chipset_t btstack_chipset_cc256x = { 377 "CC256x", 378 chipset_init, 379 chipset_next_command, 380 chipset_set_baudrate_command, 381 chipset_set_bd_addr_command, 382 }; 383 384 const btstack_chipset_t * btstack_chipset_cc256x_instance(void){ 385 return &btstack_chipset_cc256x; 386 } 387 388