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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [arm/] [edb7xxx/] [current/] [support/] [io.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//        io.c
4
//
5
//        Linux I/O support for Cirrus Logic EDB7xxx eval board tools
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     gthomas
43
// Contributors:  gthomas
44
// Date:          1999-06-16
45
// Description:   Linux I/O support for Cirrus Logic EDB7xxx FLASH tools
46
//####DESCRIPTIONEND####
47
 
48
//
49
// io.c - I/O functions
50
//
51
 
52
#include <stdio.h>
53
#include <sys/ioctl.h>
54
#include <fcntl.h>
55
#include <termios.h>
56
#include <errno.h>
57
 
58
char _ReceiveChar(long port)
59
{
60
    char buf;
61
    int res;
62
    // Port is in non-blocking mode, this needs to have a loop
63
    do {
64
        res = read(port, &buf, 1);
65
    } while (res < 0);
66
//    printf("Read: %c\n", buf);
67
    return buf;
68
}
69
 
70
static void
71
uspin(int len)
72
{
73
    volatile int cnt;
74
    while (--len >= 0) {
75
        for (cnt = 1;  cnt < 2000;  cnt++) ;
76
    }
77
}
78
 
79
void _SendChar(long port, char ch)
80
{
81
    char buf = ch;
82
    write(port, &buf, 1);
83
//    printf("Send: %x\n", ch);
84
//    usleep(100);
85
    uspin(100);
86
}
87
 
88
void _SetBaud(long port, long reqRate)
89
{
90
    struct termios buf;
91
    int rate;
92
 
93
    // Get original configuration
94
    if (tcgetattr(port, &buf)) {
95
        fprintf(stderr, "Can't get port config\n");
96
        return;
97
    }
98
 
99
    // Reset to raw.
100
    buf.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
101
                     |INLCR|IGNCR|ICRNL|IXON);
102
    buf.c_oflag &= ~OPOST;
103
    buf.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
104
    buf.c_cflag &= ~(CSIZE|PARENB);
105
    buf.c_cflag |= CS8;
106
 
107
    // Set baud rate.
108
    switch (reqRate) {
109
    case 9600:
110
        rate = B9600;
111
        break;
112
    case 38400:
113
        rate = B38400;
114
        break;
115
    case 57600:
116
        rate = B57600;
117
        break;
118
    case 115200:
119
        rate = B115200;
120
        break;
121
    }
122
    cfsetispeed(&buf, rate);
123
    cfsetospeed(&buf, rate);
124
 
125
    // Set data bits.
126
    buf.c_cflag &= ~CSIZE;
127
    buf.c_cflag |= CS8;
128
 
129
    // Set stop bits.
130
    buf.c_cflag &= ~CSTOPB;  // One stop
131
 
132
    // Set parity.
133
    buf.c_cflag &= ~(PARENB | PARODD); // no parity.
134
 
135
    // Set the new settings
136
    if (tcsetattr(port, TCSADRAIN, &buf)) {
137
        fprintf(stderr, "Error: tcsetattr\n");
138
        return;
139
    }
140
 
141
    tcdrain(port);
142
    usleep(1000000/10*2);
143
    sleep(2);
144
}
145
 
146
int _CharReady(long port)
147
{
148
#ifdef __CYGWIN__
149
    // Windows doesn't support the below ioctl
150
    return 1;
151
#else
152
    int n, res;
153
    res = ioctl(port, FIONREAD, &n);
154
    if (res < 0) {
155
        fprintf(stderr, "I/O error: %s\n", strerror(errno));
156
        exit(1);
157
    }
158
    return n;  // Non-zero if characters ready to read
159
#endif
160
}
161
 
162
void _WaitForOutputEmpty(long port)
163
{
164
    usleep(2000000);
165
}
166
 
167
long _OpenPort(char *name)
168
{
169
    int fd;
170
    fd = open(name, O_RDWR|O_NONBLOCK);
171
    return fd;
172
}

powered by: WebSVN 2.1.0

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