FreeCalypso > hg > freecalypso-sw
comparison rvinterf/old/openport.c @ 173:f42854da4563
rvinterf: beginning of refactoring
| author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
|---|---|
| date | Fri, 22 Nov 2013 05:56:07 +0000 |
| parents | rvinterf/openport.c@811b138f1bed |
| children |
comparison
equal
deleted
inserted
replaced
| 172:019120585a1c | 173:f42854da4563 |
|---|---|
| 1 /* | |
| 2 * This module takes care of opening the serial port and setting the | |
| 3 * proper "raw" termios modes, including the baud rate. | |
| 4 */ | |
| 5 | |
| 6 #include <sys/types.h> | |
| 7 #include <sys/file.h> | |
| 8 #include <sys/ioctl.h> | |
| 9 #include <termios.h> | |
| 10 #include <stdio.h> | |
| 11 #include <stdlib.h> | |
| 12 #include <string.h> | |
| 13 #include <strings.h> | |
| 14 #include <unistd.h> | |
| 15 | |
| 16 int target_fd; | |
| 17 char *baudrate_name = "115200"; | |
| 18 | |
| 19 static struct baudrate { | |
| 20 char *name; | |
| 21 speed_t termios_code; | |
| 22 } baud_rate_table[] = { | |
| 23 {"19200", B19200}, | |
| 24 {"38400", B38400}, | |
| 25 {"57600", B57600}, | |
| 26 {"115200", B115200}, | |
| 27 /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */ | |
| 28 {"203125", B230400}, | |
| 29 {"406250", B460800}, | |
| 30 {"812500", B921600}, | |
| 31 /* table search terminator */ | |
| 32 {NULL, B0} | |
| 33 }; | |
| 34 | |
| 35 static speed_t | |
| 36 get_baud_rate() | |
| 37 { | |
| 38 struct baudrate *br; | |
| 39 | |
| 40 for (br = baud_rate_table; br->name; br++) | |
| 41 if (!strcmp(br->name, baudrate_name)) | |
| 42 break; | |
| 43 if (!br->name) { | |
| 44 fprintf(stderr, "baud rate \"%s\" unknown/unsupported\n", | |
| 45 baudrate_name); | |
| 46 exit(1); | |
| 47 } | |
| 48 return br->termios_code; | |
| 49 } | |
| 50 | |
| 51 open_target_serial(ttydev) | |
| 52 char *ttydev; | |
| 53 { | |
| 54 struct termios target_termios; | |
| 55 speed_t br_code; | |
| 56 | |
| 57 br_code = get_baud_rate(); | |
| 58 target_fd = open(ttydev, O_RDWR|O_NONBLOCK); | |
| 59 if (target_fd < 0) { | |
| 60 perror(ttydev); | |
| 61 exit(1); | |
| 62 } | |
| 63 target_termios.c_iflag = IGNBRK; | |
| 64 target_termios.c_oflag = 0; | |
| 65 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8; | |
| 66 target_termios.c_lflag = 0; | |
| 67 target_termios.c_cc[VMIN] = 1; | |
| 68 target_termios.c_cc[VTIME] = 0; | |
| 69 cfsetispeed(&target_termios, br_code); | |
| 70 cfsetospeed(&target_termios, br_code); | |
| 71 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) { | |
| 72 perror("initial tcsetattr on target"); | |
| 73 exit(1); | |
| 74 } | |
| 75 return 0; | |
| 76 } | |
| 77 | |
| 78 set_serial_nonblock(state) | |
| 79 int state; | |
| 80 { | |
| 81 ioctl(target_fd, FIONBIO, &state); | |
| 82 } |
