Skip to content

Commit

Permalink
Remove template parameter from UartDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmuskleist committed Aug 7, 2023
1 parent 59d35f5 commit 7b77f09
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/modm/architecture/interface/uart_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,41 @@ namespace modm
* @author Rasmus Kleist Hørlyck Sørensen
* @ingroup modm_architecture_uart_device
*/
template < class Uart, uint8_t NestingLevels = 10, uint16_t TxUsTimeout = 1000, uint16_t RxUsTimeout = 10000 >
template < class Uart, uint8_t NestingLevels = 10 >
class UartDevice : protected modm::NestedResumable< NestingLevels + 1 >
{
public:
UartDevice() = default;
UartDevice() :
txTimeout(std::chrono::microseconds(1000)),
rxTimeout(std::chrono::microseconds(10000))
{
}

bool
hasReceived()
{
return Uart::receiveBufferSize() > 0;
}

void
setTxTimeout(ShortPreciseDuration timeout)
{
txTimeout = timeout;
}

void
setRxTimeout(ShortPreciseDuration timeout)
{
rxTimeout = timeout;
}

protected:
modm::ResumableResult<bool>
write(uint8_t data)
{
RF_BEGIN(0);

timeout.restart(std::chrono::microseconds(TxUsTimeout));
timeout.restart(txTimeout);
RF_WAIT_UNTIL(Uart::write(data) or timeout.isExpired() or Uart::hasError());
if (timeout.isExpired() or Uart::hasError())
{
Expand All @@ -62,13 +78,13 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 >
RF_BEGIN(0);

writeIndex = 0;
timeout.restart(std::chrono::microseconds(TxUsTimeout));
timeout.restart(txTimeout);
while (writeIndex < length)
{
if (size_t writeSize = Uart::write(&data[writeIndex], length - writeIndex))
{
writeIndex += writeSize;
timeout.restart(std::chrono::microseconds(TxUsTimeout));
timeout.restart(txTimeout);
}

if (timeout.isExpired() or Uart::hasError())
Expand All @@ -88,7 +104,7 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 >
{
RF_BEGIN(1);

timeout.restart(std::chrono::microseconds(RxUsTimeout));
timeout.restart(rxTimeout);
RF_WAIT_UNTIL(Uart::read(data) or timeout.isExpired() or Uart::hasError());
if (timeout.isExpired() or Uart::hasError())
{
Expand All @@ -105,13 +121,13 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 >
RF_BEGIN(1);

readIndex = 0;
timeout.restart(std::chrono::microseconds(RxUsTimeout));
timeout.restart(rxTimeout);
while (readIndex < length)
{
if (size_t writeSize = Uart::read(&buffer[readIndex], length - readIndex))
{
writeIndex += writeSize;
timeout.restart(std::chrono::microseconds(RxUsTimeout));
timeout.restart(rxTimeout);
}

if (timeout.isExpired() or Uart::hasError())
Expand All @@ -129,7 +145,10 @@ class UartDevice : protected modm::NestedResumable< NestingLevels + 1 >
private:
std::size_t readIndex;
std::size_t writeIndex;
modm::ShortPreciseTimeout timeout;

ShortPreciseDuration txTimeout;
ShortPreciseDuration rxTimeout;
ShortPreciseTimeout timeout;
};

} // namespace modm
Expand Down

0 comments on commit 7b77f09

Please sign in to comment.