source: rtems/cpukit/libmisc/serdbg/termios_printk.c @ 2fc32460

5
Last change on this file since 2fc32460 was 2fc32460, checked in by Sebastian Huber <sebastian.huber@…>, on 09/12/17 at 09:50:10

serdbg: Fix warning

Update #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  /*
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    termios_printk_tty->device.write(termios_printk_tty->minor,&c,1);
88  }
89}
90
91/*=========================================================================*\
92| Function:                                                                 |
93\*-------------------------------------------------------------------------*/
94int termios_printk_inputchar
95/*-------------------------------------------------------------------------*\
96| Purpose:                                                                  |
97|    wait for one character from serial port                                |
98+---------------------------------------------------------------------------+
99| Input Parameters:                                                         |
100\*-------------------------------------------------------------------------*/
101(
102 void  /* none */
103)
104/*-------------------------------------------------------------------------*\
105| Return Value:                                                             |
106|    received character                                                     |
107\*=========================================================================*/
108{
109  int c = -1;
110  /*
111   * check, whether debug serial port is available
112   */
113  if ((termios_printk_tty != NULL) &&
114      (termios_printk_tty->device.pollRead != NULL)) {
115    do {
116      /*
117       * call termios_printk polling callout, if available
118       */
119      if (termios_printk_conf.callout != NULL) {
120        termios_printk_conf.callout();
121      }
122      /*
123       * get character from debug serial port
124       */
125      c = termios_printk_tty->device.pollRead(termios_printk_tty->minor);
126    } while (c < 0);
127  }
128  return c;
129}
130
131
132/*=========================================================================*\
133| Function:                                                                 |
134\*-------------------------------------------------------------------------*/
135int termios_printk_open
136
137/*-------------------------------------------------------------------------*\
138| Purpose:                                                                  |
139|    try to open given serial debug port                                    |
140+---------------------------------------------------------------------------+
141| Input Parameters:                                                         |
142\*-------------------------------------------------------------------------*/
143(
144 const char *dev_name, /* name of device to open */
145 uint32_t   baudrate   /* baud rate to use       */
146)
147/*-------------------------------------------------------------------------*\
148| Return Value:                                                             |
149|    0 on success, -1 and errno otherwise                                   |
150\*=========================================================================*/
151{
152  bool err_occurred = false;
153  rtems_libio_t *iop = NULL;
154  struct termios act_termios;
155  tcflag_t baudcode = B0;
156
157  if (termios_printk_fd >= 0) {
158    /*
159     * already initialized
160     */
161    return 0;
162  }
163  /*
164   * translate baudrate into baud code
165   */
166  switch(baudrate) {
167  case     50: baudcode =     B50; break;
168  case     75: baudcode =     B75; break;
169  case    110: baudcode =    B110; break;
170  case    134: baudcode =    B134; break;
171  case    150: baudcode =    B150; break;
172  case    200: baudcode =    B200; break;
173  case    300: baudcode =    B300; break;
174  case    600: baudcode =    B600; break;
175  case   1200: baudcode =   B1200; break;
176  case   1800: baudcode =   B1800; break;
177  case   2400: baudcode =   B2400; break;
178  case   4800: baudcode =   B4800; break;
179  case   9600: baudcode =   B9600; break;
180  case  19200: baudcode =  B19200; break;
181  case  38400: baudcode =  B38400; break;
182  case  57600: baudcode =  B57600; break;
183  case 115200: baudcode = B115200; break;
184  case 230400: baudcode = B230400; break;
185  case 460800: baudcode = B460800; break;
186  default    :   err_occurred = true; errno = EINVAL; break;
187  }
188 /*
189  * open device for serdbg operation
190  */
191  if (!err_occurred &&
192      (dev_name != NULL) &&
193      (dev_name[0] != '\0')) {
194    termios_printk_fd = open(dev_name,O_RDWR);
195    if (termios_printk_fd < 0) {
196      err_occurred = true;
197    }
198  }
199  /*
200   * capture tty structure
201   */
202  if (!err_occurred) {
203    iop = &rtems_libio_iops[termios_printk_fd];
204    termios_printk_tty = iop->data1;
205  }
206  /*
207   * set device baudrate
208   * (and transp mode, this is not really needed)
209   * ...
210   */
211  /*
212   * ... get fd settings
213   */
214  if (!err_occurred &&
215      (0 != tcgetattr(termios_printk_fd,&act_termios))) {
216      err_occurred = true;
217  }
218  if (!err_occurred) {
219
220    cfsetospeed(&act_termios,baudcode);
221    cfsetispeed(&act_termios,baudcode);
222
223    if (0 != tcsetattr(termios_printk_fd,TCSANOW,&act_termios)) {
224        err_occurred = true;
225    }
226  }
227  if (!err_occurred) {
228    BSP_output_char = termios_printk_outputchar;
229    BSP_poll_char = termios_printk_inputchar;
230  }
231  return (err_occurred
232          ? -1
233          : 0);
234}
Note: See TracBrowser for help on using the repository browser.