xref: /btstack/platform/posix/btstack_uart_posix.c (revision abd4d2eb2106bc2199a1bc9ccbaf8d039a12563a)
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("posix_open: Unable to open port ");
62         perror(device_name);
63         return -1;
64     }
65 
66     if (tcgetattr(fd, &toptions) < 0) {
67         perror("posix_open: 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 	// 8E1
78  	// toptions.c_cflag |= PARENB; // enable even parity
79  	//
80 
81     if (flowcontrol) {
82         // with flow control
83         toptions.c_cflag |= CRTSCTS;
84     } else {
85         // no flow control
86         toptions.c_cflag &= ~CRTSCTS;
87     }
88 
89     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
90     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
91 
92     // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
93     toptions.c_cc[VMIN]  = 1;
94     toptions.c_cc[VTIME] = 0;
95 
96     if(tcsetattr(fd, TCSANOW, &toptions) < 0) {
97         perror("posix_open: Couldn't set term attributes");
98         return -1;
99     }
100 
101     // also set baudrate
102     if (btstack_uart_posix_set_baudrate(fd, baudrate) < 0){
103         return -1;
104     }
105 
106     return fd;
107 }
108 
109 int btstack_uart_posix_set_baudrate(int fd, uint32_t baudrate){
110     log_info("h4_set_baudrate %u", baudrate);
111 
112     struct termios toptions;
113 
114     if (tcgetattr(fd, &toptions) < 0) {
115         perror("posix_open: Couldn't get term attributes");
116         return -1;
117     }
118 
119     speed_t brate = baudrate; // let you override switch below if needed
120     switch(baudrate) {
121         case 57600:  brate=B57600;  break;
122         case 115200: brate=B115200; break;
123 #ifdef B230400
124         case 230400: brate=B230400; break;
125 #endif
126 #ifdef B460800
127         case 460800: brate=B460800; break;
128 #endif
129 #ifdef B921600
130         case 921600: brate=B921600; break;
131 #endif
132 
133 // Hacks to switch to 2/3 mbps on FTDI FT232 chipsets
134 // requires special config in Info.plist or Registry
135         case 2000000:
136 #if defined(HAVE_POSIX_B300_MAPPED_TO_2000000)
137             log_info("hci_transport_posix: using B300 for 2 mbps");
138             brate=B300;
139 #elif defined(HAVE_POSIX_B1200_MAPPED_TO_2000000)
140            log_info("hci_transport_posix: using B1200 for 2 mbps");
141             brate=B1200;
142 #endif
143             break;
144         case 3000000:
145 #if defined(HAVE_POSIX_B600_MAPPED_TO_3000000)
146             log_info("hci_transport_posix: using B600 for 3 mbps");
147             brate=B600;
148 #elif defined(HAVE_POSIX_B2400_MAPPED_TO_3000000)
149             log_info("hci_transport_posix: using B2400 for 3 mbps");
150             brate=B2400;
151 #endif
152             break;
153         default:
154             break;
155     }
156     cfsetospeed(&toptions, brate);
157     cfsetispeed(&toptions, brate);
158 
159     if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
160         perror("posix_set_baudrate: Couldn't set term attributes");
161         return -1;
162     }
163 
164     return 0;
165 }
166 
167 int btstack_uart_posix_set_parity(int fd, int parity){
168     struct termios toptions;
169     if (tcgetattr(fd, &toptions) < 0) {
170         perror("posix_set_parity: Couldn't get term attributes");
171         return -1;
172     }
173     if (parity){
174 		toptions.c_cflag |= PARENB; // enable even parity
175     } else {
176 		toptions.c_cflag &= ~PARENB; // enable even parity
177     }
178     if(tcsetattr(fd, TCSANOW, &toptions) < 0) {
179         perror("posix_set_parity: Couldn't set term attributes");
180         return -1;
181     }
182     return 0;
183 }
184 
185 // Generic helper
186 void btstack_uart_posix_write(int fd, const uint8_t * data, int size){
187     // log_info("btstack_uart_posix_write (%u bytes)", size);
188     // log_info_hexdump(data, size);
189     while (size > 0) {
190         int bytes_written = write(fd, data, size);
191         if (bytes_written < 0) {
192             usleep(5000);
193             continue;
194         }
195         data += bytes_written;
196         size -= bytes_written;
197     }
198 }
199 
200