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