summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2023-03-09 03:10:31 +0300
committerOxore <oxore@protonmail.com>2023-03-09 03:10:31 +0300
commit4c2caed73357c2173649d5a105cad5277eaa85f7 (patch)
tree74ce01d223c592d2fbadfc295761be2d74d5dab5
parent7a0b96bf5c46521d6d8d2b527eb7099420a97aad (diff)
Fix UART buffering, now it works
-rw-r--r--app/platform/stm32f0-gcc/retarget.cpp6
-rw-r--r--app/platform/stm32f0-gcc/ring_buffer.h2
-rw-r--r--app/platform/stm32f0-gcc/uart.cpp15
3 files changed, 15 insertions, 8 deletions
diff --git a/app/platform/stm32f0-gcc/retarget.cpp b/app/platform/stm32f0-gcc/retarget.cpp
index a6706d3..85e12b4 100644
--- a/app/platform/stm32f0-gcc/retarget.cpp
+++ b/app/platform/stm32f0-gcc/retarget.cpp
@@ -92,6 +92,12 @@ extern "C" ssize_t read(int fd, void *buf, size_t nbytes)
switch (file->type) {
case FileType::kUART:
ret = UARTRead(reinterpret_cast<UART*>(file->extra), buf, nbytes);
+ // XXX if fcntl support for disabling O_NONBLOCK will ever be added,
+ // then it has to something with this block of code
+ if (ret == 0) {
+ errno = EWOULDBLOCK;
+ ret = -1;
+ }
break;
case FileType::kUARTDirect:
ret = UARTReadDirect(reinterpret_cast<UART*>(file->extra), buf, nbytes);
diff --git a/app/platform/stm32f0-gcc/ring_buffer.h b/app/platform/stm32f0-gcc/ring_buffer.h
index ae4d99d..8e6c6be 100644
--- a/app/platform/stm32f0-gcc/ring_buffer.h
+++ b/app/platform/stm32f0-gcc/ring_buffer.h
@@ -46,7 +46,7 @@ public:
}
constexpr size_t Occupied()
{
- return _head - _tail + (_head >= _tail) ? 0 : _buffer_size;
+ return _head - _tail + ((_head >= _tail) ? 0 : _buffer_size);
}
constexpr bool IsEmpty() { return _head == _tail; }
constexpr bool IsFull() { return _tail + 1 == _head; }
diff --git a/app/platform/stm32f0-gcc/uart.cpp b/app/platform/stm32f0-gcc/uart.cpp
index 4d9bf5f..89472ee 100644
--- a/app/platform/stm32f0-gcc/uart.cpp
+++ b/app/platform/stm32f0-gcc/uart.cpp
@@ -42,7 +42,7 @@ UART g_uart4{
RingBuffer<uint8_t>(g_uart4_tx_buffer, kUART4TxBufferSize),
};
-static constexpr Min(size_t a, size_t b) { return (a >= b) ? a : b; }
+static constexpr Min(size_t a, size_t b) { return (a <= b) ? a : b; }
size_t UARTWrite(UART* uart, const void *buf, size_t nbytes)
{
@@ -96,19 +96,20 @@ void UARTHandleInterrupt(UART* uart)
return;
for (int _ = 0; _ < 256; _++) {
const uint32_t isr = usart_regs->ISR;
- const bool rxne = isr & USART_ISR_RXNE;
- const bool txe = isr & USART_ISR_TXE;
- if (!rxne && !txe)
- break;
- if (rxne) {
+ bool done = true;
+ if (isr & USART_ISR_RXNE) {
uart->rx_buffer.Push(usart_regs->RDR);
+ done = false;
}
- if (txe) {
+ if (isr & USART_ISR_TXE) {
if (!uart->tx_buffer.IsEmpty()) {
usart_regs->TDR = uart->tx_buffer.Pop();
+ done = false;
} else {
usart_regs->CR1 &= ~USART_CR1_TXEIE;
}
}
+ if (done)
+ break;
}
}