source: rtems/cpukit/libmisc/serdbg/serdbgio.c @ 3239698

4.104.114.84.95
Last change on this file since 3239698 was 3239698, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/15/04 at 13:26:21

Remove stray white spaces.

  • Property mode set to 100644
File size: 8.7 KB
Line 
1/*===============================================================*\
2| File: serdbgio.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 serial gdb interface support                            |
14| the functions in this file allow the standard gdb stubs like    |
15| "m68k-stub.c" to access any serial interfaces that work with    |
16| RTEMS termios in polled mode                                    |
17|                                                                 |
18+-----------------------------------------------------------------+
19|   date                      history                        ID   |
20| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
21| 10.05.02  creation                                         doe  |
22|*****************************************************************|
23|* $Id$
24 *
25|*****************************************************************|
26\*===============================================================*/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include <rtems.h>
33#include <rtems/libio_.h>
34#include <errno.h>
35#include <unistd.h> /* close */
36#include <stdio.h>
37#include <fcntl.h>
38#include <termios.h>
39
40#include <rtems/termiostypes.h>
41#include <rtems/serdbg.h>
42
43
44/*
45 * internal variables
46 */
47int serdbg_fd = -1;
48struct rtems_termios_tty *serdbg_tty;
49
50/*=========================================================================*\
51| Function:                                                                 |
52\*-------------------------------------------------------------------------*/
53int serdbg_open
54
55/*-------------------------------------------------------------------------*\
56| Purpose:                                                                  |
57|    try to open given serial debug port                                    |
58+---------------------------------------------------------------------------+
59| Input Parameters:                                                         |
60\*-------------------------------------------------------------------------*/
61(
62 const char *dev_name, /* name of device to open */
63 uint32_t   baudrate   /* baud rate to use       */
64)
65/*-------------------------------------------------------------------------*\
66| Return Value:                                                             |
67|    0 on success, -1 and errno otherwise                                   |
68\*=========================================================================*/
69{
70  boolean err_occurred = FALSE;
71  rtems_libio_t *iop = NULL;
72  struct termios act_termios;
73  tcflag_t baudcode = B0;
74
75#define FD_STORE_CNT 3
76  int fd_store[FD_STORE_CNT];
77  int fd_store_used = 0;
78
79  /*
80   * translate baudrate into baud code
81   */
82  switch(baudrate) {
83  case     50: baudcode =     B50; break;
84  case     75: baudcode =     B75; break;
85  case    110: baudcode =    B110; break;
86  case    134: baudcode =    B134; break;
87  case    150: baudcode =    B150; break;
88  case    200: baudcode =    B200; break;
89  case    300: baudcode =    B300; break;
90  case    600: baudcode =    B600; break;
91  case   1200: baudcode =   B1200; break;
92  case   1800: baudcode =   B1800; break;
93  case   2400: baudcode =   B2400; break;
94  case   4800: baudcode =   B4800; break;
95  case   9600: baudcode =   B9600; break;
96  case  19200: baudcode =  B19200; break;
97  case  38400: baudcode =  B38400; break;
98  case  57600: baudcode =  B57600; break;
99  case 115200: baudcode = B115200; break;
100  case 230400: baudcode = B230400; break;
101  case 460800: baudcode = B460800; break;
102  default    :   err_occurred = TRUE; errno = EINVAL; break;
103  }
104
105 /*
106  * open device for serdbg operation
107  * skip any fds that are between 0..2, because they are
108  * reserved for stdin/out/err
109  */
110  if (!err_occurred &&
111      (dev_name != NULL) &&
112      (dev_name[0] != '\0')) {
113    do {
114      serdbg_fd = open(dev_name,O_RDWR);
115      if (serdbg_fd < 0) {
116        err_occurred = TRUE;
117      }
118      else {
119        if (serdbg_fd < 3) {
120          if (fd_store_used >= FD_STORE_CNT) {
121            err_occurred = TRUE;
122          }
123          else {
124            fd_store[fd_store_used++] = serdbg_fd;
125          }
126        }
127      }
128    } while (!err_occurred &&
129             (serdbg_fd < 3));
130  }
131  /*
132   * close any fds, that have been placed in fd_store
133   * so fd 0..2 are reusable again
134   */
135  while (--fd_store_used >= 0) {
136    close(fd_store[fd_store_used]);
137  }
138
139  /*
140   * capture tty structure
141   */
142  if (!err_occurred) {
143    iop = &rtems_libio_iops[serdbg_fd];
144    serdbg_tty = iop->data1;
145  }
146  /*
147   * set device baudrate
148   * (and transp mode, this is not really needed)
149   * ...
150   */
151  /*
152   * ... get fd settings
153   */
154  if (!err_occurred &&
155      (0 != tcgetattr(serdbg_fd,&act_termios))) {
156      err_occurred = TRUE;
157  }
158  if (!err_occurred) {
159    act_termios.c_iflag
160      &=  ~(IGNBRK|BRKINT|PARMRK|ISTRIP
161            |INLCR|IGNCR|ICRNL|IXON);
162    act_termios.c_oflag
163      &= ~OPOST;
164   
165    act_termios.c_lflag
166      &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
167
168    cfsetospeed(&act_termios,baudcode);
169    cfsetispeed(&act_termios,baudcode);
170
171    if (0 != tcsetattr(serdbg_fd,TCSANOW,&act_termios)) {
172        err_occurred = TRUE;
173    }
174  }
175  return (err_occurred
176          ? -1
177          : 0);
178}
179
180void putDebugChar(char c) __attribute__ ((__weak__));
181/*=========================================================================*\
182| Function:                                                                 |
183\*-------------------------------------------------------------------------*/
184void putDebugChar
185/*-------------------------------------------------------------------------*\
186| Purpose:                                                                  |
187|    send one character to serial port                                      |
188+---------------------------------------------------------------------------+
189| Input Parameters:                                                         |
190\*-------------------------------------------------------------------------*/
191(
192 char c  /* character to print */
193)
194/*-------------------------------------------------------------------------*\
195| Return Value:                                                             |
196|    <none>                                                                 |
197\*=========================================================================*/
198{
199  /*
200   * call serdbg polling callout, if available
201   */
202  if (serdbg_conf.callout != NULL) {
203    serdbg_conf.callout();
204  }
205  /*
206   * check, whether debug serial port is available
207   */
208  if ((serdbg_tty != NULL) &&
209      (serdbg_tty->device.write != NULL)) {
210    /*
211     * send character to debug serial port
212     */
213    serdbg_tty->device.write(serdbg_tty->minor,&c,1);
214  }
215}
216
217int getDebugChar(void) __attribute__ ((__weak__));
218/*=========================================================================*\
219| Function:                                                                 |
220\*-------------------------------------------------------------------------*/
221int getDebugChar
222/*-------------------------------------------------------------------------*\
223| Purpose:                                                                  |
224|    wait for one character from serial port                                |
225+---------------------------------------------------------------------------+
226| Input Parameters:                                                         |
227\*-------------------------------------------------------------------------*/
228(
229 void  /* none */
230)
231/*-------------------------------------------------------------------------*\
232| Return Value:                                                             |
233|    received character                                                     |
234\*=========================================================================*/
235{
236  int c = -1;
237  /*
238   * check, whether debug serial port is available
239   */
240  if ((serdbg_tty != NULL) &&
241      (serdbg_tty->device.pollRead != NULL)) {
242    do {
243      /*
244       * call serdbg polling callout, if available
245       */
246      if (serdbg_conf.callout != NULL) {
247        serdbg_conf.callout();
248      }
249      /*
250       * get character from debug serial port
251       */
252      c = serdbg_tty->device.pollRead(serdbg_tty->minor);
253    } while (c < 0);
254  }
255  return c;
256}
Note: See TracBrowser for help on using the repository browser.