xref: /btstack/port/wiced-h4/main.c (revision 1b464e99afd70ddaf6b75be1ba7cc563a5f5dfd8)
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 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
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_run_loop_wiced.h"
43 #include "btstack_link_key_db_wiced_dct.h"
44 #include "le_device_db_wiced_dct.h"
45 
46 #include "generated_mac_address.txt"
47 
48 #include "platform_bluetooth.h"
49 #include "wiced.h"
50 #include "platform/wwd_platform_interface.h"
51 
52 const btstack_uart_block_t * btstack_uart_block_wiced_instance(void);
53 
54 // see generated_mac_address.txt - "macaddr=02:0A:F7:3d:76:be"
55 static const char * wifi_mac_address = NVRAM_GENERATED_MAC_ADDRESS;
56 
57 static btstack_packet_callback_registration_t hci_event_callback_registration;
58 
59 static const hci_transport_config_uart_t hci_transport_config_uart = {
60     HCI_TRANSPORT_CONFIG_UART,
61     115200,
62     1000000,    // 200000+ didn't work reliably
63     1,
64     NULL,
65 };
66 
67 extern int btstack_main(void);
68 
69 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
70     if (packet_type != HCI_EVENT_PACKET) return;
71     if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
72     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
73     printf("BTstack up and running.\n");
74 }
75 
76 void application_start(void){
77 
78     /* Initialise the WICED device without WLAN */
79     wiced_core_init();
80 
81     /* 32 kHz clock also needed for Bluetooth */
82     host_platform_init_wlan_powersave_clock();
83 
84     printf("BTstack on WICED\n");
85 
86 #if 0
87     // init GPIOs D0-D5 for debugging - not used
88     wiced_gpio_init(D0, OUTPUT_PUSH_PULL);
89     wiced_gpio_init(D1, OUTPUT_PUSH_PULL);
90     wiced_gpio_init(D2, OUTPUT_PUSH_PULL);
91     wiced_gpio_init(D3, OUTPUT_PUSH_PULL);
92     wiced_gpio_init(D4, OUTPUT_PUSH_PULL);
93     wiced_gpio_init(D5, OUTPUT_PUSH_PULL);
94 
95     wiced_gpio_output_low(D0);
96     wiced_gpio_output_low(D1);
97     wiced_gpio_output_low(D2);
98     wiced_gpio_output_low(D3);
99     wiced_gpio_output_low(D4);
100     wiced_gpio_output_low(D5);
101 #endif
102 
103     // start with BTstack init - especially configure HCI Transport
104     btstack_memory_init();
105     btstack_run_loop_init(btstack_run_loop_wiced_get_instance());
106 
107     // enable full log output while porting
108     // hci_dump_open(NULL, HCI_DUMP_STDOUT);
109 
110     // setup le device db storage -- not needed if used LE-only (-> start address == 0)
111     le_device_db_wiced_dct_set_start_address(btstack_link_key_db_wiced_dct_get_storage_size());
112     le_device_db_dump();
113 
114     // init HCI
115     const btstack_uart_block_t * uart_driver = btstack_uart_block_wiced_instance();
116     const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);
117     hci_init(transport, (void*) &hci_transport_config_uart);
118     hci_set_link_key_db(btstack_link_key_db_wiced_dct_instance());
119     hci_set_chipset(btstack_chipset_bcm_instance());
120 
121     // inform about BTstack state
122     hci_event_callback_registration.callback = &packet_handler;
123     hci_add_event_handler(&hci_event_callback_registration);
124 
125     // use WIFI Mac address + 1 for Bluetooth
126     bd_addr_t dummy = { 1,2,3,4,5,6};
127     sscanf_bd_addr(&wifi_mac_address[8], dummy);
128     dummy[5]++;
129     hci_set_bd_addr(dummy);
130 
131     // hand over to btstack embedded code
132     btstack_main();
133 
134     // go
135     btstack_run_loop_execute();
136 
137     while (1){};
138 }
139