Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* suu.c -- simple UART utility */
- /* ITJUSTWORKS(tm) on Cygwin, Linux. */
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/select.h>
- #include <errno.h>
- #include <termios.h>
- #include <getopt.h>
- int open_port(const char *port) {
- int fd;
- fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd == -1) {
- perror("open_port: Unable to open \"port\" - ");
- } else {
- fcntl(fd, F_SETFL, 0);
- }
- return fd;
- }
- int write_port(int fd, uint8_t *byte) {
- fd_set output;
- FD_ZERO(&output);
- FD_SET(fd, &output);
- if (select(fd+1, NULL, &output, NULL, NULL) > 0) {
- if (FD_ISSET(fd, &output)) {
- write(fd, byte, 1);
- }
- }
- return 0;
- }
- int read_port(int fd, uint8_t *byte) {
- fd_set input;
- FD_ZERO(&input);
- FD_SET(fd, &input);
- if (select(fd+1, &input, NULL, NULL, NULL) > 0) {
- if (FD_ISSET(fd, &input)) {
- read(fd, byte, 1);
- }
- }
- return 0;
- }
- int close_port(int fd) {
- if (fd == -1) {
- perror("close_port: Unable to close \"fd\" - ");
- } else {
- close(fd);
- }
- return fd;
- }
- struct termios g_backup_options;
- int restore_options(int fd) {
- tcsetattr(fd, TCSADRAIN, &g_backup_options);
- return 0;
- }
- int update_options(speed_t baud, int fd) {
- struct termios options;
- tcgetattr(fd, &g_backup_options);
- tcgetattr(fd, &options);
- cfsetispeed(&options, baud);
- cfsetospeed(&options, baud);
- options.c_cflag |= (CLOCAL | CREAD);
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- options.c_iflag |= IGNPAR;
- options.c_iflag &= ~(IXON | IXOFF | IXANY);
- options.c_oflag &= ~OPOST;
- options.c_cc[VTIME] = 0;
- options.c_cc[VMIN] = 1;
- tcsetattr(fd, TCSANOW, &options);
- return 0;
- }
- void print_usage() {
- fputs("NAME \n", stdout);
- fputs(" suu - simple UART utility \n", stdout);
- fputs(" \n", stdout);
- fputs("SYNOPSIS \n", stdout);
- fputs(" suu [-p port] [-b baud] [-m mode] [-i input] [-o output]\n", stdout);
- fputs(" \n", stdout);
- fputs("DESCRIPTION \n", stdout);
- fputs(" suu handles UART input and output in specified mode. \n", stdout);
- fputs(" \n", stdout);
- fputs(" -p port, --port=port \n", stdout);
- fputs(" Specifies serial port. \n", stdout);
- fputs(" Default is /dev/ttyS0. \n", stdout);
- fputs(" \n", stdout);
- fputs(" -b baud --baud=baud \n", stdout);
- fputs(" Specifies baud rate from 75, 110, 134, 150, 300, 600, \n", stdout);
- fputs(" 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, \n", stdout);
- fputs(" 115200. \n", stdout);
- fputs(" Default is 115200. \n", stdout);
- fputs(" \n", stdout);
- fputs(" -m mode, --mode=mode \n", stdout);
- fputs(" Specifies mode from loader or io. \n", stdout);
- fputs(" Default is loader \n", stdout);
- fputs(" \n", stdout);
- fputs(" -i input, --input=input \n", stdout);
- fputs(" Specifies input file. \n", stdout);
- fputs(" Default is - which means stdin. \n", stdout);
- fputs(" \n", stdout);
- fputs(" -o output, --output=output \n", stdout);
- fputs(" Specifies output file. \n", stdout);
- fputs(" Default is - which means stdout. \n", stdout);
- return;
- }
- int main (int argc, char *argv[]) {
- int ch;
- struct option longopts[] ={
- {"port", required_argument, NULL, 'p'},
- {"baud", required_argument, NULL, 'b'},
- {"mode", required_argument, NULL, 'm'},
- {"input", required_argument, NULL, 'i'},
- {"output", required_argument, NULL, 'o'},
- {"help", no_argument, NULL, 'h'},
- {0, 0, 0, 0 }
- };
- int opt_idx = 0;
- char port[256] = "/dev/ttyS0";
- speed_t baud = B115200;
- #define MODE_LOADER 0
- #define MODE_IO 1
- #define MODE_WRITE 2
- #define MODE_READ 3
- int mode = MODE_LOADER;
- char ifile[256] = "-";
- char ofile[256] = "-";
- while((ch = getopt_long(argc, argv, "p:b:m:i:o:h", longopts, &opt_idx)) != -1) {
- switch (ch) {
- case 'p':
- strncpy(port, optarg, sizeof(port));
- break;
- case 'b':
- if (strcmp(optarg, "75") == 0) { baud = B75; }
- else if (strcmp(optarg, "110") == 0) { baud = B110; }
- else if (strcmp(optarg, "134") == 0) { baud = B134; }
- else if (strcmp(optarg, "150") == 0) { baud = B150; }
- else if (strcmp(optarg, "300") == 0) { baud = B300; }
- else if (strcmp(optarg, "600") == 0) { baud = B600; }
- else if (strcmp(optarg, "1200") == 0) { baud = B1200; }
- else if (strcmp(optarg, "1800") == 0) { baud = B1800; }
- else if (strcmp(optarg, "2400") == 0) { baud = B2400; }
- else if (strcmp(optarg, "4800") == 0) { baud = B4800; }
- /* else if (strcmp(optarg, "7200") == 0) { baud = B7200; } */ /* Undefined on Cygwin. */
- else if (strcmp(optarg, "9600") == 0) { baud = B9600; }
- /* else if (strcmp(optarg, "14400") == 0) { baud = B14400; } */ /* Undefined on Cygwin. */
- else if (strcmp(optarg, "19200") == 0) { baud = B19200; }
- else if (strcmp(optarg, "38400") == 0) { baud = B38400; }
- else if (strcmp(optarg, "57600") == 0) { baud = B57600; }
- else if (strcmp(optarg, "115200") == 0) { baud = B115200; }
- /* else if (strcmp(optarg, "128000") == 0) { baud = B128000; } */ /* Undefined on Linux. */
- break;
- case 'm':
- if (strcmp(optarg, "loader") == 0) { mode = MODE_LOADER; }
- else if (strcmp(optarg, "io") == 0) { mode = MODE_IO; }
- else if (strcmp(optarg, "write") == 0) { mode = MODE_WRITE; }
- else if (strcmp(optarg, "read") == 0) { mode = MODE_READ; }
- break;
- case 'i':
- strncpy(ifile, optarg, sizeof(ifile));
- break;
- case 'o':
- strncpy(ofile, optarg, sizeof(ofile));
- break;
- case 'h':
- default:
- print_usage();
- return 0;
- }
- }
- int fd;
- FILE *ifp;
- FILE *ofp;
- if ((fd = open_port(port)) == -1) {
- return 0;
- }
- update_options(baud, fd);
- if (strcmp(ifile, "-") == 0) {
- ifp = stdin;
- } else {
- ifp = fopen(ifile, "r");
- }
- if (strcmp(ofile, "-") == 0) {
- ofp = stdout;
- } else {
- ofp = fopen(ofile, "w");
- }
- uint32_t winst;
- uint32_t rinst;
- uint8_t byte;
- switch (mode) {
- case MODE_LOADER:
- while (!feof(ifp)) {
- fscanf(ifp, "%05X\n", &winst);
- byte = (uint8_t)((0x000000FF & winst) >> 0*8); write_port(fd, &byte);
- byte = (uint8_t)((0x0000FF00 & winst) >> 1*8); write_port(fd, &byte);
- byte = (uint8_t)((0x00030000 & winst) >> 2*8); write_port(fd, &byte);
- fprintf(ofp, "< %05X\n", winst);
- }
- break;
- case MODE_IO:
- while (!feof(ifp)) {
- fscanf(ifp, "%06X\n", &winst);
- byte = (uint8_t)((0xFF0000 & winst) >> 2*8); write_port(fd, &byte);
- if (byte == 0x00) {
- byte = (uint8_t)((0x00FF00 & winst) >> 1*8); write_port(fd, &byte);
- byte = (uint8_t)((0x0000FF & winst) >> 0*8); write_port(fd, &byte);
- fprintf(ofp, "< %06X\n", winst);
- } else if (byte == 0x01) {
- byte = (uint8_t)((0x00FF00 & winst) >> 1*8); write_port(fd, &byte);
- fprintf(ofp, "< %04X\n", (0xFFFF00 & winst) >> 1*8);
- }
- rinst = 0x000000;
- read_port(fd, &byte); rinst |= byte << 2*8;
- read_port(fd, &byte); rinst |= byte << 1*8;
- read_port(fd, &byte); rinst |= byte << 0*8;
- fprintf(ofp, "> %06X\n", rinst);
- }
- break;
- case MODE_WRITE:
- fprintf(ofp, "< ");
- while (!feof(ifp)) {
- fscanf(ifp, "%c", &byte);
- write_port(fd, &byte);
- fprintf(ofp, "%c", byte);
- if (byte == '\n') {
- fprintf(ofp, "< ");
- }
- }
- break;
- case MODE_READ:
- fprintf(ofp, "> ");
- while (1) {
- read_port(fd, &byte);
- fprintf(ofp, "%c", byte);
- if (byte == '\n') {
- fprintf(ofp, "> ");
- }
- }
- break;
- default:
- /* NOT REACHED */
- break;
- }
- fclose(ifp);
- fclose(ofp);
- restore_options(fd);
- close_port(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement