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

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

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