xref: /btstack/port/raspi/main.c (revision 63ff2b1e64a6f7fe8fbdd0ac1abfc703a433f88e)
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"
62f5c04f62SMatthias Ringwald #include "btstack_link_key_db_fs.h"
63f5c04f62SMatthias Ringwald #include "btstack_memory.h"
64f5c04f62SMatthias Ringwald #include "btstack_run_loop.h"
65f5c04f62SMatthias Ringwald #include "btstack_run_loop_posix.h"
66f5c04f62SMatthias Ringwald #include "bluetooth_company_id.h"
67f5c04f62SMatthias Ringwald #include "hci.h"
68f5c04f62SMatthias Ringwald #include "hci_dump.h"
69f5c04f62SMatthias Ringwald #include "btstack_stdin.h"
7072f99eadSMatthias Ringwald #include "btstack_tlv_posix.h"
71f5c04f62SMatthias Ringwald 
72f5c04f62SMatthias Ringwald #include "btstack_chipset_bcm.h"
73f5c04f62SMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h"
7411e995b1SMatthias Ringwald #include "btstack_control_raspi.h"
75f5c04f62SMatthias Ringwald 
76*63ff2b1eSDirk Helbig #include "raspi_get_model.h"
77*63ff2b1eSDirk Helbig 
78f5c04f62SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
79f5c04f62SMatthias Ringwald 
8088d4cc4fSMatthias Ringwald typedef enum  {
8188d4cc4fSMatthias Ringwald     UART_INVALID,
8288d4cc4fSMatthias Ringwald     UART_SOFTWARE_NO_FLOW,
8388d4cc4fSMatthias Ringwald     UART_HARDWARE_NO_FLOW,
8488d4cc4fSMatthias Ringwald     UART_HARDWARE_FLOW
8588d4cc4fSMatthias Ringwald } uart_type_t;
86f5c04f62SMatthias Ringwald 
87e0814c90SMatthias Ringwald // default config, updated depending on RasperryPi UART configuration
88f5c04f62SMatthias Ringwald static hci_transport_config_uart_t transport_config = {
89f5c04f62SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
90f5c04f62SMatthias Ringwald     115200,
91e0814c90SMatthias Ringwald     0,       // main baudrate
92f5c04f62SMatthias Ringwald     0,       // flow control
93f5c04f62SMatthias Ringwald     NULL,
94f5c04f62SMatthias Ringwald };
95d9bed121SMatthias Ringwald 
96c41e9922SMatthias Ringwald static btstack_uart_config_t uart_config;
97d9bed121SMatthias Ringwald 
98c41e9922SMatthias Ringwald static int main_argc;
99c41e9922SMatthias Ringwald static const char ** main_argv;
100d9bed121SMatthias Ringwald 
101c41e9922SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
102c41e9922SMatthias Ringwald 
103c41e9922SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
104c41e9922SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
105c41e9922SMatthias Ringwald static char tlv_db_path[100];
106c41e9922SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
107c41e9922SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
108d9bed121SMatthias Ringwald 
109d9bed121SMatthias Ringwald 
110d9bed121SMatthias Ringwald static int raspi_speed_to_baud(speed_t baud)
111d9bed121SMatthias Ringwald {
112d9bed121SMatthias Ringwald     switch (baud) {
113d9bed121SMatthias Ringwald         case B9600:
114d9bed121SMatthias Ringwald             return 9600;
115d9bed121SMatthias Ringwald         case B19200:
116d9bed121SMatthias Ringwald             return 19200;
117d9bed121SMatthias Ringwald         case B38400:
118d9bed121SMatthias Ringwald             return 38400;
119d9bed121SMatthias Ringwald         case B57600:
120d9bed121SMatthias Ringwald             return 57600;
121d9bed121SMatthias Ringwald         case B115200:
122d9bed121SMatthias Ringwald             return 115200;
123d9bed121SMatthias Ringwald         case B230400:
124d9bed121SMatthias Ringwald             return 230400;
125d9bed121SMatthias Ringwald         case B460800:
126d9bed121SMatthias Ringwald             return 460800;
127d9bed121SMatthias Ringwald         case B500000:
128d9bed121SMatthias Ringwald             return 500000;
129d9bed121SMatthias Ringwald         case B576000:
130d9bed121SMatthias Ringwald             return 576000;
131d9bed121SMatthias Ringwald         case B921600:
132d9bed121SMatthias Ringwald             return 921600;
133d9bed121SMatthias Ringwald         case B1000000:
134d9bed121SMatthias Ringwald             return 1000000;
135d9bed121SMatthias Ringwald         case B1152000:
136d9bed121SMatthias Ringwald             return 1152000;
137d9bed121SMatthias Ringwald         case B1500000:
138d9bed121SMatthias Ringwald             return 1500000;
139d9bed121SMatthias Ringwald         case B2000000:
140d9bed121SMatthias Ringwald             return 2000000;
141d9bed121SMatthias Ringwald         case B2500000:
142d9bed121SMatthias Ringwald             return 2500000;
143d9bed121SMatthias Ringwald         case B3000000:
144d9bed121SMatthias Ringwald             return 3000000;
145d9bed121SMatthias Ringwald         case B3500000:
146d9bed121SMatthias Ringwald             return 3500000;
147d9bed121SMatthias Ringwald         case B4000000:
148d9bed121SMatthias Ringwald             return 4000000;
149d9bed121SMatthias Ringwald         default:
150d9bed121SMatthias Ringwald             return -1;
151d9bed121SMatthias Ringwald     }
152d9bed121SMatthias Ringwald }
153d9bed121SMatthias Ringwald 
154d9bed121SMatthias Ringwald static void raspi_get_terminal_params( hci_transport_config_uart_t *tc )
155d9bed121SMatthias Ringwald {
156d9bed121SMatthias Ringwald     // open serial terminal and get parameters
157d9bed121SMatthias Ringwald     int fd = open( tc->device_name, O_RDONLY );
158d9bed121SMatthias Ringwald     if( fd < 0 )
159d9bed121SMatthias Ringwald     {
160d9bed121SMatthias Ringwald         perror( "can't open serial port" );
161d9bed121SMatthias Ringwald         return;
162d9bed121SMatthias Ringwald     }
163d9bed121SMatthias Ringwald     struct termios tios;
164d9bed121SMatthias Ringwald     tcgetattr( fd, &tios );
165d9bed121SMatthias Ringwald     close( fd );
166d9bed121SMatthias Ringwald 
167d9bed121SMatthias Ringwald     speed_t ospeed = cfgetospeed( &tios );
168d9bed121SMatthias Ringwald     int baud = raspi_speed_to_baud( ospeed );
169d9bed121SMatthias Ringwald     printf( "current serial terminal parameter baudrate: %d, flow control: %s\n", baud, (tios.c_cflag&CRTSCTS)?"Hardware":"None" );
170d9bed121SMatthias Ringwald 
171d9bed121SMatthias Ringwald     // overwrites the initial baudrate only in case it was likely to be altered before
172d9bed121SMatthias Ringwald     if( baud > 9600 )
173d9bed121SMatthias Ringwald     {
174d9bed121SMatthias Ringwald         tc->baudrate_init = baud;
175d9bed121SMatthias Ringwald         tc->flowcontrol = (tios.c_cflag & CRTSCTS)?1:0;
176d9bed121SMatthias Ringwald     }
177d9bed121SMatthias Ringwald }
178d9bed121SMatthias Ringwald 
179f5c04f62SMatthias Ringwald static void sigint_handler(int param){
180f5c04f62SMatthias Ringwald     UNUSED(param);
181f5c04f62SMatthias Ringwald 
182f5c04f62SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
183f5c04f62SMatthias Ringwald     log_info("sigint_handler: shutting down");
184f5c04f62SMatthias Ringwald 
185f5c04f62SMatthias Ringwald     // reset anyway
186f5c04f62SMatthias Ringwald     btstack_stdin_reset();
187f5c04f62SMatthias Ringwald 
188f5c04f62SMatthias Ringwald     // power down
189f5c04f62SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
190f5c04f62SMatthias Ringwald     hci_close();
191f5c04f62SMatthias Ringwald     log_info("Good bye, see you.\n");
192f5c04f62SMatthias Ringwald     exit(0);
193f5c04f62SMatthias Ringwald }
194f5c04f62SMatthias Ringwald 
195f5c04f62SMatthias Ringwald static int led_state = 0;
196f5c04f62SMatthias Ringwald void hal_led_toggle(void){
197f5c04f62SMatthias Ringwald     led_state = 1 - led_state;
198f5c04f62SMatthias Ringwald     printf("LED State %u\n", led_state);
199f5c04f62SMatthias Ringwald }
200f5c04f62SMatthias Ringwald 
201f5c04f62SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
202f5c04f62SMatthias Ringwald     bd_addr_t addr;
203f5c04f62SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
204f5c04f62SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
205f5c04f62SMatthias Ringwald         case BTSTACK_EVENT_STATE:
206f5c04f62SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
207f5c04f62SMatthias Ringwald             gap_local_bd_addr(addr);
208f5c04f62SMatthias Ringwald             printf("BTstack up and running at %s\n",  bd_addr_to_str(addr));
20972f99eadSMatthias Ringwald             // setup TLV
21072f99eadSMatthias Ringwald             strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
21172f99eadSMatthias Ringwald             strcat(tlv_db_path, bd_addr_to_str(addr));
21272f99eadSMatthias Ringwald             strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
21372f99eadSMatthias Ringwald             tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
21472f99eadSMatthias Ringwald             btstack_tlv_set_instance(tlv_impl, &tlv_context);
215f5c04f62SMatthias Ringwald             break;
216d9bed121SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
217d9bed121SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
218d9bed121SMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
219d9bed121SMatthias Ringwald                 // terminate, name 248 chars
220d9bed121SMatthias Ringwald                 packet[6+248] = 0;
221d9bed121SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
222d9bed121SMatthias Ringwald 
223d9bed121SMatthias Ringwald                 btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
224d9bed121SMatthias Ringwald             }
225d9bed121SMatthias Ringwald             break;
226f5c04f62SMatthias Ringwald         default:
227f5c04f62SMatthias Ringwald             break;
228f5c04f62SMatthias Ringwald     }
229f5c04f62SMatthias Ringwald }
230f5c04f62SMatthias Ringwald 
23188d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart
232e0814c90SMatthias Ringwald static int raspi_get_bd_addr(bd_addr_t addr){
23388d4cc4fSMatthias Ringwald 
23488d4cc4fSMatthias Ringwald     FILE *fd = fopen( "/proc/device-tree/serial-number", "r" );
23588d4cc4fSMatthias Ringwald     if( fd == NULL ){
23688d4cc4fSMatthias Ringwald         fprintf(stderr, "can't read serial number, %s\n", strerror( errno ) );
23788d4cc4fSMatthias Ringwald         return -1;
23888d4cc4fSMatthias Ringwald     }
2391b6b55dfSMatthias Ringwald     fscanf( fd, "%*08x" "%*02x" "%02" SCNx8 "%02" SCNx8 "%02" SCNx8, &addr[3], &addr[4], &addr[5] );
24088d4cc4fSMatthias Ringwald     fclose( fd );
24188d4cc4fSMatthias Ringwald 
24288d4cc4fSMatthias Ringwald     addr[0] =  0xb8; addr[1]  = 0x27; addr[2] =  0xeb;
24388d4cc4fSMatthias Ringwald     addr[3] ^= 0xaa; addr[4] ^= 0xaa; addr[5] ^= 0xaa;
24488d4cc4fSMatthias Ringwald 
24588d4cc4fSMatthias Ringwald     return 0;
24688d4cc4fSMatthias Ringwald }
24788d4cc4fSMatthias Ringwald 
24888d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart
24988d4cc4fSMatthias Ringwald // on UART_INVALID errno is set
25088d4cc4fSMatthias Ringwald static uart_type_t raspi_get_bluetooth_uart_type(void){
25188d4cc4fSMatthias Ringwald 
252e0814c90SMatthias Ringwald     uint8_t deviceUart0[21] = { 0 };
253e0814c90SMatthias Ringwald     FILE *fd = fopen( "/proc/device-tree/aliases/uart0", "r" );
254e0814c90SMatthias Ringwald     if( fd == NULL ) return UART_INVALID;
25588d4cc4fSMatthias Ringwald     fscanf( fd, "%20s", deviceUart0 );
25688d4cc4fSMatthias Ringwald     fclose( fd );
25788d4cc4fSMatthias Ringwald 
258e0814c90SMatthias Ringwald     uint8_t deviceSerial1[21] = { 0 };
25988d4cc4fSMatthias Ringwald     fd = fopen( "/proc/device-tree/aliases/serial1", "r" );
26088d4cc4fSMatthias Ringwald     if( fd == NULL ) return UART_INVALID;
26188d4cc4fSMatthias Ringwald     fscanf( fd, "%20s", deviceSerial1 );
26288d4cc4fSMatthias Ringwald     fclose( fd );
26388d4cc4fSMatthias Ringwald 
264e0814c90SMatthias Ringwald     // test if uart0 is an alias for serial1
265e0814c90SMatthias Ringwald     if( strncmp( (const char *) deviceUart0, (const char *) deviceSerial1, 21 ) == 0 ){
26688d4cc4fSMatthias Ringwald         // HW uart
26788d4cc4fSMatthias Ringwald         size_t count = 0;
26888d4cc4fSMatthias Ringwald         uint8_t buf[16];
26988d4cc4fSMatthias Ringwald         fd = fopen( "/proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm,pins", "r" );
27088d4cc4fSMatthias Ringwald         if( fd == NULL ) return UART_INVALID;
27188d4cc4fSMatthias Ringwald         count = fread( buf, 1, 16, fd );
27288d4cc4fSMatthias Ringwald         fclose( fd );
27388d4cc4fSMatthias Ringwald 
274e0814c90SMatthias Ringwald         // contains assigned pins
275e0814c90SMatthias Ringwald         int pins = count / 4;
276e0814c90SMatthias Ringwald         if( pins == 4 ){
27788d4cc4fSMatthias Ringwald             return UART_HARDWARE_FLOW;
278e0814c90SMatthias Ringwald         } else {
27988d4cc4fSMatthias Ringwald             return UART_HARDWARE_NO_FLOW;
28088d4cc4fSMatthias Ringwald         }
281e0814c90SMatthias Ringwald     } else {
28288d4cc4fSMatthias Ringwald         return UART_SOFTWARE_NO_FLOW;
28388d4cc4fSMatthias Ringwald     }
28488d4cc4fSMatthias Ringwald }
28588d4cc4fSMatthias Ringwald 
286f5c04f62SMatthias Ringwald static void phase2(int status);
287f5c04f62SMatthias Ringwald int main(int argc, const char * argv[]){
288f5c04f62SMatthias Ringwald 
289f5c04f62SMatthias Ringwald     /// GET STARTED with BTstack ///
290f5c04f62SMatthias Ringwald     btstack_memory_init();
291f5c04f62SMatthias Ringwald 
292f5c04f62SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
293f5c04f62SMatthias Ringwald     const char * pklg_path = "/tmp/hci_dump.pklg";
294f5c04f62SMatthias Ringwald     hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER);
295f5c04f62SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
296f5c04f62SMatthias Ringwald 
297f5c04f62SMatthias Ringwald     // setup run loop
298f5c04f62SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
299f5c04f62SMatthias Ringwald 
300f5c04f62SMatthias Ringwald     // pick serial port and configure uart block driver
301f5c04f62SMatthias Ringwald     transport_config.device_name = "/dev/serial1";
302f5c04f62SMatthias Ringwald 
30388d4cc4fSMatthias Ringwald     // derive bd_addr from serial number
30488d4cc4fSMatthias Ringwald     bd_addr_t addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
30588d4cc4fSMatthias Ringwald     raspi_get_bd_addr(addr);
306f5c04f62SMatthias Ringwald 
30788d4cc4fSMatthias Ringwald     // set UART config based on raspi Bluetooth UART type
308c41e9922SMatthias Ringwald     int bt_reg_en_pin = -1;
30988d4cc4fSMatthias Ringwald     switch (raspi_get_bluetooth_uart_type()){
31088d4cc4fSMatthias Ringwald         case UART_INVALID:
31188d4cc4fSMatthias Ringwald             fprintf(stderr, "can't verify HW uart, %s\n", strerror( errno ) );
31288d4cc4fSMatthias Ringwald             return -1;
31388d4cc4fSMatthias Ringwald         case UART_SOFTWARE_NO_FLOW:
31455250ca2SMatthias Ringwald             // ??
315c41e9922SMatthias Ringwald             bt_reg_en_pin = 128;
31688d4cc4fSMatthias Ringwald             transport_config.baudrate_main = 460800;
31788d4cc4fSMatthias Ringwald             transport_config.flowcontrol   = 0;
31888d4cc4fSMatthias Ringwald             break;
31988d4cc4fSMatthias Ringwald         case UART_HARDWARE_NO_FLOW:
320c41e9922SMatthias Ringwald             // Raspberry Pi 3 A
32155250ca2SMatthias Ringwald             // Raspberry Pi 3 B
322c41e9922SMatthias Ringwald             bt_reg_en_pin = 128;
32388d4cc4fSMatthias Ringwald             transport_config.baudrate_main = 921600;
32488d4cc4fSMatthias Ringwald             transport_config.flowcontrol   = 0;
32588d4cc4fSMatthias Ringwald             break;
32688d4cc4fSMatthias Ringwald         case UART_HARDWARE_FLOW:
327*63ff2b1eSDirk Helbig             // Raspberry Pi Zero W gpio 45
328d9bed121SMatthias Ringwald             // Raspberry Pi 3A+ vgpio 129 but WLAN + BL
329d9bed121SMatthias Ringwald             // Raspberry Pi 3B+ vgpio 129 but WLAN + BL
330*63ff2b1eSDirk Helbig             transport_config.baudrate_main = 3000000;
33188d4cc4fSMatthias Ringwald             transport_config.flowcontrol = 1;
332*63ff2b1eSDirk Helbig 
333*63ff2b1eSDirk Helbig             // 3 mbps does not work on Zero W (investigation pending)
334*63ff2b1eSDirk Helbig             if (raspi_get_model() == MODEL_ZERO_W){
335*63ff2b1eSDirk Helbig                 transport_config.baudrate_main = 921600;
336*63ff2b1eSDirk Helbig             }
33788d4cc4fSMatthias Ringwald             break;
33888d4cc4fSMatthias Ringwald     }
339c41e9922SMatthias Ringwald     printf("%s, %u, BT_REG_EN at GPIO %u\n", transport_config.flowcontrol ? "H4":"H5", transport_config.baudrate_main, bt_reg_en_pin);
340d9bed121SMatthias Ringwald 
341f5c04f62SMatthias Ringwald     // get BCM chipset driver
342f5c04f62SMatthias Ringwald     const btstack_chipset_t * chipset = btstack_chipset_bcm_instance();
343f5c04f62SMatthias Ringwald     chipset->init(&transport_config);
344f5c04f62SMatthias Ringwald 
345f5c04f62SMatthias Ringwald     // set path to firmware files
34623a1bf14SMatthias Ringwald     btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware/brcm");
347f5c04f62SMatthias Ringwald 
348f5c04f62SMatthias Ringwald     // setup UART driver
349f5c04f62SMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance();
350f5c04f62SMatthias Ringwald 
351f5c04f62SMatthias Ringwald     // extract UART config from transport config
352f5c04f62SMatthias Ringwald     uart_config.baudrate    = transport_config.baudrate_init;
353f5c04f62SMatthias Ringwald     uart_config.flowcontrol = transport_config.flowcontrol;
354f5c04f62SMatthias Ringwald     uart_config.device_name = transport_config.device_name;
355f5c04f62SMatthias Ringwald     uart_driver->init(&uart_config);
356f5c04f62SMatthias Ringwald 
3576e4c47b0SMatthias Ringwald     // HW with FlowControl -> we can use regular h4 mode
3586e4c47b0SMatthias Ringwald     const hci_transport_t * transport;
3596e4c47b0SMatthias Ringwald     if (transport_config.flowcontrol){
3606e4c47b0SMatthias Ringwald         transport = hci_transport_h4_instance(uart_driver);
3616e4c47b0SMatthias Ringwald     } else {
3626e4c47b0SMatthias Ringwald         transport = hci_transport_h5_instance(uart_driver);
3636e4c47b0SMatthias Ringwald     }
3646e4c47b0SMatthias Ringwald 
365f5c04f62SMatthias Ringwald     // setup HCI (to be able to use bcm chipset driver)
366f5c04f62SMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
367f5c04f62SMatthias Ringwald     hci_init(transport, (void*) &transport_config);
36888d4cc4fSMatthias Ringwald     hci_set_bd_addr( addr );
369f5c04f62SMatthias Ringwald     hci_set_chipset(btstack_chipset_bcm_instance());
37011e995b1SMatthias Ringwald     hci_set_link_key_db(link_key_db);
371f5c04f62SMatthias Ringwald 
372f5c04f62SMatthias Ringwald     // inform about BTstack state
373f5c04f62SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
374f5c04f62SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
375f5c04f62SMatthias Ringwald 
376f5c04f62SMatthias Ringwald     // handle CTRL-c
377f5c04f62SMatthias Ringwald     signal(SIGINT, sigint_handler);
378f5c04f62SMatthias Ringwald 
379f5c04f62SMatthias Ringwald     main_argc = argc;
380f5c04f62SMatthias Ringwald     main_argv = argv;
381f5c04f62SMatthias Ringwald 
382c41e9922SMatthias Ringwald     if (transport_config.flowcontrol){
383c41e9922SMatthias Ringwald 
384c41e9922SMatthias Ringwald         // re-use current terminal speed
385c41e9922SMatthias Ringwald         raspi_get_terminal_params( &transport_config );
386c41e9922SMatthias Ringwald 
387c41e9922SMatthias Ringwald         // with flowcontrol, we use h4 and are done
388c41e9922SMatthias Ringwald         btstack_main(main_argc, main_argv);
389c41e9922SMatthias Ringwald 
390c41e9922SMatthias Ringwald     } else {
391c41e9922SMatthias Ringwald 
392c41e9922SMatthias Ringwald         // power cycle Bluetooth controller on older models without flowcontrol
393c41e9922SMatthias Ringwald         printf("Power Cycle Controller\n");
394c41e9922SMatthias Ringwald         btstack_control_raspi_set_bt_reg_en_pin(bt_reg_en_pin);
39511e995b1SMatthias Ringwald         btstack_control_t *control = btstack_control_raspi_get_instance();
39611e995b1SMatthias Ringwald         control->init(NULL);
39711e995b1SMatthias Ringwald         control->off();
39823a1bf14SMatthias Ringwald         usleep( 100000 );
39911e995b1SMatthias Ringwald         control->on();
40088d4cc4fSMatthias Ringwald 
401c41e9922SMatthias Ringwald         // assume BCM4343W used in Pi 3 A/B. Pi 3 A/B+ have a newer controller but support H4 with Flowcontrol
402c41e9922SMatthias Ringwald         btstack_chipset_bcm_set_device_name("BCM43430A1");
403c41e9922SMatthias Ringwald 
4046e4c47b0SMatthias Ringwald         // phase #1 download firmware
4056e4c47b0SMatthias Ringwald         printf("Phase 1: Download firmware\n");
4066e4c47b0SMatthias Ringwald 
407f5c04f62SMatthias Ringwald         // phase #2 start main app
408f5c04f62SMatthias Ringwald         btstack_chipset_bcm_download_firmware(uart_driver, transport_config.baudrate_main, &phase2);
4096e4c47b0SMatthias Ringwald     }
410f5c04f62SMatthias Ringwald 
411f5c04f62SMatthias Ringwald     // go
412f5c04f62SMatthias Ringwald     btstack_run_loop_execute();
413f5c04f62SMatthias Ringwald     return 0;
414f5c04f62SMatthias Ringwald }
415f5c04f62SMatthias Ringwald 
416f5c04f62SMatthias Ringwald static void phase2(int status){
417f5c04f62SMatthias Ringwald 
418f5c04f62SMatthias Ringwald     if (status){
419f5c04f62SMatthias Ringwald         printf("Download firmware failed\n");
420f5c04f62SMatthias Ringwald         return;
421f5c04f62SMatthias Ringwald     }
422f5c04f62SMatthias Ringwald 
423f5c04f62SMatthias Ringwald     printf("Phase 2: Main app\n");
424f5c04f62SMatthias Ringwald 
425f5c04f62SMatthias Ringwald     // setup app
426f5c04f62SMatthias Ringwald     btstack_main(main_argc, main_argv);
427f5c04f62SMatthias Ringwald }
428f5c04f62SMatthias Ringwald 
429