source: rtems/cpukit/libmisc/serdbg/serdbgio.c @ 52058a6b

4.104.114.84.95
Last change on this file since 52058a6b was 22b689b, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/14/02 at 12:13:26

2002-11-14 Ralf Corsepius <corsepiu@…>

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