xref: /btstack/port/raspi/main.c (revision 08b9cddccb3c25f750e373daf5340dc7f0a70971)
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"
70f5c04f62SMatthias Ringwald #include "btstack_stdin.h"
7172f99eadSMatthias Ringwald #include "btstack_tlv_posix.h"
72*08b9cddcSMatthias Ringwald #include "btstack_uart.h"
73*08b9cddcSMatthias Ringwald #include "btstack_uart_block.h"
74*08b9cddcSMatthias Ringwald #include "btstack_uart_slip_wrapper.h"
75f5c04f62SMatthias Ringwald 
76f5c04f62SMatthias Ringwald #include "btstack_chipset_bcm.h"
77f5c04f62SMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h"
7811e995b1SMatthias Ringwald #include "btstack_control_raspi.h"
79f5c04f62SMatthias Ringwald 
80*08b9cddcSMatthias Ringwald 
8163ff2b1eSDirk Helbig #include "raspi_get_model.h"
8263ff2b1eSDirk Helbig 
83f5c04f62SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
84f5c04f62SMatthias Ringwald 
8588d4cc4fSMatthias Ringwald typedef enum  {
8688d4cc4fSMatthias Ringwald     UART_INVALID,
8788d4cc4fSMatthias Ringwald     UART_SOFTWARE_NO_FLOW,
8888d4cc4fSMatthias Ringwald     UART_HARDWARE_NO_FLOW,
8988d4cc4fSMatthias Ringwald     UART_HARDWARE_FLOW
9088d4cc4fSMatthias Ringwald } uart_type_t;
91f5c04f62SMatthias Ringwald 
92e0814c90SMatthias Ringwald // default config, updated depending on RasperryPi UART configuration
93f5c04f62SMatthias Ringwald static hci_transport_config_uart_t transport_config = {
94f5c04f62SMatthias Ringwald     HCI_TRANSPORT_CONFIG_UART,
95f5c04f62SMatthias Ringwald     115200,
96e0814c90SMatthias Ringwald     0,       // main baudrate
97f5c04f62SMatthias Ringwald     0,       // flow control
98f5c04f62SMatthias Ringwald     NULL,
99f5c04f62SMatthias Ringwald };
100d9bed121SMatthias Ringwald 
101c41e9922SMatthias Ringwald static btstack_uart_config_t uart_config;
102d9bed121SMatthias Ringwald 
103c41e9922SMatthias Ringwald static int main_argc;
104c41e9922SMatthias Ringwald static const char ** main_argv;
105d9bed121SMatthias Ringwald 
106c41e9922SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
107c41e9922SMatthias Ringwald 
108c41e9922SMatthias Ringwald #define TLV_DB_PATH_PREFIX "/tmp/btstack_"
109c41e9922SMatthias Ringwald #define TLV_DB_PATH_POSTFIX ".tlv"
110c41e9922SMatthias Ringwald static char tlv_db_path[100];
111c41e9922SMatthias Ringwald static const btstack_tlv_t * tlv_impl;
112c41e9922SMatthias Ringwald static btstack_tlv_posix_t   tlv_context;
113d9bed121SMatthias Ringwald 
114d9bed121SMatthias Ringwald 
115d9bed121SMatthias Ringwald static int raspi_speed_to_baud(speed_t baud)
116d9bed121SMatthias Ringwald {
117d9bed121SMatthias Ringwald     switch (baud) {
118d9bed121SMatthias Ringwald         case B9600:
119d9bed121SMatthias Ringwald             return 9600;
120d9bed121SMatthias Ringwald         case B19200:
121d9bed121SMatthias Ringwald             return 19200;
122d9bed121SMatthias Ringwald         case B38400:
123d9bed121SMatthias Ringwald             return 38400;
124d9bed121SMatthias Ringwald         case B57600:
125d9bed121SMatthias Ringwald             return 57600;
126d9bed121SMatthias Ringwald         case B115200:
127d9bed121SMatthias Ringwald             return 115200;
128d9bed121SMatthias Ringwald         case B230400:
129d9bed121SMatthias Ringwald             return 230400;
130d9bed121SMatthias Ringwald         case B460800:
131d9bed121SMatthias Ringwald             return 460800;
132d9bed121SMatthias Ringwald         case B500000:
133d9bed121SMatthias Ringwald             return 500000;
134d9bed121SMatthias Ringwald         case B576000:
135d9bed121SMatthias Ringwald             return 576000;
136d9bed121SMatthias Ringwald         case B921600:
137d9bed121SMatthias Ringwald             return 921600;
138d9bed121SMatthias Ringwald         case B1000000:
139d9bed121SMatthias Ringwald             return 1000000;
140d9bed121SMatthias Ringwald         case B1152000:
141d9bed121SMatthias Ringwald             return 1152000;
142d9bed121SMatthias Ringwald         case B1500000:
143d9bed121SMatthias Ringwald             return 1500000;
144d9bed121SMatthias Ringwald         case B2000000:
145d9bed121SMatthias Ringwald             return 2000000;
146d9bed121SMatthias Ringwald         case B2500000:
147d9bed121SMatthias Ringwald             return 2500000;
148d9bed121SMatthias Ringwald         case B3000000:
149d9bed121SMatthias Ringwald             return 3000000;
150d9bed121SMatthias Ringwald         case B3500000:
151d9bed121SMatthias Ringwald             return 3500000;
152d9bed121SMatthias Ringwald         case B4000000:
153d9bed121SMatthias Ringwald             return 4000000;
154d9bed121SMatthias Ringwald         default:
155d9bed121SMatthias Ringwald             return -1;
156d9bed121SMatthias Ringwald     }
157d9bed121SMatthias Ringwald }
158d9bed121SMatthias Ringwald 
159d9bed121SMatthias Ringwald static void raspi_get_terminal_params( hci_transport_config_uart_t *tc )
160d9bed121SMatthias Ringwald {
161d9bed121SMatthias Ringwald     // open serial terminal and get parameters
162d9bed121SMatthias Ringwald     int fd = open( tc->device_name, O_RDONLY );
163d9bed121SMatthias Ringwald     if( fd < 0 )
164d9bed121SMatthias Ringwald     {
165d9bed121SMatthias Ringwald         perror( "can't open serial port" );
166d9bed121SMatthias Ringwald         return;
167d9bed121SMatthias Ringwald     }
168d9bed121SMatthias Ringwald     struct termios tios;
169d9bed121SMatthias Ringwald     tcgetattr( fd, &tios );
170d9bed121SMatthias Ringwald     close( fd );
171d9bed121SMatthias Ringwald 
172d9bed121SMatthias Ringwald     speed_t ospeed = cfgetospeed( &tios );
173d9bed121SMatthias Ringwald     int baud = raspi_speed_to_baud( ospeed );
174d9bed121SMatthias Ringwald     printf( "current serial terminal parameter baudrate: %d, flow control: %s\n", baud, (tios.c_cflag&CRTSCTS)?"Hardware":"None" );
175d9bed121SMatthias Ringwald 
176d9bed121SMatthias Ringwald     // overwrites the initial baudrate only in case it was likely to be altered before
177d9bed121SMatthias Ringwald     if( baud > 9600 )
178d9bed121SMatthias Ringwald     {
179d9bed121SMatthias Ringwald         tc->baudrate_init = baud;
180d9bed121SMatthias Ringwald         tc->flowcontrol = (tios.c_cflag & CRTSCTS)?1:0;
181d9bed121SMatthias Ringwald     }
182d9bed121SMatthias Ringwald }
183d9bed121SMatthias Ringwald 
184f5c04f62SMatthias Ringwald static void sigint_handler(int param){
185f5c04f62SMatthias Ringwald     UNUSED(param);
186f5c04f62SMatthias Ringwald 
187f5c04f62SMatthias Ringwald     printf("CTRL-C - SIGINT received, shutting down..\n");
188f5c04f62SMatthias Ringwald     log_info("sigint_handler: shutting down");
189f5c04f62SMatthias Ringwald 
190f5c04f62SMatthias Ringwald     // reset anyway
191f5c04f62SMatthias Ringwald     btstack_stdin_reset();
192f5c04f62SMatthias Ringwald 
193f5c04f62SMatthias Ringwald     // power down
194f5c04f62SMatthias Ringwald     hci_power_control(HCI_POWER_OFF);
195f5c04f62SMatthias Ringwald     hci_close();
196f5c04f62SMatthias Ringwald     log_info("Good bye, see you.\n");
197f5c04f62SMatthias Ringwald     exit(0);
198f5c04f62SMatthias Ringwald }
199f5c04f62SMatthias Ringwald 
200f5c04f62SMatthias Ringwald static int led_state = 0;
201f5c04f62SMatthias Ringwald void hal_led_toggle(void){
202f5c04f62SMatthias Ringwald     led_state = 1 - led_state;
203f5c04f62SMatthias Ringwald     printf("LED State %u\n", led_state);
204f5c04f62SMatthias Ringwald }
205f5c04f62SMatthias Ringwald 
206f5c04f62SMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
207f5c04f62SMatthias Ringwald     bd_addr_t addr;
208f5c04f62SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
209f5c04f62SMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
210f5c04f62SMatthias Ringwald         case BTSTACK_EVENT_STATE:
211f5c04f62SMatthias Ringwald             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
212f5c04f62SMatthias Ringwald             gap_local_bd_addr(addr);
213f5c04f62SMatthias Ringwald             printf("BTstack up and running at %s\n",  bd_addr_to_str(addr));
21472f99eadSMatthias Ringwald             // setup TLV
21572f99eadSMatthias Ringwald             strcpy(tlv_db_path, TLV_DB_PATH_PREFIX);
21672f99eadSMatthias Ringwald             strcat(tlv_db_path, bd_addr_to_str(addr));
21772f99eadSMatthias Ringwald             strcat(tlv_db_path, TLV_DB_PATH_POSTFIX);
21872f99eadSMatthias Ringwald             tlv_impl = btstack_tlv_posix_init_instance(&tlv_context, tlv_db_path);
21972f99eadSMatthias Ringwald             btstack_tlv_set_instance(tlv_impl, &tlv_context);
2208c8a87eeSMatthias Ringwald #ifdef ENABLE_CLASSIC
2218c8a87eeSMatthias Ringwald             hci_set_link_key_db(btstack_link_key_db_tlv_get_instance(tlv_impl, &tlv_context));
2228c8a87eeSMatthias Ringwald #endif
2238c8a87eeSMatthias Ringwald #ifdef ENABLE_BLE
2248c8a87eeSMatthias Ringwald             le_device_db_tlv_configure(tlv_impl, &tlv_context);
2258c8a87eeSMatthias Ringwald #endif
226f5c04f62SMatthias Ringwald             break;
227d9bed121SMatthias Ringwald         case HCI_EVENT_COMMAND_COMPLETE:
228d9bed121SMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
229d9bed121SMatthias Ringwald                 if (hci_event_command_complete_get_return_parameters(packet)[0]) break;
230d9bed121SMatthias Ringwald                 // terminate, name 248 chars
231d9bed121SMatthias Ringwald                 packet[6+248] = 0;
232d9bed121SMatthias Ringwald                 printf("Local name: %s\n", &packet[6]);
233d9bed121SMatthias Ringwald 
234d9bed121SMatthias Ringwald                 btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
235d9bed121SMatthias Ringwald             }
236d9bed121SMatthias Ringwald             break;
237f5c04f62SMatthias Ringwald         default:
238f5c04f62SMatthias Ringwald             break;
239f5c04f62SMatthias Ringwald     }
240f5c04f62SMatthias Ringwald }
241f5c04f62SMatthias Ringwald 
24288d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart
243e0814c90SMatthias Ringwald static int raspi_get_bd_addr(bd_addr_t addr){
24488d4cc4fSMatthias Ringwald 
24588d4cc4fSMatthias Ringwald     FILE *fd = fopen( "/proc/device-tree/serial-number", "r" );
24688d4cc4fSMatthias Ringwald     if( fd == NULL ){
24788d4cc4fSMatthias Ringwald         fprintf(stderr, "can't read serial number, %s\n", strerror( errno ) );
24888d4cc4fSMatthias Ringwald         return -1;
24988d4cc4fSMatthias Ringwald     }
2501b6b55dfSMatthias Ringwald     fscanf( fd, "%*08x" "%*02x" "%02" SCNx8 "%02" SCNx8 "%02" SCNx8, &addr[3], &addr[4], &addr[5] );
25188d4cc4fSMatthias Ringwald     fclose( fd );
25288d4cc4fSMatthias Ringwald 
25388d4cc4fSMatthias Ringwald     addr[0] =  0xb8; addr[1]  = 0x27; addr[2] =  0xeb;
25488d4cc4fSMatthias Ringwald     addr[3] ^= 0xaa; addr[4] ^= 0xaa; addr[5] ^= 0xaa;
25588d4cc4fSMatthias Ringwald 
25688d4cc4fSMatthias Ringwald     return 0;
25788d4cc4fSMatthias Ringwald }
25888d4cc4fSMatthias Ringwald 
25988d4cc4fSMatthias Ringwald // see https://github.com/RPi-Distro/pi-bluetooth/blob/master/usr/bin/btuart
26088d4cc4fSMatthias Ringwald // on UART_INVALID errno is set
26188d4cc4fSMatthias Ringwald static uart_type_t raspi_get_bluetooth_uart_type(void){
26288d4cc4fSMatthias Ringwald 
263e0814c90SMatthias Ringwald     uint8_t deviceUart0[21] = { 0 };
264e0814c90SMatthias Ringwald     FILE *fd = fopen( "/proc/device-tree/aliases/uart0", "r" );
265e0814c90SMatthias Ringwald     if( fd == NULL ) return UART_INVALID;
26688d4cc4fSMatthias Ringwald     fscanf( fd, "%20s", deviceUart0 );
26788d4cc4fSMatthias Ringwald     fclose( fd );
26888d4cc4fSMatthias Ringwald 
269e0814c90SMatthias Ringwald     uint8_t deviceSerial1[21] = { 0 };
27088d4cc4fSMatthias Ringwald     fd = fopen( "/proc/device-tree/aliases/serial1", "r" );
27188d4cc4fSMatthias Ringwald     if( fd == NULL ) return UART_INVALID;
27288d4cc4fSMatthias Ringwald     fscanf( fd, "%20s", deviceSerial1 );
27388d4cc4fSMatthias Ringwald     fclose( fd );
27488d4cc4fSMatthias Ringwald 
275e0814c90SMatthias Ringwald     // test if uart0 is an alias for serial1
276e0814c90SMatthias Ringwald     if( strncmp( (const char *) deviceUart0, (const char *) deviceSerial1, 21 ) == 0 ){
27788d4cc4fSMatthias Ringwald         // HW uart
27888d4cc4fSMatthias Ringwald         size_t count = 0;
27988d4cc4fSMatthias Ringwald         uint8_t buf[16];
28088d4cc4fSMatthias Ringwald         fd = fopen( "/proc/device-tree/soc/gpio@7e200000/uart0_pins/brcm,pins", "r" );
28188d4cc4fSMatthias Ringwald         if( fd == NULL ) return UART_INVALID;
28288d4cc4fSMatthias Ringwald         count = fread( buf, 1, 16, fd );
28388d4cc4fSMatthias Ringwald         fclose( fd );
28488d4cc4fSMatthias Ringwald 
285e0814c90SMatthias Ringwald         // contains assigned pins
286e0814c90SMatthias Ringwald         int pins = count / 4;
287e0814c90SMatthias Ringwald         if( pins == 4 ){
28888d4cc4fSMatthias Ringwald             return UART_HARDWARE_FLOW;
289e0814c90SMatthias Ringwald         } else {
29088d4cc4fSMatthias Ringwald             return UART_HARDWARE_NO_FLOW;
29188d4cc4fSMatthias Ringwald         }
292e0814c90SMatthias Ringwald     } else {
29388d4cc4fSMatthias Ringwald         return UART_SOFTWARE_NO_FLOW;
29488d4cc4fSMatthias Ringwald     }
29588d4cc4fSMatthias Ringwald }
29688d4cc4fSMatthias Ringwald 
297f5c04f62SMatthias Ringwald static void phase2(int status);
298f5c04f62SMatthias Ringwald int main(int argc, const char * argv[]){
299f5c04f62SMatthias Ringwald 
300f5c04f62SMatthias Ringwald     /// GET STARTED with BTstack ///
301f5c04f62SMatthias Ringwald     btstack_memory_init();
302f5c04f62SMatthias Ringwald 
303f5c04f62SMatthias Ringwald     // use logger: format HCI_DUMP_PACKETLOGGER, HCI_DUMP_BLUEZ or HCI_DUMP_STDOUT
304f5c04f62SMatthias Ringwald     const char * pklg_path = "/tmp/hci_dump.pklg";
305f5c04f62SMatthias Ringwald     hci_dump_open(pklg_path, HCI_DUMP_PACKETLOGGER);
306f5c04f62SMatthias Ringwald     printf("Packet Log: %s\n", pklg_path);
307f5c04f62SMatthias Ringwald 
308f5c04f62SMatthias Ringwald     // setup run loop
309f5c04f62SMatthias Ringwald     btstack_run_loop_init(btstack_run_loop_posix_get_instance());
310f5c04f62SMatthias Ringwald 
311f5c04f62SMatthias Ringwald     // pick serial port and configure uart block driver
312f5c04f62SMatthias Ringwald     transport_config.device_name = "/dev/serial1";
313f5c04f62SMatthias Ringwald 
31488d4cc4fSMatthias Ringwald     // derive bd_addr from serial number
31588d4cc4fSMatthias Ringwald     bd_addr_t addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
31688d4cc4fSMatthias Ringwald     raspi_get_bd_addr(addr);
317f5c04f62SMatthias Ringwald 
31888d4cc4fSMatthias Ringwald     // set UART config based on raspi Bluetooth UART type
319c41e9922SMatthias Ringwald     int bt_reg_en_pin = -1;
320e6a3daa4SMatthias Ringwald     bool power_cycle = true;
32188d4cc4fSMatthias Ringwald     switch (raspi_get_bluetooth_uart_type()){
32288d4cc4fSMatthias Ringwald         case UART_INVALID:
32388d4cc4fSMatthias Ringwald             fprintf(stderr, "can't verify HW uart, %s\n", strerror( errno ) );
32488d4cc4fSMatthias Ringwald             return -1;
32588d4cc4fSMatthias Ringwald         case UART_SOFTWARE_NO_FLOW:
32655250ca2SMatthias Ringwald             // ??
327c41e9922SMatthias Ringwald             bt_reg_en_pin = 128;
32888d4cc4fSMatthias Ringwald             transport_config.baudrate_main = 460800;
32988d4cc4fSMatthias Ringwald             transport_config.flowcontrol   = 0;
33088d4cc4fSMatthias Ringwald             break;
33188d4cc4fSMatthias Ringwald         case UART_HARDWARE_NO_FLOW:
332c41e9922SMatthias Ringwald             // Raspberry Pi 3 A
33355250ca2SMatthias Ringwald             // Raspberry Pi 3 B
334e6a3daa4SMatthias Ringwald             // power up with H5 and without power cycle untested/unsupported
335c41e9922SMatthias Ringwald             bt_reg_en_pin = 128;
33688d4cc4fSMatthias Ringwald             transport_config.baudrate_main = 921600;
33788d4cc4fSMatthias Ringwald             transport_config.flowcontrol   = 0;
33888d4cc4fSMatthias Ringwald             break;
33988d4cc4fSMatthias Ringwald         case UART_HARDWARE_FLOW:
340e6a3daa4SMatthias Ringwald             // Raspberry Pi Zero W gpio 45, 3 mbps does not work (investigation pending)
341d9bed121SMatthias Ringwald             // Raspberry Pi 3A+ vgpio 129 but WLAN + BL
342d9bed121SMatthias Ringwald             // Raspberry Pi 3B+ vgpio 129 but WLAN + BL
34388d4cc4fSMatthias Ringwald             transport_config.flowcontrol = 1;
344e6a3daa4SMatthias Ringwald             int model = raspi_get_model();
345e6a3daa4SMatthias Ringwald             if (model == MODEL_ZERO_W){
346e6a3daa4SMatthias Ringwald                 bt_reg_en_pin =  45;
34763ff2b1eSDirk Helbig                 transport_config.baudrate_main =  921600;
348e6a3daa4SMatthias Ringwald             } else {
349e6a3daa4SMatthias Ringwald                 bt_reg_en_pin = 129;
350e6a3daa4SMatthias Ringwald                 transport_config.baudrate_main = 3000000;
35163ff2b1eSDirk Helbig             }
352e6a3daa4SMatthias Ringwald 
353e6a3daa4SMatthias Ringwald #ifdef ENABLE_CONTROLLER_WARM_BOOT
354e6a3daa4SMatthias Ringwald             power_cycle = false;
355e6a3daa4SMatthias Ringwald #else
356e6a3daa4SMatthias Ringwald             // warn about power cycle on devices with shared reg_en pins
357e6a3daa4SMatthias Ringwald             if (model == MODEL_3APLUS || model == MODEL_3BPLUS){
358e6a3daa4SMatthias Ringwald                 printf("Wifi and Bluetooth share a single RESET line and BTstack needs to reset Bluetooth -> SSH over Wifi will fail\n");
359e6a3daa4SMatthias Ringwald                 printf("Please add ENABLE_CONTROLLER_WARM_BOOT to btstack_config.h to enable startup without RESET\n");
360e6a3daa4SMatthias Ringwald             }
361e6a3daa4SMatthias Ringwald #endif
36288d4cc4fSMatthias Ringwald             break;
36388d4cc4fSMatthias Ringwald     }
364e6a3daa4SMatthias 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");
365d9bed121SMatthias Ringwald 
366f5c04f62SMatthias Ringwald     // get BCM chipset driver
367f5c04f62SMatthias Ringwald     const btstack_chipset_t * chipset = btstack_chipset_bcm_instance();
368f5c04f62SMatthias Ringwald     chipset->init(&transport_config);
369f5c04f62SMatthias Ringwald 
370f5c04f62SMatthias Ringwald     // set path to firmware files
37123a1bf14SMatthias Ringwald     btstack_chipset_bcm_set_hcd_folder_path("/lib/firmware/brcm");
372f5c04f62SMatthias Ringwald 
373f5c04f62SMatthias Ringwald     // setup UART driver
374*08b9cddcSMatthias Ringwald     const btstack_uart_block_t * uart_block_driver = btstack_uart_block_posix_instance();
375f5c04f62SMatthias Ringwald 
376f5c04f62SMatthias Ringwald     // extract UART config from transport config
377f5c04f62SMatthias Ringwald     uart_config.baudrate    = transport_config.baudrate_init;
378f5c04f62SMatthias Ringwald     uart_config.flowcontrol = transport_config.flowcontrol;
379f5c04f62SMatthias Ringwald     uart_config.device_name = transport_config.device_name;
380*08b9cddcSMatthias Ringwald     uart_block_driver->init(&uart_config);
381f5c04f62SMatthias Ringwald 
3826e4c47b0SMatthias Ringwald     // HW with FlowControl -> we can use regular h4 mode
3836e4c47b0SMatthias Ringwald     const hci_transport_t * transport;
3846e4c47b0SMatthias Ringwald     if (transport_config.flowcontrol){
385*08b9cddcSMatthias Ringwald         transport = hci_transport_h4_instance(uart_block_driver);
3866e4c47b0SMatthias Ringwald     } else {
387*08b9cddcSMatthias Ringwald         const btstack_uart_t * uart_slip_driver = btstack_uart_slip_wrapper_instance(uart_block_driver);
388*08b9cddcSMatthias Ringwald         transport = hci_transport_h5_instance(uart_slip_driver);
3896e4c47b0SMatthias Ringwald     }
3906e4c47b0SMatthias Ringwald 
391f5c04f62SMatthias Ringwald     // setup HCI (to be able to use bcm chipset driver)
392f5c04f62SMatthias Ringwald     hci_init(transport, (void*) &transport_config);
39388d4cc4fSMatthias Ringwald     hci_set_bd_addr( addr );
394f5c04f62SMatthias Ringwald     hci_set_chipset(btstack_chipset_bcm_instance());
395f5c04f62SMatthias Ringwald 
396f5c04f62SMatthias Ringwald     // inform about BTstack state
397f5c04f62SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
398f5c04f62SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
399f5c04f62SMatthias Ringwald 
400f5c04f62SMatthias Ringwald     // handle CTRL-c
401f5c04f62SMatthias Ringwald     signal(SIGINT, sigint_handler);
402f5c04f62SMatthias Ringwald 
403f5c04f62SMatthias Ringwald     main_argc = argc;
404f5c04f62SMatthias Ringwald     main_argv = argv;
405f5c04f62SMatthias Ringwald 
406c41e9922SMatthias Ringwald     // power cycle Bluetooth controller on older models without flowcontrol
407e6a3daa4SMatthias Ringwald     if (power_cycle){
408c41e9922SMatthias Ringwald         btstack_control_raspi_set_bt_reg_en_pin(bt_reg_en_pin);
40911e995b1SMatthias Ringwald         btstack_control_t *control = btstack_control_raspi_get_instance();
41011e995b1SMatthias Ringwald         control->init(NULL);
41111e995b1SMatthias Ringwald         control->off();
41223a1bf14SMatthias Ringwald         usleep( 100000 );
41311e995b1SMatthias Ringwald         control->on();
414e6a3daa4SMatthias Ringwald     }
415e6a3daa4SMatthias Ringwald 
416e6a3daa4SMatthias Ringwald     if (transport_config.flowcontrol){
417e6a3daa4SMatthias Ringwald 
418e6a3daa4SMatthias Ringwald         // re-use current terminal speed (if there was no power cycle)
419e6a3daa4SMatthias Ringwald         if (!power_cycle){
420e6a3daa4SMatthias Ringwald             raspi_get_terminal_params( &transport_config );
421e6a3daa4SMatthias Ringwald         }
422e6a3daa4SMatthias Ringwald 
423e6a3daa4SMatthias Ringwald         // with flowcontrol, we use h4 and are done
424e6a3daa4SMatthias Ringwald         btstack_main(main_argc, main_argv);
425e6a3daa4SMatthias Ringwald 
426e6a3daa4SMatthias Ringwald     } else {
42788d4cc4fSMatthias Ringwald 
428c41e9922SMatthias Ringwald         // assume BCM4343W used in Pi 3 A/B. Pi 3 A/B+ have a newer controller but support H4 with Flowcontrol
429c41e9922SMatthias Ringwald         btstack_chipset_bcm_set_device_name("BCM43430A1");
430c41e9922SMatthias Ringwald 
4316e4c47b0SMatthias Ringwald         // phase #1 download firmware
4326e4c47b0SMatthias Ringwald         printf("Phase 1: Download firmware\n");
4336e4c47b0SMatthias Ringwald 
434f5c04f62SMatthias Ringwald         // phase #2 start main app
435*08b9cddcSMatthias Ringwald         btstack_chipset_bcm_download_firmware(uart_block_driver, transport_config.baudrate_main, &phase2);
4366e4c47b0SMatthias Ringwald     }
437f5c04f62SMatthias Ringwald 
438f5c04f62SMatthias Ringwald     // go
439f5c04f62SMatthias Ringwald     btstack_run_loop_execute();
440f5c04f62SMatthias Ringwald     return 0;
441f5c04f62SMatthias Ringwald }
442f5c04f62SMatthias Ringwald 
443f5c04f62SMatthias Ringwald static void phase2(int status){
444f5c04f62SMatthias Ringwald 
445f5c04f62SMatthias Ringwald     if (status){
446f5c04f62SMatthias Ringwald         printf("Download firmware failed\n");
447f5c04f62SMatthias Ringwald         return;
448f5c04f62SMatthias Ringwald     }
449f5c04f62SMatthias Ringwald 
450f5c04f62SMatthias Ringwald     printf("Phase 2: Main app\n");
451f5c04f62SMatthias Ringwald 
452f5c04f62SMatthias Ringwald     // setup app
453f5c04f62SMatthias Ringwald     btstack_main(main_argc, main_argv);
454f5c04f62SMatthias Ringwald }
455f5c04f62SMatthias Ringwald 
456