Flashrom

Flashrom Svn Source Tree

Root/trunk/serial.c

1/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Urja Rannikko <urjaman@gmail.com>
5 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <string.h>
26#include <ctype.h>
27#include <fcntl.h>
28#include <sys/stat.h>
29#include <errno.h>
30#include <inttypes.h>
31#ifdef _WIN32
32#include <conio.h>
33#else
34#include <termios.h>
35#include <unistd.h>
36#include <sys/types.h>
37#include <sys/ioctl.h>
38#endif
39#include "flash.h"
40#include "programmer.h"
41
42fdtype sp_fd;
43
44void __attribute__((noreturn)) sp_die(char *msg)
45{
46perror(msg);
47exit(1);
48}
49
50#ifndef _WIN32
51struct baudentry {
52int flag;
53unsigned int baud;
54};
55
56/* I'd like if the C preprocessor could have directives in macros */
57#define BAUDENTRY(baud) { B##baud, baud },
58static const struct baudentry sp_baudtable[] = {
59BAUDENTRY(9600)
60BAUDENTRY(19200)
61BAUDENTRY(38400)
62BAUDENTRY(57600)
63BAUDENTRY(115200)
64#ifdef B230400
65BAUDENTRY(230400)
66#endif
67#ifdef B460800
68BAUDENTRY(460800)
69#endif
70#ifdef B500000
71BAUDENTRY(500000)
72#endif
73#ifdef B576000
74BAUDENTRY(576000)
75#endif
76#ifdef B921600
77BAUDENTRY(921600)
78#endif
79#ifdef B1000000
80BAUDENTRY(1000000)
81#endif
82#ifdef B1152000
83BAUDENTRY(1152000)
84#endif
85#ifdef B1500000
86BAUDENTRY(1500000)
87#endif
88#ifdef B2000000
89BAUDENTRY(2000000)
90#endif
91#ifdef B2500000
92BAUDENTRY(2500000)
93#endif
94#ifdef B3000000
95BAUDENTRY(3000000)
96#endif
97#ifdef B3500000
98BAUDENTRY(3500000)
99#endif
100#ifdef B4000000
101BAUDENTRY(4000000)
102#endif
103{0, 0}/* Terminator */
104};
105#endif
106
107fdtype sp_openserport(char *dev, unsigned int baud)
108{
109#ifdef _WIN32
110HANDLE fd;
111char *dev2 = dev;
112if ((strlen(dev) > 3) && (tolower((unsigned char)dev[0]) == 'c') &&
113 (tolower((unsigned char)dev[1]) == 'o') &&
114 (tolower((unsigned char)dev[2]) == 'm')) {
115dev2 = malloc(strlen(dev) + 5);
116if (!dev2)
117sp_die("Error: Out of memory");
118strcpy(dev2, "\\\\.\\");
119strcpy(dev2 + 4, dev);
120}
121fd = CreateFile(dev2, GENERIC_READ | GENERIC_WRITE, 0, NULL,
122OPEN_EXISTING, 0, NULL);
123if (dev2 != dev)
124free(dev2);
125if (fd == INVALID_HANDLE_VALUE) {
126sp_die("Error: cannot open serial port");
127}
128DCB dcb;
129if (!GetCommState(fd, &dcb)) {
130sp_die("Error: Could not fetch serial port configuration");
131}
132switch (baud) {
133case 9600: dcb.BaudRate = CBR_9600; break;
134case 19200: dcb.BaudRate = CBR_19200; break;
135case 38400: dcb.BaudRate = CBR_38400; break;
136case 57600: dcb.BaudRate = CBR_57600; break;
137case 115200: dcb.BaudRate = CBR_115200; break;
138default: sp_die("Error: Could not set baud rate");
139}
140dcb.ByteSize = 8;
141dcb.Parity = NOPARITY;
142dcb.StopBits = ONESTOPBIT;
143if (!SetCommState(fd, &dcb)) {
144sp_die("Error: Could not change serial port configuration");
145}
146return fd;
147#else
148struct termios options;
149int fd, i;
150fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
151if (fd < 0)
152sp_die("Error: cannot open serial port");
153fcntl(fd, F_SETFL, 0);
154tcgetattr(fd, &options);
155for (i = 0;; i++) {
156if (sp_baudtable[i].baud == 0) {
157close(fd);
158msg_perr("Error: cannot configure for baudrate %d\n",
159 baud);
160exit(1);
161}
162if (sp_baudtable[i].baud == baud) {
163cfsetispeed(&options, sp_baudtable[i].flag);
164cfsetospeed(&options, sp_baudtable[i].flag);
165break;
166}
167}
168options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
169options.c_cflag |= (CS8 | CLOCAL | CREAD);
170options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
171options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL | IGNCR | INLCR);
172options.c_oflag &= ~OPOST;
173tcsetattr(fd, TCSANOW, &options);
174return fd;
175#endif
176}
177
178void sp_set_pin(enum SP_PIN pin, int val) {
179#ifdef _WIN32
180DWORD ctl;
181
182if(pin == PIN_TXD) {
183ctl = val ? SETBREAK: CLRBREAK;
184}
185else if(pin == PIN_DTR) {
186ctl = val ? SETDTR: CLRDTR;
187}
188else {
189ctl = val ? SETRTS: CLRRTS;
190}
191EscapeCommFunction(sp_fd, ctl);
192#else
193int ctl, s;
194
195if(pin == PIN_TXD) {
196ioctl(sp_fd, val ? TIOCSBRK : TIOCCBRK, 0);
197}
198else {
199s = (pin == PIN_DTR) ? TIOCM_DTR : TIOCM_RTS;
200ioctl(sp_fd, TIOCMGET, &ctl);
201
202if (val) {
203ctl |= s;
204}
205else {
206ctl &= ~s;
207}
208ioctl(sp_fd, TIOCMSET, &ctl);
209}
210#endif
211}
212
213int sp_get_pin(enum SP_PIN pin) {
214int s;
215#ifdef _WIN32
216DWORD ctl;
217
218s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
219GetCommModemStatus(sp_fd, &ctl);
220#else
221int ctl;
222s = (pin == PIN_CTS) ? TIOCM_CTS : TIOCM_DSR;
223ioctl(sp_fd, TIOCMGET, &ctl);
224#endif
225
226return ((ctl & s) ? 1 : 0);
227
228}
229
230void sp_flush_incoming(void)
231{
232#ifdef _WIN32
233PurgeComm(sp_fd, PURGE_RXCLEAR);
234#else
235tcflush(sp_fd, TCIFLUSH);
236#endif
237return;
238}
239
240int serialport_shutdown(void *data)
241{
242#ifdef _WIN32
243CloseHandle(sp_fd);
244#else
245close(sp_fd);
246#endif
247return 0;
248}
249
250int serialport_write(unsigned char *buf, unsigned int writecnt)
251{
252#ifdef _WIN32
253DWORD tmp = 0;
254#else
255ssize_t tmp = 0;
256#endif
257
258while (writecnt > 0) {
259#ifdef _WIN32
260WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
261#else
262tmp = write(sp_fd, buf, writecnt);
263#endif
264if (tmp == -1) {
265msg_perr("Serial port write error!\n");
266return 1;
267}
268if (!tmp)
269msg_pdbg("Empty write\n");
270writecnt -= tmp;
271buf += tmp;
272}
273
274return 0;
275}
276
277int serialport_read(unsigned char *buf, unsigned int readcnt)
278{
279#ifdef _WIN32
280DWORD tmp = 0;
281#else
282ssize_t tmp = 0;
283#endif
284
285while (readcnt > 0) {
286#ifdef _WIN32
287ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
288#else
289tmp = read(sp_fd, buf, readcnt);
290#endif
291if (tmp == -1) {
292msg_perr("Serial port read error!\n");
293return 1;
294}
295if (!tmp)
296msg_pdbg("Empty read\n");
297readcnt -= tmp;
298buf += tmp;
299}
300
301return 0;
302}
303

Archive Download this file

Revision: HEAD