1*86a86a65SMatthias Ringwald /* 2*86a86a65SMatthias Ringwald * Copyright (C) 2015 BlueKitchen GmbH 3*86a86a65SMatthias Ringwald * 4*86a86a65SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*86a86a65SMatthias Ringwald * modification, are permitted provided that the following conditions 6*86a86a65SMatthias Ringwald * are met: 7*86a86a65SMatthias Ringwald * 8*86a86a65SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*86a86a65SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*86a86a65SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*86a86a65SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*86a86a65SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*86a86a65SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*86a86a65SMatthias Ringwald * contributors may be used to endorse or promote products derived 15*86a86a65SMatthias Ringwald * from this software without specific prior written permission. 16*86a86a65SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*86a86a65SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*86a86a65SMatthias Ringwald * monetary gain. 19*86a86a65SMatthias Ringwald * 20*86a86a65SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*86a86a65SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*86a86a65SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*86a86a65SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*86a86a65SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*86a86a65SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*86a86a65SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*86a86a65SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*86a86a65SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*86a86a65SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*86a86a65SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*86a86a65SMatthias Ringwald * SUCH DAMAGE. 32*86a86a65SMatthias Ringwald * 33*86a86a65SMatthias Ringwald * Please inquire about commercial licensing options at 34*86a86a65SMatthias Ringwald * [email protected] 35*86a86a65SMatthias Ringwald * 36*86a86a65SMatthias Ringwald */ 37*86a86a65SMatthias Ringwald 38*86a86a65SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39*86a86a65SMatthias Ringwald 40*86a86a65SMatthias Ringwald #include "btstack.h" 41*86a86a65SMatthias Ringwald #include "btstack_chipset_bcm.h" 42*86a86a65SMatthias Ringwald #include "btstack_run_loop_wiced.h" 43*86a86a65SMatthias Ringwald #include "btstack_link_key_db_wiced_dct.h" 44*86a86a65SMatthias Ringwald 45*86a86a65SMatthias Ringwald #include "generated_mac_address.txt" 46*86a86a65SMatthias Ringwald 47*86a86a65SMatthias Ringwald #include "platform_bluetooth.h" 48*86a86a65SMatthias Ringwald #include "wiced.h" 49*86a86a65SMatthias Ringwald #include "platform/wwd_platform_interface.h" 50*86a86a65SMatthias Ringwald 51*86a86a65SMatthias Ringwald const btstack_uart_block_t * btstack_uart_block_wiced_instance(void); 52*86a86a65SMatthias Ringwald 53*86a86a65SMatthias Ringwald // see generated_mac_address.txt - "macaddr=02:0A:F7:3d:76:be" 54*86a86a65SMatthias Ringwald static const char * wifi_mac_address = NVRAM_GENERATED_MAC_ADDRESS; 55*86a86a65SMatthias Ringwald 56*86a86a65SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 57*86a86a65SMatthias Ringwald 58*86a86a65SMatthias Ringwald static const hci_transport_config_uart_t hci_transport_config_uart = { 59*86a86a65SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 60*86a86a65SMatthias Ringwald 115200, 61*86a86a65SMatthias Ringwald 2000000, // 300000+ didn't work reliably, the 48 MHz UART config might be needed for this 62*86a86a65SMatthias Ringwald 1, 63*86a86a65SMatthias Ringwald NULL, 64*86a86a65SMatthias Ringwald }; 65*86a86a65SMatthias Ringwald 66*86a86a65SMatthias Ringwald extern int btstack_main(void); 67*86a86a65SMatthias Ringwald 68*86a86a65SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 69*86a86a65SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 70*86a86a65SMatthias Ringwald if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return; 71*86a86a65SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 72*86a86a65SMatthias Ringwald printf("BTstack up and running.\n"); 73*86a86a65SMatthias Ringwald } 74*86a86a65SMatthias Ringwald 75*86a86a65SMatthias Ringwald void application_start(void){ 76*86a86a65SMatthias Ringwald 77*86a86a65SMatthias Ringwald /* Initialise the WICED device without WLAN */ 78*86a86a65SMatthias Ringwald wiced_core_init(); 79*86a86a65SMatthias Ringwald 80*86a86a65SMatthias Ringwald /* 32 kHz clock also needed for Bluetooth */ 81*86a86a65SMatthias Ringwald host_platform_init_wlan_powersave_clock(); 82*86a86a65SMatthias Ringwald 83*86a86a65SMatthias Ringwald printf("BTstack on WICED\n"); 84*86a86a65SMatthias Ringwald 85*86a86a65SMatthias Ringwald #if 0 86*86a86a65SMatthias Ringwald // init GPIOs D0-D5 for debugging - not used 87*86a86a65SMatthias Ringwald wiced_gpio_init(D0, OUTPUT_PUSH_PULL); 88*86a86a65SMatthias Ringwald wiced_gpio_init(D1, OUTPUT_PUSH_PULL); 89*86a86a65SMatthias Ringwald wiced_gpio_init(D2, OUTPUT_PUSH_PULL); 90*86a86a65SMatthias Ringwald wiced_gpio_init(D3, OUTPUT_PUSH_PULL); 91*86a86a65SMatthias Ringwald wiced_gpio_init(D4, OUTPUT_PUSH_PULL); 92*86a86a65SMatthias Ringwald wiced_gpio_init(D5, OUTPUT_PUSH_PULL); 93*86a86a65SMatthias Ringwald 94*86a86a65SMatthias Ringwald wiced_gpio_output_low(D0); 95*86a86a65SMatthias Ringwald wiced_gpio_output_low(D1); 96*86a86a65SMatthias Ringwald wiced_gpio_output_low(D2); 97*86a86a65SMatthias Ringwald wiced_gpio_output_low(D3); 98*86a86a65SMatthias Ringwald wiced_gpio_output_low(D4); 99*86a86a65SMatthias Ringwald wiced_gpio_output_low(D5); 100*86a86a65SMatthias Ringwald #endif 101*86a86a65SMatthias Ringwald 102*86a86a65SMatthias Ringwald // start with BTstack init - especially configure HCI Transport 103*86a86a65SMatthias Ringwald btstack_memory_init(); 104*86a86a65SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_wiced_get_instance()); 105*86a86a65SMatthias Ringwald 106*86a86a65SMatthias Ringwald // enable full log output while porting 107*86a86a65SMatthias Ringwald // hci_dump_open(NULL, HCI_DUMP_STDOUT); 108*86a86a65SMatthias Ringwald 109*86a86a65SMatthias Ringwald // init HCI 110*86a86a65SMatthias Ringwald const btstack_uart_block_t * uart_driver = btstack_uart_block_wiced_instance(); 111*86a86a65SMatthias Ringwald const hci_transport_t * transport = hci_transport_h4_instance(uart_driver); 112*86a86a65SMatthias Ringwald hci_init(transport, (void*) &hci_transport_config_uart); 113*86a86a65SMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_wiced_dct_instance()); 114*86a86a65SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 115*86a86a65SMatthias Ringwald 116*86a86a65SMatthias Ringwald // inform about BTstack state 117*86a86a65SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 118*86a86a65SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 119*86a86a65SMatthias Ringwald 120*86a86a65SMatthias Ringwald // use WIFI Mac address + 1 for Bluetooth 121*86a86a65SMatthias Ringwald bd_addr_t dummy = { 1,2,3,4,5,6}; 122*86a86a65SMatthias Ringwald sscanf_bd_addr(&wifi_mac_address[8], dummy); 123*86a86a65SMatthias Ringwald dummy[5]++; 124*86a86a65SMatthias Ringwald hci_set_bd_addr(dummy); 125*86a86a65SMatthias Ringwald 126*86a86a65SMatthias Ringwald // hand over to btstack embedded code 127*86a86a65SMatthias Ringwald btstack_main(); 128*86a86a65SMatthias Ringwald 129*86a86a65SMatthias Ringwald // go 130*86a86a65SMatthias Ringwald btstack_run_loop_execute(); 131*86a86a65SMatthias Ringwald 132*86a86a65SMatthias Ringwald while (1){}; 133*86a86a65SMatthias Ringwald } 134