xref: /btstack/port/archive/wiced-h5/main.c (revision bc6a318f2177319997f3b7da7b6f161b4ec94fed)
11182cb7eSMatthias Ringwald /*
21182cb7eSMatthias Ringwald  * Copyright (C) 2015 BlueKitchen GmbH
31182cb7eSMatthias Ringwald  *
41182cb7eSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
51182cb7eSMatthias Ringwald  * modification, are permitted provided that the following conditions
61182cb7eSMatthias Ringwald  * are met:
71182cb7eSMatthias Ringwald  *
81182cb7eSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
91182cb7eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
101182cb7eSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111182cb7eSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
121182cb7eSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
131182cb7eSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
141182cb7eSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
151182cb7eSMatthias Ringwald  *    from this software without specific prior written permission.
161182cb7eSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
171182cb7eSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
181182cb7eSMatthias Ringwald  *    monetary gain.
191182cb7eSMatthias Ringwald  *
201182cb7eSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211182cb7eSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221182cb7eSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251182cb7eSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261182cb7eSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271182cb7eSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281182cb7eSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291182cb7eSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301182cb7eSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311182cb7eSMatthias Ringwald  * SUCH DAMAGE.
321182cb7eSMatthias Ringwald  *
331182cb7eSMatthias Ringwald  * Please inquire about commercial licensing options at
341182cb7eSMatthias Ringwald  * [email protected]
351182cb7eSMatthias Ringwald  *
361182cb7eSMatthias Ringwald  */
371182cb7eSMatthias Ringwald 
38*bc6a318fSMatthias Ringwald #define BTSTACK_FILE__ "main.c"
391182cb7eSMatthias Ringwald 
401182cb7eSMatthias Ringwald #include "btstack.h"
411182cb7eSMatthias Ringwald #include "btstack_chipset_bcm.h"
421182cb7eSMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h"
431182cb7eSMatthias Ringwald #include "btstack_run_loop_wiced.h"
441182cb7eSMatthias Ringwald #include "btstack_link_key_db_wiced_dct.h"
451182cb7eSMatthias Ringwald #include "le_device_db_wiced_dct.h"
461182cb7eSMatthias Ringwald #include "btstack_uart_slip_wrapper.h"
471182cb7eSMatthias Ringwald 
481182cb7eSMatthias Ringwald #include "generated_mac_address.txt"
491182cb7eSMatthias Ringwald 
501182cb7eSMatthias Ringwald #include "platform_bluetooth.h"
511182cb7eSMatthias Ringwald #include "wiced.h"
521182cb7eSMatthias Ringwald #include "platform/wwd_platform_interface.h"
531182cb7eSMatthias Ringwald 
541182cb7eSMatthias Ringwald extern int btstack_main(int argc, char ** argv);
551182cb7eSMatthias Ringwald extern const btstack_uart_block_t * btstack_uart_block_wiced_instance(void);
561182cb7eSMatthias Ringwald 
571182cb7eSMatthias Ringwald static void phase2(int status);
581182cb7eSMatthias Ringwald 
591182cb7eSMatthias Ringwald // see generated_mac_address.txt - "macaddr=02:0A:F7:3d:76:be"
601182cb7eSMatthias Ringwald static const char * wifi_mac_address = NVRAM_GENERATED_MAC_ADDRESS;
611182cb7eSMatthias Ringwald 
621182cb7eSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
631182cb7eSMatthias Ringwald 
641182cb7eSMatthias Ringwald static btstack_uart_config_t uart_config;
651182cb7eSMatthias Ringwald 
661182cb7eSMatthias Ringwald static const hci_transport_config_uart_t transport_config = {
671182cb7eSMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
681182cb7eSMatthias Ringwald     115200,
691182cb7eSMatthias Ringwald     1000000,    // 200000+ didn't work reliably
701182cb7eSMatthias Ringwald     0,
711182cb7eSMatthias Ringwald     NULL,
721182cb7eSMatthias Ringwald };
731182cb7eSMatthias Ringwald 
741182cb7eSMatthias Ringwald 
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)751182cb7eSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
761182cb7eSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
771182cb7eSMatthias Ringwald     if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
781182cb7eSMatthias Ringwald     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
791182cb7eSMatthias Ringwald     printf("BTstack up and running.\n");
801182cb7eSMatthias Ringwald }
811182cb7eSMatthias Ringwald 
application_start(void)821182cb7eSMatthias Ringwald void application_start(void){
831182cb7eSMatthias Ringwald 
841182cb7eSMatthias Ringwald     /* Initialise the WICED device without WLAN */
851182cb7eSMatthias Ringwald     wiced_core_init();
861182cb7eSMatthias Ringwald 
871182cb7eSMatthias Ringwald     /* 32 kHz clock also needed for Bluetooth */
881182cb7eSMatthias Ringwald     host_platform_init_wlan_powersave_clock();
891182cb7eSMatthias Ringwald 
901182cb7eSMatthias Ringwald     printf("BTstack on WICED\n");
911182cb7eSMatthias Ringwald 
921182cb7eSMatthias Ringwald #if 0
931182cb7eSMatthias Ringwald     // init GPIOs D0-D5 for debugging - not used
941182cb7eSMatthias Ringwald     wiced_gpio_init(D0, OUTPUT_PUSH_PULL);
951182cb7eSMatthias Ringwald     wiced_gpio_init(D1, OUTPUT_PUSH_PULL);
961182cb7eSMatthias Ringwald     wiced_gpio_init(D2, OUTPUT_PUSH_PULL);
971182cb7eSMatthias Ringwald     wiced_gpio_init(D3, OUTPUT_PUSH_PULL);
981182cb7eSMatthias Ringwald     wiced_gpio_init(D4, OUTPUT_PUSH_PULL);
991182cb7eSMatthias Ringwald     wiced_gpio_init(D5, OUTPUT_PUSH_PULL);
1001182cb7eSMatthias Ringwald 
1011182cb7eSMatthias Ringwald     wiced_gpio_output_low(D0);
1021182cb7eSMatthias Ringwald     wiced_gpio_output_low(D1);
1031182cb7eSMatthias Ringwald     wiced_gpio_output_low(D2);
1041182cb7eSMatthias Ringwald     wiced_gpio_output_low(D3);
1051182cb7eSMatthias Ringwald     wiced_gpio_output_low(D4);
1061182cb7eSMatthias Ringwald     wiced_gpio_output_low(D5);
1071182cb7eSMatthias Ringwald #endif
1081182cb7eSMatthias Ringwald 
1091182cb7eSMatthias Ringwald     // start with BTstack init - especially configure HCI Transport
1101182cb7eSMatthias Ringwald     btstack_memory_init();
1111182cb7eSMatthias Ringwald 
1121182cb7eSMatthias Ringwald     // enable full log output while porting
1131182cb7eSMatthias Ringwald     // hci_dump_open(NULL, HCI_DUMP_STDOUT);
1141182cb7eSMatthias Ringwald 
1151182cb7eSMatthias Ringwald     // setup run loop
1161182cb7eSMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_wiced_get_instance());
1171182cb7eSMatthias Ringwald 
1181182cb7eSMatthias Ringwald     // get BCM chipset driver
1191182cb7eSMatthias Ringwald     const btstack_chipset_t *  chipset = btstack_chipset_bcm_instance();
1201182cb7eSMatthias Ringwald     chipset->init(&transport_config);
1211182cb7eSMatthias Ringwald 
1221182cb7eSMatthias Ringwald     // setup uart driver
1231182cb7eSMatthias Ringwald     const btstack_uart_block_t * uart_block_driver = btstack_uart_block_wiced_instance();
1241182cb7eSMatthias Ringwald     const btstack_uart_t * uart_slip_driver = btstack_uart_slip_wrapper_instance(uart_block_driver);
1251182cb7eSMatthias Ringwald 
1261182cb7eSMatthias Ringwald     // extract UART config from transport config
1271182cb7eSMatthias Ringwald     uart_config.baudrate    = transport_config.baudrate_init;
1281182cb7eSMatthias Ringwald     uart_config.flowcontrol = transport_config.flowcontrol;
1291182cb7eSMatthias Ringwald     uart_config.device_name = transport_config.device_name;
1301182cb7eSMatthias Ringwald     uart_block_driver->init(&uart_config);
1311182cb7eSMatthias Ringwald 
1321182cb7eSMatthias Ringwald     // init HCI
1331182cb7eSMatthias Ringwald     const hci_transport_t * transport = hci_transport_h5_instance(uart_slip_driver);
1341182cb7eSMatthias Ringwald     hci_init(transport, (void*) &transport_config);
1351182cb7eSMatthias Ringwald     hci_set_link_key_db(btstack_link_key_db_wiced_dct_instance());
1361182cb7eSMatthias Ringwald     hci_set_chipset(chipset);
1371182cb7eSMatthias Ringwald 
1381182cb7eSMatthias Ringwald     // inform about BTstack state
1391182cb7eSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1401182cb7eSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1411182cb7eSMatthias Ringwald 
1421182cb7eSMatthias Ringwald     // use WIFI Mac address + 1 for Bluetooth
1431182cb7eSMatthias Ringwald     bd_addr_t dummy = { 1,2,3,4,5,6};
1441182cb7eSMatthias Ringwald     sscanf_bd_addr(&wifi_mac_address[8], dummy);
1451182cb7eSMatthias Ringwald     dummy[5]++;
1461182cb7eSMatthias Ringwald     hci_set_bd_addr(dummy);
1471182cb7eSMatthias Ringwald 
1481182cb7eSMatthias Ringwald     // setup le device db storage -- not needed if used LE-only (-> start address == 0)
1491182cb7eSMatthias Ringwald     le_device_db_wiced_dct_set_start_address(btstack_link_key_db_wiced_dct_get_storage_size());
1501182cb7eSMatthias Ringwald     le_device_db_dump();
1511182cb7eSMatthias Ringwald 
1521182cb7eSMatthias Ringwald     // phase #1 download firmware
1531182cb7eSMatthias Ringwald     printf("Phase 1: Download firmware\n");
1541182cb7eSMatthias Ringwald 
1551182cb7eSMatthias Ringwald     // phase #2 start main app
1561182cb7eSMatthias Ringwald     btstack_chipset_bcm_download_firmware(uart_block_driver, transport_config.baudrate_main, &phase2);
1571182cb7eSMatthias Ringwald 
1581182cb7eSMatthias Ringwald     // go
1591182cb7eSMatthias Ringwald     btstack_run_loop_execute();
1601182cb7eSMatthias Ringwald }
1611182cb7eSMatthias Ringwald 
phase2(int status)1621182cb7eSMatthias Ringwald static void phase2(int status){
1631182cb7eSMatthias Ringwald 
1641182cb7eSMatthias Ringwald 
1651182cb7eSMatthias Ringwald     if (status){
1661182cb7eSMatthias Ringwald         printf("Download firmware failed\n");
1671182cb7eSMatthias Ringwald         return;
1681182cb7eSMatthias Ringwald     }
1691182cb7eSMatthias Ringwald 
1701182cb7eSMatthias Ringwald     printf("Phase 2: Main app\n");
1711182cb7eSMatthias Ringwald 
1721182cb7eSMatthias Ringwald     // setup app
1731182cb7eSMatthias Ringwald     btstack_main(0, NULL);
1741182cb7eSMatthias Ringwald }
175