1 /*
2 * Copyright (C) 2015 BlueKitchen GmbH
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 BLUEKITCHEN GMBH 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 BLUEKITCHEN
24 * GMBH 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
34 * [email protected]
35 *
36 */
37
38 #define BTSTACK_FILE__ "main.c"
39
40 #include "btstack.h"
41 #include "btstack_chipset_bcm.h"
42 #include "btstack_chipset_bcm_download_firmware.h"
43 #include "btstack_run_loop_wiced.h"
44 #include "btstack_link_key_db_wiced_dct.h"
45 #include "le_device_db_wiced_dct.h"
46 #include "btstack_uart_slip_wrapper.h"
47
48 #include "generated_mac_address.txt"
49
50 #include "platform_bluetooth.h"
51 #include "wiced.h"
52 #include "platform/wwd_platform_interface.h"
53
54 extern int btstack_main(int argc, char ** argv);
55 extern const btstack_uart_block_t * btstack_uart_block_wiced_instance(void);
56
57 static void phase2(int status);
58
59 // see generated_mac_address.txt - "macaddr=02:0A:F7:3d:76:be"
60 static const char * wifi_mac_address = NVRAM_GENERATED_MAC_ADDRESS;
61
62 static btstack_packet_callback_registration_t hci_event_callback_registration;
63
64 static btstack_uart_config_t uart_config;
65
66 static const hci_transport_config_uart_t transport_config = {
67 HCI_TRANSPORT_CONFIG_UART,
68 115200,
69 1000000, // 200000+ didn't work reliably
70 0,
71 NULL,
72 };
73
74
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)75 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
76 if (packet_type != HCI_EVENT_PACKET) return;
77 if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
78 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
79 printf("BTstack up and running.\n");
80 }
81
application_start(void)82 void application_start(void){
83
84 /* Initialise the WICED device without WLAN */
85 wiced_core_init();
86
87 /* 32 kHz clock also needed for Bluetooth */
88 host_platform_init_wlan_powersave_clock();
89
90 printf("BTstack on WICED\n");
91
92 #if 0
93 // init GPIOs D0-D5 for debugging - not used
94 wiced_gpio_init(D0, OUTPUT_PUSH_PULL);
95 wiced_gpio_init(D1, OUTPUT_PUSH_PULL);
96 wiced_gpio_init(D2, OUTPUT_PUSH_PULL);
97 wiced_gpio_init(D3, OUTPUT_PUSH_PULL);
98 wiced_gpio_init(D4, OUTPUT_PUSH_PULL);
99 wiced_gpio_init(D5, OUTPUT_PUSH_PULL);
100
101 wiced_gpio_output_low(D0);
102 wiced_gpio_output_low(D1);
103 wiced_gpio_output_low(D2);
104 wiced_gpio_output_low(D3);
105 wiced_gpio_output_low(D4);
106 wiced_gpio_output_low(D5);
107 #endif
108
109 // start with BTstack init - especially configure HCI Transport
110 btstack_memory_init();
111
112 // enable full log output while porting
113 // hci_dump_open(NULL, HCI_DUMP_STDOUT);
114
115 // setup run loop
116 btstack_run_loop_init(btstack_run_loop_wiced_get_instance());
117
118 // get BCM chipset driver
119 const btstack_chipset_t * chipset = btstack_chipset_bcm_instance();
120 chipset->init(&transport_config);
121
122 // setup uart driver
123 const btstack_uart_block_t * uart_block_driver = btstack_uart_block_wiced_instance();
124 const btstack_uart_t * uart_slip_driver = btstack_uart_slip_wrapper_instance(uart_block_driver);
125
126 // extract UART config from transport config
127 uart_config.baudrate = transport_config.baudrate_init;
128 uart_config.flowcontrol = transport_config.flowcontrol;
129 uart_config.device_name = transport_config.device_name;
130 uart_block_driver->init(&uart_config);
131
132 // init HCI
133 const hci_transport_t * transport = hci_transport_h5_instance(uart_slip_driver);
134 hci_init(transport, (void*) &transport_config);
135 hci_set_link_key_db(btstack_link_key_db_wiced_dct_instance());
136 hci_set_chipset(chipset);
137
138 // inform about BTstack state
139 hci_event_callback_registration.callback = &packet_handler;
140 hci_add_event_handler(&hci_event_callback_registration);
141
142 // use WIFI Mac address + 1 for Bluetooth
143 bd_addr_t dummy = { 1,2,3,4,5,6};
144 sscanf_bd_addr(&wifi_mac_address[8], dummy);
145 dummy[5]++;
146 hci_set_bd_addr(dummy);
147
148 // setup le device db storage -- not needed if used LE-only (-> start address == 0)
149 le_device_db_wiced_dct_set_start_address(btstack_link_key_db_wiced_dct_get_storage_size());
150 le_device_db_dump();
151
152 // phase #1 download firmware
153 printf("Phase 1: Download firmware\n");
154
155 // phase #2 start main app
156 btstack_chipset_bcm_download_firmware(uart_block_driver, transport_config.baudrate_main, &phase2);
157
158 // go
159 btstack_run_loop_execute();
160 }
161
phase2(int status)162 static void phase2(int status){
163
164
165 if (status){
166 printf("Download firmware failed\n");
167 return;
168 }
169
170 printf("Phase 2: Main app\n");
171
172 // setup app
173 btstack_main(0, NULL);
174 }
175