1 | /* |
---|
2 | * Multi UART console serial I/O. |
---|
3 | * |
---|
4 | * TO DO: Add DMA input/output |
---|
5 | */ |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | #include <fcntl.h> |
---|
9 | #include <rtems/libio.h> |
---|
10 | #include <rtems/termiostypes.h> |
---|
11 | #include <termios.h> |
---|
12 | #include <bsp.h> |
---|
13 | #include <malloc.h> |
---|
14 | #include <rtems/mw_uid.h> |
---|
15 | |
---|
16 | #include <rtems/bspIo.h> |
---|
17 | |
---|
18 | #define UART_INTC0_IRQ_VECTOR(x) (64+13+(x)) |
---|
19 | |
---|
20 | #define MCF5282_UART_USR_ERROR ( MCF5282_UART_USR_RB | \ |
---|
21 | MCF5282_UART_USR_FE | \ |
---|
22 | MCF5282_UART_USR_PE | \ |
---|
23 | MCF5282_UART_USR_OE ) |
---|
24 | |
---|
25 | static int IntUartPollWrite(int minor, const char *buf, int len); |
---|
26 | static int IntUartInterruptWrite (int minor, const char *buf, int len); |
---|
27 | |
---|
28 | static void |
---|
29 | _BSP_null_char( char c ) |
---|
30 | { |
---|
31 | int level; |
---|
32 | |
---|
33 | if (c == '\n') |
---|
34 | _BSP_null_char('\r'); |
---|
35 | rtems_interrupt_disable(level); |
---|
36 | while ( (MCF5282_UART_USR(CONSOLE_PORT) & MCF5282_UART_USR_TXRDY) == 0 ) |
---|
37 | continue; |
---|
38 | MCF5282_UART_UTB(CONSOLE_PORT) = c; |
---|
39 | while ( (MCF5282_UART_USR(CONSOLE_PORT) & MCF5282_UART_USR_TXRDY) == 0 ) |
---|
40 | continue; |
---|
41 | rtems_interrupt_enable(level); |
---|
42 | } |
---|
43 | BSP_output_char_function_type BSP_output_char = _BSP_null_char; |
---|
44 | |
---|
45 | #define MAX_UART_INFO 3 |
---|
46 | #define RX_BUFFER_SIZE 512 |
---|
47 | |
---|
48 | struct IntUartInfoStruct |
---|
49 | { |
---|
50 | int iomode; |
---|
51 | volatile int uimr; |
---|
52 | int baud; |
---|
53 | int databits; |
---|
54 | int parity; |
---|
55 | int stopbits; |
---|
56 | int hwflow; |
---|
57 | int rx_in; |
---|
58 | int rx_out; |
---|
59 | char rx_buffer[RX_BUFFER_SIZE]; |
---|
60 | void *ttyp; |
---|
61 | }; |
---|
62 | |
---|
63 | struct IntUartInfoStruct IntUartInfo[MAX_UART_INFO]; |
---|
64 | |
---|
65 | static int GetBaud( int baudHandle ) |
---|
66 | { |
---|
67 | int baud = 9600; |
---|
68 | switch ( baudHandle ) |
---|
69 | { |
---|
70 | case B0: |
---|
71 | baud = (int)0; |
---|
72 | break; |
---|
73 | case B1200: |
---|
74 | baud = (int)1200; |
---|
75 | break; |
---|
76 | case B2400: |
---|
77 | baud = (int)2400; |
---|
78 | break; |
---|
79 | case B4800: |
---|
80 | baud = (int)4800; |
---|
81 | break; |
---|
82 | case B9600: |
---|
83 | baud = (int)9600; |
---|
84 | break; |
---|
85 | case B19200: |
---|
86 | baud = (int)19200; |
---|
87 | break; |
---|
88 | case B38400: |
---|
89 | baud = (int)38400; |
---|
90 | break; |
---|
91 | case B57600: |
---|
92 | baud = (int)57600; |
---|
93 | break; |
---|
94 | case B115200: |
---|
95 | baud = (int)115200; |
---|
96 | break; |
---|
97 | /* case B576000: |
---|
98 | baud = (int)576000; */ |
---|
99 | break; |
---|
100 | } |
---|
101 | return ( baud ); |
---|
102 | } |
---|
103 | |
---|
104 | /*************************************************************************** |
---|
105 | Function : IntUartSet |
---|
106 | |
---|
107 | Description : This updates the hardware UART settings. |
---|
108 | ***************************************************************************/ |
---|
109 | static void |
---|
110 | IntUartSet(int minor, int baud, int databits, int parity, int stopbits, int hwflow) |
---|
111 | { |
---|
112 | int divisor; |
---|
113 | unsigned32 clock_speed; |
---|
114 | unsigned8 umr1 = 0; |
---|
115 | unsigned8 umr2 = 0; |
---|
116 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
117 | int level; |
---|
118 | |
---|
119 | rtems_interrupt_disable(level); |
---|
120 | |
---|
121 | |
---|
122 | /* disable interrupts, clear RTS line, and disable the UARTS */ |
---|
123 | MCF5282_UART_UIMR(minor) = 0; |
---|
124 | MCF5282_UART_UOP0(minor) = 1; |
---|
125 | MCF5282_UART_UCR(minor) = (MCF5282_UART_UCR_TX_DISABLED | MCF5282_UART_UCR_RX_DISABLED); |
---|
126 | |
---|
127 | /* save the current values */ |
---|
128 | info->uimr = 0; |
---|
129 | info->baud = baud; |
---|
130 | info->databits = databits; |
---|
131 | info->parity = parity; |
---|
132 | info->stopbits = stopbits; |
---|
133 | info->hwflow = hwflow; |
---|
134 | |
---|
135 | clock_speed = get_CPU_clock_speed(); |
---|
136 | /* determine the baud divisor value */ |
---|
137 | divisor = (clock_speed / ( 32 * baud )); |
---|
138 | if ( divisor < 2 ) |
---|
139 | divisor = 2; |
---|
140 | |
---|
141 | /* check to see if doing hardware flow control */ |
---|
142 | if ( hwflow ) |
---|
143 | { |
---|
144 | /* set hardware flow options */ |
---|
145 | umr1 |= MCF5282_UART_UMR1_RXRTS; |
---|
146 | umr2 |= MCF5282_UART_UMR2_TXCTS; |
---|
147 | } |
---|
148 | |
---|
149 | /* determine the new umr values */ |
---|
150 | umr1 |= (parity | databits); |
---|
151 | umr2 |= (stopbits); |
---|
152 | |
---|
153 | /* reset the uart */ |
---|
154 | MCF5282_UART_UCR(minor) = MCF5282_UART_UCR_RESET_ERROR; |
---|
155 | MCF5282_UART_UCR(minor) = MCF5282_UART_UCR_RESET_RX; |
---|
156 | MCF5282_UART_UCR(minor) = MCF5282_UART_UCR_RESET_TX; |
---|
157 | |
---|
158 | /* reset the uart mode register and update values */ |
---|
159 | MCF5282_UART_UCR(minor) = MCF5282_UART_UCR_RESET_MR; |
---|
160 | MCF5282_UART_UMR(minor) = umr1; |
---|
161 | MCF5282_UART_UMR(minor) = umr2; |
---|
162 | |
---|
163 | /* set the baud rate values */ |
---|
164 | MCF5282_UART_UCSR(minor) = (MCF5282_UART_UCSR_RCS_SYS_CLK | MCF5282_UART_UCSR_TCS_SYS_CLK); |
---|
165 | MCF5282_UART_UBG1(minor) = (divisor & 0xff00) >> 8; |
---|
166 | MCF5282_UART_UBG2(minor) = (divisor & 0x00ff); |
---|
167 | |
---|
168 | /* enable the uart */ |
---|
169 | MCF5282_UART_UCR(minor) = (MCF5282_UART_UCR_TX_ENABLED | MCF5282_UART_UCR_RX_ENABLED); |
---|
170 | |
---|
171 | /* check to see if interrupts need to be enabled */ |
---|
172 | if ( info->iomode != TERMIOS_POLLED ) |
---|
173 | { |
---|
174 | /* enable rx interrupts */ |
---|
175 | info->uimr |= MCF5282_UART_UIMR_FFULL; |
---|
176 | MCF5282_UART_UIMR(minor) = info->uimr; |
---|
177 | } |
---|
178 | |
---|
179 | /* check to see if doing hardware flow control */ |
---|
180 | if ( hwflow ) |
---|
181 | { |
---|
182 | /* assert the RTS line */ |
---|
183 | MCF5282_UART_UOP1(minor) = 1; |
---|
184 | } |
---|
185 | |
---|
186 | rtems_interrupt_enable(level); |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | /*************************************************************************** |
---|
192 | Function : IntUartSetAttributes |
---|
193 | |
---|
194 | Description : This provides the hardware-dependent portion of tcsetattr(). |
---|
195 | value and sets it. At the moment this just sets the baud rate. |
---|
196 | |
---|
197 | Note: The highest baudrate is 115200 as this stays within |
---|
198 | an error of +/- 5% at 25MHz processor clock |
---|
199 | ***************************************************************************/ |
---|
200 | static int |
---|
201 | IntUartSetAttributes(int minor, const struct termios *t) |
---|
202 | { |
---|
203 | /* set default index values */ |
---|
204 | int baud = (int)9600; |
---|
205 | int databits = (int)MCF5282_UART_UMR1_BC_8; |
---|
206 | int parity = (int)MCF5282_UART_UMR1_PM_NONE; |
---|
207 | int stopbits = (int)MCF5282_UART_UMR2_STOP_BITS_1; |
---|
208 | int hwflow = (int)0; |
---|
209 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
210 | |
---|
211 | /* check to see if input is valid */ |
---|
212 | if ( t != (const struct termios *)0 ) |
---|
213 | { |
---|
214 | /* determine baud rate index */ |
---|
215 | baud = GetBaud( t->c_cflag & CBAUD ); |
---|
216 | |
---|
217 | /* determine data bits */ |
---|
218 | switch ( t->c_cflag & CSIZE ) |
---|
219 | { |
---|
220 | case CS5: |
---|
221 | databits = (int)MCF5282_UART_UMR1_BC_5; |
---|
222 | break; |
---|
223 | case CS6: |
---|
224 | databits = (int)MCF5282_UART_UMR1_BC_6; |
---|
225 | break; |
---|
226 | case CS7: |
---|
227 | databits = (int)MCF5282_UART_UMR1_BC_7; |
---|
228 | break; |
---|
229 | case CS8: |
---|
230 | databits = (int)MCF5282_UART_UMR1_BC_8; |
---|
231 | break; |
---|
232 | } |
---|
233 | |
---|
234 | /* determine if parity is enabled */ |
---|
235 | if ( t->c_cflag & PARENB ) |
---|
236 | { |
---|
237 | if ( t->c_cflag & PARODD ) |
---|
238 | { |
---|
239 | /* odd parity */ |
---|
240 | parity = (int)MCF5282_UART_UMR1_PM_ODD; |
---|
241 | } |
---|
242 | else |
---|
243 | { |
---|
244 | /* even parity */ |
---|
245 | parity = (int)MCF5282_UART_UMR1_PM_EVEN; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | /* determine stop bits */ |
---|
250 | if ( t->c_cflag & CSTOPB ) |
---|
251 | { |
---|
252 | /* two stop bits */ |
---|
253 | stopbits = (int)MCF5282_UART_UMR2_STOP_BITS_2; |
---|
254 | } |
---|
255 | |
---|
256 | /* check to see if hardware flow control */ |
---|
257 | if ( t->c_cflag & CRTSCTS ) |
---|
258 | { |
---|
259 | hwflow = 1; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | /* check to see if values have changed */ |
---|
264 | if ( ( baud != info->baud ) || |
---|
265 | ( databits != info->databits ) || |
---|
266 | ( parity != info->parity ) || |
---|
267 | ( stopbits != info->stopbits ) || |
---|
268 | ( hwflow != info->hwflow ) ) |
---|
269 | { |
---|
270 | |
---|
271 | /* call function to set values */ |
---|
272 | IntUartSet(minor, baud, databits, parity, stopbits, hwflow); |
---|
273 | } |
---|
274 | |
---|
275 | return( RTEMS_SUCCESSFUL ); |
---|
276 | |
---|
277 | } |
---|
278 | |
---|
279 | /*************************************************************************** |
---|
280 | Function : IntUartInterruptHandler |
---|
281 | |
---|
282 | Description : This is the interrupt handler for the internal uart. It |
---|
283 | determines which channel caused the interrupt before queueing any received |
---|
284 | chars and dequeueing chars waiting for transmission. |
---|
285 | ***************************************************************************/ |
---|
286 | static rtems_isr |
---|
287 | IntUartInterruptHandler(rtems_vector_number v) |
---|
288 | { |
---|
289 | unsigned int chan = v - UART_INTC0_IRQ_VECTOR(0); |
---|
290 | struct IntUartInfoStruct *info = &IntUartInfo[chan]; |
---|
291 | |
---|
292 | /* check to see if received data */ |
---|
293 | if ( MCF5282_UART_UISR(chan) & MCF5282_UART_UISR_RXRDY ) |
---|
294 | { |
---|
295 | /* read data and put into the receive buffer */ |
---|
296 | while ( MCF5282_UART_USR(chan) & MCF5282_UART_USR_RXRDY ) |
---|
297 | { |
---|
298 | |
---|
299 | if ( MCF5282_UART_USR(chan) & MCF5282_UART_USR_ERROR ) |
---|
300 | { |
---|
301 | /* clear the error */ |
---|
302 | MCF5282_UART_UCR(chan) = MCF5282_UART_UCR_RESET_ERROR; |
---|
303 | } |
---|
304 | /* put data in rx buffer and check for errors */ |
---|
305 | info->rx_buffer[info->rx_in] = MCF5282_UART_URB(chan); |
---|
306 | |
---|
307 | /* update buffer values */ |
---|
308 | info->rx_in++; |
---|
309 | |
---|
310 | if ( info->rx_in >= RX_BUFFER_SIZE ) |
---|
311 | { |
---|
312 | info->rx_in = 0; |
---|
313 | } |
---|
314 | } |
---|
315 | /* Make sure the port has been opened */ |
---|
316 | if ( info->ttyp ) |
---|
317 | { |
---|
318 | |
---|
319 | /* check to see if task driven */ |
---|
320 | if ( info->iomode == TERMIOS_TASK_DRIVEN ) |
---|
321 | { |
---|
322 | /* notify rx task that rx buffer has data */ |
---|
323 | rtems_termios_rxirq_occured(info->ttyp); |
---|
324 | } |
---|
325 | else |
---|
326 | { |
---|
327 | /* Push up the received data */ |
---|
328 | rtems_termios_enqueue_raw_characters(info->ttyp, info->rx_buffer, info->rx_in); |
---|
329 | info->rx_in = 0; |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | /* check to see if data needs to be transmitted */ |
---|
335 | if ( ( info->uimr & MCF5282_UART_UIMR_TXRDY ) && |
---|
336 | ( MCF5282_UART_UISR(chan) & MCF5282_UART_UISR_TXRDY ) ) |
---|
337 | { |
---|
338 | |
---|
339 | /* disable tx interrupts */ |
---|
340 | info->uimr &= ~MCF5282_UART_UIMR_TXRDY; |
---|
341 | MCF5282_UART_UIMR(chan) = info->uimr; |
---|
342 | |
---|
343 | /* tell upper level that character has been sent */ |
---|
344 | if ( info->ttyp ) |
---|
345 | rtems_termios_dequeue_characters(info->ttyp, 1); |
---|
346 | } |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | /*************************************************************************** |
---|
352 | Function : IntUartInitialize |
---|
353 | |
---|
354 | Description : This initialises the internal uart hardware for all |
---|
355 | internal uarts. If the internal uart is to be interrupt driven then the |
---|
356 | interrupt vectors are hooked. |
---|
357 | ***************************************************************************/ |
---|
358 | static void |
---|
359 | IntUartInitialize(void) |
---|
360 | { |
---|
361 | unsigned int chan; |
---|
362 | struct IntUartInfoStruct *info; |
---|
363 | rtems_isr_entry old_handler; |
---|
364 | int level; |
---|
365 | |
---|
366 | for ( chan = 0; chan < MAX_UART_INFO; chan++ ) |
---|
367 | { |
---|
368 | info = &IntUartInfo[chan]; |
---|
369 | |
---|
370 | info->ttyp = NULL; |
---|
371 | info->rx_in = 0; |
---|
372 | info->rx_out = 0; |
---|
373 | info->baud = -1; |
---|
374 | info->databits = -1; |
---|
375 | info->parity = -1; |
---|
376 | info->stopbits = -1; |
---|
377 | info->hwflow = -1; |
---|
378 | |
---|
379 | MCF5282_UART_UACR(chan) = 0; |
---|
380 | MCF5282_UART_UIMR(chan) = 0; |
---|
381 | if ( info->iomode != TERMIOS_POLLED ) |
---|
382 | { |
---|
383 | rtems_interrupt_catch (IntUartInterruptHandler, |
---|
384 | UART_INTC0_IRQ_VECTOR(chan), |
---|
385 | &old_handler); |
---|
386 | } |
---|
387 | |
---|
388 | /* set uart default values */ |
---|
389 | IntUartSetAttributes(chan, NULL); |
---|
390 | |
---|
391 | /* unmask interrupt */ |
---|
392 | rtems_interrupt_disable(level); |
---|
393 | switch(chan) { |
---|
394 | case 0: |
---|
395 | MCF5282_INTC0_ICR13 = MCF5282_INTC_ICR_IL(UART0_IRQ_LEVEL) | |
---|
396 | MCF5282_INTC_ICR_IP(UART0_IRQ_PRIORITY); |
---|
397 | MCF5282_INTC0_IMRL &= ~(MCF5282_INTC_IMRL_INT13 | |
---|
398 | MCF5282_INTC_IMRL_MASKALL); |
---|
399 | break; |
---|
400 | |
---|
401 | case 1: |
---|
402 | MCF5282_INTC0_ICR14 = MCF5282_INTC_ICR_IL(UART1_IRQ_LEVEL) | |
---|
403 | MCF5282_INTC_ICR_IP(UART1_IRQ_PRIORITY); |
---|
404 | MCF5282_INTC0_IMRL &= ~(MCF5282_INTC_IMRL_INT14 | |
---|
405 | MCF5282_INTC_IMRL_MASKALL); |
---|
406 | break; |
---|
407 | |
---|
408 | case 2: |
---|
409 | MCF5282_INTC0_ICR15 = MCF5282_INTC_ICR_IL(UART2_IRQ_LEVEL) | |
---|
410 | MCF5282_INTC_ICR_IP(UART2_IRQ_PRIORITY); |
---|
411 | MCF5282_INTC0_IMRL &= ~(MCF5282_INTC_IMRL_INT15 | |
---|
412 | MCF5282_INTC_IMRL_MASKALL); |
---|
413 | break; |
---|
414 | } |
---|
415 | rtems_interrupt_enable(level); |
---|
416 | |
---|
417 | } /* of chan loop */ |
---|
418 | |
---|
419 | |
---|
420 | } /* IntUartInitialise */ |
---|
421 | |
---|
422 | |
---|
423 | /*************************************************************************** |
---|
424 | Function : IntUartInterruptWrite |
---|
425 | |
---|
426 | Description : This writes a single character to the appropriate uart |
---|
427 | channel. This is either called during an interrupt or in the user's task |
---|
428 | to initiate a transmit sequence. Calling this routine enables Tx |
---|
429 | interrupts. |
---|
430 | ***************************************************************************/ |
---|
431 | static int |
---|
432 | IntUartInterruptWrite (int minor, const char *buf, int len) |
---|
433 | { |
---|
434 | int level; |
---|
435 | |
---|
436 | rtems_interrupt_disable(level); |
---|
437 | |
---|
438 | /* write out character */ |
---|
439 | MCF5282_UART_UTB(minor) = *buf; |
---|
440 | |
---|
441 | /* enable tx interrupt */ |
---|
442 | IntUartInfo[minor].uimr |= MCF5282_UART_UIMR_TXRDY; |
---|
443 | MCF5282_UART_UIMR(minor) = IntUartInfo[minor].uimr; |
---|
444 | |
---|
445 | rtems_interrupt_enable(level); |
---|
446 | return( 0 ); |
---|
447 | } |
---|
448 | |
---|
449 | /*************************************************************************** |
---|
450 | Function : IntUartInterruptOpen |
---|
451 | |
---|
452 | Description : This enables interrupts when the tty is opened. |
---|
453 | ***************************************************************************/ |
---|
454 | static int |
---|
455 | IntUartInterruptOpen(int major, int minor, void *arg) |
---|
456 | { |
---|
457 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
458 | |
---|
459 | /* enable the uart */ |
---|
460 | MCF5282_UART_UCR(minor) = (MCF5282_UART_UCR_TX_ENABLED | MCF5282_UART_UCR_RX_ENABLED); |
---|
461 | |
---|
462 | /* check to see if interrupts need to be enabled */ |
---|
463 | if ( info->iomode != TERMIOS_POLLED ) |
---|
464 | { |
---|
465 | /* enable rx interrupts */ |
---|
466 | info->uimr |= MCF5282_UART_UIMR_FFULL; |
---|
467 | MCF5282_UART_UIMR(minor) = info->uimr; |
---|
468 | } |
---|
469 | |
---|
470 | /* check to see if doing hardware flow control */ |
---|
471 | if ( info->hwflow ) |
---|
472 | { |
---|
473 | /* assert the RTS line */ |
---|
474 | MCF5282_UART_UOP1(minor) = 1; |
---|
475 | } |
---|
476 | |
---|
477 | return( 0 ); |
---|
478 | } |
---|
479 | |
---|
480 | |
---|
481 | |
---|
482 | /*************************************************************************** |
---|
483 | Function : IntUartInterruptClose |
---|
484 | |
---|
485 | Description : This disables interrupts when the tty is closed. |
---|
486 | ***************************************************************************/ |
---|
487 | static int |
---|
488 | IntUartInterruptClose(int major, int minor, void *arg) |
---|
489 | { |
---|
490 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
491 | |
---|
492 | /* disable the interrupts and the uart */ |
---|
493 | MCF5282_UART_UIMR(minor) = 0; |
---|
494 | MCF5282_UART_UCR(minor) = (MCF5282_UART_UCR_TX_DISABLED | MCF5282_UART_UCR_RX_DISABLED); |
---|
495 | |
---|
496 | /* reset values */ |
---|
497 | info->ttyp = NULL; |
---|
498 | info->uimr = 0; |
---|
499 | info->rx_in = 0; |
---|
500 | info->rx_out = 0; |
---|
501 | |
---|
502 | return( 0 ); |
---|
503 | } |
---|
504 | |
---|
505 | /*************************************************************************** |
---|
506 | Function : IntUartTaskRead |
---|
507 | |
---|
508 | Description : This reads all available characters from the internal uart |
---|
509 | and places them into the termios buffer. The rx interrupts will be |
---|
510 | re-enabled after all data has been read. |
---|
511 | ***************************************************************************/ |
---|
512 | static int |
---|
513 | IntUartTaskRead(int minor) |
---|
514 | { |
---|
515 | char buffer[RX_BUFFER_SIZE]; |
---|
516 | int count; |
---|
517 | int rx_in; |
---|
518 | int index = 0; |
---|
519 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
520 | |
---|
521 | /* determine number of values to copy out */ |
---|
522 | rx_in = info->rx_in; |
---|
523 | if ( info->rx_out <= rx_in ) |
---|
524 | { |
---|
525 | count = rx_in - info->rx_out; |
---|
526 | } |
---|
527 | else |
---|
528 | { |
---|
529 | count = (RX_BUFFER_SIZE - info->rx_out) + rx_in; |
---|
530 | } |
---|
531 | |
---|
532 | /* copy data into local buffer from rx buffer */ |
---|
533 | while ( ( index < count ) && ( index < RX_BUFFER_SIZE ) ) |
---|
534 | { |
---|
535 | /* copy data byte */ |
---|
536 | buffer[index] = info->rx_buffer[info->rx_out]; |
---|
537 | index++; |
---|
538 | |
---|
539 | /* increment rx buffer values */ |
---|
540 | info->rx_out++; |
---|
541 | if ( info->rx_out >= RX_BUFFER_SIZE ) |
---|
542 | { |
---|
543 | info->rx_out = 0; |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | /* check to see if buffer is not empty */ |
---|
548 | if ( count > 0 ) |
---|
549 | { |
---|
550 | /* set characters into termios buffer */ |
---|
551 | rtems_termios_enqueue_raw_characters(info->ttyp, buffer, count); |
---|
552 | } |
---|
553 | |
---|
554 | return( EOF ); |
---|
555 | } |
---|
556 | |
---|
557 | |
---|
558 | |
---|
559 | /*************************************************************************** |
---|
560 | Function : IntUartPollRead |
---|
561 | |
---|
562 | Description : This reads a character from the internal uart. It returns |
---|
563 | to the caller without blocking if not character is waiting. |
---|
564 | ***************************************************************************/ |
---|
565 | static int |
---|
566 | IntUartPollRead (int minor) |
---|
567 | { |
---|
568 | if ( (MCF5282_UART_USR(minor) & MCF5282_UART_USR_RXRDY) == 0 ) |
---|
569 | return(-1); |
---|
570 | |
---|
571 | return(MCF5282_UART_URB(minor)); |
---|
572 | } |
---|
573 | |
---|
574 | |
---|
575 | /*************************************************************************** |
---|
576 | Function : IntUartPollWrite |
---|
577 | |
---|
578 | Description : This writes out each character in the buffer to the |
---|
579 | appropriate internal uart channel waiting till each one is sucessfully |
---|
580 | transmitted. |
---|
581 | ***************************************************************************/ |
---|
582 | static int |
---|
583 | IntUartPollWrite (int minor, const char *buf, int len) |
---|
584 | { |
---|
585 | /* loop over buffer */ |
---|
586 | while ( len-- ) |
---|
587 | { |
---|
588 | /* block until we can transmit */ |
---|
589 | while ( (MCF5282_UART_USR(minor) & MCF5282_UART_USR_TXRDY) == 0 ) |
---|
590 | continue; |
---|
591 | /* transmit data byte */ |
---|
592 | MCF5282_UART_UTB(minor) = *buf++; |
---|
593 | } |
---|
594 | return(0); |
---|
595 | } |
---|
596 | |
---|
597 | /*************************************************************************** |
---|
598 | Function : console_reserve_resources |
---|
599 | |
---|
600 | Description : This reserves resources consumed by this driver. It passes |
---|
601 | the request on to the termios subsystem. |
---|
602 | ***************************************************************************/ |
---|
603 | void console_reserve_resources( rtems_configuration_table *configuration ) |
---|
604 | { |
---|
605 | rtems_termios_reserve_resources (configuration, 1); |
---|
606 | } |
---|
607 | |
---|
608 | |
---|
609 | /*************************************************************************** |
---|
610 | Function : console_initialize |
---|
611 | |
---|
612 | Description : This initialises termios, both sets of uart hardware before |
---|
613 | registering /dev/tty devices for each channel and the system /dev/console. |
---|
614 | ***************************************************************************/ |
---|
615 | rtems_device_driver console_initialize( |
---|
616 | rtems_device_major_number major, |
---|
617 | rtems_device_minor_number minor, |
---|
618 | void *arg ) |
---|
619 | { |
---|
620 | rtems_status_code status; |
---|
621 | |
---|
622 | |
---|
623 | /* Set up TERMIOS */ |
---|
624 | rtems_termios_initialize (); |
---|
625 | |
---|
626 | /* set io modes for the different channels and initialize device */ |
---|
627 | IntUartInfo[minor].iomode = TERMIOS_IRQ_DRIVEN; |
---|
628 | IntUartInitialize(); |
---|
629 | |
---|
630 | /* Register the console port */ |
---|
631 | status = rtems_io_register_name ("/dev/console", major, CONSOLE_PORT); |
---|
632 | if ( status != RTEMS_SUCCESSFUL ) |
---|
633 | { |
---|
634 | rtems_fatal_error_occurred (status); |
---|
635 | } |
---|
636 | |
---|
637 | /* Register the other port */ |
---|
638 | if ( CONSOLE_PORT != 0 ) |
---|
639 | { |
---|
640 | status = rtems_io_register_name ("/dev/tty00", major, 0); |
---|
641 | if ( status != RTEMS_SUCCESSFUL ) |
---|
642 | { |
---|
643 | rtems_fatal_error_occurred (status); |
---|
644 | } |
---|
645 | } |
---|
646 | if ( CONSOLE_PORT != 1 ) |
---|
647 | { |
---|
648 | status = rtems_io_register_name ("/dev/tty01", major, 1); |
---|
649 | if ( status != RTEMS_SUCCESSFUL ) |
---|
650 | { |
---|
651 | rtems_fatal_error_occurred (status); |
---|
652 | } |
---|
653 | } |
---|
654 | |
---|
655 | return(RTEMS_SUCCESSFUL); |
---|
656 | } |
---|
657 | |
---|
658 | /*************************************************************************** |
---|
659 | Function : console_open |
---|
660 | |
---|
661 | Description : This actually opens the device depending on the minor |
---|
662 | number set during initialisation. The device specific access routines are |
---|
663 | passed to termios when the devices is opened depending on whether it is |
---|
664 | polled or not. |
---|
665 | ***************************************************************************/ |
---|
666 | rtems_device_driver console_open( |
---|
667 | rtems_device_major_number major, |
---|
668 | rtems_device_minor_number minor, |
---|
669 | void * arg) |
---|
670 | { |
---|
671 | rtems_status_code status = RTEMS_INVALID_NUMBER; |
---|
672 | rtems_libio_open_close_args_t *args = (rtems_libio_open_close_args_t *)arg; |
---|
673 | struct IntUartInfoStruct *info; |
---|
674 | |
---|
675 | static const rtems_termios_callbacks IntUartPollCallbacks = { |
---|
676 | NULL, /* firstOpen */ |
---|
677 | NULL, /* lastClose */ |
---|
678 | IntUartPollRead, /* pollRead */ |
---|
679 | IntUartPollWrite, /* write */ |
---|
680 | IntUartSetAttributes, /* setAttributes */ |
---|
681 | NULL, /* stopRemoteTx */ |
---|
682 | NULL, /* startRemoteTx */ |
---|
683 | TERMIOS_POLLED /* mode */ |
---|
684 | }; |
---|
685 | static const rtems_termios_callbacks IntUartIntrCallbacks = { |
---|
686 | IntUartInterruptOpen, /* firstOpen */ |
---|
687 | IntUartInterruptClose, /* lastClose */ |
---|
688 | NULL, /* pollRead */ |
---|
689 | IntUartInterruptWrite, /* write */ |
---|
690 | IntUartSetAttributes, /* setAttributes */ |
---|
691 | NULL, /* stopRemoteTx */ |
---|
692 | NULL, /* startRemoteTx */ |
---|
693 | TERMIOS_IRQ_DRIVEN /* mode */ |
---|
694 | }; |
---|
695 | |
---|
696 | static const rtems_termios_callbacks IntUartTaskCallbacks = { |
---|
697 | IntUartInterruptOpen, /* firstOpen */ |
---|
698 | IntUartInterruptClose, /* lastClose */ |
---|
699 | IntUartTaskRead, /* pollRead */ |
---|
700 | IntUartInterruptWrite, /* write */ |
---|
701 | IntUartSetAttributes, /* setAttributes */ |
---|
702 | NULL, /* stopRemoteTx */ |
---|
703 | NULL, /* startRemoteTx */ |
---|
704 | TERMIOS_TASK_DRIVEN /* mode */ |
---|
705 | }; |
---|
706 | |
---|
707 | /* open the port depending on the minor device number */ |
---|
708 | if ( ( minor >= 0 ) && ( minor < MAX_UART_INFO ) ) |
---|
709 | { |
---|
710 | info = &IntUartInfo[minor]; |
---|
711 | switch ( info->iomode ) |
---|
712 | { |
---|
713 | case TERMIOS_POLLED: |
---|
714 | status = rtems_termios_open(major, minor, arg, &IntUartPollCallbacks); |
---|
715 | break; |
---|
716 | case TERMIOS_IRQ_DRIVEN: |
---|
717 | status = rtems_termios_open(major, minor, arg, &IntUartIntrCallbacks); |
---|
718 | info->ttyp = args->iop->data1; |
---|
719 | break; |
---|
720 | case TERMIOS_TASK_DRIVEN: |
---|
721 | status = rtems_termios_open(major, minor, arg, &IntUartTaskCallbacks); |
---|
722 | info->ttyp = args->iop->data1; |
---|
723 | break; |
---|
724 | } |
---|
725 | } |
---|
726 | |
---|
727 | return( status ); |
---|
728 | } |
---|
729 | |
---|
730 | /*************************************************************************** |
---|
731 | Function : console_close |
---|
732 | |
---|
733 | Description : This closes the device via termios |
---|
734 | ***************************************************************************/ |
---|
735 | rtems_device_driver console_close( |
---|
736 | rtems_device_major_number major, |
---|
737 | rtems_device_minor_number minor, |
---|
738 | void * arg) |
---|
739 | { |
---|
740 | return(rtems_termios_close (arg)); |
---|
741 | } |
---|
742 | |
---|
743 | /****************** |
---|
744 | ********************************************************* |
---|
745 | Function : console_read |
---|
746 | |
---|
747 | Description : Read from the device via termios |
---|
748 | ***************************************************************************/ |
---|
749 | rtems_device_driver console_read( |
---|
750 | rtems_device_major_number major, |
---|
751 | rtems_device_minor_number minor, |
---|
752 | void * arg) |
---|
753 | { |
---|
754 | return(rtems_termios_read (arg)); |
---|
755 | } |
---|
756 | |
---|
757 | /*************************************************************************** |
---|
758 | Function : console_write |
---|
759 | |
---|
760 | Description : Write to the device via termios |
---|
761 | ***************************************************************************/ |
---|
762 | rtems_device_driver console_write( |
---|
763 | rtems_device_major_number major, |
---|
764 | rtems_device_minor_number minor, |
---|
765 | void * arg) |
---|
766 | { |
---|
767 | return(rtems_termios_write (arg)); |
---|
768 | } |
---|
769 | |
---|
770 | /*************************************************************************** |
---|
771 | Function : console_ioctl |
---|
772 | |
---|
773 | Description : Pass the IOCtl call to termios |
---|
774 | ***************************************************************************/ |
---|
775 | rtems_device_driver console_control( |
---|
776 | rtems_device_major_number major, |
---|
777 | rtems_device_minor_number minor, |
---|
778 | void * arg) |
---|
779 | { |
---|
780 | return( rtems_termios_ioctl (arg) ); |
---|
781 | } |
---|
782 | int DEBUG_OUTCHAR(int c) |
---|
783 | { |
---|
784 | if(c == '\n') |
---|
785 | DEBUG_OUTCHAR('\r'); |
---|
786 | _BSP_null_char(c); |
---|
787 | return c; |
---|
788 | } |
---|
789 | void DEBUG_OUTSTR(const char *msg) |
---|
790 | { |
---|
791 | while (*msg) |
---|
792 | DEBUG_OUTCHAR(*msg++); |
---|
793 | } |
---|
794 | void DEBUG_OUTNUM(int i) |
---|
795 | { |
---|
796 | int n; |
---|
797 | static const char map[] = "0123456789ABCDEF"; |
---|
798 | DEBUG_OUTCHAR(' '); |
---|
799 | for (n = 28 ; n >= 0 ; n -= 4) |
---|
800 | DEBUG_OUTCHAR(map[(i >> n) & 0xF]); |
---|
801 | } |
---|