summaryrefslogtreecommitdiffstats
path: root/c/src/libmisc/serdbg/serdbgio.c
blob: 57a10d8e2028b16763d8afda086e0588eed37807 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*===============================================================*\
| File: serdbgio.c                                                |
+-----------------------------------------------------------------+
|                    Copyright (c) 2002 IMD                       |
|      Ingenieurbuero fuer Microcomputertechnik Th. Doerfler      |
|      Hebststr. 8, 82178 Puchheim, Germany                       |
|      <Thomas.Doerfler@imd-systems.de>                           |
|  The license and distribution terms for this file may be        |
|  found in the file LICENSE in this distribution or at           |
|  http://www.OARcorp.com/rtems/license.html.                     |
|                       all rights reserved                       |
+-----------------------------------------------------------------+
| TERMIOS serial gdb interface support                            |
| the functions in this file allow the standard gdb stubs like    |
| "m68k-stub.c" to access any serial interfaces that work with    |
| RTEMS termios in polled mode                                    |
|                                                                 |
+-----------------------------------------------------------------+
|   date                      history                        ID   |
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10.05.02  creation                                         doe  |
|*****************************************************************|
|* $Id$
 *
|*****************************************************************|
\*===============================================================*/

#include <rtems.h>
#include <rtems/libio_.h>
#include <errno.h>
#include <unistd.h> /* close */
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>

#include <rtems/termiostypes.h>
#include <serdbg.h>


/*
 * internal variables
 */
int serdbg_fd = -1;
struct rtems_termios_tty *serdbg_tty;

/*=========================================================================*\
| Function:                                                                 |
\*-------------------------------------------------------------------------*/
int serdbg_open

/*-------------------------------------------------------------------------*\
| Purpose:                                                                  |
|    try to open given serial debug port                                    |
+---------------------------------------------------------------------------+
| Input Parameters:                                                         |
\*-------------------------------------------------------------------------*/
(
 const char *dev_name, /* name of device to open */
 unsigned32 baudrate   /* baud rate to use       */
)
/*-------------------------------------------------------------------------*\
| Return Value:                                                             |
|    0 on success, -1 and errno otherwise                                   |
\*=========================================================================*/
{
  boolean err_occurred = FALSE;
  rtems_libio_t *iop = NULL;
  struct termios act_termios;
  tcflag_t baudcode = B0;

#define FD_STORE_CNT 3
  int fd_store[FD_STORE_CNT];
  int fd_store_used = 0;

  /*
   * translate baudrate into baud code
   */
  switch(baudrate) {
  case     50: baudcode =     B50; break;
  case     75: baudcode =     B75; break;
  case    110: baudcode =    B110; break;
  case    134: baudcode =    B134; break;
  case    150: baudcode =    B150; break;
  case    200: baudcode =    B200; break;
  case    300: baudcode =    B300; break;
  case    600: baudcode =    B600; break;
  case   1200: baudcode =   B1200; break;
  case   1800: baudcode =   B1800; break;
  case   2400: baudcode =   B2400; break;
  case   4800: baudcode =   B4800; break;
  case   9600: baudcode =   B9600; break;
  case  19200: baudcode =  B19200; break;
  case  38400: baudcode =  B38400; break;
  case  57600: baudcode =  B57600; break;
  case 115200: baudcode = B115200; break;
  case 230400: baudcode = B230400; break;
  case 460800: baudcode = B460800; break;
  default    :   err_occurred = TRUE; errno = EINVAL; break;
  }

 /*
  * open device for serdbg operation
  * skip any fds that are between 0..2, because they are
  * reserved for stdin/out/err
  */ 
  if (!err_occurred &&
      (dev_name != NULL) &&
      (dev_name[0] != '\0')) {
    do {
      serdbg_fd = open(dev_name,O_RDWR);
      if (serdbg_fd < 0) {
	err_occurred = TRUE;
      }
      else {
	if (serdbg_fd < 3) {
	  if (fd_store_used >= FD_STORE_CNT) {
	    err_occurred = TRUE;
	  }
	  else {
	    fd_store[fd_store_used++] = serdbg_fd;
	  }
	}
      }
    } while (!err_occurred && 
	     (serdbg_fd < 3));
  }
  /*
   * close any fds, that have been placed in fd_store
   * so fd 0..2 are reusable again
   */
  while (--fd_store_used >= 0) {
    close(fd_store[fd_store_used]);
  }

  /*
   * capture tty structure 
   */
  if (!err_occurred) {
    iop = &rtems_libio_iops[serdbg_fd];
    serdbg_tty = iop->data1;
  }
  /*
   * set device baudrate 
   * (and transp mode, this is not really needed)
   * ...
   */
  /*
   * ... get fd settings
   */
  if (!err_occurred &&
      (0 != tcgetattr(serdbg_fd,&act_termios))) {
      err_occurred = TRUE;
  }
  if (!err_occurred) {
    act_termios.c_iflag 
      &=  ~(IGNBRK|BRKINT|PARMRK|ISTRIP
	    |INLCR|IGNCR|ICRNL|IXON);
    act_termios.c_oflag 
      &= ~OPOST;
    
    act_termios.c_lflag
      &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);

    cfsetospeed(&act_termios,baudcode);
    cfsetispeed(&act_termios,baudcode);

    if (0 != tcsetattr(serdbg_fd,TCSANOW,&act_termios)) {
	err_occurred = TRUE;
    }
  }
  return (err_occurred 
	  ? -1 
	  : 0);
}

void putDebugChar(char c) __attribute__ ((__weak__));
/*=========================================================================*\
| Function:                                                                 |
\*-------------------------------------------------------------------------*/
void putDebugChar
/*-------------------------------------------------------------------------*\
| Purpose:                                                                  |
|    send one character to serial port                                      |
+---------------------------------------------------------------------------+
| Input Parameters:                                                         |
\*-------------------------------------------------------------------------*/
(
 char c  /* character to print */
)
/*-------------------------------------------------------------------------*\
| Return Value:                                                             |
|    <none>                                                                 |
\*=========================================================================*/
{
  /*
   * call serdbg polling callout, if available 
   */
  if (serdbg_conf.callout != NULL) {
    serdbg_conf.callout();
  }
  /*
   * check, whether debug serial port is available
   */
  if ((serdbg_tty != NULL) && 
      (serdbg_tty->device.write != NULL)) {
    /*
     * send character to debug serial port
     */
    serdbg_tty->device.write(serdbg_tty->minor,&c,1);
  }
}

int getDebugChar(void) __attribute__ ((__weak__));
/*=========================================================================*\
| Function:                                                                 |
\*-------------------------------------------------------------------------*/
int getDebugChar
/*-------------------------------------------------------------------------*\
| Purpose:                                                                  |
|    wait for one character from serial port                                |
+---------------------------------------------------------------------------+
| Input Parameters:                                                         |
\*-------------------------------------------------------------------------*/
(
 void  /* none */
)
/*-------------------------------------------------------------------------*\
| Return Value:                                                             |
|    received character                                                     |
\*=========================================================================*/
{
  int c = -1;
  /*
   * check, whether debug serial port is available
   */
  if ((serdbg_tty != NULL) && 
      (serdbg_tty->device.pollRead != NULL)) {
    do {
      /*
       * call serdbg polling callout, if available 
       */
      if (serdbg_conf.callout != NULL) {
	serdbg_conf.callout();
      }
      /*
       * get character from debug serial port
       */
      c = serdbg_tty->device.pollRead(serdbg_tty->minor);
    } while (c < 0);
  }
  return c;
}