source: rtems/cpukit/libmisc/serdbg/termios_printk.c @ 1bc0ad2

5
Last change on this file since 1bc0ad2 was 1bc0ad2, checked in by Sebastian Huber <sebastian.huber@…>, on 09/08/17 at 08:38:46

Simplify and unify BSP_output_char

The BSP_output_char should output a char and not mingle with high level
processing, e.g. '\n' to '\r\n' translation. Move this translation to
rtems_putc(). Remove it from all the BSP_output_char implementations.

Close #3122.

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