File: sercom.cc

package info (click to toggle)
estic 1.61-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,968 kB
  • ctags: 6,407
  • sloc: cpp: 41,916; asm: 1,620; makefile: 436; ansic: 402; sh: 40
file content (649 lines) | stat: -rw-r--r-- 17,126 bytes parent folder | download | duplicates (6)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*****************************************************************************/
/*                                                                           */
/*                                 SERCOM.CC                                 */
/*                                                                           */
/* (C) 1995-96  Ullrich von Bassewitz                                        */
/*              Zwehrenbuehlstrasse 33                                       */
/*              D-72070 Tuebingen                                            */
/* EMail:       uz@ibb.schwaben.com                                          */
/*                                                                           */
/*****************************************************************************/



// $Id$
//
// $Log$
//
//



// System independent serial communication package for the SPUNK library,
// Linux version.



#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <linux/serial.h>

#include "../check.h"
#include "../sercom.h"
#include "../filepath.h"



/*****************************************************************************/
/*                            Types and constants                            */
/*****************************************************************************/



// internal used data
struct _ComData {

    int                 Handle;         // Port handle

    unsigned            RXCount;        // characters in receive queue
    unsigned            TXCount;        // characters in transmit queue

    termios             StartupSettings;// Device settings on startup
    termios             CurrentSettings;// Current device settings

    ComErrorCounter     ErrorCounter;   // Error counters
};



/******************************************************************************/
/*                       Utility and support functions                        */
/******************************************************************************/



static void SetReadTimeout (_ComData* ComData, int Min, int Time)
{
    if (ComData->CurrentSettings.c_cc [VMIN] != Min ||
        ComData->CurrentSettings.c_cc [VTIME] != Time) {
        // Need to set the new values
        ComData->CurrentSettings.c_cc [VMIN]  = Min;
        ComData->CurrentSettings.c_cc [VTIME] = Time;
        ZCHECK (tcsetattr (ComData->Handle, TCSANOW, &ComData->CurrentSettings));
    }
}



/******************************************************************************/
/*                                   Code                                     */
/******************************************************************************/



ComPort::ComPort (const String& aPortName,
                  u32  aBaudrate,
                  char aDatabits,
                  char aParity,
                  char aStopbits,
                  char aConnection,
                  char aXonXoff,
                  unsigned /*UARTBase*/,
                  unsigned /*IntNum*/):
    PortName (aPortName),
    Baudrate (aBaudrate),
    Databits (aDatabits),
    Parity (aParity),
    Stopbits (aStopbits),
    Connection (aConnection),
    XonXoff (aXonXoff),
    RXBufSize (4096),               // Just some value...
    TXBufSize (4096),
    RXTimeout (5.0),
    TXTimeout (5.0)
// Create a ComPort object, use defaults for timeouts and buffer sizes
{
    Init ();
}



ComPort::~ComPort ()
{
    if (IsOpen()) {
        Close();
    }
    delete ComData;
}



void ComPort::Init (unsigned /*UARTBase*/, unsigned /*IntNum*/)
// Initialization procedure, called from the constructors
{
    // Create an internally used data structure
    ComData = new _ComData;

    // Reset the error counters
    memset (ComData->ErrorCounter, 0, sizeof (ComData->ErrorCounter));

    // setting up ComData
    ComData->Handle    = -1;                // Port not open
    ComData->RXCount   = 0;
    ComData->TXCount   = 0;
}



void ComPort::SetBufferSize (u16 /*aRXBufSize*/, u16 /*aTXBufSize*/)
// Set the sizes for receive and transmit buffer. This function cannot
// be called if the port is already open, you have to call it after
// constructing the object or after a close. The function may be ignored
// if it is not possible to change buffer sizes.
{
    // This function cannot be called if the port is already open
    PRECONDITION (!IsOpen ());

    // Otherwise this function is ignored under Linux as we could not set any
    // buffer sizes (no harm done anyway)
}



