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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [peripheral/] [channels/] [tty.c] - Diff between revs 1127 and 1128

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1127 Rev 1128
/* tty.c -- Definition of functions for peripheral to
/* tty.c -- Definition of functions for peripheral to
 * communicate with host via a tty.
 * communicate with host via a tty.
 
 
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
   Copyright (C) 2002 Richard Prescott <rip@step.polymtl.ca>
 
 
This file is part of OpenRISC 1000 Architectural Simulator.
This file is part of OpenRISC 1000 Architectural Simulator.
 
 
This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
 
#include <stdio.h>
#include <stdio.h>
#include <termios.h>
#include <termios.h>
#include <unistd.h>
#include <unistd.h>
#include <string.h>
#include <string.h>
 
#include <fcntl.h>
 
 
#include "channel.h"
#include "channel.h"
#include "generic.h"
#include "generic.h"
#include "fd.h"
#include "fd.h"
 
 
// Default parameters if not specified in config file
// Default parameters if not specified in config file
#define DEFAULT_BAUD B19200
#define DEFAULT_BAUD B19200
#define DEFAULT_TTY_DEVICE "/dev/ttyS0"
#define DEFAULT_TTY_DEVICE "/dev/ttyS0"
 
 
struct tty_channel
struct tty_channel
{
{
    struct fd_channel fds;
    struct fd_channel fds;
};
};
 
 
static struct {
static struct {
        char* name;
        char* name;
        int value;
        int value;
} baud_table[] = {
} baud_table[] = {
        {"50", B50},
        {"50", B50},
        {"2400",   B2400},
        {"2400",   B2400},
        {"4800",   B4800},
        {"4800",   B4800},
        {"9600",   B9600},
        {"9600",   B9600},
        {"19200",  B19200},
        {"19200",  B19200},
        {"38400",  B38400},
        {"38400",  B38400},
        {"115200", B115200},
        {"115200", B115200},
        {"230400", B230400},
        {"230400", B230400},
        {0,        0}
        {0,        0}
};
};
 
 
// Convert baud rate string to termio baud rate constant
// Convert baud rate string to termio baud rate constant
int
int
parse_baud(char* baud_string)
parse_baud(char* baud_string)
{
{
        int i;
        int i;
        for (i = 0; baud_table[i].name; i++) {
        for (i = 0; baud_table[i].name; i++) {
                if (!strcmp(baud_table[i].name, baud_string))
                if (!strcmp(baud_table[i].name, baud_string))
                        return baud_table[i].value;
                        return baud_table[i].value;
        }
        }
 
 
        fprintf(stderr, "Error: unknown baud rate: %s\n", baud_string);
        fprintf(stderr, "Error: unknown baud rate: %s\n", baud_string);
        fprintf(stderr, "       Known baud rates: ");
        fprintf(stderr, "       Known baud rates: ");
 
 
        for (i = 0; baud_table[i].name; i++) {
        for (i = 0; baud_table[i].name; i++) {
                fprintf(stderr, "%s%s", baud_table[i].name, baud_table[i+1].name ? ", " : "\n");
                fprintf(stderr, "%s%s", baud_table[i].name, baud_table[i+1].name ? ", " : "\n");
        }
        }
        return B0;
        return B0;
}
}
 
 
static void * tty_init(const char * input)
static void * tty_init(const char * input)
{
{
        int fd, baud;
        int fd, baud;
        char *param_name, *param_value, *device;
        char *param_name, *param_value, *device;
    struct termios options;
    struct termios options;
    struct tty_channel* channel;
    struct tty_channel* channel;
 
 
        channel = (struct tty_channel*)malloc(sizeof(struct tty_channel));
        channel = (struct tty_channel*)malloc(sizeof(struct tty_channel));
    if (!channel)
    if (!channel)
                return NULL;
                return NULL;
 
 
        // Make a copy of config string, because we're about to mutate it
        // Make a copy of config string, because we're about to mutate it
        input = strdup(input);
        input = strdup(input);
        if (!input)
        if (!input)
                goto error;
                goto error;
 
 
        baud = DEFAULT_BAUD;
        baud = DEFAULT_BAUD;
        device = DEFAULT_TTY_DEVICE;
        device = DEFAULT_TTY_DEVICE;
 
 
        // Parse command-line parameters
        // Parse command-line parameters
        // Command line looks like name1=value1,name2,name3=value3,...
        // Command line looks like name1=value1,name2,name3=value3,...
        while (param_name = strtok((char*)input, ",")) {
        while (param_name = strtok((char*)input, ",")) {
 
 
                input = NULL;
                input = NULL;
 
 
                // Parse a parameter's name and value
                // Parse a parameter's name and value
                param_value = strchr(param_name,'=');
                param_value = strchr(param_name,'=');
                if (param_value != NULL) {
                if (param_value != NULL) {
                        *param_value = '\0';
                        *param_value = '\0';
                        param_value++;          // Advance past '=' character
                        param_value++;          // Advance past '=' character
                }
                }
 
 
                if (!strcmp(param_name, "baud") && param_value) {
                if (!strcmp(param_name, "baud") && param_value) {
                        baud = parse_baud(param_value);
                        baud = parse_baud(param_value);
                        if (baud == B0) {
                        if (baud == B0) {
                                goto error;
                                goto error;
                        }
                        }
                } else if (!strcmp(param_name, "device")) {
                } else if (!strcmp(param_name, "device")) {
                        device = param_value;
                        device = param_value;
                } else {
                } else {
                        fprintf(stderr, "error: unknown tty channel parameter \"%s\"\n", param_name);
                        fprintf(stderr, "error: unknown tty channel parameter \"%s\"\n", param_name);
                        goto error;
                        goto error;
                }
                }
        }
        }
 
 
        fd = open(device);
        fd = open(device, O_RDWR);
        if (fd < 0)
        if (fd < 0)
                goto error;
                goto error;
 
 
    // Get the current options for the port...
    // Get the current options for the port...
    if (tcgetattr(fd, &options) < 0)
    if (tcgetattr(fd, &options) < 0)
                goto error;
                goto error;
 
 
    // Set the serial baud rate
    // Set the serial baud rate
    cfsetispeed(&options, baud);
    cfsetispeed(&options, baud);
    cfsetospeed(&options, baud);
    cfsetospeed(&options, baud);
 
 
    // Enable the receiver and set local mode...
    // Enable the receiver and set local mode...
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag |= (CLOCAL | CREAD);
 
 
    // Set the new options for the port...
    // Set the new options for the port...
    if (tcsetattr(fd, TCSANOW, &options) < 0)
    if (tcsetattr(fd, TCSANOW, &options) < 0)
                goto error;
                goto error;
 
 
    channel->fds.fdin = channel->fds.fdout = fd;
    channel->fds.fdin = channel->fds.fdout = fd;
        free(input);
        free(input);
    return channel;
    return channel;
 
 
error:
error:
    if (fd > 0)
    if (fd > 0)
                close(fd);
                close(fd);
    free(channel);
    free(channel);
        if (input)
        if (input)
                free(input);
                free(input);
    return NULL;
    return NULL;
}
}
 
 
static int tty_open(void * data)
static int tty_open(void * data)
{
{
    return 0;
    return 0;
}
}
 
 
static int tty_read(void * data, char * buffer, int size)
static int tty_read(void * data, char * buffer, int size)
{
{
    return fd_read(data, buffer, size);
    return fd_read(data, buffer, size);
}
}
 
 
static int tty_write(void * data, const char * buffer, int size)
static int tty_write(void * data, const char * buffer, int size)
{
{
    return fd_write(data, buffer, size);
    return fd_write(data, buffer, size);
}
}
 
 
struct channel_ops tty_channel_ops =
struct channel_ops tty_channel_ops =
{
{
    init:   tty_init,
    init:   tty_init,
    open:   tty_open,
    open:   tty_open,
    close:  generic_close,
    close:  generic_close,
    read:   fd_read,
    read:   fd_read,
    write:  fd_write,
    write:  fd_write,
    free:   generic_free,
    free:   generic_free,
};
};
 
 
/*
/*
 * Local variables:
 * Local variables:
 * c-file-style: "linux"
 * c-file-style: "linux"
 * End:
 * End:
 */
 */
 
 
 
 

powered by: WebSVN 2.1.0

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