source: rtems/cpukit/libmisc/serdbg/termios_printk.c @ f26145b

4.104.114.84.95
Last change on this file since f26145b was aed742c, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/16/04 at 12:06:28

Remove stray white spaces.

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/*===============================================================*\
2| File: termios_printk.c                                          |
3+-----------------------------------------------------------------+
4|                    Copyright (c) 2002 IMD                       |
5|      Ingenieurbuero fuer Microcomputertechnik Th. Doerfler      |
6|      Hebststr. 8, 82178 Puchheim, Germany                       |
7|      <Thomas.Doerfler@imd-systems.de>                           |
8|  The license and distribution terms for this file may be        |
9|  found in the file LICENSE in this distribution or at           |
10|  http://www.rtems.com/license/LICENSE.                     |
11|                       all rights reserved                       |
12+-----------------------------------------------------------------+
13| TERMIOS printk support                                          |
14| this module performs low-level printk output using              |
15| a polled termios driver                                         |
16|                                                                 |
17+-----------------------------------------------------------------+
18|   date                      history                        ID   |
19| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
20| 13.05.02  creation                                         doe  |
21|*****************************************************************|
22|* $Id$
23 *
24|*****************************************************************|
25\*===============================================================*/
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#include <rtems.h>
32#include <rtems/libio_.h>
33#include <errno.h>
34#include <stdio.h>
35#include <fcntl.h>
36#include <termios.h>
37
38#include <rtems/termiostypes.h>
39#include <rtems/bspIo.h>
40#include <rtems/termios_printk.h>
41
42/*
43 * internal variables
44 */
45int termios_printk_fd = -1;
46struct rtems_termios_tty *termios_printk_tty;
47
48static void _termios_printk_null_char( char c ) {return;}
49
50BSP_output_char_function_type BSP_output_char = _termios_printk_null_char;
51BSP_polling_getchar_function_type BSP_poll_char;
52
53/*=========================================================================*\
54| Function:                                                                 |
55\*-------------------------------------------------------------------------*/
56void termios_printk_outputchar
57/*-------------------------------------------------------------------------*\
58| Purpose:                                                                  |
59|    send one character to serial port                                      |
60+---------------------------------------------------------------------------+
61| Input Parameters:                                                         |
62\*-------------------------------------------------------------------------*/
63(
64 char c  /* character to print */
65)
66/*-------------------------------------------------------------------------*\
67| Return Value:                                                             |
68|    <none>                                                                 |
69\*=========================================================================*/
70{
71  static const char cr = '\r';
72  /*
73   * check, whether printk serial port is available
74   */
75
76  if ((termios_printk_tty != NULL) &&
77      (termios_printk_tty->device.write != NULL)) {
78    /*
79     * call termios_printk polling callout, if available
80     */
81    if (termios_printk_conf.callout != NULL) {
82      termios_printk_conf.callout();
83    }
84    /*
85     * send character to debug serial port
86     */
87    if (c == '\n') {
88      termios_printk_tty->device.write(termios_printk_tty->minor,&cr,1);
89    }
90    termios_printk_tty->device.write(termios_printk_tty->minor,&c,1);
91  }
92}
93
94/*=========================================================================*\
95| Function:                                                                 |
96\*-------------------------------------------------------------------------*/
97char termios_printk_inputchar
98/*-------------------------------------------------------------------------*\
99| Purpose:                                                                  |
100|    wait for one character from serial port                                |
101+---------------------------------------------------------------------------+
102| Input Parameters:                                                         |
103\*-------------------------------------------------------------------------*/
104(
105 void  /* none */
106)
107/*-------------------------------------------------------------------------*\
108| Return Value:                                                             |
109|    received character                                                     |
110\*=========================================================================*/
111{
112  int c = -1;
113  /*
114   * check, whether debug serial port is available
115   */
116  if ((termios_printk_tty != NULL) &&
117      (termios_printk_tty->device.pollRead != NULL)) {
118    do {
119      /*
120       * call termios_printk polling callout, if available
121       */
122      if (termios_printk_conf.callout != NULL) {
123        termios_printk_conf.callout();
124      }
125      /*
126       * get character from debug serial port
127       */
128      c = termios_printk_tty->device.pollRead(termios_printk_tty->minor);
129    } while (c < 0);
130  }
131  return c;
132}
133
134
135/*=========================================================================*\
136| Function:                                                                 |
137\*-------------------------------------------------------------------------*/
138int termios_printk_open
139
140/*-------------------------------------------------------------------------*\
141| Purpose:                                                                  |
142|    try to open given serial debug port                                    |
143+---------------------------------------------------------------------------+
144| Input Parameters:                                                         |
145\*-------------------------------------------------------------------------*/
146(
147 const char *dev_name, /* name of device to open */
148 uint32_t   baudrate   /* baud rate to use       */
149)
150/*-------------------------------------------------------------------------*\
151| Return Value:                                                             |
152|    0 on success, -1 and errno otherwise                                   |
153\*=========================================================================*/
154{
155  boolean err_occurred = FALSE;
156  rtems_libio_t *iop = NULL;
157  struct termios act_termios;
158  tcflag_t baudcode = B0;
159
160  if (termios_printk_fd >= 0) {
161    /*
162     * already initialized
163     */
164    return 0;
165  }
166  /*
167   * translate baudrate into baud code
168   */
169  switch(baudrate) {
170  case     50: baudcode =     B50; break;
171  case     75: baudcode =     B75; break;
172  case    110: baudcode =    B110; break;
173  case    134: baudcode =    B134; break;
174  case    150: baudcode =    B150; break;
175  case    200: baudcode =    B200; break;
176  case    300: baudcode =    B300; break;
177  case    600: baudcode =    B600; break;
178  case   1200: baudcode =   B1200; break;
179  case   1800: baudcode =   B1800; break;
180  case   2400: baudcode =   B2400; break;
181  case   4800: baudcode =   B4800; break;
182  case   9600: baudcode =   B9600; break;
183  case  19200: baudcode =  B19200; break;
184  case  38400: baudcode =  B38400; break;
185  case  57600: baudcode =  B57600; break;
186  case 115200: baudcode = B115200; break;
187  case 230400: baudcode = B230400; break;
188  case 460800: baudcode = B460800; break;
189  default    :   err_occurred = TRUE; errno = EINVAL; break;
190  }
191 /*
192  * open device for serdbg operation
193  */
194  if (!err_occurred &&
195      (dev_name != NULL) &&
196      (dev_name[0] != '\0')) {
197    termios_printk_fd = open(dev_name,O_RDWR);
198    if (termios_printk_fd < 0) {
199      err_occurred = TRUE;
200    }
201  }
202  /*
203   * capture tty structure
204   */
205  if (!err_occurred) {
206    iop = &rtems_libio_iops[termios_printk_fd];
207    termios_printk_tty = iop->data1;
208  }
209  /*
210   * set device baudrate
211   * (and transp mode, this is not really needed)
212   * ...
213   */
214  /*
215   * ... get fd settings
216   */
217  if (!err_occurred &&
218      (0 != tcgetattr(termios_printk_fd,&act_termios))) {
219      err_occurred = TRUE;
220  }
221  if (!err_occurred) {
222
223    cfsetospeed(&act_termios,baudcode);
224    cfsetispeed(&act_termios,baudcode);
225
226    if (0 != tcsetattr(termios_printk_fd,TCSANOW,&act_termios)) {
227        err_occurred = TRUE;
228    }
229  }
230  if (!err_occurred) {
231    BSP_output_char = termios_printk_outputchar;
232    BSP_poll_char = termios_printk_inputchar;
233  }
234  return (err_occurred
235          ? -1
236          : 0);
237}
Note: See TracBrowser for help on using the repository browser.