1 |
2 |
alfik |
/******************************************************************************
|
2 |
|
|
* *
|
3 |
|
|
* License Agreement *
|
4 |
|
|
* *
|
5 |
|
|
* Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
|
6 |
|
|
* All rights reserved. *
|
7 |
|
|
* *
|
8 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
9 |
|
|
* copy of this software and associated documentation files (the "Software"), *
|
10 |
|
|
* to deal in the Software without restriction, including without limitation *
|
11 |
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
12 |
|
|
* and/or sell copies of the Software, and to permit persons to whom the *
|
13 |
|
|
* Software is furnished to do so, subject to the following conditions: *
|
14 |
|
|
* *
|
15 |
|
|
* The above copyright notice and this permission notice shall be included in *
|
16 |
|
|
* all copies or substantial portions of the Software. *
|
17 |
|
|
* *
|
18 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
19 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
21 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
22 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
23 |
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
24 |
|
|
* DEALINGS IN THE SOFTWARE. *
|
25 |
|
|
* *
|
26 |
|
|
* This agreement shall be governed in all respects by the laws of the State *
|
27 |
|
|
* of California and by the laws of the United States of America. *
|
28 |
|
|
* *
|
29 |
|
|
******************************************************************************/
|
30 |
|
|
|
31 |
|
|
#include <string.h>
|
32 |
|
|
#include <fcntl.h>
|
33 |
|
|
#include <errno.h>
|
34 |
|
|
#include <limits.h>
|
35 |
|
|
|
36 |
|
|
#include <sys/stat.h>
|
37 |
|
|
|
38 |
|
|
#include "sys/alt_irq.h"
|
39 |
|
|
#include "sys/alt_alarm.h"
|
40 |
|
|
#include "sys/ioctl.h"
|
41 |
|
|
#include "alt_types.h"
|
42 |
|
|
|
43 |
|
|
#include "altera_avalon_jtag_uart_regs.h"
|
44 |
|
|
#include "altera_avalon_jtag_uart.h"
|
45 |
|
|
|
46 |
|
|
#include "sys/alt_log_printf.h"
|
47 |
|
|
|
48 |
|
|
#ifdef __ucosii__
|
49 |
|
|
#include "includes.h"
|
50 |
|
|
#endif /* __ucosii__ */
|
51 |
|
|
|
52 |
|
|
#ifdef ALTERA_AVALON_JTAG_UART_SMALL
|
53 |
|
|
|
54 |
|
|
/* ----------------------------------------------------------- */
|
55 |
|
|
/* ------------------------ SMALL DRIVER --------------------- */
|
56 |
|
|
/* ----------------------------------------------------------- */
|
57 |
|
|
|
58 |
|
|
/* Write routine. The small version blocks when there is no space to write
|
59 |
|
|
* into, so it's performance will be very bad if you are writing more than
|
60 |
|
|
* one FIFOs worth of data. But you said you didn't want to use interrupts :-)
|
61 |
|
|
*/
|
62 |
|
|
|
63 |
|
|
int altera_avalon_jtag_uart_write(altera_avalon_jtag_uart_state* sp,
|
64 |
|
|
const char * ptr, int count, int flags)
|
65 |
|
|
{
|
66 |
|
|
unsigned int base = sp->base;
|
67 |
|
|
|
68 |
|
|
const char * end = ptr + count;
|
69 |
|
|
|
70 |
|
|
while (ptr < end)
|
71 |
|
|
if ((IORD_ALTERA_AVALON_JTAG_UART_CONTROL(base) & ALTERA_AVALON_JTAG_UART_CONTROL_WSPACE_MSK) != 0)
|
72 |
|
|
IOWR_ALTERA_AVALON_JTAG_UART_DATA(base, *ptr++);
|
73 |
|
|
|
74 |
|
|
return count;
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
#else /* !ALTERA_AVALON_JTAG_UART_SMALL */
|
78 |
|
|
|
79 |
|
|
/* ----------------------------------------------------------- */
|
80 |
|
|
/* ------------------------- FAST DRIVER --------------------- */
|
81 |
|
|
/* ----------------------------------------------------------- */
|
82 |
|
|
|
83 |
|
|
int
|
84 |
|
|
altera_avalon_jtag_uart_write(altera_avalon_jtag_uart_state* sp,
|
85 |
|
|
const char * ptr, int count, int flags)
|
86 |
|
|
{
|
87 |
|
|
/* Remove warning at optimisation level 03 by seting out to 0 */
|
88 |
|
|
unsigned int in, out=0;
|
89 |
|
|
unsigned int n;
|
90 |
|
|
alt_irq_context context;
|
91 |
|
|
|
92 |
|
|
const char * start = ptr;
|
93 |
|
|
|
94 |
|
|
/*
|
95 |
|
|
* When running in a multi threaded environment, obtain the "write_lock"
|
96 |
|
|
* semaphore. This ensures that writing to the device is thread-safe.
|
97 |
|
|
*/
|
98 |
|
|
ALT_SEM_PEND (sp->write_lock, 0);
|
99 |
|
|
|
100 |
|
|
do
|
101 |
|
|
{
|
102 |
|
|
/* Copy as much as we can into the transmit buffer */
|
103 |
|
|
while (count > 0)
|
104 |
|
|
{
|
105 |
|
|
/* We need a stable value of the out pointer to calculate the space available */
|
106 |
|
|
in = sp->tx_in;
|
107 |
|
|
out = sp->tx_out;
|
108 |
|
|
|
109 |
|
|
if (in < out)
|
110 |
|
|
n = out - 1 - in;
|
111 |
|
|
else if (out > 0)
|
112 |
|
|
n = ALTERA_AVALON_JTAG_UART_BUF_LEN - in;
|
113 |
|
|
else
|
114 |
|
|
n = ALTERA_AVALON_JTAG_UART_BUF_LEN - 1 - in;
|
115 |
|
|
|
116 |
|
|
if (n == 0)
|
117 |
|
|
break;
|
118 |
|
|
|
119 |
|
|
if (n > count)
|
120 |
|
|
n = count;
|
121 |
|
|
|
122 |
|
|
memcpy(sp->tx_buf + in, ptr, n);
|
123 |
|
|
ptr += n;
|
124 |
|
|
count -= n;
|
125 |
|
|
|
126 |
|
|
sp->tx_in = (in + n) % ALTERA_AVALON_JTAG_UART_BUF_LEN;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/*
|
130 |
|
|
* If interrupts are disabled then we could transmit here, we only need
|
131 |
|
|
* to enable interrupts if there is no space left in the FIFO
|
132 |
|
|
*
|
133 |
|
|
* For now kick the interrupt routine every time to make it transmit
|
134 |
|
|
* the data
|
135 |
|
|
*/
|
136 |
|
|
context = alt_irq_disable_all();
|
137 |
|
|
sp->irq_enable |= ALTERA_AVALON_JTAG_UART_CONTROL_WE_MSK;
|
138 |
|
|
IOWR_ALTERA_AVALON_JTAG_UART_CONTROL(sp->base, sp->irq_enable);
|
139 |
|
|
alt_irq_enable_all(context);
|
140 |
|
|
|
141 |
|
|
/*
|
142 |
|
|
* If there is any data left then either return now or block until
|
143 |
|
|
* some has been sent
|
144 |
|
|
*/
|
145 |
|
|
/* consider: test whether there is anything there while doing this and delay for at most 2s. */
|
146 |
|
|
if (count > 0)
|
147 |
|
|
{
|
148 |
|
|
if (flags & O_NONBLOCK)
|
149 |
|
|
break;
|
150 |
|
|
|
151 |
|
|
#ifdef __ucosii__
|
152 |
|
|
/* OS Present: Pend on a flag if the OS is running, otherwise spin */
|
153 |
|
|
if(OSRunning == OS_TRUE) {
|
154 |
|
|
/*
|
155 |
|
|
* When running in a multi-threaded mode, we pend on the write event
|
156 |
|
|
* flag set or the timeout flag in the isr. This avoids wasting CPU
|
157 |
|
|
* cycles waiting in this thread, when we could be doing something
|
158 |
|
|
* more profitable elsewhere.
|
159 |
|
|
*/
|
160 |
|
|
#ifdef ALTERA_AVALON_JTAG_UART_IGNORE_FIFO_FULL_ERROR
|
161 |
|
|
if(!sp->host_inactive)
|
162 |
|
|
#endif
|
163 |
|
|
ALT_FLAG_PEND (sp->events,
|
164 |
|
|
ALT_JTAG_UART_WRITE_RDY | ALT_JTAG_UART_TIMEOUT,
|
165 |
|
|
OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
|
166 |
|
|
0);
|
167 |
|
|
}
|
168 |
|
|
else {
|
169 |
|
|
/*
|
170 |
|
|
* OS not running: Wait for data to be removed from buffer.
|
171 |
|
|
* Once the interrupt routine has removed some data then we
|
172 |
|
|
* will be able to insert some more.
|
173 |
|
|
*/
|
174 |
|
|
while (out == sp->tx_out && sp->host_inactive < sp->timeout)
|
175 |
|
|
;
|
176 |
|
|
}
|
177 |
|
|
#else
|
178 |
|
|
/*
|
179 |
|
|
* No OS present: Always wait for data to be removed from buffer. Once
|
180 |
|
|
* the interrupt routine has removed some data then we will be able to
|
181 |
|
|
* insert some more.
|
182 |
|
|
*/
|
183 |
|
|
while (out == sp->tx_out && sp->host_inactive < sp->timeout)
|
184 |
|
|
;
|
185 |
|
|
#endif /* __ucosii__ */
|
186 |
|
|
|
187 |
|
|
if (out == sp->tx_out)
|
188 |
|
|
break;
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
while (count > 0);
|
192 |
|
|
|
193 |
|
|
/*
|
194 |
|
|
* Now that access to the circular buffer is complete, release the write
|
195 |
|
|
* semaphore so that other threads can access the buffer.
|
196 |
|
|
*/
|
197 |
|
|
ALT_SEM_POST (sp->write_lock);
|
198 |
|
|
|
199 |
|
|
if (ptr != start)
|
200 |
|
|
return ptr - start;
|
201 |
|
|
else if (flags & O_NONBLOCK)
|
202 |
|
|
return -EWOULDBLOCK;
|
203 |
|
|
#ifdef ALTERA_AVALON_JTAG_UART_IGNORE_FIFO_FULL_ERROR
|
204 |
|
|
else if (sp->host_inactive >= sp->timeout) {
|
205 |
|
|
/*
|
206 |
|
|
* Reset the software FIFO, hardware FIFO could not be reset.
|
207 |
|
|
* Just throw away characters without reporting error.
|
208 |
|
|
*/
|
209 |
|
|
sp->tx_out = sp->tx_in = 0;
|
210 |
|
|
return ptr - start + count;
|
211 |
|
|
}
|
212 |
|
|
#endif
|
213 |
|
|
else
|
214 |
|
|
return -EIO; /* Host not connected */
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
#endif /* ALTERA_AVALON_JTAG_UART_SMALL */
|