Add inital serial port support for Linux platform

This commit is contained in:
Nanosonde 2020-09-07 22:37:48 +02:00
parent 77a796a39c
commit 4623385b00
2 changed files with 115 additions and 3 deletions

View File

@ -21,6 +21,7 @@
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h> // Needed for SPI port
#include <linux/spi/spidev.h> // Needed for SPI port
@ -400,6 +401,107 @@ std::string LinuxPlatform::flashFilePath()
return _flashFilePath;
}
size_t LinuxPlatform::readBytesUart(uint8_t *buffer, size_t length)
{
return read(_uartFd, buffer, length);
}
int LinuxPlatform::readUart()
{
uint8_t x ;
if (read(_uartFd, &x, 1) != 1)
{
return -1;
}
return ((int)x) & 0xFF ;
}
size_t LinuxPlatform::writeUart(const uint8_t *buffer, size_t size)
{
return write(_uartFd, buffer, size) ;
}
size_t LinuxPlatform::writeUart(const uint8_t data)
{
return write(_uartFd, &data, 1) ;
}
int LinuxPlatform::uartAvailable()
{
int result ;
if (ioctl(_uartFd, FIONREAD, &result) == -1)
{
return -1;
}
return result ;
}
void LinuxPlatform::closeUart()
{
if (_uartFd >= 0)
{
close(_uartFd);
}
}
void LinuxPlatform::setupUart()
{
/*
* 19200,8E1, no handshake
*/
struct termios options; /* Schnittstellenoptionen */
/* Port oeffnen - read/write, kein "controlling tty", Status von DCD ignorieren */
_uartFd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if (_uartFd >= 0)
{
/* get the current options */
fcntl(_uartFd, F_SETFL, 0);
if (tcgetattr(_uartFd, &options) != 0)
{
close(_uartFd);
_uartFd = -1;
return;
}
memset(&options, 0, sizeof(options)); /* Structur loeschen, ggf. vorher sichern
und bei Programmende wieder restaurieren */
/* Baudrate setzen */
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
/* setze Optionen */
options.c_cflag |= PARENB; /* Enable Paritybit */
options.c_cflag &= ~PARODD; /* Even parity */
options.c_cflag &= ~CSTOPB; /* 1 Stoppbit */
options.c_cflag &= ~CSIZE; /* 8 Datenbits */
options.c_cflag |= CS8;
/* 19200 bps, 8 Datenbits, CD-Signal ignorieren, Lesen erlauben */
options.c_cflag |= (CLOCAL | CREAD);
/* Kein Echo, keine Steuerzeichen, keine Interrupts */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag = IGNPAR; /* Parity-Fehler ignorieren */
options.c_oflag &= ~OPOST; /* setze "raw" Input */
options.c_cc[VMIN] = 0; /* warten auf min. 0 Zeichen */
options.c_cc[VTIME] = 10; /* Timeout 1 Sekunde */
tcflush(_uartFd,TCIOFLUSH); /* Puffer leeren */
if (tcsetattr(_uartFd, TCSAFLUSH, &options) != 0)
{
close(_uartFd);
_uartFd = -1;
return;
}
}
}
void print(const char* s)
{
printf("%s", s);

View File

@ -40,6 +40,15 @@ public:
int readBytesMultiCast(uint8_t* buffer, uint16_t maxLen) override;
bool sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len) override;
//UART
void setupUart() override;
void closeUart() override;
int uartAvailable() override;
size_t writeUart(const uint8_t data) override;
size_t writeUart(const uint8_t* buffer, size_t size) override;
int readUart() override;
size_t readBytesUart(uint8_t* buffer, size_t length) override;
//spi
void setupSpi() override;
void closeSpi() override;
@ -59,6 +68,7 @@ public:
uint8_t* _mappedFile = 0;
int _fd = -1;
int _spiFd = -1;
int _uartFd = -1;
std::string _flashFilePath = "flash.bin";
char** _args = 0;