diff --git a/stmhal/uart.c b/stmhal/uart.c
index 884490a5f449a55d8b78ba84517ae95bf7cf0700..84ea22fd58193f6135b7b7de2f8eeedd9780d29d 100644
--- a/stmhal/uart.c
+++ b/stmhal/uart.c
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
+#include <errno.h>
 
 #include "stm32f4xx_hal.h"
 
@@ -488,12 +489,11 @@ mp_uint_t uart_ioctl(mp_obj_t self_in, mp_uint_t request, int *errcode, ...) {
         if ((flags & MP_IOCTL_POLL_RD) && uart_rx_any(self)) {
             ret |= MP_IOCTL_POLL_RD;
         }
-        if (flags & MP_IOCTL_POLL_WR) {
-            // TODO can we always write?
+        if ((flags & MP_IOCTL_POLL_WR) && __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_TXE)) {
             ret |= MP_IOCTL_POLL_WR;
         }
     } else {
-        *errcode = 1; // EPERM, operation not permitted
+        *errcode = EINVAL;
         ret = -1;
     }
     va_end(vargs);