YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
uart_BBB.cpp
1#include <unistd.h> // sleep, close
2#include <fcntl.h> // O_RDWR
3#include <string.h> // memset
4
5#include "uart_BBB.h"
6#include "yahal_assert.h"
7#include "stdio.h"
8
9#define PARITYON 0
10#define PARITY 0
11#define STOP 0
12#define HWFLOW 0
13
14#define BAUDRATE B9600
15
16uart_BBB::uart_BBB(const char *dev){
17 //memset(&config, 0, sizeof(config));
18
19 _fd=open(dev, O_RDWR | O_NOCTTY);
20 yahal_assert(_fd > 0);
21
22 int ret=isatty(_fd);
23 yahal_assert(ret > 0);
24
25 ret=tcgetattr(_fd, &config);
26 yahal_assert(ret == 0);
27
28 config.c_cflag = B9600 | HWFLOW | CS8 | STOP | PARITYON | PARITY | CLOCAL | CREAD;
29
30 config.c_iflag = IGNBRK | IGNCR;
31 config.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off SW-FlowControl
32
33 config.c_oflag = 0;
34 config.c_lflag = 0; //ICANON;
35
36 //Anzahl zu lesender Bytes
37 config.c_cc[VMIN]=4;
38
39 //Timeout zw 2 Bytes in Zehntel-Sekunden
40 config.c_cc[VTIME]=0;
41
42 cfsetospeed (&config, BAUDRATE);
43 cfsetispeed (&config, BAUDRATE);
44
45 tcflush(_fd, TCIFLUSH);
46
47 tcsetattr(_fd,TCSANOW, &config);
48
49}
50
51uart_BBB::~uart_BBB(){
52 int ret = close( _fd );
53 yahal_assert(ret != -1);
54}
55
56int16_t uart_BBB::write(uint8_t *txbuf, uint8_t len){
57 return ::write(_fd, txbuf, len);
58}
59
60int16_t uart_BBB::read (uint8_t *rxbuf, uint8_t len){
61 return ::read(_fd, rxbuf, len);
62}
63
64unsigned uart_BBB::switch_baud(unsigned baud){
65 cfsetospeed (&config, baud);
66 cfsetispeed (&config, baud);
67
68 return baud;
69}
70
71void uart_BBB::flush(){
72 tcflush(_fd, TCIOFLUSH);
73}
74
75void uart_BBB::setattributes(){
76 tcsetattr(_fd,TCSANOW, &config);
77}
78
79void uart_BBB::setBytesToRead(unsigned bytes){
80 config.c_cc[VMIN]=bytes;
81 setattributes();
82}
83
84void uart_BBB::setReadTimeout(unsigned timeout){
85 config.c_cc[VTIME]=timeout;
86 setattributes();
87}