OpenCores
URL https://opencores.org/ocsvn/s6soc/s6soc/trunk

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [host/] [dumpuart.cpp] - Blame information for rev 45

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    dumpuart.cpp
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     To produce any and all data from the UART port onto an output
8
//              pipe and/or an output file.  This replaces the minicom
9
//      interface.  This is necessary because 1) minicom can take a while to
10
//      set up, 2) the default interface is at 9600 Baud (not minicom's 
11
//      default), 3) this produces an unfiltered/unbuffered terminal output,
12
//      and 4) it also guarantees that output goes to a file (when requested).
13
//
14
// Creator:     Dan Gisselquist, Ph.D.
15
//              Gisselquist Technology, LLC
16
//
17
////////////////////////////////////////////////////////////////////////////////
18
//
19
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
20
//
21
// This program is free software (firmware): you can redistribute it and/or
22
// modify it under the terms of  the GNU General Public License as published
23
// by the Free Software Foundation, either version 3 of the License, or (at
24
// your option) any later version.
25
//
26
// This program is distributed in the hope that it will be useful, but WITHOUT
27
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
28
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29
// for more details.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
//
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <sys/types.h>
42
#include <sys/stat.h>
43
#include <fcntl.h>
44
#include <unistd.h>
45
#include <strings.h>
46
#include <ctype.h>
47
#include <string.h>
48
#include <signal.h>
49
#include <assert.h>
50
#include <termios.h>
51
 
52
int main(int argc, char **argv) {
53
        const char      *dev = argv[1];
54
        const   char    *fname = (argc>=3)?argv[2]:NULL;
55
        int     ttyfd = -1, dumpfd = -1;
56
 
57 45 dgisselq
        if (argc < 2) {
58
                fprintf(stderr, "USAGE: dumpuart /dev/tty... \n");
59
                exit(EXIT_FAILURE);
60
        }
61
 
62 19 dgisselq
        ttyfd = open(dev, O_RDONLY);
63 45 dgisselq
        if (ttyfd < 0) {
64
                fprintf(stderr, "Could not open device, %s\n", dev);
65
                exit(EXIT_FAILURE);
66
        }
67
        if (!isatty(ttyfd)) {
68
                fprintf(stderr, "Err: %s is not a terminal!\n", dev);
69
                exit(EXIT_FAILURE);
70
        }
71
 
72 19 dgisselq
        if (fname)
73
                dumpfd = open(fname, O_CREAT|O_TRUNC|O_WRONLY, 0644);
74
 
75
        // Set the baud rate ...
76
        {
77
                struct  termios tb;
78
 
79
                tcgetattr(ttyfd, &tb);
80
                // Set to raw mode
81
                cfmakeraw(&tb);
82
                // Non-canonical mode (no line editing ...)
83
                tb.c_lflag &= (~(ICANON));
84
                // Set no parity, 8 bit
85
                tb.c_cflag &= (~(CRTSCTS));
86
                // One stop bit
87
                tb.c_cflag &= (~(CSTOPB));
88
                // 9600 Baud
89
                cfsetspeed(&tb, B9600);
90
                // Block until at least one byte is available
91
                tb.c_cc[VMIN] = 1;
92
                tb.c_cc[VTIME] = 0;
93
                // Commit the changes
94
                tcsetattr(ttyfd, TCSANOW, &tb);
95
        }
96
 
97
        /*
98
        if (isatty(STDOUT_FILENO)) {
99
                struct  termios tb;
100
                tcgetattr(STDOUT_FILENO, &tb);
101
        }
102
        */
103
 
104
        char    buf[64];
105
        int     nr;
106
 
107
        while((nr=read(ttyfd, buf, sizeof(buf)))>0) {
108
                if (dumpfd >= 0)
109
                        write(dumpfd, buf, nr);
110
                write(STDOUT_FILENO, buf, nr);
111
        }
112
 
113
        close(ttyfd);
114
        if (dumpfd >= 0)
115
                close(dumpfd);
116
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.