diff --git a/drv/b15f.cpp b/drv/b15f.cpp index 0637124..f503a71 100644 --- a/drv/b15f.cpp +++ b/drv/b15f.cpp @@ -15,6 +15,7 @@ void B15F::init() usart.openDevice(SERIAL_DEVICE); std::cout << "OK" << std::endl; + delay_ms(1); std::cout << PRE << "Teste Verbindung... " << std::flush; @@ -39,7 +40,6 @@ void B15F::init() // Gib board info aus std::vector info = getBoardInfo(); std::cout << PRE << "AVR Firmware Version: " << info[0] << " um " << info[1] << " Uhr (" << info[2] << ")" << std::endl; - } void B15F::reconnect() @@ -123,7 +123,7 @@ std::vector B15F::getBoardInfo(void) uint8_t aw = usart.readByte(); if(aw != MSG_OK) - throw DriverException("Board Info fehlerhalft"); + throw DriverException("Board Info fehlerhalft: code " + std::to_string((int) aw)); return info; } @@ -185,6 +185,9 @@ uint16_t B15F::analogRead(uint8_t channel) bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count) { + buffer_a += offset_a; + buffer_b += offset_b; + usart.writeByte(RQ_ADC_DAC_STROKE); usart.writeByte(channel_a); usart.writeByte(channel_b); @@ -198,20 +201,22 @@ bool B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset throw DriverException("Out of sync"); } - uint8_t block[5]; // 4 Datenbyte + crc + //uint8_t block[5]; // 4 Datenbyte + crc for(uint16_t i = 0; i < count; i++) { - bool crc_ok = usart.readBlock(&block[0], 0); + /*bool crc_ok = usart.readBlock(&block[0], 0); if (!crc_ok) { std::cout << PRE << "bad crc" << std::endl; return analogSequence(channel_a, buffer_a, offset_a, channel_b, buffer_b, offset_b, start, delta, count); - } + }*/ + buffer_a[i] = usart.readInt(); + buffer_b[i] = usart.readInt(); + //std::cout << buffer_a[i] << " - " << buffer_b[i] << std::endl; - - buffer_a[offset_a + i] = ((uint16_t) block[0]) | (((uint16_t) block[1]) << 8); - buffer_b[offset_b + i] = ((uint16_t) block[2]) | (((uint16_t) block[3]) << 8); + /*buffer_a[i] = ((uint16_t) block[0]) | (((uint16_t) block[1]) << 8); + buffer_b[i] = ((uint16_t) block[2]) | (((uint16_t) block[3]) << 8);*/ } aw = usart.readByte(); diff --git a/drv/b15f.o b/drv/b15f.o index 00c28b9..80956df 100644 Binary files a/drv/b15f.o and b/drv/b15f.o differ diff --git a/drv/usart.cpp b/drv/usart.cpp index 4deff04..d9c0dbf 100644 --- a/drv/usart.cpp +++ b/drv/usart.cpp @@ -51,6 +51,7 @@ void USART::clearOutputBuffer() void USART::writeByte(uint8_t b) { + //std::cout << "write(): " << (int) b << std::endl; if(write(file_desc, &b, 1) != 1) throw USARTException("Fehler beim Senden: writeByte()"); } @@ -60,6 +61,38 @@ void USART::writeInt(uint16_t d) if(write(file_desc, reinterpret_cast(&d), 2) != 2) throw USARTException("Fehler beim Senden: writeInt()"); } + +void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len) +{ + buffer += offset; + uint8_t crc = 0; + + writeByte(len); + + while(len--) + { + writeByte(*buffer); + + crc ^= *buffer; + for (uint8_t i = 0; i < 8; i++) + { + if (crc & 1) + crc ^= CRC7_POLY; + crc >>= 1; + } + + buffer++; + } + + writeByte(crc); + writeByte(0x80); // Stoppzeichen für Block + + uint8_t aw = readByte(); + if(aw != 0xFF) + { + std::cout << "Ende Gelände" << std::endl; + } +} uint8_t USART::readByte(void) { diff --git a/drv/usart.h b/drv/usart.h index a23cab1..a52d74a 100644 --- a/drv/usart.h +++ b/drv/usart.h @@ -65,8 +65,19 @@ public: */ void writeInt(uint16_t d); + /** + * Empfängt ein Byte über die USART Schnittstelle + * \throws USARTException + */ uint8_t readByte(void); + + /** + * Empfängt ein Integer über die USART Schnittstelle + * \throws USARTException + */ uint16_t readInt(void); + + void writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len); bool readBlock(uint8_t* buffer, uint16_t offset); /*************************************/ diff --git a/drv/usart.o b/drv/usart.o index 9e8a0be..57ade56 100644 Binary files a/drv/usart.o and b/drv/usart.o differ diff --git a/main b/main index 03affce..8bc0c14 100755 Binary files a/main and b/main differ diff --git a/main.cpp b/main.cpp index 22cd69f..40dc56c 100644 --- a/main.cpp +++ b/main.cpp @@ -108,86 +108,38 @@ void kennlinieZweiterQuadrant() pf.startPlotty("test_plot"); } -void beispielFunktionen() +void testFunktionen() { B15F& drv = B15F::getInstance(); - for(uint16_t i = 0; i < 256; i++) - { - drv.digitalWrite0(i); - drv.delay_ms(100); - } + drv.digitalWrite0(0xFF); + drv.analogWrite0(128); + std::cout << (int) drv.digitalRead0() << std::endl;; + std::cout << "adc: " << (int) drv.analogRead(4) << std::endl; + + drv.digitalWrite0(0x00); + drv.analogWrite0(0); + std::cout << (int) drv.digitalRead0() << std::endl;; + std::cout << "adc: " << (int) drv.analogRead(4) << std::endl; + + drv.digitalWrite0(0xFF); + drv.analogWrite0(255); + std::cout << (int) drv.digitalRead0() << std::endl; + std::cout << "adc: " << (int) drv.analogRead(4) << std::endl; + uint16_t a[1024]; + uint16_t b[1024]; + drv.analogSequence(0, &a[0], 0, 1, &b[0], 0, 0, 1, 1024); - while(1) - { - for(double d = 0; d < M_PI*2; d += .1) - { - uint16_t v = (sin(d) * 511 + 511); - drv.analogWrite0(v); - } - - } - - - - - uint16_t schwelle_unten = 1023; - for(uint16_t i = 0; i < 1024; i++) - { - drv.analogWrite0(i); - drv.delay_ms(1); - if(drv.digitalRead0() & 0x01) - { - drv.discard(); - uint16_t val = drv.analogRead(0); - if(val > 1023) - { - std::cout << "Fehler: " << val << std::endl; - continue; - } - if(val < schwelle_unten) - schwelle_unten = val; - } - } - - std::cout << "OK" << std::endl; - - uint16_t schwelle_oben = 0; - for(uint16_t i = 1023; i > 0; i--) - { - drv.analogWrite0(i); - if(!(drv.digitalRead0() & 0x01)) - { - drv.discard(); - uint16_t val = drv.analogRead(0); - if(val > 1023) - { - std::cout << "Fehler: " << val << std::endl; - continue; - } - if(val > schwelle_oben) - schwelle_oben = val; - } - } - - std::cout << "Schwelle für low: " << schwelle_unten << std::endl; - std::cout << "Schwelle für high: " << schwelle_oben << std::endl; - std::cout << "Verbotene Zone: " << (schwelle_oben - schwelle_unten) << std::endl; } int main() { - beispielFunktionen(); - - //USART usart; - //usart.openDevice("/dev/ttyUSB0"); - //usart.writeByte(5); - + kennlinieZweiterQuadrant(); std::cout << "Schluss." << std::endl; } diff --git a/main.o b/main.o index b170546..c3c698d 100644 Binary files a/main.o and b/main.o differ