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