unsigned ComPort::Open ()
// Open the port, return an error code or 0 on success
{
    // First, check if the port contains a directory. If not, add /dev
    String Dir, Name;
    FSplit (PortName, Dir, Name);
    if (Dir.IsEmpty ()) {
        PortName.Ins (0, "/dev/");
    }

    // Open the port
    ComData->Handle = open (PortName.GetStr (), O_RDWR | O_NONBLOCK);
    if (ComData->Handle == -1) {
        // Got an error, return the error code
        return errno;
    }

    // Device must be a tty
    if (!isatty (ComData->Handle)) {
        // Error, close and exit
        Close ();
        return ENOTTY;
    }

    // Remember the current settings
    tcgetattr (ComData->Handle, &ComData->StartupSettings);

    // Set up the new settings
    ComData->CurrentSettings = ComData->StartupSettings;

    // Set up input flags
    tcflag_t iflag = IGNBRK | IGNPAR;
    if (Databits == 7) {
        iflag |= ISTRIP;                // Strip bit 7
    }
    if (XonXoff == 'E') {
        iflag |= IXON | IXOFF;          // Enable XON/XOFF protocol
    }
    ComData->CurrentSettings.c_iflag = iflag;

    // Set up output flags
    tcflag_t oflag = 0;
    ComData->CurrentSettings.c_oflag = oflag;

    // Set up control flags
    tcflag_t cflag = CREAD | HUPCL;
    switch (Databits) {
        case 5: cflag |= CS5;   break;
        case 6: cflag |= CS6;   break;
        case 7: cflag |= CS7;   break;
        case 8: cflag |= CS8;   break;
        default: FAIL ("ComPort::Init: Unsupported databits value");
    }
    switch (Parity) {
        case 'N': break;
        case 'E': cflag |= PARENB;              break;
        case 'O': cflag |= PARENB | PARODD;     break;
        default:  FAIL ("ComPort::Init: Unsupported parity setting");
    }
    switch (Stopbits) {
        case 1: break;
        case 2: cflag |= CSTOPB; break;
        default: FAIL ("ComPort::Init: Unsupported stopbits value");
    }
    switch (Connection) {
        case 'M': cflag |= CRTSCTS; break;      // not POSIX!
        case 'D': cflag |= CLOCAL;  break;
        default: FAIL ("ComPort::Init: Unsupported connection setting");
    }
    ComData->CurrentSettings.c_cflag = cflag;

    // Set up local flags
    tcflag_t lflag = 0;
    ComData->CurrentSettings.c_lflag = lflag;

    // Set XON/XOFF to Ctrl-S/Ctrl-Q
    ComData->CurrentSettings.c_cc [VSTART] = 0x11;
    ComData->CurrentSettings.c_cc [VSTOP]  = 0x13;

    // Set the baudrate
    speed_t Baud = 0;
    switch (Baudrate) {
        case     50: Baud = B50;        break;
        case     75: Baud = B75;        break;
        case    110: Baud = B110;       break;
        case    134: Baud = B134;       break;
        case    150: Baud = B150;       break;
        case    200: Baud = B200;       break;
        case    300: Baud = B300;       break;
        case    600: Baud = B600;       break;
        case   1200: Baud = B1200;      break;
        case   2400: Baud = B2400;      break;
        case   4800: Baud = B4800;      break;
        case   9600: Baud = B9600;      break;
        case  19200: Baud = B19200;     break;
        case  38400: Baud = B38400;     break;
        case  57600: Baud = B57600;     break;
        case 115200: Baud = B115200;    break;
        case 230400: Baud = B230400;    break;
        default: FAIL ("ComPort::Init: Unsupported baudrate value");
    }
    ComData->CurrentSettings.c_cflag |= Baud;
    cfsetispeed (&ComData->CurrentSettings, Baud);
    cfsetospeed (&ComData->CurrentSettings, Baud);

    // Set timeouts
    ComData->CurrentSettings.c_cc [VMIN] = 1;
    ComData->CurrentSettings.c_cc [VTIME] = int (RXTimeout / 0.1);

    // Actually set up the device
    if (tcsetattr (ComData->Handle, TCSANOW, &ComData->CurrentSettings) < 0) {
        // Could not set
        int Result = errno;
        Close ();
        return Result;
    }

    // When we set cflags == CLOCAL on a direct connection, the RTS and DTR
    // lines will become active. This is not compatible to the other sercom
    // modules, so change it. Also make DTR low if hardware handshake is
    // enabled.
    int HandshakeLines = TIOCM_DTR;
    if (Connection == 'D') {
        HandshakeLines |= TIOCM_RTS;
    }
    ZCHECK (ioctl (ComData->Handle, TIOCMBIC, &HandshakeLines));

    // Reset nonblocking mode
    int Flags = fcntl (ComData->Handle, F_GETFL, 0);
    if (Flags < 0) {
        // Error
        int Result = errno;
        Close ();
        return Result;
    }
    Flags &= ~O_NONBLOCK;
    if (fcntl (ComData->Handle, F_SETFL, Flags) < 0) {
        // Error
        int Result = errno;
        Close ();
        return Result;
    }

    // Success
    return 0;
}



