es kompiliert
This commit is contained in:
parent
70e85ab8e9
commit
17416f357c
9 changed files with 189 additions and 216 deletions
225
drv/b15f.cpp
225
drv/b15f.cpp
|
@ -11,24 +11,7 @@ void B15F::init()
|
|||
{
|
||||
|
||||
std::cout << PRE << "Stelle Verbindung mit Adapter her... " << std::flush;
|
||||
int code = system(std::string("stty " + std::to_string(BAUDRATE) + " -F " + SERIAL_DEVICE).c_str());
|
||||
if(code)
|
||||
{
|
||||
throw DriverException("Konnte serielle Verbindung nicht initialisieren. Ist der Adapter angeschlossen?");
|
||||
}
|
||||
|
||||
usart = open(SERIAL_DEVICE.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
struct termios options;
|
||||
tcgetattr(usart, &options);
|
||||
options.c_cflag = CS8 | CLOCAL | CREAD;
|
||||
options.c_iflag = IGNPAR;
|
||||
options.c_oflag = 0;
|
||||
options.c_lflag = 0;
|
||||
options.c_cc[VTIME]=100; // timeout in Dezisekunden
|
||||
cfsetspeed(&options, BAUDRATE);
|
||||
tcsetattr(usart, TCSANOW, &options);
|
||||
tcflush(usart, TCIOFLUSH); // leere Puffer in beiden Richtungen
|
||||
|
||||
usart.openDevice(SERIAL_DEVICE);
|
||||
std::cout << "OK" << std::endl;
|
||||
|
||||
delay_ms(1);
|
||||
|
@ -82,13 +65,13 @@ void B15F::reconnect()
|
|||
|
||||
void B15F::discard(void)
|
||||
{
|
||||
tcflush(usart, TCOFLUSH); // leere Ausgangspuffer
|
||||
usart.clearOutputBuffer();
|
||||
for(uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
|
||||
usart.writeByte(RQ_DISC); // sende discard Befehl (verwerfe input)
|
||||
delay_ms((16000 / BAUDRATE) + 1); // warte mindestens eine Millisekunde, gegebenenfalls mehr
|
||||
}
|
||||
tcflush(usart, TCIFLUSH); // leere Eingangspuffer
|
||||
usart.clearInputBuffer();
|
||||
}
|
||||
|
||||
bool B15F::testConnection()
|
||||
|
@ -97,11 +80,11 @@ bool B15F::testConnection()
|
|||
srand(time(NULL));
|
||||
uint8_t dummy = rand() % 256;
|
||||
|
||||
writeByte(RQ_TEST);
|
||||
writeByte(dummy);
|
||||
usart.writeByte(RQ_TEST);
|
||||
usart.writeByte(dummy);
|
||||
|
||||
uint8_t aw = readByte();
|
||||
uint8_t mirror = readByte();
|
||||
uint8_t aw = usart.readByte();
|
||||
uint8_t mirror = usart.readByte();
|
||||
|
||||
return aw == MSG_OK && mirror == dummy;
|
||||
}
|
||||
|
@ -111,10 +94,10 @@ bool B15F::testIntConv()
|
|||
srand(time(NULL));
|
||||
uint16_t dummy = rand() % (0xFFFF / 3);
|
||||
|
||||
writeByte(RQ_INT);
|
||||
writeInt(dummy);
|
||||
usart.writeByte(RQ_INT);
|
||||
usart.writeInt(dummy);
|
||||
|
||||
uint16_t aw = readInt();
|
||||
uint16_t aw = usart.readInt();
|
||||
return aw == dummy * 3;
|
||||
}
|
||||
|
||||
|
@ -125,20 +108,20 @@ std::vector<std::string> B15F::getBoardInfo(void)
|
|||
{
|
||||
std::vector<std::string> info;
|
||||
|
||||
writeByte(RQ_INFO);
|
||||
uint8_t n = readByte();
|
||||
usart.writeByte(RQ_INFO);
|
||||
uint8_t n = usart.readByte();
|
||||
while(n--)
|
||||
{
|
||||
uint8_t len = readByte();
|
||||
uint8_t len = usart.readByte();
|
||||
std::string str;
|
||||
|
||||
while(len--)
|
||||
str += static_cast<char>(readByte());
|
||||
str += static_cast<char>(usart.readByte());
|
||||
|
||||
info.push_back(str);
|
||||
}
|
||||
|
||||
uint8_t aw = readByte();
|
||||
uint8_t aw = usart.readByte();
|
||||
|
||||
if(aw != MSG_OK)
|
||||
throw DriverException("Board Info fehlerhalft");
|
||||
|
@ -156,10 +139,10 @@ bool B15F::digitalWrite0(uint8_t port)
|
|||
{
|
||||
try
|
||||
{
|
||||
writeByte(RQ_BA0);
|
||||
writeByte(port);
|
||||
usart.writeByte(RQ_BA0);
|
||||
usart.writeByte(port);
|
||||
|
||||
uint8_t aw = readByte();
|
||||
uint8_t aw = usart.readByte();
|
||||
return aw == MSG_OK;
|
||||
}
|
||||
catch(DriverException& de)
|
||||
|
@ -173,10 +156,10 @@ bool B15F::digitalWrite1(uint8_t port)
|
|||
{
|
||||
try
|
||||
{
|
||||
writeByte(RQ_BA1);
|
||||
writeByte(port);
|
||||
usart.writeByte(RQ_BA1);
|
||||
usart.writeByte(port);
|
||||
|
||||
uint8_t aw = readByte();
|
||||
uint8_t aw = usart.readByte();
|
||||
return aw == MSG_OK;
|
||||
}
|
||||
catch(DriverException& de)
|
||||
|
@ -190,8 +173,8 @@ uint8_t B15F::digitalRead0()
|
|||
{
|
||||
try
|
||||
{
|
||||
writeByte(RQ_BE0);
|
||||
return readByte();
|
||||
usart.writeByte(RQ_BE0);
|
||||
return usart.readByte();
|
||||
}
|
||||
catch(DriverException& de)
|
||||
{
|
||||
|
@ -204,8 +187,8 @@ uint8_t B15F::digitalRead1()
|
|||
{
|
||||
try
|
||||
{
|
||||
writeByte(RQ_BE1);
|
||||
return readByte();
|
||||
usart.writeByte(RQ_BE1);
|
||||
return usart.readByte();
|
||||
}
|
||||
catch(DriverException& de)
|
||||
{
|
||||
|
@ -218,12 +201,12 @@ bool B15F::analogWrite0(uint16_t value)
|
|||
{
|
||||
try
|
||||
{
|
||||
writeByte(RQ_AA0);
|
||||
usart.writeByte(RQ_AA0);
|
||||
delay_ms(1);
|
||||
writeInt(value);
|
||||
usart.writeInt(value);
|
||||
delay_ms(1);
|
||||
|
||||
uint8_t aw = readByte();
|
||||
uint8_t aw = usart.readByte();
|
||||
return aw == MSG_OK;
|
||||
}
|
||||
catch(DriverException& de)
|
||||
|
@ -237,10 +220,10 @@ bool B15F::analogWrite1(uint16_t value)
|
|||
{
|
||||
try
|
||||
{
|
||||
writeByte(RQ_AA1);
|
||||
writeInt(value);
|
||||
usart.writeByte(RQ_AA1);
|
||||
usart.writeInt(value);
|
||||
|
||||
uint8_t aw = readByte();
|
||||
uint8_t aw = usart.readByte();
|
||||
return aw == MSG_OK;
|
||||
}
|
||||
catch(DriverException& de)
|
||||
|
@ -254,10 +237,10 @@ uint16_t B15F::analogRead(uint8_t channel)
|
|||
{
|
||||
try
|
||||
{
|
||||
writeByte(RQ_ADC);
|
||||
usart.writeByte(RQ_ADC);
|
||||
delay_ms(1);
|
||||
writeByte(channel);
|
||||
return readInt();
|
||||
usart.writeByte(channel);
|
||||
return usart.readInt();
|
||||
}
|
||||
catch(DriverException& de)
|
||||
{
|
||||
|
@ -273,13 +256,13 @@ bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset
|
|||
|
||||
try
|
||||
{
|
||||
writeByte(RQ_ADC_DAC_STROKE);
|
||||
writeByte(channel_a);
|
||||
writeByte(channel_b);
|
||||
writeInt(start);
|
||||
writeInt(static_cast<uint16_t>(delta));
|
||||
writeInt(count);
|
||||
uint8_t aw = readByte();
|
||||
usart.writeByte(RQ_ADC_DAC_STROKE);
|
||||
usart.writeByte(channel_a);
|
||||
usart.writeByte(channel_b);
|
||||
usart.writeInt(start);
|
||||
usart.writeInt(static_cast<uint16_t>(delta));
|
||||
usart.writeInt(count);
|
||||
uint8_t aw = usart.readByte();
|
||||
|
||||
if(aw != MSG_OK)
|
||||
{
|
||||
|
@ -290,7 +273,7 @@ bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset
|
|||
uint8_t block[5]; // 4 Datenbyte + crc
|
||||
for(uint16_t i = 0; i < count; i++)
|
||||
{
|
||||
bool crc_ok = readBlock(&block[0], 0);
|
||||
bool crc_ok = usart.readBlock(&block[0], 0);
|
||||
|
||||
if (!crc_ok)
|
||||
{
|
||||
|
@ -303,7 +286,7 @@ bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset
|
|||
buffer_b[offset_b + i] = ((uint16_t) block[2]) | (((uint16_t) block[3]) << 8);
|
||||
}
|
||||
|
||||
aw = readByte();
|
||||
aw = usart.readByte();
|
||||
if(aw == MSG_OK)
|
||||
return aw;
|
||||
|
||||
|
@ -317,126 +300,6 @@ bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void B15F::writeByte(uint8_t b)
|
||||
{
|
||||
if(write(usart, &b, 1) != 1)
|
||||
throw DriverException("Fehler beim Senden. (byte)");
|
||||
}
|
||||
|
||||
void B15F::writeInt(uint16_t v)
|
||||
{
|
||||
if(write(usart, reinterpret_cast<char*>(&v), 2) != 2)
|
||||
throw DriverException("Fehler beim Senden. (int)");
|
||||
}
|
||||
|
||||
uint8_t B15F::readByte()
|
||||
{
|
||||
char b;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
auto end = start;
|
||||
uint16_t elapsed = 0;
|
||||
while(elapsed < timeout)
|
||||
{
|
||||
int n_ready;
|
||||
int code = ioctl(usart, FIONREAD, &n_ready);
|
||||
if(code != 0)
|
||||
std::cout << PRE << "n_ready code: " << code << std::endl;
|
||||
|
||||
if(n_ready > 0)
|
||||
{
|
||||
//std::cout << code << " \tready: " << n_ready << std::endl;
|
||||
|
||||
code = read(usart, &b, 1);
|
||||
if (code > 0)
|
||||
return static_cast<uint8_t>(b);
|
||||
if (code < 0)
|
||||
std::cout << PRE << "usart code: " << code << std::endl;
|
||||
}
|
||||
|
||||
end = std::chrono::steady_clock::now();
|
||||
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
}
|
||||
|
||||
if(elapsed >= timeout)
|
||||
throw DriverException("Verbindung unterbrochen. (timeout)");
|
||||
}
|
||||
|
||||
uint16_t B15F::readInt()
|
||||
{
|
||||
return readByte() | readByte() << 8;
|
||||
}
|
||||
|
||||
bool B15F::readBlock(uint8_t* buffer, uint16_t offset)
|
||||
{
|
||||
uint8_t len = readByte();
|
||||
uint8_t crc = 0;
|
||||
buffer += offset;
|
||||
|
||||
// wait for block
|
||||
int n_ready;
|
||||
uint16_t elapsed = 0;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
auto end = start;
|
||||
while(elapsed < block_timeout)
|
||||
{
|
||||
int code = ioctl(usart, FIONREAD, &n_ready);
|
||||
if(code != 0)
|
||||
{
|
||||
std::cout << PRE << "n_ready code: " << code << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(n_ready >= len + 1)
|
||||
break;
|
||||
|
||||
end = std::chrono::steady_clock::now();
|
||||
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
}
|
||||
if(elapsed >= timeout)
|
||||
{
|
||||
std::cout << PRE << "block timeout: " << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
while(len--)
|
||||
{
|
||||
*buffer = readByte();
|
||||
|
||||
crc ^= *buffer++;
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (crc & 1)
|
||||
crc ^= CRC7_POLY;
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
crc ^= readByte();
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (crc & 1)
|
||||
crc ^= CRC7_POLY;
|
||||
crc >>= 1;
|
||||
}
|
||||
|
||||
if(TEST == 1)
|
||||
crc = 1;
|
||||
if(TEST > 100)
|
||||
TEST = 0;
|
||||
|
||||
if (crc == 0)
|
||||
{
|
||||
writeByte(MSG_OK);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
writeByte(MSG_FAIL);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void B15F::delay_ms(uint16_t ms)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue