1 /* 2 * Copyright (C) 2016 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /* 39 * btstack_uart_posix.c 40 * 41 * Common code to access serial port via POSIX interface 42 * Used by hci_transport_h4_posix.c and hci_transport_h5.posix 43 * 44 */ 45 46 #include "btstack_uart_posix.h" 47 #include "btstack_debug.h" 48 49 #include <termios.h> /* POSIX terminal control definitions */ 50 #include <fcntl.h> /* File control definitions */ 51 // #include <unistd.h> /* UNIX standard function definitions */ 52 // #include <stdio.h> 53 // #include <string.h> 54 55 int btstack_uart_posix_set_baudrate(int fd, uint32_t baudrate){ 56 log_info("h4_set_baudrate %u", baudrate); 57 58 struct termios toptions; 59 60 if (tcgetattr(fd, &toptions) < 0) { 61 perror("init_serialport: Couldn't get term attributes"); 62 return -1; 63 } 64 65 speed_t brate = baudrate; // let you override switch below if needed 66 switch(baudrate) { 67 case 57600: brate=B57600; break; 68 case 115200: brate=B115200; break; 69 #ifdef B230400 70 case 230400: brate=B230400; break; 71 #endif 72 #ifdef B460800 73 case 460800: brate=B460800; break; 74 #endif 75 #ifdef B921600 76 case 921600: brate=B921600; break; 77 #endif 78 79 // Hacks to switch to 2/3 mbps on FTDI FT232 chipsets 80 // requires special config in Info.plist or Registry 81 case 2000000: 82 #if defined(HAVE_POSIX_B300_MAPPED_TO_2000000) 83 log_info("hci_transport_posix: using B300 for 2 mbps"); 84 brate=B300; 85 #elif defined(HAVE_POSIX_B1200_MAPPED_TO_2000000) 86 log_info("hci_transport_posix: using B1200 for 2 mbps"); 87 brate=B1200; 88 #endif 89 break; 90 case 3000000: 91 #if defined(HAVE_POSIX_B600_MAPPED_TO_3000000) 92 log_info("hci_transport_posix: using B600 for 3 mbps"); 93 brate=B600; 94 #elif defined(HAVE_POSIX_B2400_MAPPED_TO_3000000) 95 log_info("hci_transport_posix: using B2400 for 3 mbps"); 96 brate=B2400; 97 #endif 98 break; 99 default: 100 break; 101 } 102 cfsetospeed(&toptions, brate); 103 cfsetispeed(&toptions, brate); 104 105 if( tcsetattr(fd, TCSANOW, &toptions) < 0) { 106 perror("init_serialport: Couldn't set term attributes"); 107 return -1; 108 } 109 110 return 0; 111 } 112