xref: /btstack/port/raspi/main.c (revision c41e99220ec957db1b4a31a6bd5900185fcee9f8)
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 //
42*c41e9922SMatthias Ringwald //   Port for Rasperry Pi with built-in BCM chipset via H4 or H5
43f5c04f62SMatthias Ringwald //
44f5c04f62SMatthias Ringwald // *****************************************************************************
45f5c04f62SMatthias Ringwald 
46*c41e9922SMatthias Ringwald #include <errno.h>
47*c41e9922SMatthias Ringwald #include <fcntl.h>
4888d4cc4fSMatthias Ringwald #include <inttypes.h>
49*c41e9922SMatthias Ringwald #include <signal.h>
50*c41e9922SMatthias Ringwald #include <stdint.h>
51f5c04f62SMatthias Ringwald #include <stdio.h>
52f5c04f62SMatthias Ringwald #include <stdlib.h>
53f5c04f62SMatthias Ringwald #include <string.h>
54*c41e9922SMatthias Ringwald #include <sys/stat.h>
55*c41e9922SMatthias Ringwald #include <termios.h>
56*c41e9922SMatthias 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 
76f5c04f62SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
77f5c04f62SMatthias Ringwald 
7888d4cc4fSMatthias Ringwald typedef enum  {
7988d4cc4fSMatthias Ringwald     UART_INVALID,
8088d4cc4fSMatthias Ringwald     UART_SOFTWARE_NO_FLOW,
8188d4cc4fSMatthias Ringwald     UART_HARDWARE_NO_FLOW,
8288d4cc4fSMatthias Ringwald     UART_HARDWARE_FLOW
8388d4cc4fSMatthias Ringwald } uart_type_t;
84f5c04f62SMatthias Ringwald 
85e0814c90SMatthias Ringwald // default config, updated depending on RasperryPi UART configuration
86f5c04f62SMatthias Ringwald static hci_transport_config_uart_t transport_config = {
87f5c04f62SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
88f5c04f62SMatthias Ringwald     115200,
89e0814c90SMatthias Ringwald     0,       // main baudrate
90f5c04f62SMatthias Ringwald     0,       // flow control
91f5c04f62SMatthias Ringwald     NULL,
92f5c04f62SMatthias Ringwald };
93d9bed121SMatthias Ringwald 
94*c41e9922SMatthias Ringwald static btstack_uart_config_t uart_config;
95d9bed121SMatthias Ringwald 
96*c41e9922SMatthias Ringwald static int main_argc;
97*c41e9922SMatthias Ringwald static const char ** main_argv;
98d9bed121SMatthias Ringwald 
99*c41e9922SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
100*c41e9922SMatthias Ringwald 
101*c41e9922SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
102*c41e9922SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
103*c41e9922SMatthias Ringwald static char tlv_db_path[100];
104*c41e9922SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
105*c41e9922SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
106d9bed121SMatthias Ringwald 
107d9bed121SMatthias Ringwald 
108d9bed121SMatthias Ringwald static int raspi_speed_to_baud(speed_t baud)
109d9bed121SMatthias Ringwald {
110d9bed121SMatthias Ringwald     switch (baud) {
111d9bed121SMatthias Ringwald         case B9600:
112d9bed121SMatthias Ringwald             return 9600;
113d9bed121SMatthias Ringwald         case B19200:
114d9bed121SMatthias Ringwald             return 19200;
115d9bed121SMatthias Ringwald         case B38400:
116d9bed121SMatthias Ringwald             return 38400;
117d9bed121SMatthias Ringwald         case B57600:
118d9bed121SMatthias Ringwald             return 57600;
119d9bed121SMatthias Ringwald         case B115200:
120d9bed121SMatthias Ringwald             return 115200;
121d9bed121SMatthias Ringwald         case B230400:
122d9bed121SMatthias Ringwald             return 230400;
123d9bed121SMatthias Ringwald         case B460800:
124d9bed121SMatthias Ringwald             return 460800;
125d9bed121SMatthias Ringwald         case B500000:
126d9bed121SMatthias Ringwald             return 500000;
127d9bed121SMatthias Ringwald         case B576000:
128d9bed121SMatthias Ringwald             return 576000;
129d9bed121SMatthias Ringwald         case B921600:
130d9bed121SMatthias Ringwald             return 921600;
131d9bed121SMatthias Ringwald         case B1000000:
132d9bed121SMatthias Ringwald             return 1000000;
133d9bed121SMatthias Ringwald         case B1152000:
134d9bed121SMatthias Ringwald             return 1152000;
135d9bed121SMatthias Ringwald         case B1500000:
136d9bed121SMatthias Ringwald             return 1500000;
137d9bed121SMatthias Ringwald         case B2000000:
138d9bed121SMatthias Ringwald             return 2000000;
139d9bed121SMatthias Ringwald         case B2500000:
140d9bed121SMatthias Ringwald             return 2500000;
141d9bed121SMatthias Ringwald         case B3000000:
142d9bed121SMatthias Ringwald             return 3000000;
143d9bed121SMatthias Ringwald         case B3500000:
144d9bed121SMatthias Ringwald             return 3500000;
145d9bed121SMatthias Ringwald         case B4000000:
146d9bed121SMatthias Ringwald             return 4000000;
147d9bed121SMatthias Ringwald         default:
148d9bed121SMatthias Ringwald             return -1;
149d9bed121SMatthias Ringwald     }
150d9bed121SMatthias Ringwald }
151d9bed121SMatthias Ringwald 
152d9bed121SMatthias Ringwald static void raspi_get_terminal_params( hci_transport_config_uart_t *tc )
153d9bed121SMatthias Ringwald {
154d9bed121SMatthias Ringwald     // open serial terminal and get parameters
155d9bed121SMatthias Ringwald     int fd = open( tc->device_name, O_RDONLY );
156d9bed121SMatthias Ringwald     if( fd < 0 )
157d9bed121SMatthias Ringwald     {
158d9bed121SMatthias Ringwald         perror( "can't open serial port" );
159d9bed121SMatthias Ringwald         return;
160d9bed121SMatthias Ringwald     }
161d9bed121SMatthias Ringwald     struct termios tios;
162d9bed121SMatthias Ringwald     tcgetattr( fd, &tios );
163d9bed121SMatthias Ringwald     close( fd );
164d9bed121SMatthias Ringwald 
165d9bed121SMatthias Ringwald     speed_t ospeed = cfgetospeed( &tios );
166d9bed121SMatthias Ringwald     int baud = raspi_speed_to_baud( ospeed );
167d9bed121SMatthias Ringwald     printf( "current serial terminal parameter baudrate: %d, flow control: %s\n", baud, (tios.c_cflag&CRTSCTS)?"Hardware":"None" );
168d9bed121SMatthias Ringwald 
169d9bed121SMatthias Ringwald     // overwrites the initial baudrate only in case it was likely to be altered before
170d9bed121SMatthias Ringwald     if( baud > 9600 )
171d9bed121SMatthias Ringwald     {
172d9bed121SMatthias Ringwald         tc->baudrate_init = baud;
173d9bed121SMatthias Ringwald         tc->flowcontrol = (tios.c_cflag & CRTSCTS)?1:0;
174d9bed121SMatthias Ringwald     }
175d9bed121SMatthias Ringwald }
176d9bed121SMatthias Ringwald 
177f5c04f62SMatthias Ringwald static void sigint_handler(int param){
178f5c04f62SMatthias Ringwald     UNUSED(param);
179f5c04f62SMatthias Ringwald 
180f5c04f62SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
181f5c04f62SMatthias Ringwald     log_info("sigint_handler: shutting down");
182f5c04f62SMatthias Ringwald 
183f5c04f62SMatthias Ringwald     // reset anyway
184f5c04f62SMatthias Ringwald     btstack_stdin_reset();
185f5c04f62SMatthias Ringwald 
186f5c04f62SMatthias Ringwald     // power down
187f5c04f62SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
188f5c04f62SMatthias Ringwald     hci_close();
189f5c04f62SMatthias Ringwald     log_info("Good bye, see you.\n");
190f5c04f62SMatthias Ringwald     exit(0);
191f5c04f62SMatthias Ringwald }
192f5c04f62SMatthias Ringwald 
193f5c04f62SMatthias Ringwald static int led_state = 0;
194f5c04f62SMatthias Ringwald void hal_led_toggle(void){
195f5c04f62SMatthias Ringwald     led_state = 1 - led_state;
196f5c04f62SMatthias Ringwald     printf("LED State %u\n", led_state);
197f5c04f62SMatthias Ringwald }
198f5c04f62SMatthias Ringwald 
199f5c04f62SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
200f5c04f62SMatthias Ringwald     bd_addr_t addr;
201f5c04f62SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
202f5c04f62SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
203f5c04f62SMatthias Ringwald         case BTSTACK_EVENT_STATE:
204f5c04f62SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
205f5c04f62SMatthias Ringwald             gap_local_bd_addr(addr);
206f5c04f62SMatthias Ringwald             printf("BTstack up and running at %s\n",  bd_addr_to_str(addr));
20772f99eadSMatthias Ringwald             // setup TLV
20872f99eadSMatthias Ringwald             strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
20972f99eadSMatthias Ringwald             strcat(tlv_db_path, bd_addr_to_str(addr));
21072f99eadSMatthias Ringwald             strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
21172f99eadSMatthias Ringwald             tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
21272f99eadSMatthias Ringwald             btstack_tlv_set_instance(tlv_impl, &tlv_context);
213f5c04f62SMatthias Ringwald             break;
214d9bed121SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
215d9bed121SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
216d9bed121SMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
217d9bed121SMatthias Ringwald                 // terminate, name 248 chars
218d9bed121SMatthias Ringwald                 packet[6+248] = 0;
219d9bed121SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
220d9bed121SMatthias Ringwald 
221d9bed121SMatthias Ringwald                 btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
222d9bed121SMatthias Ringwald             }
223d9bed121SMatthias Ringwald             break;
224f5c04f62SMatthias Ringwald         default:
225f5c04f62SMatthias Ringwald             break;
226f5c04f62SMatthias Ringwald     }
227f5c04f62SMatthias Ringwald }
228f5c04f62SMatthias Ringwald 
22988d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart
230e0814c90SMatthias Ringwald static int raspi_get_bd_addr(bd_addr_t addr){
23188d4cc4fSMatthias Ringwald 
23288d4cc4fSMatthias Ringwald     FILE *fd = fopen( "/proc/device-tree/serial-number", "r" );
23388d4cc4fSMatthias Ringwald     if( fd == NULL ){
23488d4cc4fSMatthias Ringwald         fprintf(stderr, "can't read serial number, %s\n", strerror( errno ) );
23588d4cc4fSMatthias Ringwald         return -1;
23688d4cc4fSMatthias Ringwald     }
2371b6b55dfSMatthias Ringwald     fscanf( fd, "%*08x" "%*02x" "%02" SCNx8 "%02" SCNx8 "%02" SCNx8, &addr[3], &addr[4], &addr[5] );
23888d4cc4fSMatthias Ringwald     fclose( fd );
23988d4cc4fSMatthias Ringwald 
24088d4cc4fSMatthias Ringwald     addr[0] =  0xb8; addr[1]  = 0x27; addr[2] =  0xeb;
24188d4cc4fSMatthias Ringwald     addr[3] ^= 0xaa; addr[4] ^= 0xaa; addr[5] ^= 0xaa;
24288d4cc4fSMatthias Ringwald 
24388d4cc4fSMatthias Ringwald     return 0;
24488d4cc4fSMatthias Ringwald }
24588d4cc4fSMatthias Ringwald 
24688d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart
24788d4cc4fSMatthias Ringwald // on UART_INVALID errno is set
24888d4cc4fSMatthias Ringwald static uart_type_t raspi_get_bluetooth_uart_type(void){
24988d4cc4fSMatthias Ringwald 
250e0814c90SMatthias Ringwald     uint8_t deviceUart0[21] = { 0 };
251e0814c90SMatthias Ringwald     FILE *fd = fopen( "/proc/device-tree/aliases/uart0", "r" );
252e0814c90SMatthias Ringwald     if( fd == NULL ) return UART_INVALID;
25388d4cc4fSMatthias Ringwald     fscanf( fd, "%20s", deviceUart0 );
25488d4cc4fSMatthias Ringwald     fclose( fd );
25588d4cc4fSMatthias Ringwald 
256e0814c90SMatthias Ringwald     uint8_t deviceSerial1[21] = { 0 };
25788d4cc4fSMatthias Ringwald     fd = fopen( "/proc/device-tree/aliases/serial1", "r" );
25888d4cc4fSMatthias Ringwald     if( fd == NULL ) return UART_INVALID;
25988d4cc4fSMatthias Ringwald     fscanf( fd, "%20s", deviceSerial1 );
26088d4cc4fSMatthias Ringwald     fclose( fd );
26188d4cc4fSMatthias Ringwald 
262e0814c90SMatthias Ringwald     // test if uart0 is an alias for serial1
263e0814c90SMatthias Ringwald     if( strncmp( (const char *) deviceUart0, (const char *) deviceSerial1, 21 ) == 0 ){
26488d4cc4fSMatthias Ringwald         // HW uart
26588d4cc4fSMatthias Ringwald         size_t count = 0;
26688d4cc4fSMatthias Ringwald         uint8_t buf[16];
26788d4cc4fSMatthias Ringwald         fd = fopen( "/proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm,pins", "r" );
26888d4cc4fSMatthias Ringwald         if( fd == NULL ) return UART_INVALID;
26988d4cc4fSMatthias Ringwald         count = fread( buf, 1, 16, fd );
27088d4cc4fSMatthias Ringwald         fclose( fd );
27188d4cc4fSMatthias Ringwald 
272e0814c90SMatthias Ringwald         // contains assigned pins
273e0814c90SMatthias Ringwald         int pins = count / 4;
274e0814c90SMatthias Ringwald         if( pins == 4 ){
27588d4cc4fSMatthias Ringwald             return UART_HARDWARE_FLOW;
276e0814c90SMatthias Ringwald         } else {
27788d4cc4fSMatthias Ringwald             return UART_HARDWARE_NO_FLOW;
27888d4cc4fSMatthias Ringwald         }
279e0814c90SMatthias Ringwald     } else {
28088d4cc4fSMatthias Ringwald         return UART_SOFTWARE_NO_FLOW;
28188d4cc4fSMatthias Ringwald     }
28288d4cc4fSMatthias Ringwald }
28388d4cc4fSMatthias Ringwald 
284f5c04f62SMatthias Ringwald static void phase2(int status);
285f5c04f62SMatthias Ringwald int main(int argc, const char * argv[]){
286f5c04f62SMatthias Ringwald 
287f5c04f62SMatthias Ringwald     /// GET STARTED with BTstack ///
288f5c04f62SMatthias Ringwald     btstack_memory_init();
289f5c04f62SMatthias Ringwald 
290f5c04f62SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
291f5c04f62SMatthias Ringwald     const char * pklg_path = "/tmp/hci_dump.pklg";
292f5c04f62SMatthias Ringwald     hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER);
293f5c04f62SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
294f5c04f62SMatthias Ringwald 
295f5c04f62SMatthias Ringwald     // setup run loop
296f5c04f62SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
297f5c04f62SMatthias Ringwald 
298f5c04f62SMatthias Ringwald     // pick serial port and configure uart block driver
299f5c04f62SMatthias Ringwald     transport_config.device_name = "/dev/serial1";
300f5c04f62SMatthias Ringwald 
30188d4cc4fSMatthias Ringwald     // derive bd_addr from serial number
30288d4cc4fSMatthias Ringwald     bd_addr_t addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
30388d4cc4fSMatthias Ringwald     raspi_get_bd_addr(addr);
304f5c04f62SMatthias Ringwald 
30588d4cc4fSMatthias Ringwald     // set UART config based on raspi Bluetooth UART type
306*c41e9922SMatthias Ringwald     int bt_reg_en_pin = -1;
30788d4cc4fSMatthias Ringwald     switch (raspi_get_bluetooth_uart_type()){
30888d4cc4fSMatthias Ringwald         case UART_INVALID:
30988d4cc4fSMatthias Ringwald             fprintf(stderr, "can't verify HW uart, %s\n", strerror( errno ) );
31088d4cc4fSMatthias Ringwald             return -1;
31188d4cc4fSMatthias Ringwald         case UART_SOFTWARE_NO_FLOW:
31255250ca2SMatthias Ringwald             // ??
313*c41e9922SMatthias Ringwald             bt_reg_en_pin = 128;
31488d4cc4fSMatthias Ringwald             transport_config.baudrate_main = 460800;
31588d4cc4fSMatthias Ringwald             transport_config.flowcontrol   = 0;
31688d4cc4fSMatthias Ringwald             break;
31788d4cc4fSMatthias Ringwald         case UART_HARDWARE_NO_FLOW:
318*c41e9922SMatthias Ringwald             // Raspberry Pi 3 A
31955250ca2SMatthias Ringwald             // Raspberry Pi 3 B
320*c41e9922SMatthias Ringwald             bt_reg_en_pin = 128;
32188d4cc4fSMatthias Ringwald             transport_config.baudrate_main = 921600;
32288d4cc4fSMatthias Ringwald             transport_config.flowcontrol   = 0;
32388d4cc4fSMatthias Ringwald             break;
32488d4cc4fSMatthias Ringwald         case UART_HARDWARE_FLOW:
32555250ca2SMatthias Ringwald             // Raspberry Pi Zero W
326d9bed121SMatthias Ringwald             // Raspberry Pi 3A+ vgpio 129 but WLAN + BL
327d9bed121SMatthias Ringwald             // Raspberry Pi 3B+ vgpio 129 but WLAN + BL
328*c41e9922SMatthias Ringwald             bt_reg_en_pin = 45;
329*c41e9922SMatthias Ringwald             transport_config.baudrate_main = 921600;
33088d4cc4fSMatthias Ringwald             transport_config.flowcontrol   = 1;
33188d4cc4fSMatthias Ringwald             break;
33288d4cc4fSMatthias Ringwald     }
333*c41e9922SMatthias Ringwald     printf("%s, %u, BT_REG_EN at GPIO %u\n", transport_config.flowcontrol ? "H4":"H5", transport_config.baudrate_main, bt_reg_en_pin);
334d9bed121SMatthias Ringwald 
335f5c04f62SMatthias Ringwald     // get BCM chipset driver
336f5c04f62SMatthias Ringwald     const btstack_chipset_t * chipset = btstack_chipset_bcm_instance();
337f5c04f62SMatthias Ringwald     chipset->init(&transport_config);
338f5c04f62SMatthias Ringwald 
339f5c04f62SMatthias Ringwald     // set path to firmware files
34023a1bf14SMatthias Ringwald     btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware/brcm");
341f5c04f62SMatthias Ringwald 
342f5c04f62SMatthias Ringwald     // setup UART driver
343f5c04f62SMatthias Ringwald     const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance();
344f5c04f62SMatthias Ringwald 
345f5c04f62SMatthias Ringwald     // extract UART config from transport config
346f5c04f62SMatthias Ringwald     uart_config.baudrate    = transport_config.baudrate_init;
347f5c04f62SMatthias Ringwald     uart_config.flowcontrol = transport_config.flowcontrol;
348f5c04f62SMatthias Ringwald     uart_config.device_name = transport_config.device_name;
349f5c04f62SMatthias Ringwald     uart_driver->init(&uart_config);
350f5c04f62SMatthias Ringwald 
3516e4c47b0SMatthias Ringwald     // HW with FlowControl -> we can use regular h4 mode
3526e4c47b0SMatthias Ringwald     const hci_transport_t * transport;
3536e4c47b0SMatthias Ringwald     if (transport_config.flowcontrol){
3546e4c47b0SMatthias Ringwald         transport = hci_transport_h4_instance(uart_driver);
3556e4c47b0SMatthias Ringwald     } else {
3566e4c47b0SMatthias Ringwald         transport = hci_transport_h5_instance(uart_driver);
3576e4c47b0SMatthias Ringwald     }
3586e4c47b0SMatthias Ringwald 
359f5c04f62SMatthias Ringwald     // setup HCI (to be able to use bcm chipset driver)
360f5c04f62SMatthias Ringwald     const btstack_link_key_db_t * link_key_db = btstack_link_key_db_fs_instance();
361f5c04f62SMatthias Ringwald     hci_init(transport, (void*) &transport_config);
36288d4cc4fSMatthias Ringwald     hci_set_bd_addr( addr );
363f5c04f62SMatthias Ringwald     hci_set_chipset(btstack_chipset_bcm_instance());
36411e995b1SMatthias Ringwald     hci_set_link_key_db(link_key_db);
365f5c04f62SMatthias Ringwald 
366f5c04f62SMatthias Ringwald     // inform about BTstack state
367f5c04f62SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
368f5c04f62SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
369f5c04f62SMatthias Ringwald 
370f5c04f62SMatthias Ringwald     // handle CTRL-c
371f5c04f62SMatthias Ringwald     signal(SIGINT, sigint_handler);
372f5c04f62SMatthias Ringwald 
373f5c04f62SMatthias Ringwald     main_argc = argc;
374f5c04f62SMatthias Ringwald     main_argv = argv;
375f5c04f62SMatthias Ringwald 
376*c41e9922SMatthias Ringwald     if (transport_config.flowcontrol){
377*c41e9922SMatthias Ringwald 
378*c41e9922SMatthias Ringwald         // re-use current terminal speed
379*c41e9922SMatthias Ringwald         raspi_get_terminal_params( &transport_config );
380*c41e9922SMatthias Ringwald 
381*c41e9922SMatthias Ringwald         // with flowcontrol, we use h4 and are done
382*c41e9922SMatthias Ringwald         btstack_main(main_argc, main_argv);
383*c41e9922SMatthias Ringwald 
384*c41e9922SMatthias Ringwald     } else {
385*c41e9922SMatthias Ringwald 
386*c41e9922SMatthias Ringwald         // power cycle Bluetooth controller on older models without flowcontrol
387*c41e9922SMatthias Ringwald         printf("Power Cycle Controller\n");
388*c41e9922SMatthias Ringwald         btstack_control_raspi_set_bt_reg_en_pin(bt_reg_en_pin);
38911e995b1SMatthias Ringwald         btstack_control_t *control = btstack_control_raspi_get_instance();
39011e995b1SMatthias Ringwald         control->init(NULL);
39111e995b1SMatthias Ringwald         control->off();
39223a1bf14SMatthias Ringwald         usleep( 100000 );
39311e995b1SMatthias Ringwald         control->on();
39488d4cc4fSMatthias Ringwald 
395*c41e9922SMatthias Ringwald         // assume BCM4343W used in Pi 3 A/B. Pi 3 A/B+ have a newer controller but support H4 with Flowcontrol
396*c41e9922SMatthias Ringwald         btstack_chipset_bcm_set_device_name("BCM43430A1");
397*c41e9922SMatthias Ringwald 
3986e4c47b0SMatthias Ringwald         // phase #1 download firmware
3996e4c47b0SMatthias Ringwald         printf("Phase 1: Download firmware\n");
4006e4c47b0SMatthias Ringwald 
401f5c04f62SMatthias Ringwald         // phase #2 start main app
402f5c04f62SMatthias Ringwald         btstack_chipset_bcm_download_firmware(uart_driver, transport_config.baudrate_main, &phase2);
4036e4c47b0SMatthias Ringwald     }
404f5c04f62SMatthias Ringwald 
405f5c04f62SMatthias Ringwald     // go
406f5c04f62SMatthias Ringwald     btstack_run_loop_execute();
407f5c04f62SMatthias Ringwald     return 0;
408f5c04f62SMatthias Ringwald }
409f5c04f62SMatthias Ringwald 
410f5c04f62SMatthias Ringwald static void phase2(int status){
411f5c04f62SMatthias Ringwald 
412f5c04f62SMatthias Ringwald     if (status){
413f5c04f62SMatthias Ringwald         printf("Download firmware failed\n");
414f5c04f62SMatthias Ringwald         return;
415f5c04f62SMatthias Ringwald     }
416f5c04f62SMatthias Ringwald 
417f5c04f62SMatthias Ringwald     printf("Phase 2: Main app\n");
418f5c04f62SMatthias Ringwald 
419f5c04f62SMatthias Ringwald     // setup app
420f5c04f62SMatthias Ringwald     btstack_main(main_argc, main_argv);
421f5c04f62SMatthias Ringwald }
422f5c04f62SMatthias Ringwald 
423