xref: /btstack/platform/posix/btstack_uart_posix.c (revision 7aaf71bd93de8eac8e43db5da97e2aee739d7b46)
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_open(const char * device_name, int flowcontrol, uint32_t baudrate){
56 
57     struct termios toptions;
58     int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
59     int fd = open(device_name, flags);
60     if (fd == -1)  {
61         perror("init_serialport: Unable to open port ");
62         perror(device_name);
63         return -1;
64     }
65 
66     if (tcgetattr(fd, &toptions) < 0) {
67         perror("init_serialport: Couldn't get term attributes");
68         return -1;
69     }
70 
71     cfmakeraw(&toptions);   // make raw
72 
73     // 8N1
74     toptions.c_cflag &= ~CSTOPB;
75     toptions.c_cflag |= CS8;
76 
77     if (flowcontrol) {
78         // with flow control
79         toptions.c_cflag |= CRTSCTS;
80     } else {
81         // no flow control
82         toptions.c_cflag &= ~CRTSCTS;
83     }
84 
85     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
86     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
87 
88     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
89     toptions.c_cc[VMIN]  = 1;
90     toptions.c_cc[VTIME] = 0;
91 
92     if(tcsetattr(fd, TCSANOW, &toptions) < 0) {
93         perror("init_serialport: Couldn't set term attributes");
94         return -1;
95     }
96 
97     // also set baudrate
98     if (btstack_uart_posix_set_baudrate(fd, baudrate) < 0){
99         return -1;
100     }
101 
102     return fd;
103 }
104 
105 int btstack_uart_posix_set_baudrate(int fd, uint32_t baudrate){
106     log_info("h4_set_baudrate %u", baudrate);
107 
108     struct termios toptions;
109 
110     if (tcgetattr(fd, &toptions) < 0) {
111         perror("init_serialport: Couldn't get term attributes");
112         return -1;
113     }
114 
115     speed_t brate = baudrate; // let you override switch below if needed
116     switch(baudrate) {
117         case 57600:  brate=B57600;  break;
118         case 115200: brate=B115200; break;
119 #ifdef B230400
120         case 230400: brate=B230400; break;
121 #endif
122 #ifdef B460800
123         case 460800: brate=B460800; break;
124 #endif
125 #ifdef B921600
126         case 921600: brate=B921600; break;
127 #endif
128 
129 // Hacks to switch to 2/3 mbps on FTDI FT232 chipsets
130 // requires special config in Info.plist or Registry
131         case 2000000:
132 #if defined(HAVE_POSIX_B300_MAPPED_TO_2000000)
133             log_info("hci_transport_posix: using B300 for 2 mbps");
134             brate=B300;
135 #elif defined(HAVE_POSIX_B1200_MAPPED_TO_2000000)
136            log_info("hci_transport_posix: using B1200 for 2 mbps");
137             brate=B1200;
138 #endif
139             break;
140         case 3000000:
141 #if defined(HAVE_POSIX_B600_MAPPED_TO_3000000)
142             log_info("hci_transport_posix: using B600 for 3 mbps");
143             brate=B600;
144 #elif defined(HAVE_POSIX_B2400_MAPPED_TO_3000000)
145             log_info("hci_transport_posix: using B2400 for 3 mbps");
146             brate=B2400;
147 #endif
148             break;
149         default:
150             break;
151     }
152     cfsetospeed(&toptions, brate);
153     cfsetispeed(&toptions, brate);
154 
155     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
156         perror("init_serialport: Couldn't set term attributes");
157         return -1;
158     }
159 
160     return 0;
161 }
162