chiark / gitweb /
Updated wiringPi.c to work with some pullUpDown stuff
[wiringPi.git] / wiringPi / wiringSerial.c
1 /*
2  * wiringSerial.c:
3  *      Handle a serial port
4  ***********************************************************************
5  * This file is part of wiringPi:
6  *      https://projects.drogon.net/raspberry-pi/wiringpi/
7  *
8  *    wiringPi is free software: you can redistribute it and/or modify
9  *    it under the terms of the GNU Lesser General Public License as published by
10  *    the Free Software Foundation, either version 3 of the License, or
11  *    (at your option) any later version.
12  *
13  *    wiringPi is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU Lesser General Public License for more details.
17  *
18  *    You should have received a copy of the GNU Lesser General Public License
19  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
20  ***********************************************************************
21  */
22
23 #undef  DEBUG
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include "wiringSerial.h"
38
39 /*
40  * serialOpen:
41  *      Open and initialise the serial port, setting all the right
42  *      port parameters - or as many as are required - hopefully!
43  *********************************************************************************
44  */
45
46 int serialOpen (char *device, int baud)
47 {
48   struct termios options ;
49   speed_t myBaud ;
50   int     status, fd ;
51
52 #ifdef  DEBUG
53   printf ("openSerialPort: <%s> baud: $d\n", device, baud) ;
54 #endif
55
56   switch (baud)
57   {
58     case     50:        myBaud =     B50 ; break ;
59     case     75:        myBaud =     B75 ; break ;
60     case    110:        myBaud =    B110 ; break ;
61     case    134:        myBaud =    B134 ; break ;
62     case    150:        myBaud =    B150 ; break ;
63     case    200:        myBaud =    B200 ; break ;
64     case    300:        myBaud =    B300 ; break ;
65     case    600:        myBaud =    B600 ; break ;
66     case   1200:        myBaud =   B1200 ; break ;
67     case   1800:        myBaud =   B1800 ; break ;
68     case   2400:        myBaud =   B2400 ; break ;
69     case   9600:        myBaud =   B9600 ; break ;
70     case  19200:        myBaud =  B19200 ; break ;
71     case  38400:        myBaud =  B38400 ; break ;
72     case  57600:        myBaud =  B57600 ; break ;
73     case 115200:        myBaud = B115200 ; break ;
74     case 230400:        myBaud = B230400 ; break ;
75
76     default:
77       return -2 ;
78   }
79
80   if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
81     return -1 ;
82
83   fcntl (fd, F_SETFL, O_RDWR) ;
84
85 // Get and modify current options:
86
87   tcgetattr (fd, &options) ;
88
89     cfmakeraw   (&options) ;
90     cfsetispeed (&options, myBaud) ;
91     cfsetospeed (&options, myBaud) ;
92
93     options.c_cflag |= (CLOCAL | CREAD) ;
94     options.c_cflag &= ~PARENB ;
95     options.c_cflag &= ~CSTOPB ;
96     options.c_cflag &= ~CSIZE ;
97     options.c_cflag |= CS8 ;
98     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
99     options.c_oflag &= ~OPOST ;
100
101     options.c_cc [VMIN]  =   0 ;
102     options.c_cc [VTIME] = 100 ;        // Ten seconds (100 deciseconds)
103
104   tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
105
106   ioctl (fd, TIOCMGET, &status);
107
108   status |= TIOCM_DTR ;
109   status |= TIOCM_RTS ;
110
111   ioctl (fd, TIOCMSET, &status);
112
113   usleep (10000) ;      // 10mS
114
115   return fd ;
116 }
117
118
119 /*
120  * serialFlush:
121  *      Flush the serial buffers (both tx & rx)
122  *********************************************************************************
123  */
124
125 void serialFlush (int fd)
126 {
127   tcflush (fd, TCIOFLUSH) ;
128 }
129
130
131 /*
132  * serialClose:
133  *      Release the serial port
134  *********************************************************************************
135  */
136
137 void serialClose (int fd)
138 {
139   close (fd) ;
140 }
141
142
143 /*
144  * serialPutchar:
145  *      Send a single character to the serial port
146  *********************************************************************************
147  */
148
149 void serialPutchar (int fd, unsigned char c)
150 {
151   write (fd, &c, 1) ;
152 }
153
154
155 /*
156  * serialPuts:
157  *      Send a string to the serial port
158  *********************************************************************************
159  */
160
161 void serialPuts (int fd, char *s)
162 {
163   write (fd, s, strlen (s)) ;
164 }
165
166 /*
167  * serialPrintf:
168  *      Printf over Serial
169  *********************************************************************************
170  */
171
172 void serialPrintf (int fd, char *message, ...)
173 {
174   va_list argp ;
175   char buffer [1024] ;
176
177   va_start (argp, message) ;
178     vsnprintf (buffer, 1023, message, argp) ;
179   va_end (argp) ;
180
181   serialPuts (fd, buffer) ;
182 }
183
184
185 /*
186  * serialDataAvail:
187  *      Return the number of bytes of data avalable to be read in the serial port
188  *********************************************************************************
189  */
190
191 int serialDataAvail (int fd)
192 {
193   int result ;
194
195   if (ioctl (fd, FIONREAD, &result) == -1)
196     return -1 ;
197
198   return result ;
199 }
200
201
202 /*
203  * serialGetchar:
204  *      Get a single character from the serial device.
205  *      Note: Zero is a valid character and this function will time-out after
206  *      10 seconds.
207  *********************************************************************************
208  */
209
210 int serialGetchar (int fd)
211 {
212   uint8_t x ;
213
214   if (read (fd, &x, 1) != 1)
215     return -1 ;
216
217   return ((int)x) & 0xFF ;
218 }