-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart.cpp
More file actions
124 lines (98 loc) · 2.44 KB
/
uart.cpp
File metadata and controls
124 lines (98 loc) · 2.44 KB
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
#include <pi/uart.hpp>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstdarg>
#include <cstring>
#include <pi/utils.hpp>
pi::baudMap baudInit()
{
pi::baudMap m;
m[50] = B50;
m[75] = B75;
m[110] = B110;
m[134] = B134;
m[150] = B150;
m[200] = B200;
m[300] = B300;
m[600] = B600;
m[1200] = B1200;
m[1800] = B1800;
m[2400] = B2400;
m[4800] = B4800;
m[9600] = B9600;
m[19200] = B19200;
m[38400] = B38400;
m[57600] = B57600;
m[115200] = B115200;
m[230400] = B230400;
return m;
}
const pi::baudMap pi::Uart::_supportedBaud = baudInit();
pi::Uart::Uart()
{
}
pi::Uart::Uart(unsigned int baud, std::string device /*= "/dev/ttyAMA0"*/) : _isOpen(false)
{
this->open(baud, device);
}
void pi::Uart::open(unsigned int baud, std::string device /*= "/dev/ttyAMA0"*/)
{
termios options ;
speed_t termBaud ;
int status;
DEBUG("BAUD: " << baud << " termBaud: " << termBaud << " device: " << device);
baudMap::const_iterator it = _supportedBaud.find(baud);
if(it == _supportedBaud.end())
return;
termBaud = it->second;
if ((_fd = ::open (device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return;
fcntl (_fd, F_SETFL, O_RDWR) ;
tcgetattr (_fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, termBaud) ;
cfsetospeed (&options, termBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ;
tcsetattr (_fd, TCSANOW | TCSAFLUSH, &options) ;
ioctl (_fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (_fd, TIOCMSET, &status);
usleep (10000) ;
DEBUG("FD : " << _fd);
_isOpen=true;
}
pi::Uart::~Uart()
{
::close (_fd) ;
}
bool pi::Uart::isOpen()
{
return _isOpen;
}
void pi::Uart::read(std::string& )
{
}
char pi::Uart::read()
{
uint8_t x ;
if (::read (_fd, &x, 1) != 1)
return -1 ;
DEBUG("READ: " << x);
return ((int)x) & 0xFF ;
}