void ComPort::Close ()
// Close the port
{
    // Cannot close a port that is not open
    PRECONDITION (IsOpen ());

    // Close the device
    close (ComData->Handle);

    // reset handle
    ComData->Handle = -1;
}



int ComPort::IsOpen () const
// Return a value != zero if the port is opened, return 0 otherwise
{
    return ComData->Handle != -1;
}



void ComPort::SetRXTimeout (double aRXTimeout)
// Set the timeout value
{
    // Check the parameter
    PRECONDITION (aRXTimeout >= 0.0);

    // Make shure the timeout is not too small
    if (aRXTimeout > 0 && aRXTimeout < 0.1) {
        aRXTimeout = 0.1;
    }

    // Remember the timeout
    RXTimeout = aRXTimeout;

    // Set the timeout
    SetReadTimeout (ComData, 0, int (RXTimeout / 0.1));
}



void ComPort::SetTXTimeout (double aTXTimeout)
// Set the timeout value
{
    // Check the parameter
    PRECONDITION (aTXTimeout >= 0.0);

    // Make shure the timeout is not too small
    if (aTXTimeout < 0.01) {
        aTXTimeout = 0.01;
    }

    // Remember the timeout
    TXTimeout = aTXTimeout;
}



void ComPort::DTROn ()
// Make the DTR line active
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Set DTR
    int Status = TIOCM_DTR;
    ZCHECK (ioctl (ComData->Handle, TIOCMBIS, &Status));
}



void ComPort::DTROff ()
// Make the DTR line inactive
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Clear DTR
    int Status = TIOCM_DTR;
    ZCHECK (ioctl (ComData->Handle, TIOCMBIC, &Status));
}



void ComPort::RTSOn ()
// Make the RTS line active. A call to this function is not allowed if the
// connection type is 'M'odem
{
    // Port must be open and no hardware handshaking enabled
    PRECONDITION (IsOpen () && Connection != 'M');

    // Set RTS
    int Status = TIOCM_RTS;
    ZCHECK (ioctl (ComData->Handle, TIOCMBIS, &Status));
}



void ComPort::RTSOff ()
// Make the RTS line inactive. A call to this function is not allowed if the
// connection type is 'M'odem
{
    // Port must be open and no hardware handshaking enabled
    PRECONDITION (IsOpen () && Connection != 'M');

    // Clear RTS
    int Status = TIOCM_RTS;
    ZCHECK (ioctl (ComData->Handle, TIOCMBIC, &Status));
}



unsigned ComPort::RXCount () const
// Return the count of chars in the receive buffer
{
    // Port must be open
    PRECONDITION (IsOpen ());

    int Count;
    ZCHECK (ioctl (ComData->Handle, TIOCINQ, &Count));
    return Count;
}



unsigned ComPort::TXCount () const
// Return the count of chars in the transmit buffer
{
    // Port must be open
    PRECONDITION (IsOpen ());

    int Count;
    ZCHECK (ioctl (ComData->Handle, TIOCOUTQ, &Count));
    return Count;
}



void ComPort::RXClear ()
// Clear the receive buffer
{
    // Port must be open
    PRECONDITION (IsOpen ());

    ZCHECK (tcflush (ComData->Handle, TCIFLUSH));
}



void ComPort::TXClear ()
// Clear the transmit buffer
{
    // Port must be open
    PRECONDITION (IsOpen ());

    ZCHECK (tcflush (ComData->Handle, TCOFLUSH));
}



