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