This commit is contained in:
Tristan Krause 2019-05-29 11:07:42 +02:00
parent 63d81c6e7e
commit 0ec3988c58
4 changed files with 70 additions and 76 deletions

View file

@ -229,9 +229,7 @@ uint16_t B15F::analogRead(uint8_t channel)
channel
};
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
if(n_sent != sizeof(rq))
abort("Sent failed");
usart.transmit(&rq[0], 0, sizeof(rq));
uint16_t adc = usart.readInt();
@ -302,9 +300,7 @@ uint8_t B15F::pwmSetFrequency(uint32_t freq)
static_cast<uint8_t>((freq >> 24) & 0xFF)
};
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
if(n_sent != sizeof(rq))
abort("Sent failed");
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t byte = usart.readByte();
delay_us(10);
@ -321,9 +317,7 @@ bool B15F::pwmSetValue(uint8_t value)
value
};
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
if(n_sent != sizeof(rq))
abort("Sent failed");
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);
@ -341,9 +335,7 @@ bool B15F::setRegister(uint8_t adr, uint8_t val)
val
};
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
if(n_sent != sizeof(rq))
abort("Sent failed");
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t byte = usart.readByte();
delay_us(10);
@ -360,9 +352,7 @@ uint8_t B15F::getRegister(uint8_t adr)
adr
};
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
if(n_sent != sizeof(rq))
abort("Sent failed");
usart.transmit(&rq[0], 0, sizeof(rq));
uint8_t aw = usart.readByte();
delay_us(10);

View file

@ -85,43 +85,18 @@ void USART::writeU32(uint32_t w)
throw USARTException("Fehler beim Senden: writeU32()");
}
int USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len)
void USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len)
{
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 + offset, len);
if (n_read == len)
return n_read;
end = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
}
return 0;
int n = read(file_desc, buffer + offset, len);
if(n != len)
throw USARTException(std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__));
}
int USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len)
void USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len)
{
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);
flushOutputBuffer();
if (n_sent == len)
return n_sent;
end = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
}
return n_sent;
int n = write(file_desc, buffer + offset, len);
if(n != len)
throw USARTException(std::string(__FUNCTION__) + " failed: " + std::string(__FILE__) + "#" + std::to_string(__LINE__));
}
uint8_t USART::readByte(void)
{

View file

@ -95,8 +95,21 @@ public:
*/
uint16_t readInt(void);
int transmit(uint8_t *buffer, uint16_t offset, uint8_t len);
int receive(uint8_t *buffer, uint16_t offset, uint8_t len);
/**
* Sends n bytes from the buffer over USART
* \param buffer target buffer
* \param offset in buffer (mostly 0)
* \throws USARTException
*/
void transmit(uint8_t *buffer, uint16_t offset, uint8_t len);
/**
* Receives n bytes from USART and writes them into the buffer
* \param buffer target buffer
* \param offset in buffer (mostly 0)
* \throws USARTException
*/
void receive(uint8_t *buffer, uint16_t offset, uint8_t len);
/*************************************/