diff --git a/drv/usart.cpp b/drv/usart.cpp index 964ddfc..70e738e 100644 --- a/drv/usart.cpp +++ b/drv/usart.cpp @@ -74,14 +74,13 @@ void USART::writeInt(uint16_t d) int USART::read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout) { - buffer += offset; uint32_t elapsed = 0; int n_read = -1; auto start = std::chrono::steady_clock::now(); auto end = start; while(elapsed < timeout) { - n_read = read(file_desc, buffer, len); + n_read = read(file_desc, buffer + offset, len); if (n_read == len) return n_read; @@ -92,6 +91,24 @@ int USART::read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t return n_read; } +int USART::write_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout) +{ + uint32_t elapsed = 0; + int n_sent = -1; + auto start = std::chrono::steady_clock::now(); + auto end = start; + while(elapsed < timeout) + { + n_sent = write(file_desc, buffer + offset, len); + if (n_sent == len) + return n_sent; + + end = std::chrono::steady_clock::now(); + elapsed = std::chrono::duration_cast(end - start).count(); + } + + return n_sent; +} void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len) { @@ -99,15 +116,25 @@ void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len) uint8_t crc; uint8_t aw; uint16_t us_per_bit = (1000000 / baudrate) * 16; + uint8_t block_end = BLOCK_END; do { crc = 0; - writeByte(len); + // send block length + int n_sent = write_timeout(&len, 0, 1, us_per_bit); + if(n_sent != 1) + throw std::runtime_error("fatal (send): " + std::to_string(n_sent)); + + // send block + n_sent = write_timeout(buffer, 0, len, us_per_bit * len); + if(n_sent != len) + throw std::runtime_error("fatal (send #2): " + std::to_string(n_sent)); + + // calc crc for(uint8_t i = 0; i < len; i++) { - writeByte(buffer[i]); usleep(us_per_bit); crc ^= buffer[i]; @@ -119,10 +146,14 @@ void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len) } } - writeByte(crc); + n_sent = write_timeout(&crc, 0, 1, us_per_bit); + if(n_sent != 1) + throw std::runtime_error("fatal (send): " + std::to_string(n_sent)); usleep(us_per_bit); - writeByte(0x80); // Stoppzeichen für Block + n_sent = write_timeout(&block_end, 0, 1, us_per_bit); + if(n_sent != 1) + throw std::runtime_error("fatal (send): " + std::to_string(n_sent)); usleep(us_per_bit); int n_read = read_timeout(&aw, 0, 1, us_per_bit); diff --git a/drv/usart.h b/drv/usart.h index 2355f2b..36a0c33 100644 --- a/drv/usart.h +++ b/drv/usart.h @@ -79,6 +79,7 @@ public: uint16_t readInt(void); int read_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout); + int write_timeout(uint8_t* buffer, uint16_t offset, uint8_t len, uint32_t timeout); void writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len); bool readBlock(uint8_t* buffer, uint16_t offset); @@ -116,15 +117,16 @@ public: /***************************************/ + constexpr static uint8_t CRC7_POLY = 0x91; + constexpr static uint8_t MAX_BLOCK_SIZE = 16; + constexpr static uint8_t BLOCK_END = 0x80; private: int file_desc = -1; // Linux Dateideskriptor uint32_t baudrate = 9600; int TEST = 0; uint8_t timeout = 10; // in Dezisekunden - - constexpr static uint8_t CRC7_POLY = 0x91; - constexpr static uint8_t MAX_BLOCK_SIZE = 16; + uint8_t buffer[MAX_BLOCK_SIZE + 3]; }; diff --git a/drv/usart.o b/drv/usart.o index 56837e6..58b46cd 100644 Binary files a/drv/usart.o and b/drv/usart.o differ diff --git a/main b/main index 25cec96..bb787b8 100755 Binary files a/main and b/main differ