unsigned ComPort::TXFree () const
// Return the amount of free space in the transmit buffer. The function
// may return the exact free space or just 1, if at least one character
// can be placed into the send buffer (meaning, the Send function will
// not block).
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Timeout is zero
    timeval Timeout;
    Timeout.tv_usec = 0;
    Timeout.tv_sec  = 0;

    // Set the file descriptor to ComData->Handle
    fd_set Desc;
    FD_ZERO (&Desc);
    FD_SET (ComData->Handle, &Desc);

    // Check output status
    if (select (ComData->Handle+1, NULL, &Desc, NULL, &Timeout) > 0) {
        return 1;
    } else {
        return 0;
    }
}



int ComPort::Receive ()
// Return a character from the receive buffer. If the buffer is empty,
// the function waits until a character is available.
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Set no timeout mode
    SetReadTimeout (ComData, 0, 0);

    // read one character
    unsigned char B;
    int ReadCount;
    do {
        ReadCount = read (ComData->Handle, &B, 1);
    } while (ReadCount != 1);

    // return character
    return B;
}



int ComPort::Send (unsigned char B)
// Send the character (put it into the transmit buffer). If there is no
// room in the transmit buffer, the error counter is incremented, the
// character is discarded and the function returns -1. To avoid this,
// check TXFree before calling this function. If the character could be
// placed into the transmit buffer, B is returned.
{
    // Port must be open
    PRECONDITION (IsOpen ());

    if (TXFree () == 0) {
        ComData->ErrorCounter [ceTXOverflow]++;
        return -1;
    } else {
        int WriteCount;
        do {
            WriteCount = write (ComData->Handle, &B, 1);
        } while (WriteCount != 1);
        return B;
    }
}



int ComPort::TimedReceive ()
// Return a character from the receive buffer. If the buffer is empty,
// the function waits until a character is available or the time given
// with SetReceiveTimeout is over. If a timeout condition occurred, the
// function returns -1, otherwise the character received.
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Set up the device for timeout read
    SetReadTimeout (ComData, 0, int (RXTimeout / 0.1));

    unsigned char B;
    int ReadCount = read (ComData->Handle, &B, 1);

    // Return the character read or -1 on timeout
    return ReadCount == 1 ? B : -1;
}



int ComPort::TimedSend (unsigned char B)
// Send the character (put it into the transmit buffer). If there is no
// room in the transmit buffer, the function waits until there is free
// room in the transmit buffer or the time given with SetSendTimeout is
// over. If a timeout condition occurred, the error counter is incremented,
// the character is discarded and the function returns -1. To avoid this,
// check TXFree before calling this function. If the character could be
// placed into the transmit buffer, B is returned.
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Use select to check if write is possible
    timeval Timeout;
    Timeout.tv_usec = u32 (TXTimeout * 1000000) % 1000000;
    Timeout.tv_sec  = long (TXTimeout);

    fd_set Desc;
    FD_ZERO (&Desc);
    FD_SET (ComData->Handle, &Desc);

    if (select (ComData->Handle + 1, NULL, &Desc, NULL, &Timeout)) {
        // Descriptor is ready for writing
        write (ComData->Handle, &B, 1);
        return B;
    } else {
        // Timeout or signal. Return an timeout error code when a signal
        // occurs
        ComData->ErrorCounter [ceTXOverflow]++;
        return -1;
    }
}



void ComPort::Break (double /*Duration*/)
// Send a break with the given time in seconds
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Ignore Duration as values != zero have undefined behavior
    ZCHECK (tcsendbreak (ComData->Handle, 0));
}



#if 0
// gcc 2.5.8 is not able to compile this (bug using the typedef)
// Leave it out and hope no one will notice...
ComErrorCounter& ComPort::GetErrors ()
// Get a reference to the array of error counters. These counters are
// incremented but never decremented or zeroed by the object.
{
    return ComData->ErrorCounter;
}
#endif



unsigned ComPort::ModemStatus () const
// Return the state of the modem status lines
{
    // Port must be open
    PRECONDITION (IsOpen ());

    // Get the modem lines
    int Lines;
    ZCHECK (ioctl (ComData->Handle, TIOCMGET, &Lines));

    // Convert to sercom format, ignore the "delta" lines
    unsigned Status = 0;
    if (Lines & TIOCM_CTS)      Status |= csCTS;
    if (Lines & TIOCM_DSR)      Status |= csDSR;
    if (Lines & TIOCM_RNG)      Status |= csRI;
    if (Lines & TIOCM_CAR)      Status |= csCD;

    // Return the result
    return Status;
}