186a86a65SMatthias Ringwald /* 286a86a65SMatthias Ringwald * Copyright (C) 2015 BlueKitchen GmbH 386a86a65SMatthias Ringwald * 486a86a65SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 586a86a65SMatthias Ringwald * modification, are permitted provided that the following conditions 686a86a65SMatthias Ringwald * are met: 786a86a65SMatthias Ringwald * 886a86a65SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 986a86a65SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1086a86a65SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1186a86a65SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1286a86a65SMatthias Ringwald * documentation and/or other materials provided with the distribution. 1386a86a65SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1486a86a65SMatthias Ringwald * contributors may be used to endorse or promote products derived 1586a86a65SMatthias Ringwald * from this software without specific prior written permission. 1686a86a65SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1786a86a65SMatthias Ringwald * personal benefit and not for any commercial purpose or for 1886a86a65SMatthias Ringwald * monetary gain. 1986a86a65SMatthias Ringwald * 2086a86a65SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2186a86a65SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2286a86a65SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2386a86a65SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2486a86a65SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2586a86a65SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2686a86a65SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2786a86a65SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2886a86a65SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2986a86a65SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3086a86a65SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3186a86a65SMatthias Ringwald * SUCH DAMAGE. 3286a86a65SMatthias Ringwald * 3386a86a65SMatthias Ringwald * Please inquire about commercial licensing options at 3486a86a65SMatthias Ringwald * [email protected] 3586a86a65SMatthias Ringwald * 3686a86a65SMatthias Ringwald */ 3786a86a65SMatthias Ringwald 3886a86a65SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 3986a86a65SMatthias Ringwald 4086a86a65SMatthias Ringwald #include "btstack.h" 4186a86a65SMatthias Ringwald #include "btstack_chipset_bcm.h" 4286a86a65SMatthias Ringwald #include "btstack_run_loop_wiced.h" 4386a86a65SMatthias Ringwald #include "btstack_link_key_db_wiced_dct.h" 4413e71c7bSMatthias Ringwald #include "le_device_db_wiced_dct.h" 4522dc8250SMatthias Ringwald #incldue "hci_dump_embedded_stdout.h" 46*c8dfe071SMatthias Ringwald #include "hci_transport.h" 47*c8dfe071SMatthias Ringwald #include "hci_transport_h4.h" 4886a86a65SMatthias Ringwald 4986a86a65SMatthias Ringwald #include "generated_mac_address.txt" 5086a86a65SMatthias Ringwald 5186a86a65SMatthias Ringwald #include "platform_bluetooth.h" 5286a86a65SMatthias Ringwald #include "wiced.h" 5386a86a65SMatthias Ringwald #include "platform/wwd_platform_interface.h" 5486a86a65SMatthias Ringwald 5586a86a65SMatthias Ringwald const btstack_uart_block_t * btstack_uart_block_wiced_instance(void); 5686a86a65SMatthias Ringwald 5786a86a65SMatthias Ringwald // see generated_mac_address.txt - "macaddr=02:0A:F7:3d:76:be" 5886a86a65SMatthias Ringwald static const char * wifi_mac_address = NVRAM_GENERATED_MAC_ADDRESS; 5986a86a65SMatthias Ringwald 6086a86a65SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 6186a86a65SMatthias Ringwald 6286a86a65SMatthias Ringwald static const hci_transport_config_uart_t hci_transport_config_uart = { 6386a86a65SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 6486a86a65SMatthias Ringwald 115200, 65f984cbe3SMatthias Ringwald 1000000, // 200000+ didn't work reliably 6686a86a65SMatthias Ringwald 1, 6786a86a65SMatthias Ringwald NULL, 6886a86a65SMatthias Ringwald }; 6986a86a65SMatthias Ringwald 7086a86a65SMatthias Ringwald extern int btstack_main(void); 7186a86a65SMatthias Ringwald 7286a86a65SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 7386a86a65SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 7486a86a65SMatthias Ringwald if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return; 7586a86a65SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 7686a86a65SMatthias Ringwald printf("BTstack up and running.\n"); 7786a86a65SMatthias Ringwald } 7886a86a65SMatthias Ringwald 7986a86a65SMatthias Ringwald void application_start(void){ 8086a86a65SMatthias Ringwald 8186a86a65SMatthias Ringwald /* Initialise the WICED device without WLAN */ 8286a86a65SMatthias Ringwald wiced_core_init(); 8386a86a65SMatthias Ringwald 8486a86a65SMatthias Ringwald /* 32 kHz clock also needed for Bluetooth */ 8586a86a65SMatthias Ringwald host_platform_init_wlan_powersave_clock(); 8686a86a65SMatthias Ringwald 8786a86a65SMatthias Ringwald printf("BTstack on WICED\n"); 8886a86a65SMatthias Ringwald 8986a86a65SMatthias Ringwald #if 0 9086a86a65SMatthias Ringwald // init GPIOs D0-D5 for debugging - not used 9186a86a65SMatthias Ringwald wiced_gpio_init(D0, OUTPUT_PUSH_PULL); 9286a86a65SMatthias Ringwald wiced_gpio_init(D1, OUTPUT_PUSH_PULL); 9386a86a65SMatthias Ringwald wiced_gpio_init(D2, OUTPUT_PUSH_PULL); 9486a86a65SMatthias Ringwald wiced_gpio_init(D3, OUTPUT_PUSH_PULL); 9586a86a65SMatthias Ringwald wiced_gpio_init(D4, OUTPUT_PUSH_PULL); 9686a86a65SMatthias Ringwald wiced_gpio_init(D5, OUTPUT_PUSH_PULL); 9786a86a65SMatthias Ringwald 9886a86a65SMatthias Ringwald wiced_gpio_output_low(D0); 9986a86a65SMatthias Ringwald wiced_gpio_output_low(D1); 10086a86a65SMatthias Ringwald wiced_gpio_output_low(D2); 10186a86a65SMatthias Ringwald wiced_gpio_output_low(D3); 10286a86a65SMatthias Ringwald wiced_gpio_output_low(D4); 10386a86a65SMatthias Ringwald wiced_gpio_output_low(D5); 10486a86a65SMatthias Ringwald #endif 10586a86a65SMatthias Ringwald 10686a86a65SMatthias Ringwald // start with BTstack init - especially configure HCI Transport 10786a86a65SMatthias Ringwald btstack_memory_init(); 10886a86a65SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_wiced_get_instance()); 10986a86a65SMatthias Ringwald 11086a86a65SMatthias Ringwald // enable full log output while porting 11122dc8250SMatthias Ringwald // hci_dump_init(hci_dump_embedded_stdout_get_instance()); 11286a86a65SMatthias Ringwald 11313e71c7bSMatthias Ringwald // setup le device db storage -- not needed if used LE-only (-> start address == 0) 11413e71c7bSMatthias Ringwald le_device_db_wiced_dct_set_start_address(btstack_link_key_db_wiced_dct_get_storage_size()); 11513e71c7bSMatthias Ringwald le_device_db_dump(); 11613e71c7bSMatthias Ringwald 11786a86a65SMatthias Ringwald // init HCI 11886a86a65SMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_wiced_instance(); 11986a86a65SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 12086a86a65SMatthias Ringwald hci_init(transport, (void*) &hci_transport_config_uart); 12186a86a65SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_wiced_dct_instance()); 12286a86a65SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 12386a86a65SMatthias Ringwald 12486a86a65SMatthias Ringwald // inform about BTstack state 12586a86a65SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 12686a86a65SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 12786a86a65SMatthias Ringwald 12886a86a65SMatthias Ringwald // use WIFI Mac address + 1 for Bluetooth 12986a86a65SMatthias Ringwald bd_addr_t dummy = { 1,2,3,4,5,6}; 13086a86a65SMatthias Ringwald sscanf_bd_addr(&wifi_mac_address[8], dummy); 13186a86a65SMatthias Ringwald dummy[5]++; 13286a86a65SMatthias Ringwald hci_set_bd_addr(dummy); 13386a86a65SMatthias Ringwald 13486a86a65SMatthias Ringwald // hand over to btstack embedded code 13586a86a65SMatthias Ringwald btstack_main(); 13686a86a65SMatthias Ringwald 13786a86a65SMatthias Ringwald // go 13886a86a65SMatthias Ringwald btstack_run_loop_execute(); 13986a86a65SMatthias Ringwald 14086a86a65SMatthias Ringwald while (1){}; 14186a86a65SMatthias Ringwald } 142