xref: /btstack/chipset/bcm/btstack_chipset_bcm.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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  *  bt_control_bcm.c
39  *
40  *  Adapter to use Broadcom-based chipsets with BTstack
41  */
42 
43 
44 #include "btstack_config.h"
45 
46 #include <stddef.h>   /* NULL */
47 #include <stdio.h>
48 #include <string.h>   /* memcpy */
49 
50 #include "btstack_control.h"
51 #include "btstack_debug.h"
52 #include "btstack_chipset_bcm.h"
53 
54 // actual init script provided by separate bt_firmware_image.c from WICED SDK
55 extern const uint8_t brcm_patchram_buf[];
56 extern const int     brcm_patch_ram_length;
57 extern const char    brcm_patch_version[];
58 
59 //
60 static uint32_t init_script_offset;
61 static int send_download_command;
62 
63 static void chipset_init(const void * config){
64     log_info("Broadcom init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
65     init_script_offset = 0;
66     send_download_command = 1;
67 }
68 
69 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c
70 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
71     hci_cmd_buffer[0] = 0x18;
72     hci_cmd_buffer[1] = 0xfc;
73     hci_cmd_buffer[2] = 0x06;
74     hci_cmd_buffer[3] = 0x00;
75     hci_cmd_buffer[4] = 0x00;
76     little_endian_store_32(hci_cmd_buffer, 5, baudrate);
77 }
78 
79 // @note: bd addr has to be set after sending init script (it might just get re-set)
80 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){
81     hci_cmd_buffer[0] = 0x01;
82     hci_cmd_buffer[1] = 0xfc;
83     hci_cmd_buffer[2] = 0x06;
84     reverse_bd_addr(addr, &hci_cmd_buffer[3]);
85 }
86 
87 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
88     // send download firmware command
89     if (send_download_command){
90         send_download_command = 0;
91         hci_cmd_buffer[0] = 0x2e;
92         hci_cmd_buffer[1] = 0xfc;
93         hci_cmd_buffer[2] = 0x00;
94         return BTSTACK_CHIPSET_VALID_COMMAND;
95     }
96 
97     if (init_script_offset >= brcm_patch_ram_length) {
98         return BTSTACK_CHIPSET_DONE;
99     }
100 
101     int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2];
102     memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len);
103     init_script_offset += cmd_len;
104     return BTSTACK_CHIPSET_VALID_COMMAND;
105 }
106 
107 
108 static const btstack_chipset_t btstack_chipset_bcm = {
109     "BCM",
110     chipset_init,
111     chipset_next_command,
112     chipset_set_baudrate_command,
113     chipset_set_bd_addr_command,
114 };
115 
116 // MARK: public API
117 const btstack_chipset_t * btstack_chipset_bcm_instance(void){
118     return &btstack_chipset_bcm;
119 }
120