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

Subversion Repositories or1k

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

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
 
27
#include "channel.h"
28
#include "generic.h"
29
#include "fd.h"
30
 
31
// Default parameters if not specified in config file
32
#define DEFAULT_BAUD B19200
33
#define DEFAULT_TTY_DEVICE "/dev/ttyS0"
34
 
35
struct tty_channel
36
{
37
    struct fd_channel fds;
38
};
39
 
40
static struct {
41
        char* name;
42
        int value;
43
} baud_table[] = {
44
        {"50", B50},
45
        {"2400",   B2400},
46
        {"4800",   B4800},
47
        {"9600",   B9600},
48
        {"19200",  B19200},
49
        {"38400",  B38400},
50
        {"115200", B115200},
51
        {"230400", B230400},
52
        {0,        0}
53
};
54
 
55
// Convert baud rate string to termio baud rate constant
56
int
57
parse_baud(char* baud_string)
58
{
59
        int i;
60
        for (i = 0; baud_table[i].name; i++) {
61
                if (!strcmp(baud_table[i].name, baud_string))
62
                        return baud_table[i].value;
63
        }
64
 
65
        fprintf(stderr, "Error: unknown baud rate: %s\n", baud_string);
66
        fprintf(stderr, "       Known baud rates: ");
67
 
68
        for (i = 0; baud_table[i].name; i++) {
69
                fprintf(stderr, "%s%s", baud_table[i].name, baud_table[i+1].name ? ", " : "\n");
70
        }
71
        return B0;
72
}
73
 
74
static void * tty_init(const char * input)
75
{
76
        int fd, baud;
77
        char *param_name, *param_value, *device;
78
    struct termios options;
79
    struct tty_channel* channel;
80
 
81
        channel = (struct tty_channel*)malloc(sizeof(struct tty_channel));
82
    if (!channel)
83
                return NULL;
84
 
85
        // Make a copy of config string, because we're about to mutate it
86
        input = strdup(input);
87
        if (!input)
88
                goto error;
89
 
90
        baud = DEFAULT_BAUD;
91
        device = DEFAULT_TTY_DEVICE;
92
 
93
        // Parse command-line parameters
94
        // Command line looks like name1=value1,name2,name3=value3,...
95
        while (param_name = strtok((char*)input, ",")) {
96
 
97
                input = NULL;
98
 
99
                // Parse a parameter's name and value
100
                param_value = strchr(param_name,'=');
101
                if (param_value != NULL) {
102
                        *param_value = '\0';
103
                        param_value++;          // Advance past '=' character
104
                }
105
 
106
                if (!strcmp(param_name, "baud") && param_value) {
107
                        baud = parse_baud(param_value);
108
                        if (baud == B0) {
109
                                goto error;
110
                        }
111
                } else if (!strcmp(param_name, "device")) {
112
                        device = param_value;
113
                } else {
114
                        fprintf(stderr, "error: unknown tty channel parameter \"%s\"\n", param_name);
115
                        goto error;
116
                }
117
        }
118
 
119
        fd = open(device);
120
        if (fd < 0)
121
                goto error;
122
 
123
    // Get the current options for the port...
124
    if (tcgetattr(fd, &options) < 0)
125
                goto error;
126
 
127
    // Set the serial baud rate
128
    cfsetispeed(&options, baud);
129
    cfsetospeed(&options, baud);
130
 
131
    // Enable the receiver and set local mode...
132
    options.c_cflag |= (CLOCAL | CREAD);
133
 
134
    // Set the new options for the port...
135
    if (tcsetattr(fd, TCSANOW, &options) < 0)
136
                goto error;
137
 
138
    channel->fds.fdin = channel->fds.fdout = fd;
139
        free(input);
140
    return channel;
141
 
142
error:
143
    if (fd > 0)
144
                close(fd);
145
    free(channel);
146
        if (input)
147
                free(input);
148
    return NULL;
149
}
150
 
151
static int tty_open(void * data)
152
{
153
    return 0;
154
}
155
 
156
static int tty_read(void * data, char * buffer, int size)
157
{
158
    return fd_read(data, buffer, size);
159
}
160
 
161
static int tty_write(void * data, const char * buffer, int size)
162
{
163
    return fd_write(data, buffer, size);
164
}
165
 
166
struct channel_ops tty_channel_ops =
167
{
168
    init:   tty_init,
169
    open:   tty_open,
170
    close:  generic_close,
171
    read:   fd_read,
172
    write:  fd_write,
173
    free:   generic_free,
174
};
175
 
176
/*
177
 * Local variables:
178
 * c-file-style: "linux"
179
 * End:
180
 */
181
 

powered by: WebSVN 2.1.0

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