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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [peripheral/] [channels/] [tty.c] - Blame information for rev 1154

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1127 sfurman
/* tty.c -- Definition of functions for peripheral to
2
 * communicate with host via a tty.
3
 
4
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
5
 
6
This file is part of OpenRISC 1000 Architectural Simulator.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
 
13
This program 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 General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
 
22
#include <stdio.h>
23
#include <termios.h>
24
#include <unistd.h>
25
#include <string.h>
26 1128 sfurman
#include <fcntl.h>
27 1127 sfurman
 
28
#include "channel.h"
29
#include "generic.h"
30
#include "fd.h"
31
 
32
// Default parameters if not specified in config file
33
#define DEFAULT_BAUD B19200
34
#define DEFAULT_TTY_DEVICE "/dev/ttyS0"
35
 
36
struct tty_channel
37
{
38
    struct fd_channel fds;
39
};
40
 
41
static struct {
42
        char* name;
43
        int value;
44
} baud_table[] = {
45
        {"50", B50},
46
        {"2400",   B2400},
47
        {"4800",   B4800},
48
        {"9600",   B9600},
49
        {"19200",  B19200},
50
        {"38400",  B38400},
51
        {"115200", B115200},
52
        {"230400", B230400},
53
        {0,        0}
54
};
55
 
56
// Convert baud rate string to termio baud rate constant
57
int
58
parse_baud(char* baud_string)
59
{
60
        int i;
61
        for (i = 0; baud_table[i].name; i++) {
62
                if (!strcmp(baud_table[i].name, baud_string))
63
                        return baud_table[i].value;
64
        }
65
 
66
        fprintf(stderr, "Error: unknown baud rate: %s\n", baud_string);
67
        fprintf(stderr, "       Known baud rates: ");
68
 
69
        for (i = 0; baud_table[i].name; i++) {
70
                fprintf(stderr, "%s%s", baud_table[i].name, baud_table[i+1].name ? ", " : "\n");
71
        }
72
        return B0;
73
}
74
 
75
static void * tty_init(const char * input)
76
{
77
        int fd, baud;
78
        char *param_name, *param_value, *device;
79
    struct termios options;
80
    struct tty_channel* channel;
81
 
82
        channel = (struct tty_channel*)malloc(sizeof(struct tty_channel));
83
    if (!channel)
84
                return NULL;
85
 
86
        // Make a copy of config string, because we're about to mutate it
87
        input = strdup(input);
88
        if (!input)
89
                goto error;
90
 
91
        baud = DEFAULT_BAUD;
92
        device = DEFAULT_TTY_DEVICE;
93
 
94
        // Parse command-line parameters
95
        // Command line looks like name1=value1,name2,name3=value3,...
96
        while (param_name = strtok((char*)input, ",")) {
97
 
98
                input = NULL;
99
 
100
                // Parse a parameter's name and value
101
                param_value = strchr(param_name,'=');
102
                if (param_value != NULL) {
103
                        *param_value = '\0';
104
                        param_value++;          // Advance past '=' character
105
                }
106
 
107
                if (!strcmp(param_name, "baud") && param_value) {
108
                        baud = parse_baud(param_value);
109
                        if (baud == B0) {
110
                                goto error;
111
                        }
112
                } else if (!strcmp(param_name, "device")) {
113
                        device = param_value;
114
                } else {
115
                        fprintf(stderr, "error: unknown tty channel parameter \"%s\"\n", param_name);
116
                        goto error;
117
                }
118
        }
119
 
120 1128 sfurman
        fd = open(device, O_RDWR);
121 1127 sfurman
        if (fd < 0)
122
                goto error;
123
 
124
    // Get the current options for the port...
125
    if (tcgetattr(fd, &options) < 0)
126
                goto error;
127
 
128
    // Set the serial baud rate
129
    cfsetispeed(&options, baud);
130
    cfsetospeed(&options, baud);
131
 
132
    // Enable the receiver and set local mode...
133 1154 sfurman
    cfmakeraw(&options);
134 1127 sfurman
    options.c_cflag |= (CLOCAL | CREAD);
135
 
136 1154 sfurman
 
137 1127 sfurman
    // Set the new options for the port...
138
    if (tcsetattr(fd, TCSANOW, &options) < 0)
139
                goto error;
140
 
141
    channel->fds.fdin = channel->fds.fdout = fd;
142
        free(input);
143
    return channel;
144
 
145
error:
146
    if (fd > 0)
147
                close(fd);
148
    free(channel);
149
        if (input)
150
                free(input);
151
    return NULL;
152
}
153
 
154
static int tty_open(void * data)
155
{
156
    return 0;
157
}
158
 
159
static int tty_read(void * data, char * buffer, int size)
160
{
161
    return fd_read(data, buffer, size);
162
}
163
 
164
static int tty_write(void * data, const char * buffer, int size)
165
{
166
    return fd_write(data, buffer, size);
167
}
168
 
169
struct channel_ops tty_channel_ops =
170
{
171
    init:   tty_init,
172
    open:   tty_open,
173
    close:  generic_close,
174
    read:   fd_read,
175
    write:  fd_write,
176
    free:   generic_free,
177
};
178
 
179
/*
180
 * Local variables:
181
 * c-file-style: "linux"
182
 * End:
183
 */
184
 

powered by: WebSVN 2.1.0

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