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 38*f11fd9a9SMatthias 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 60*f11fd9a9SMatthias Ringwald #include "ble/le_device_db_tlv.h" 61*f11fd9a9SMatthias Ringwald #include "bluetooth_company_id.h" 62*f11fd9a9SMatthias Ringwald #include "btstack_chipset_bcm.h" 63*f11fd9a9SMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h" 64*f11fd9a9SMatthias Ringwald #include "btstack_control_raspi.h" 65f5c04f62SMatthias Ringwald #include "btstack_debug.h" 66f5c04f62SMatthias Ringwald #include "btstack_event.h" 67f5c04f62SMatthias Ringwald #include "btstack_memory.h" 68f5c04f62SMatthias Ringwald #include "btstack_run_loop.h" 69f5c04f62SMatthias Ringwald #include "btstack_run_loop_posix.h" 70*f11fd9a9SMatthias Ringwald #include "btstack_signal.h" 71*f11fd9a9SMatthias Ringwald #include "btstack_stdin.h" 72*f11fd9a9SMatthias Ringwald #include "btstack_tlv_posix.h" 73*f11fd9a9SMatthias Ringwald #include "btstack_uart.h" 74*f11fd9a9SMatthias Ringwald #include "btstack_uart_block.h" 75*f11fd9a9SMatthias Ringwald #include "btstack_uart_slip_wrapper.h" 76*f11fd9a9SMatthias Ringwald #include "classic/btstack_link_key_db_tlv.h" 77f5c04f62SMatthias Ringwald #include "hci.h" 78f5c04f62SMatthias Ringwald #include "hci_dump.h" 797435ec7bSMatthias Ringwald #include "hci_dump_posix_fs.h" 80c8dfe071SMatthias Ringwald #include "hci_transport.h" 81c8dfe071SMatthias Ringwald #include "hci_transport_h4.h" 82c8dfe071SMatthias Ringwald #include "hci_transport_h5.h" 8363ff2b1eSDirk Helbig #include "raspi_get_model.h" 8463ff2b1eSDirk Helbig 85*f11fd9a9SMatthias Ringwald 86f5c04f62SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 87f5c04f62SMatthias Ringwald 8888d4cc4fSMatthias Ringwald typedef enum { 8988d4cc4fSMatthias Ringwald UART_INVALID, 9088d4cc4fSMatthias Ringwald UART_SOFTWARE_NO_FLOW, 9188d4cc4fSMatthias Ringwald UART_HARDWARE_NO_FLOW, 9288d4cc4fSMatthias Ringwald UART_HARDWARE_FLOW 9388d4cc4fSMatthias Ringwald } uart_type_t; 94f5c04f62SMatthias Ringwald 95e0814c90SMatthias Ringwald // default config, updated depending on RasperryPi UART configuration 96f5c04f62SMatthias Ringwald static hci_transport_config_uart_t transport_config = { 97f5c04f62SMatthias Ringwald HCI_TRANSPORT_CONFIG_UART, 98f5c04f62SMatthias Ringwald 115200, 99e0814c90SMatthias Ringwald 0, // main baudrate 100f5c04f62SMatthias Ringwald 0, // flow control 101f5c04f62SMatthias Ringwald NULL, 102f5c04f62SMatthias Ringwald }; 103d9bed121SMatthias Ringwald 104c41e9922SMatthias Ringwald static btstack_uart_config_t uart_config; 105d9bed121SMatthias Ringwald 106c41e9922SMatthias Ringwald static int main_argc; 107c41e9922SMatthias Ringwald static const char ** main_argv; 108d9bed121SMatthias Ringwald 109c41e9922SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 110c41e9922SMatthias Ringwald 111c41e9922SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_" 112c41e9922SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv" 113c41e9922SMatthias Ringwald static char tlv_db_path[100]; 114c41e9922SMatthias Ringwald static const btstack_tlv_t * tlv_impl; 115c41e9922SMatthias Ringwald static btstack_tlv_posix_t tlv_context; 116*f11fd9a9SMatthias Ringwald static bd_addr_t local_addr; 117*f11fd9a9SMatthias Ringwald // shutdown 118*f11fd9a9SMatthias Ringwald static bool shutdown_triggered; 119d9bed121SMatthias Ringwald 120d9bed121SMatthias Ringwald 121d9bed121SMatthias Ringwald static int raspi_speed_to_baud(speed_t baud) 122d9bed121SMatthias Ringwald { 123d9bed121SMatthias Ringwald switch (baud) { 124d9bed121SMatthias Ringwald case B9600: 125d9bed121SMatthias Ringwald return 9600; 126d9bed121SMatthias Ringwald case B19200: 127d9bed121SMatthias Ringwald return 19200; 128d9bed121SMatthias Ringwald case B38400: 129d9bed121SMatthias Ringwald return 38400; 130d9bed121SMatthias Ringwald case B57600: 131d9bed121SMatthias Ringwald return 57600; 132d9bed121SMatthias Ringwald case B115200: 133d9bed121SMatthias Ringwald return 115200; 134d9bed121SMatthias Ringwald case B230400: 135d9bed121SMatthias Ringwald return 230400; 136d9bed121SMatthias Ringwald case B460800: 137d9bed121SMatthias Ringwald return 460800; 138d9bed121SMatthias Ringwald case B500000: 139d9bed121SMatthias Ringwald return 500000; 140d9bed121SMatthias Ringwald case B576000: 141d9bed121SMatthias Ringwald return 576000; 142d9bed121SMatthias Ringwald case B921600: 143d9bed121SMatthias Ringwald return 921600; 144d9bed121SMatthias Ringwald case B1000000: 145d9bed121SMatthias Ringwald return 1000000; 146d9bed121SMatthias Ringwald case B1152000: 147d9bed121SMatthias Ringwald return 1152000; 148d9bed121SMatthias Ringwald case B1500000: 149d9bed121SMatthias Ringwald return 1500000; 150d9bed121SMatthias Ringwald case B2000000: 151d9bed121SMatthias Ringwald return 2000000; 152d9bed121SMatthias Ringwald case B2500000: 153d9bed121SMatthias Ringwald return 2500000; 154d9bed121SMatthias Ringwald case B3000000: 155d9bed121SMatthias Ringwald return 3000000; 156d9bed121SMatthias Ringwald case B3500000: 157d9bed121SMatthias Ringwald return 3500000; 158d9bed121SMatthias Ringwald case B4000000: 159d9bed121SMatthias Ringwald return 4000000; 160d9bed121SMatthias Ringwald default: 161d9bed121SMatthias Ringwald return -1; 162d9bed121SMatthias Ringwald } 163d9bed121SMatthias Ringwald } 164d9bed121SMatthias Ringwald 165d9bed121SMatthias Ringwald static void raspi_get_terminal_params( hci_transport_config_uart_t *tc ) 166d9bed121SMatthias Ringwald { 167d9bed121SMatthias Ringwald // open serial terminal and get parameters 168d9bed121SMatthias Ringwald int fd = open( tc->device_name, O_RDONLY ); 169d9bed121SMatthias Ringwald if( fd < 0 ) 170d9bed121SMatthias Ringwald { 171d9bed121SMatthias Ringwald perror( "can't open serial port" ); 172d9bed121SMatthias Ringwald return; 173d9bed121SMatthias Ringwald } 174d9bed121SMatthias Ringwald struct termios tios; 175d9bed121SMatthias Ringwald tcgetattr( fd, &tios ); 176d9bed121SMatthias Ringwald close( fd ); 177d9bed121SMatthias Ringwald 178d9bed121SMatthias Ringwald speed_t ospeed = cfgetospeed( &tios ); 179d9bed121SMatthias Ringwald int baud = raspi_speed_to_baud( ospeed ); 180d9bed121SMatthias Ringwald printf( "current serial terminal parameter baudrate: %d, flow control: %s\n", baud, (tios.c_cflag&CRTSCTS)?"Hardware":"None" ); 181d9bed121SMatthias Ringwald 182d9bed121SMatthias Ringwald // overwrites the initial baudrate only in case it was likely to be altered before 183d9bed121SMatthias Ringwald if( baud > 9600 ) 184d9bed121SMatthias Ringwald { 185d9bed121SMatthias Ringwald tc->baudrate_init = baud; 186d9bed121SMatthias Ringwald tc->flowcontrol = (tios.c_cflag & CRTSCTS)?1:0; 187d9bed121SMatthias Ringwald } 188d9bed121SMatthias Ringwald } 189d9bed121SMatthias Ringwald 190*f11fd9a9SMatthias Ringwald static void trigger_shutdown(void){ 191f5c04f62SMatthias Ringwald printf("CTRL-C - SIGINT received, shutting down..\n"); 192f5c04f62SMatthias Ringwald log_info("sigint_handler: shutting down"); 193*f11fd9a9SMatthias Ringwald shutdown_triggered = true; 194f5c04f62SMatthias Ringwald hci_power_control(HCI_POWER_OFF); 195f5c04f62SMatthias Ringwald } 196f5c04f62SMatthias Ringwald 197f5c04f62SMatthias Ringwald static int led_state = 0; 198f5c04f62SMatthias Ringwald void hal_led_toggle(void){ 199f5c04f62SMatthias Ringwald led_state = 1 - led_state; 200f5c04f62SMatthias Ringwald printf("LED State %u\n", led_state); 201f5c04f62SMatthias Ringwald } 202f5c04f62SMatthias Ringwald 203f5c04f62SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 204f5c04f62SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 205f5c04f62SMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 206f5c04f62SMatthias Ringwald case BTSTACK_EVENT_STATE: 207*f11fd9a9SMatthias Ringwald switch(btstack_event_state_get_state(packet)){ 208*f11fd9a9SMatthias Ringwald case HCI_STATE_WORKING: 209*f11fd9a9SMatthias Ringwald gap_local_bd_addr(local_addr); 210*f11fd9a9SMatthias Ringwald printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); 21172f99eadSMatthias Ringwald strcpy(tlv_db_path, TLV_DB_PATH_PREFIX); 212*f11fd9a9SMatthias Ringwald strcat(tlv_db_path, bd_addr_to_str(local_addr)); 21372f99eadSMatthias Ringwald strcat(tlv_db_path, TLV_DB_PATH_POSTFIX); 21472f99eadSMatthias Ringwald tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path); 21572f99eadSMatthias Ringwald btstack_tlv_set_instance(tlv_impl, &tlv_context); 2168c8a87eeSMatthias Ringwald #ifdef ENABLE_CLASSIC 2178c8a87eeSMatthias Ringwald hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context)); 2188c8a87eeSMatthias Ringwald #endif 2198c8a87eeSMatthias Ringwald #ifdef ENABLE_BLE 2208c8a87eeSMatthias Ringwald le_device_db_tlv_configure(tlv_impl, &tlv_context); 2218c8a87eeSMatthias Ringwald #endif 222f5c04f62SMatthias Ringwald break; 223*f11fd9a9SMatthias Ringwald case HCI_STATE_OFF: 224*f11fd9a9SMatthias Ringwald btstack_tlv_posix_deinit(&tlv_context); 225*f11fd9a9SMatthias Ringwald if (!shutdown_triggered) break; 226*f11fd9a9SMatthias Ringwald // reset stdin 227*f11fd9a9SMatthias Ringwald btstack_stdin_reset(); 228*f11fd9a9SMatthias Ringwald log_info("Good bye, see you.\n"); 229*f11fd9a9SMatthias Ringwald exit(0); 230*f11fd9a9SMatthias Ringwald break; 231*f11fd9a9SMatthias Ringwald default: 232*f11fd9a9SMatthias Ringwald break; 233*f11fd9a9SMatthias Ringwald } 234*f11fd9a9SMatthias Ringwald break; 235d9bed121SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 236d9bed121SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 237d9bed121SMatthias Ringwald if (hci_event_command_complete_get_return_parameters(packet)[0]) break; 238d9bed121SMatthias Ringwald // terminate, name 248 chars 239d9bed121SMatthias Ringwald packet[6+248] = 0; 240d9bed121SMatthias Ringwald printf("Local name: %s\n", &packet[6]); 241d9bed121SMatthias Ringwald 242d9bed121SMatthias Ringwald btstack_chipset_bcm_set_device_name((const char *)&packet[6]); 243d9bed121SMatthias Ringwald } 244d9bed121SMatthias Ringwald break; 245f5c04f62SMatthias Ringwald default: 246f5c04f62SMatthias Ringwald break; 247f5c04f62SMatthias Ringwald } 248f5c04f62SMatthias Ringwald } 249f5c04f62SMatthias Ringwald 25088d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 251e0814c90SMatthias Ringwald static int raspi_get_bd_addr(bd_addr_t addr){ 25288d4cc4fSMatthias Ringwald 25388d4cc4fSMatthias Ringwald FILE *fd = fopen( "/proc/device-tree/serial-number", "r" ); 25488d4cc4fSMatthias Ringwald if( fd == NULL ){ 25588d4cc4fSMatthias Ringwald fprintf(stderr, "can't read serial number, %s\n", strerror( errno ) ); 25688d4cc4fSMatthias Ringwald return -1; 25788d4cc4fSMatthias Ringwald } 2581b6b55dfSMatthias Ringwald fscanf( fd, "%*08x" "%*02x" "%02" SCNx8 "%02" SCNx8 "%02" SCNx8, &addr[3], &addr[4], &addr[5] ); 25988d4cc4fSMatthias Ringwald fclose( fd ); 26088d4cc4fSMatthias Ringwald 26188d4cc4fSMatthias Ringwald addr[0] = 0xb8; addr[1] = 0x27; addr[2] = 0xeb; 26288d4cc4fSMatthias Ringwald addr[3] ^= 0xaa; addr[4] ^= 0xaa; addr[5] ^= 0xaa; 26388d4cc4fSMatthias Ringwald 26488d4cc4fSMatthias Ringwald return 0; 26588d4cc4fSMatthias Ringwald } 26688d4cc4fSMatthias Ringwald 26788d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart 26888d4cc4fSMatthias Ringwald // on UART_INVALID errno is set 26988d4cc4fSMatthias Ringwald static uart_type_t raspi_get_bluetooth_uart_type(void){ 27088d4cc4fSMatthias Ringwald 271e0814c90SMatthias Ringwald uint8_t deviceUart0[21] = { 0 }; 272e0814c90SMatthias Ringwald FILE *fd = fopen( "/proc/device-tree/aliases/uart0", "r" ); 273e0814c90SMatthias Ringwald if( fd == NULL ) return UART_INVALID; 27488d4cc4fSMatthias Ringwald fscanf( fd, "%20s", deviceUart0 ); 27588d4cc4fSMatthias Ringwald fclose( fd ); 27688d4cc4fSMatthias Ringwald 277e0814c90SMatthias Ringwald uint8_t deviceSerial1[21] = { 0 }; 27888d4cc4fSMatthias Ringwald fd = fopen( "/proc/device-tree/aliases/serial1", "r" ); 27988d4cc4fSMatthias Ringwald if( fd == NULL ) return UART_INVALID; 28088d4cc4fSMatthias Ringwald fscanf( fd, "%20s", deviceSerial1 ); 28188d4cc4fSMatthias Ringwald fclose( fd ); 28288d4cc4fSMatthias Ringwald 283e0814c90SMatthias Ringwald // test if uart0 is an alias for serial1 284e0814c90SMatthias Ringwald if( strncmp( (const char *) deviceUart0, (const char *) deviceSerial1, 21 ) == 0 ){ 28588d4cc4fSMatthias Ringwald // HW uart 28688d4cc4fSMatthias Ringwald size_t count = 0; 28788d4cc4fSMatthias Ringwald uint8_t buf[16]; 28888d4cc4fSMatthias Ringwald fd = fopen( "/proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm,pins", "r" ); 28988d4cc4fSMatthias Ringwald if( fd == NULL ) return UART_INVALID; 29088d4cc4fSMatthias Ringwald count = fread( buf, 1, 16, fd ); 29188d4cc4fSMatthias Ringwald fclose( fd ); 29288d4cc4fSMatthias Ringwald 293e0814c90SMatthias Ringwald // contains assigned pins 294e0814c90SMatthias Ringwald int pins = count / 4; 295e0814c90SMatthias Ringwald if( pins == 4 ){ 29688d4cc4fSMatthias Ringwald return UART_HARDWARE_FLOW; 297e0814c90SMatthias Ringwald } else { 29888d4cc4fSMatthias Ringwald return UART_HARDWARE_NO_FLOW; 29988d4cc4fSMatthias Ringwald } 300e0814c90SMatthias Ringwald } else { 30188d4cc4fSMatthias Ringwald return UART_SOFTWARE_NO_FLOW; 30288d4cc4fSMatthias Ringwald } 30388d4cc4fSMatthias Ringwald } 30488d4cc4fSMatthias Ringwald 305f5c04f62SMatthias Ringwald static void phase2(int status); 306f5c04f62SMatthias Ringwald int main(int argc, const char * argv[]){ 307f5c04f62SMatthias Ringwald 308f5c04f62SMatthias Ringwald /// GET STARTED with BTstack /// 309f5c04f62SMatthias Ringwald btstack_memory_init(); 310f5c04f62SMatthias Ringwald 3117435ec7bSMatthias Ringwald // log into file using HCI_DUMP_PACKETLOGGER format 312f5c04f62SMatthias Ringwald const char * pklg_path = "/tmp/hci_dump.pklg"; 3137435ec7bSMatthias Ringwald hci_dump_posix_fs_open(pklg_path, HCI_DUMP_PACKETLOGGER); 3147435ec7bSMatthias Ringwald const hci_dump_t * hci_dump_impl = hci_dump_posix_fs_get_instance(); 3157435ec7bSMatthias Ringwald hci_dump_init(hci_dump_impl); 316f5c04f62SMatthias Ringwald printf("Packet Log: %s\n", pklg_path); 317f5c04f62SMatthias Ringwald 318f5c04f62SMatthias Ringwald // setup run loop 319f5c04f62SMatthias Ringwald btstack_run_loop_init(btstack_run_loop_posix_get_instance()); 320f5c04f62SMatthias Ringwald 321f5c04f62SMatthias Ringwald // pick serial port and configure uart block driver 322f5c04f62SMatthias Ringwald transport_config.device_name = "/dev/serial1"; 323f5c04f62SMatthias Ringwald 32488d4cc4fSMatthias Ringwald // derive bd_addr from serial number 32588d4cc4fSMatthias Ringwald bd_addr_t addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; 32688d4cc4fSMatthias Ringwald raspi_get_bd_addr(addr); 327f5c04f62SMatthias Ringwald 32888d4cc4fSMatthias Ringwald // set UART config based on raspi Bluetooth UART type 329c41e9922SMatthias Ringwald int bt_reg_en_pin = -1; 330e6a3daa4SMatthias Ringwald bool power_cycle = true; 33188d4cc4fSMatthias Ringwald switch (raspi_get_bluetooth_uart_type()){ 33288d4cc4fSMatthias Ringwald case UART_INVALID: 33388d4cc4fSMatthias Ringwald fprintf(stderr, "can't verify HW uart, %s\n", strerror( errno ) ); 33488d4cc4fSMatthias Ringwald return -1; 33588d4cc4fSMatthias Ringwald case UART_SOFTWARE_NO_FLOW: 33655250ca2SMatthias Ringwald // ?? 337c41e9922SMatthias Ringwald bt_reg_en_pin = 128; 33888d4cc4fSMatthias Ringwald transport_config.baudrate_main = 460800; 33988d4cc4fSMatthias Ringwald transport_config.flowcontrol = 0; 34088d4cc4fSMatthias Ringwald break; 34188d4cc4fSMatthias Ringwald case UART_HARDWARE_NO_FLOW: 342c41e9922SMatthias Ringwald // Raspberry Pi 3 A 34355250ca2SMatthias Ringwald // Raspberry Pi 3 B 344e6a3daa4SMatthias Ringwald // power up with H5 and without power cycle untested/unsupported 345c41e9922SMatthias Ringwald bt_reg_en_pin = 128; 34688d4cc4fSMatthias Ringwald transport_config.baudrate_main = 921600; 34788d4cc4fSMatthias Ringwald transport_config.flowcontrol = 0; 34888d4cc4fSMatthias Ringwald break; 34988d4cc4fSMatthias Ringwald case UART_HARDWARE_FLOW: 350e6a3daa4SMatthias Ringwald // Raspberry Pi Zero W gpio 45, 3 mbps does not work (investigation pending) 351d9bed121SMatthias Ringwald // Raspberry Pi 3A+ vgpio 129 but WLAN + BL 352d9bed121SMatthias Ringwald // Raspberry Pi 3B+ vgpio 129 but WLAN + BL 35388d4cc4fSMatthias Ringwald transport_config.flowcontrol = 1; 354e6a3daa4SMatthias Ringwald int model = raspi_get_model(); 355e6a3daa4SMatthias Ringwald if (model == MODEL_ZERO_W){ 356e6a3daa4SMatthias Ringwald bt_reg_en_pin = 45; 35763ff2b1eSDirk Helbig transport_config.baudrate_main = 921600; 358e6a3daa4SMatthias Ringwald } else { 359e6a3daa4SMatthias Ringwald bt_reg_en_pin = 129; 360e6a3daa4SMatthias Ringwald transport_config.baudrate_main = 3000000; 36163ff2b1eSDirk Helbig } 362e6a3daa4SMatthias Ringwald 363e6a3daa4SMatthias Ringwald #ifdef ENABLE_CONTROLLER_WARM_BOOT 364e6a3daa4SMatthias Ringwald power_cycle = false; 365e6a3daa4SMatthias Ringwald #else 366e6a3daa4SMatthias Ringwald // warn about power cycle on devices with shared reg_en pins 367e6a3daa4SMatthias Ringwald if (model == MODEL_3APLUS || model == MODEL_3BPLUS){ 368e6a3daa4SMatthias Ringwald printf("Wifi and Bluetooth share a single RESET line and BTstack needs to reset Bluetooth -> SSH over Wifi will fail\n"); 369e6a3daa4SMatthias Ringwald printf("Please add ENABLE_CONTROLLER_WARM_BOOT to btstack_config.h to enable startup without RESET\n"); 370e6a3daa4SMatthias Ringwald } 371e6a3daa4SMatthias Ringwald #endif 37288d4cc4fSMatthias Ringwald break; 37388d4cc4fSMatthias Ringwald } 374e6a3daa4SMatthias 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"); 375d9bed121SMatthias Ringwald 376f5c04f62SMatthias Ringwald // get BCM chipset driver 377f5c04f62SMatthias Ringwald const btstack_chipset_t * chipset = btstack_chipset_bcm_instance(); 378f5c04f62SMatthias Ringwald chipset->init(&transport_config); 379f5c04f62SMatthias Ringwald 380f5c04f62SMatthias Ringwald // set path to firmware files 38123a1bf14SMatthias Ringwald btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware/brcm"); 382f5c04f62SMatthias Ringwald 383f5c04f62SMatthias Ringwald // setup UART driver 38479530e37SMatthias Ringwald const btstack_uart_t * uart_driver = btstack_uart_posix_instance(); 385f5c04f62SMatthias Ringwald 386f5c04f62SMatthias Ringwald // extract UART config from transport config 387f5c04f62SMatthias Ringwald uart_config.baudrate = transport_config.baudrate_init; 388f5c04f62SMatthias Ringwald uart_config.flowcontrol = transport_config.flowcontrol; 389f5c04f62SMatthias Ringwald uart_config.device_name = transport_config.device_name; 39079530e37SMatthias Ringwald uart_driver->init(&uart_config); 391f5c04f62SMatthias Ringwald 3926e4c47b0SMatthias Ringwald // HW with FlowControl -> we can use regular h4 mode 3936e4c47b0SMatthias Ringwald const hci_transport_t * transport; 3946e4c47b0SMatthias Ringwald if (transport_config.flowcontrol){ 39579530e37SMatthias Ringwald transport = hci_transport_h4_instance(uart_driver); 3966e4c47b0SMatthias Ringwald } else { 39779530e37SMatthias Ringwald transport = hci_transport_h5_instance(uart_driver); 3986e4c47b0SMatthias Ringwald } 3996e4c47b0SMatthias Ringwald 400f5c04f62SMatthias Ringwald // setup HCI (to be able to use bcm chipset driver) 401f5c04f62SMatthias Ringwald hci_init(transport, (void*) &transport_config); 40288d4cc4fSMatthias Ringwald hci_set_bd_addr( addr ); 403f5c04f62SMatthias Ringwald hci_set_chipset(btstack_chipset_bcm_instance()); 404f5c04f62SMatthias Ringwald 405f5c04f62SMatthias Ringwald // inform about BTstack state 406f5c04f62SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 407f5c04f62SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 408f5c04f62SMatthias Ringwald 409*f11fd9a9SMatthias Ringwald // register callback for CTRL-c 410*f11fd9a9SMatthias Ringwald btstack_signal_register_callback(SIGINT, &trigger_shutdown); 411f5c04f62SMatthias Ringwald 412f5c04f62SMatthias Ringwald main_argc = argc; 413f5c04f62SMatthias Ringwald main_argv = argv; 414f5c04f62SMatthias Ringwald 415c41e9922SMatthias Ringwald // power cycle Bluetooth controller on older models without flowcontrol 416e6a3daa4SMatthias Ringwald if (power_cycle){ 417c41e9922SMatthias Ringwald btstack_control_raspi_set_bt_reg_en_pin(bt_reg_en_pin); 41811e995b1SMatthias Ringwald btstack_control_t *control = btstack_control_raspi_get_instance(); 41911e995b1SMatthias Ringwald control->init(NULL); 42011e995b1SMatthias Ringwald control->off(); 42123a1bf14SMatthias Ringwald usleep( 100000 ); 42211e995b1SMatthias Ringwald control->on(); 423e6a3daa4SMatthias Ringwald } 424e6a3daa4SMatthias Ringwald 425e6a3daa4SMatthias Ringwald if (transport_config.flowcontrol){ 426e6a3daa4SMatthias Ringwald 427e6a3daa4SMatthias Ringwald // re-use current terminal speed (if there was no power cycle) 428e6a3daa4SMatthias Ringwald if (!power_cycle){ 429e6a3daa4SMatthias Ringwald raspi_get_terminal_params( &transport_config ); 430e6a3daa4SMatthias Ringwald } 431e6a3daa4SMatthias Ringwald 432e6a3daa4SMatthias Ringwald // with flowcontrol, we use h4 and are done 433e6a3daa4SMatthias Ringwald btstack_main(main_argc, main_argv); 434e6a3daa4SMatthias Ringwald 435e6a3daa4SMatthias Ringwald } else { 43688d4cc4fSMatthias Ringwald 437c41e9922SMatthias Ringwald // assume BCM4343W used in Pi 3 A/B. Pi 3 A/B+ have a newer controller but support H4 with Flowcontrol 438c41e9922SMatthias Ringwald btstack_chipset_bcm_set_device_name("BCM43430A1"); 439c41e9922SMatthias Ringwald 4406e4c47b0SMatthias Ringwald // phase #1 download firmware 4416e4c47b0SMatthias Ringwald printf("Phase 1: Download firmware\n"); 4426e4c47b0SMatthias Ringwald 443f5c04f62SMatthias Ringwald // phase #2 start main app 44479530e37SMatthias Ringwald btstack_chipset_bcm_download_firmware(uart_driver, transport_config.baudrate_main, &phase2); 4456e4c47b0SMatthias Ringwald } 446f5c04f62SMatthias Ringwald 447f5c04f62SMatthias Ringwald // go 448f5c04f62SMatthias Ringwald btstack_run_loop_execute(); 449f5c04f62SMatthias Ringwald return 0; 450f5c04f62SMatthias Ringwald } 451f5c04f62SMatthias Ringwald 452f5c04f62SMatthias Ringwald static void phase2(int status){ 453f5c04f62SMatthias Ringwald 454f5c04f62SMatthias Ringwald if (status){ 455f5c04f62SMatthias Ringwald printf("Download firmware failed\n"); 456f5c04f62SMatthias Ringwald return; 457f5c04f62SMatthias Ringwald } 458f5c04f62SMatthias Ringwald 459f5c04f62SMatthias Ringwald printf("Phase 2: Main app\n"); 460f5c04f62SMatthias Ringwald 461f5c04f62SMatthias Ringwald // setup app 462f5c04f62SMatthias Ringwald btstack_main(main_argc, main_argv); 463f5c04f62SMatthias Ringwald } 464f5c04f62SMatthias Ringwald 465