1f5c04f62SMatthias Ringwald /* 2f5c04f62SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3f5c04f62SMatthias Ringwald * 4f5c04f62SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5f5c04f62SMatthias Ringwald * modification, are permitted provided that the following conditions 6f5c04f62SMatthias Ringwald * are met: 7f5c04f62SMatthias Ringwald * 8f5c04f62SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9f5c04f62SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10f5c04f62SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11f5c04f62SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12f5c04f62SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13f5c04f62SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14f5c04f62SMatthias Ringwald * contributors may be used to endorse or promote products derived 15f5c04f62SMatthias Ringwald * from this software without specific prior written permission. 16f5c04f62SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17f5c04f62SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18f5c04f62SMatthias Ringwald * monetary gain. 19f5c04f62SMatthias Ringwald * 20f5c04f62SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21f5c04f62SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22f5c04f62SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23f5c04f62SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24f5c04f62SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25f5c04f62SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26f5c04f62SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27f5c04f62SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28f5c04f62SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29f5c04f62SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30f5c04f62SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31f5c04f62SMatthias Ringwald * SUCH DAMAGE. 32f5c04f62SMatthias Ringwald * 33f5c04f62SMatthias Ringwald * Please inquire about commercial licensing options at 34f5c04f62SMatthias Ringwald * [email protected] 35f5c04f62SMatthias Ringwald * 36f5c04f62SMatthias Ringwald */ 37f5c04f62SMatthias Ringwald 38f5c04f62SMatthias Ringwald #define __BTSTACK_FILE__ "main.c" 39f5c04f62SMatthias Ringwald 40f5c04f62SMatthias Ringwald // ***************************************************************************** 41f5c04f62SMatthias Ringwald // 42c41e9922SMatthias Ringwald // Port for Rasperry Pi with built-in BCM chipset via H4 or H5 43f5c04f62SMatthias Ringwald // 44f5c04f62SMatthias Ringwald // ***************************************************************************** 45f5c04f62SMatthias Ringwald 46c41e9922SMatthias Ringwald #include <errno.h> 47c41e9922SMatthias Ringwald #include <fcntl.h> 4888d4cc4fSMatthias Ringwald #include <inttypes.h> 49c41e9922SMatthias Ringwald #include <signal.h> 50c41e9922SMatthias Ringwald #include <stdint.h> 51f5c04f62SMatthias Ringwald #include <stdio.h> 52f5c04f62SMatthias Ringwald #include <stdlib.h> 53f5c04f62SMatthias Ringwald #include <string.h> 54c41e9922SMatthias Ringwald #include <sys/stat.h> 55c41e9922SMatthias Ringwald #include <termios.h> 56c41e9922SMatthias Ringwald #include <unistd.h> 57f5c04f62SMatthias Ringwald 58f5c04f62SMatthias Ringwald #include "btstack_config.h" 59f5c04f62SMatthias Ringwald 60f5c04f62SMatthias Ringwald #include "btstack_debug.h" 61f5c04f62SMatthias Ringwald #include "btstack_event.h" 628c8a87eeSMatthias Ringwald #include "ble/le_device_db_tlv.h" 638c8a87eeSMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 64f5c04f62SMatthias Ringwald #include "btstack_memory.h" 65f5c04f62SMatthias Ringwald #include "btstack_run_loop.h" 66f5c04f62SMatthias Ringwald #include "btstack_run_loop_posix.h" 67f5c04f62SMatthias Ringwald #include "bluetooth_company_id.h" 68f5c04f62SMatthias Ringwald #include "hci.h" 69f5c04f62SMatthias Ringwald #include "hci_dump.h" 70*7435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 71f5c04f62SMatthias Ringwald #include "btstack_stdin.h" 7272f99eadSMatthias Ringwald #include "btstack_tlv_posix.h" 7308b9cddcSMatthias Ringwald #include "btstack_uart.h" 7408b9cddcSMatthias Ringwald #include "btstack_uart_block.h" 7508b9cddcSMatthias Ringwald #include "btstack_uart_slip_wrapper.h" 76f5c04f62SMatthias Ringwald 77f5c04f62SMatthias Ringwald #include "btstack_chipset_bcm.h" 78f5c04f62SMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h" 7911e995b1SMatthias Ringwald #include "btstack_control_raspi.h" 80f5c04f62SMatthias Ringwald 8108b9cddcSMatthias Ringwald 8263ff2b1eSDirk Helbig #include "raspi_get_model.h" 8363ff2b1eSDirk Helbig 84f5c04f62SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 85f5c04f62SMatthias Ringwald 8688d4cc4fSMatthias Ringwald typedef enum { 8788d4cc4fSMatthias Ringwald UART_INVALID, 8888d4cc4fSMatthias Ringwald UART_SOFTWARE_NO_FLOW, 8988d4cc4fSMatthias Ringwald UART_HARDWARE_NO_FLOW, 9088d4cc4fSMatthias Ringwald UART_HARDWARE_FLOW 9188d4cc4fSMatthias Ringwald } uart_type_t; 92f5c04f62SMatthias Ringwald 93e0814c90SMatthias Ringwald // default config, updated depending on RasperryPi UART configuration 94f5c04f62SMatthias Ringwald static hci_transport_config_uart_t transport_config = { 95f5c04f62SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 96f5c04f62SMatthias Ringwald 115200, 97e0814c90SMatthias Ringwald 0, // main baudrate 98f5c04f62SMatthias Ringwald 0, // flow control 99f5c04f62SMatthias Ringwald NULL, 100f5c04f62SMatthias Ringwald }; 101d9bed121SMatthias Ringwald 102c41e9922SMatthias Ringwald static btstack_uart_config_t uart_config; 103d9bed121SMatthias Ringwald 104c41e9922SMatthias Ringwald static int main_argc; 105c41e9922SMatthias Ringwald static const char ** main_argv; 106d9bed121SMatthias Ringwald 107c41e9922SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 108c41e9922SMatthias Ringwald 109c41e9922SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 110c41e9922SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 111c41e9922SMatthias Ringwald static char tlv_db_path[100]; 112c41e9922SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 113c41e9922SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 114d9bed121SMatthias Ringwald 115d9bed121SMatthias Ringwald 116d9bed121SMatthias Ringwald static int raspi_speed_to_baud(speed_t baud) 117d9bed121SMatthias Ringwald { 118d9bed121SMatthias Ringwald switch (baud) { 119d9bed121SMatthias Ringwald case B9600: 120d9bed121SMatthias Ringwald return 9600; 121d9bed121SMatthias Ringwald case B19200: 122d9bed121SMatthias Ringwald return 19200; 123d9bed121SMatthias Ringwald case B38400: 124d9bed121SMatthias Ringwald return 38400; 125d9bed121SMatthias Ringwald case B57600: 126d9bed121SMatthias Ringwald return 57600; 127d9bed121SMatthias Ringwald case B115200: 128d9bed121SMatthias Ringwald return 115200; 129d9bed121SMatthias Ringwald case B230400: 130d9bed121SMatthias Ringwald return 230400; 131d9bed121SMatthias Ringwald case B460800: 132d9bed121SMatthias Ringwald return 460800; 133d9bed121SMatthias Ringwald case B500000: 134d9bed121SMatthias Ringwald return 500000; 135d9bed121SMatthias Ringwald case B576000: 136d9bed121SMatthias Ringwald return 576000; 137d9bed121SMatthias Ringwald case B921600: 138d9bed121SMatthias Ringwald return 921600; 139d9bed121SMatthias Ringwald case B1000000: 140d9bed121SMatthias Ringwald return 1000000; 141d9bed121SMatthias Ringwald case B1152000: 142d9bed121SMatthias Ringwald return 1152000; 143d9bed121SMatthias Ringwald case B1500000: 144d9bed121SMatthias Ringwald return 1500000; 145d9bed121SMatthias Ringwald case B2000000: 146d9bed121SMatthias Ringwald return 2000000; 147d9bed121SMatthias Ringwald case B2500000: 148d9bed121SMatthias Ringwald return 2500000; 149d9bed121SMatthias Ringwald case B3000000: 150d9bed121SMatthias Ringwald return 3000000; 151d9bed121SMatthias Ringwald case B3500000: 152d9bed121SMatthias Ringwald return 3500000; 153d9bed121SMatthias Ringwald case B4000000: 154d9bed121SMatthias Ringwald return 4000000; 155d9bed121SMatthias Ringwald default: 156d9bed121SMatthias Ringwald return -1; 157d9bed121SMatthias Ringwald } 158d9bed121SMatthias Ringwald } 159d9bed121SMatthias Ringwald 160d9bed121SMatthias Ringwald static void raspi_get_terminal_params( hci_transport_config_uart_t *tc ) 161d9bed121SMatthias Ringwald { 162d9bed121SMatthias Ringwald // open serial terminal and get parameters 163d9bed121SMatthias Ringwald int fd = open( tc->device_name, O_RDONLY ); 164d9bed121SMatthias Ringwald if( fd < 0 ) 165d9bed121SMatthias Ringwald { 166d9bed121SMatthias Ringwald perror( "can't open serial port" ); 167d9bed121SMatthias Ringwald return; 168d9bed121SMatthias Ringwald } 169d9bed121SMatthias Ringwald struct termios tios; 170d9bed121SMatthias Ringwald tcgetattr( fd, &tios ); 171d9bed121SMatthias Ringwald close( fd ); 172d9bed121SMatthias Ringwald 173d9bed121SMatthias Ringwald speed_t ospeed = cfgetospeed( &tios ); 174d9bed121SMatthias Ringwald int baud = raspi_speed_to_baud( ospeed ); 175d9bed121SMatthias Ringwald printf( "current serial terminal parameter baudrate: %d, flow control: %s\n", baud, (tios.c_cflag&CRTSCTS)?"Hardware":"None" ); 176d9bed121SMatthias Ringwald 177d9bed121SMatthias Ringwald // overwrites the initial baudrate only in case it was likely to be altered before 178d9bed121SMatthias Ringwald if( baud > 9600 ) 179d9bed121SMatthias Ringwald { 180d9bed121SMatthias Ringwald tc->baudrate_init = baud; 181d9bed121SMatthias Ringwald tc->flowcontrol = (tios.c_cflag & CRTSCTS)?1:0; 182d9bed121SMatthias Ringwald } 183d9bed121SMatthias Ringwald } 184d9bed121SMatthias Ringwald 185f5c04f62SMatthias Ringwald static void sigint_handler(int param){ 186f5c04f62SMatthias Ringwald UNUSED(param); 187f5c04f62SMatthias Ringwald 188f5c04f62SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 189f5c04f62SMatthias Ringwald log_info("sigint_handler: shutting down"); 190f5c04f62SMatthias Ringwald 191f5c04f62SMatthias Ringwald // reset anyway 192f5c04f62SMatthias Ringwald btstack_stdin_reset(); 193f5c04f62SMatthias Ringwald 194f5c04f62SMatthias Ringwald // power down 195f5c04f62SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 196f5c04f62SMatthias Ringwald hci_close(); 197f5c04f62SMatthias Ringwald log_info("Good bye, see you.\n"); 198f5c04f62SMatthias Ringwald exit(0); 199f5c04f62SMatthias Ringwald } 200f5c04f62SMatthias Ringwald 201f5c04f62SMatthias Ringwald static int led_state = 0; 202f5c04f62SMatthias Ringwald void hal_led_toggle(void){ 203f5c04f62SMatthias Ringwald led_state = 1 - led_state; 204f5c04f62SMatthias Ringwald printf("LED State %u\n", led_state); 205f5c04f62SMatthias Ringwald } 206f5c04f62SMatthias Ringwald 207f5c04f62SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 208f5c04f62SMatthias Ringwald bd_addr_t addr; 209f5c04f62SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 210f5c04f62SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 211f5c04f62SMatthias Ringwald case BTSTACK_EVENT_STATE: 212f5c04f62SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 213f5c04f62SMatthias Ringwald gap_local_bd_addr(addr); 214f5c04f62SMatthias Ringwald printf("BTstack up and running at %s\n", bd_addr_to_str(addr)); 21572f99eadSMatthias Ringwald // setup TLV 21672f99eadSMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 21772f99eadSMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(addr)); 21872f99eadSMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 21972f99eadSMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 22072f99eadSMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 2218c8a87eeSMatthias Ringwald #ifdef ENABLE_CLASSIC 2228c8a87eeSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 2238c8a87eeSMatthias Ringwald #endif 2248c8a87eeSMatthias Ringwald #ifdef ENABLE_BLE 2258c8a87eeSMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 2268c8a87eeSMatthias Ringwald #endif 227f5c04f62SMatthias Ringwald break; 228d9bed121SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 229d9bed121SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 230d9bed121SMatthias Ringwald if (hci_event_command_complete_get_return_parameters(packet)[0]) break; 231d9bed121SMatthias Ringwald // terminate, name 248 chars 232d9bed121SMatthias Ringwald packet[6+248] = 0; 233d9bed121SMatthias Ringwald printf("Local name: %s\n", &packet[6]); 234d9bed121SMatthias Ringwald 235d9bed121SMatthias Ringwald btstack_chipset_bcm_set_device_name((const char *)&packet[6]); 236d9bed121SMatthias Ringwald } 237d9bed121SMatthias Ringwald break; 238f5c04f62SMatthias Ringwald default: 239f5c04f62SMatthias Ringwald break; 240f5c04f62SMatthias Ringwald } 241f5c04f62SMatthias Ringwald } 242f5c04f62SMatthias Ringwald 24388d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 244e0814c90SMatthias Ringwald static int raspi_get_bd_addr(bd_addr_t addr){ 24588d4cc4fSMatthias Ringwald 24688d4cc4fSMatthias Ringwald FILE *fd = fopen( "/proc/device-tree/serial-number", "r" ); 24788d4cc4fSMatthias Ringwald if( fd == NULL ){ 24888d4cc4fSMatthias Ringwald fprintf(stderr, "can't read serial number, %s\n", strerror( errno ) ); 24988d4cc4fSMatthias Ringwald return -1; 25088d4cc4fSMatthias Ringwald } 2511b6b55dfSMatthias Ringwald fscanf( fd, "%*08x" "%*02x" "%02" SCNx8 "%02" SCNx8 "%02" SCNx8, &addr[3], &addr[4], &addr[5] ); 25288d4cc4fSMatthias Ringwald fclose( fd ); 25388d4cc4fSMatthias Ringwald 25488d4cc4fSMatthias Ringwald addr[0] = 0xb8; addr[1] = 0x27; addr[2] = 0xeb; 25588d4cc4fSMatthias Ringwald addr[3] ^= 0xaa; addr[4] ^= 0xaa; addr[5] ^= 0xaa; 25688d4cc4fSMatthias Ringwald 25788d4cc4fSMatthias Ringwald return 0; 25888d4cc4fSMatthias Ringwald } 25988d4cc4fSMatthias Ringwald 26088d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 26188d4cc4fSMatthias Ringwald // on UART_INVALID errno is set 26288d4cc4fSMatthias Ringwald static uart_type_t raspi_get_bluetooth_uart_type(void){ 26388d4cc4fSMatthias Ringwald 264e0814c90SMatthias Ringwald uint8_t deviceUart0[21] = { 0 }; 265e0814c90SMatthias Ringwald FILE *fd = fopen( "/proc/device-tree/aliases/uart0", "r" ); 266e0814c90SMatthias Ringwald if( fd == NULL ) return UART_INVALID; 26788d4cc4fSMatthias Ringwald fscanf( fd, "%20s", deviceUart0 ); 26888d4cc4fSMatthias Ringwald fclose( fd ); 26988d4cc4fSMatthias Ringwald 270e0814c90SMatthias Ringwald uint8_t deviceSerial1[21] = { 0 }; 27188d4cc4fSMatthias Ringwald fd = fopen( "/proc/device-tree/aliases/serial1", "r" ); 27288d4cc4fSMatthias Ringwald if( fd == NULL ) return UART_INVALID; 27388d4cc4fSMatthias Ringwald fscanf( fd, "%20s", deviceSerial1 ); 27488d4cc4fSMatthias Ringwald fclose( fd ); 27588d4cc4fSMatthias Ringwald 276e0814c90SMatthias Ringwald // test if uart0 is an alias for serial1 277e0814c90SMatthias Ringwald if( strncmp( (const char *) deviceUart0, (const char *) deviceSerial1, 21 ) == 0 ){ 27888d4cc4fSMatthias Ringwald // HW uart 27988d4cc4fSMatthias Ringwald size_t count = 0; 28088d4cc4fSMatthias Ringwald uint8_t buf[16]; 28188d4cc4fSMatthias Ringwald fd = fopen( "/proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm,pins", "r" ); 28288d4cc4fSMatthias Ringwald if( fd == NULL ) return UART_INVALID; 28388d4cc4fSMatthias Ringwald count = fread( buf, 1, 16, fd ); 28488d4cc4fSMatthias Ringwald fclose( fd ); 28588d4cc4fSMatthias Ringwald 286e0814c90SMatthias Ringwald // contains assigned pins 287e0814c90SMatthias Ringwald int pins = count / 4; 288e0814c90SMatthias Ringwald if( pins == 4 ){ 28988d4cc4fSMatthias Ringwald return UART_HARDWARE_FLOW; 290e0814c90SMatthias Ringwald } else { 29188d4cc4fSMatthias Ringwald return UART_HARDWARE_NO_FLOW; 29288d4cc4fSMatthias Ringwald } 293e0814c90SMatthias Ringwald } else { 29488d4cc4fSMatthias Ringwald return UART_SOFTWARE_NO_FLOW; 29588d4cc4fSMatthias Ringwald } 29688d4cc4fSMatthias Ringwald } 29788d4cc4fSMatthias Ringwald 298f5c04f62SMatthias Ringwald static void phase2(int status); 299f5c04f62SMatthias Ringwald int main(int argc, const char * argv[]){ 300f5c04f62SMatthias Ringwald 301f5c04f62SMatthias Ringwald /// GET STARTED with BTstack /// 302f5c04f62SMatthias Ringwald btstack_memory_init(); 303f5c04f62SMatthias Ringwald 304*7435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 305f5c04f62SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 306*7435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 307*7435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 308*7435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 309f5c04f62SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 310f5c04f62SMatthias Ringwald 311f5c04f62SMatthias Ringwald // setup run loop 312f5c04f62SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 313f5c04f62SMatthias Ringwald 314f5c04f62SMatthias Ringwald // pick serial port and configure uart block driver 315f5c04f62SMatthias Ringwald transport_config.device_name = "/dev/serial1"; 316f5c04f62SMatthias Ringwald 31788d4cc4fSMatthias Ringwald // derive bd_addr from serial number 31888d4cc4fSMatthias Ringwald bd_addr_t addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; 31988d4cc4fSMatthias Ringwald raspi_get_bd_addr(addr); 320f5c04f62SMatthias Ringwald 32188d4cc4fSMatthias Ringwald // set UART config based on raspi Bluetooth UART type 322c41e9922SMatthias Ringwald int bt_reg_en_pin = -1; 323e6a3daa4SMatthias Ringwald bool power_cycle = true; 32488d4cc4fSMatthias Ringwald switch (raspi_get_bluetooth_uart_type()){ 32588d4cc4fSMatthias Ringwald case UART_INVALID: 32688d4cc4fSMatthias Ringwald fprintf(stderr, "can't verify HW uart, %s\n", strerror( errno ) ); 32788d4cc4fSMatthias Ringwald return -1; 32888d4cc4fSMatthias Ringwald case UART_SOFTWARE_NO_FLOW: 32955250ca2SMatthias Ringwald // ?? 330c41e9922SMatthias Ringwald bt_reg_en_pin = 128; 33188d4cc4fSMatthias Ringwald transport_config.baudrate_main = 460800; 33288d4cc4fSMatthias Ringwald transport_config.flowcontrol = 0; 33388d4cc4fSMatthias Ringwald break; 33488d4cc4fSMatthias Ringwald case UART_HARDWARE_NO_FLOW: 335c41e9922SMatthias Ringwald // Raspberry Pi 3 A 33655250ca2SMatthias Ringwald // Raspberry Pi 3 B 337e6a3daa4SMatthias Ringwald // power up with H5 and without power cycle untested/unsupported 338c41e9922SMatthias Ringwald bt_reg_en_pin = 128; 33988d4cc4fSMatthias Ringwald transport_config.baudrate_main = 921600; 34088d4cc4fSMatthias Ringwald transport_config.flowcontrol = 0; 34188d4cc4fSMatthias Ringwald break; 34288d4cc4fSMatthias Ringwald case UART_HARDWARE_FLOW: 343e6a3daa4SMatthias Ringwald // Raspberry Pi Zero W gpio 45, 3 mbps does not work (investigation pending) 344d9bed121SMatthias Ringwald // Raspberry Pi 3A+ vgpio 129 but WLAN + BL 345d9bed121SMatthias Ringwald // Raspberry Pi 3B+ vgpio 129 but WLAN + BL 34688d4cc4fSMatthias Ringwald transport_config.flowcontrol = 1; 347e6a3daa4SMatthias Ringwald int model = raspi_get_model(); 348e6a3daa4SMatthias Ringwald if (model == MODEL_ZERO_W){ 349e6a3daa4SMatthias Ringwald bt_reg_en_pin = 45; 35063ff2b1eSDirk Helbig transport_config.baudrate_main = 921600; 351e6a3daa4SMatthias Ringwald } else { 352e6a3daa4SMatthias Ringwald bt_reg_en_pin = 129; 353e6a3daa4SMatthias Ringwald transport_config.baudrate_main = 3000000; 35463ff2b1eSDirk Helbig } 355e6a3daa4SMatthias Ringwald 356e6a3daa4SMatthias Ringwald #ifdef ENABLE_CONTROLLER_WARM_BOOT 357e6a3daa4SMatthias Ringwald power_cycle = false; 358e6a3daa4SMatthias Ringwald #else 359e6a3daa4SMatthias Ringwald // warn about power cycle on devices with shared reg_en pins 360e6a3daa4SMatthias Ringwald if (model == MODEL_3APLUS || model == MODEL_3BPLUS){ 361e6a3daa4SMatthias Ringwald printf("Wifi and Bluetooth share a single RESET line and BTstack needs to reset Bluetooth -> SSH over Wifi will fail\n"); 362e6a3daa4SMatthias Ringwald printf("Please add ENABLE_CONTROLLER_WARM_BOOT to btstack_config.h to enable startup without RESET\n"); 363e6a3daa4SMatthias Ringwald } 364e6a3daa4SMatthias Ringwald #endif 36588d4cc4fSMatthias Ringwald break; 36688d4cc4fSMatthias Ringwald } 367e6a3daa4SMatthias Ringwald printf("%s, %u, BT_REG_EN at GPIO %u, %s\n", transport_config.flowcontrol ? "H4":"H5", transport_config.baudrate_main, bt_reg_en_pin, power_cycle ? "Reset Controller" : "Warm Boot"); 368d9bed121SMatthias Ringwald 369f5c04f62SMatthias Ringwald // get BCM chipset driver 370f5c04f62SMatthias Ringwald const btstack_chipset_t * chipset = btstack_chipset_bcm_instance(); 371f5c04f62SMatthias Ringwald chipset->init(&transport_config); 372f5c04f62SMatthias Ringwald 373f5c04f62SMatthias Ringwald // set path to firmware files 37423a1bf14SMatthias Ringwald btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware/brcm"); 375f5c04f62SMatthias Ringwald 376f5c04f62SMatthias Ringwald // setup UART driver 37779530e37SMatthias Ringwald const btstack_uart_t * uart_driver = btstack_uart_posix_instance(); 378f5c04f62SMatthias Ringwald 379f5c04f62SMatthias Ringwald // extract UART config from transport config 380f5c04f62SMatthias Ringwald uart_config.baudrate = transport_config.baudrate_init; 381f5c04f62SMatthias Ringwald uart_config.flowcontrol = transport_config.flowcontrol; 382f5c04f62SMatthias Ringwald uart_config.device_name = transport_config.device_name; 38379530e37SMatthias Ringwald uart_driver->init(&uart_config); 384f5c04f62SMatthias Ringwald 3856e4c47b0SMatthias Ringwald // HW with FlowControl -> we can use regular h4 mode 3866e4c47b0SMatthias Ringwald const hci_transport_t * transport; 3876e4c47b0SMatthias Ringwald if (transport_config.flowcontrol){ 38879530e37SMatthias Ringwald transport = hci_transport_h4_instance(uart_driver); 3896e4c47b0SMatthias Ringwald } else { 39079530e37SMatthias Ringwald transport = hci_transport_h5_instance(uart_driver); 3916e4c47b0SMatthias Ringwald } 3926e4c47b0SMatthias Ringwald 393f5c04f62SMatthias Ringwald // setup HCI (to be able to use bcm chipset driver) 394f5c04f62SMatthias Ringwald hci_init(transport, (void*) &transport_config); 39588d4cc4fSMatthias Ringwald hci_set_bd_addr( addr ); 396f5c04f62SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 397f5c04f62SMatthias Ringwald 398f5c04f62SMatthias Ringwald // inform about BTstack state 399f5c04f62SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 400f5c04f62SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 401f5c04f62SMatthias Ringwald 402f5c04f62SMatthias Ringwald // handle CTRL-c 403f5c04f62SMatthias Ringwald signal(SIGINT, sigint_handler); 404f5c04f62SMatthias Ringwald 405f5c04f62SMatthias Ringwald main_argc = argc; 406f5c04f62SMatthias Ringwald main_argv = argv; 407f5c04f62SMatthias Ringwald 408c41e9922SMatthias Ringwald // power cycle Bluetooth controller on older models without flowcontrol 409e6a3daa4SMatthias Ringwald if (power_cycle){ 410c41e9922SMatthias Ringwald btstack_control_raspi_set_bt_reg_en_pin(bt_reg_en_pin); 41111e995b1SMatthias Ringwald btstack_control_t *control = btstack_control_raspi_get_instance(); 41211e995b1SMatthias Ringwald control->init(NULL); 41311e995b1SMatthias Ringwald control->off(); 41423a1bf14SMatthias Ringwald usleep( 100000 ); 41511e995b1SMatthias Ringwald control->on(); 416e6a3daa4SMatthias Ringwald } 417e6a3daa4SMatthias Ringwald 418e6a3daa4SMatthias Ringwald if (transport_config.flowcontrol){ 419e6a3daa4SMatthias Ringwald 420e6a3daa4SMatthias Ringwald // re-use current terminal speed (if there was no power cycle) 421e6a3daa4SMatthias Ringwald if (!power_cycle){ 422e6a3daa4SMatthias Ringwald raspi_get_terminal_params( &transport_config ); 423e6a3daa4SMatthias Ringwald } 424e6a3daa4SMatthias Ringwald 425e6a3daa4SMatthias Ringwald // with flowcontrol, we use h4 and are done 426e6a3daa4SMatthias Ringwald btstack_main(main_argc, main_argv); 427e6a3daa4SMatthias Ringwald 428e6a3daa4SMatthias Ringwald } else { 42988d4cc4fSMatthias Ringwald 430c41e9922SMatthias Ringwald // assume BCM4343W used in Pi 3 A/B. Pi 3 A/B+ have a newer controller but support H4 with Flowcontrol 431c41e9922SMatthias Ringwald btstack_chipset_bcm_set_device_name("BCM43430A1"); 432c41e9922SMatthias Ringwald 4336e4c47b0SMatthias Ringwald // phase #1 download firmware 4346e4c47b0SMatthias Ringwald printf("Phase 1: Download firmware\n"); 4356e4c47b0SMatthias Ringwald 436f5c04f62SMatthias Ringwald // phase #2 start main app 43779530e37SMatthias Ringwald btstack_chipset_bcm_download_firmware(uart_driver, transport_config.baudrate_main, &phase2); 4386e4c47b0SMatthias Ringwald } 439f5c04f62SMatthias Ringwald 440f5c04f62SMatthias Ringwald // go 441f5c04f62SMatthias Ringwald btstack_run_loop_execute(); 442f5c04f62SMatthias Ringwald return 0; 443f5c04f62SMatthias Ringwald } 444f5c04f62SMatthias Ringwald 445f5c04f62SMatthias Ringwald static void phase2(int status){ 446f5c04f62SMatthias Ringwald 447f5c04f62SMatthias Ringwald if (status){ 448f5c04f62SMatthias Ringwald printf("Download firmware failed\n"); 449f5c04f62SMatthias Ringwald return; 450f5c04f62SMatthias Ringwald } 451f5c04f62SMatthias Ringwald 452f5c04f62SMatthias Ringwald printf("Phase 2: Main app\n"); 453f5c04f62SMatthias Ringwald 454f5c04f62SMatthias Ringwald // setup app 455f5c04f62SMatthias Ringwald btstack_main(main_argc, main_argv); 456f5c04f62SMatthias Ringwald } 457f5c04f62SMatthias Ringwald 458