From b4a8bb0653311c8befebf2e011d310da11f32440 Mon Sep 17 00:00:00 2001
From: Markus Sattler <help.markus+git@gmail.com>
Date: Thu, 14 May 2015 21:41:43 +0200
Subject: [PATCH] fix bug when TX buffer is full and os will write.

in this case we hang endless or until wtd triggers.

new:
 now we overdrive the data in FIFO --> no hang / crash but we loss chars.
 only happens by extensive use of os_printf!
---
 cores/esp8266/HardwareSerial.cpp | 36 ++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp
index 87fd6dfbb..bdcd45716 100644
--- a/cores/esp8266/HardwareSerial.cpp
+++ b/cores/esp8266/HardwareSerial.cpp
@@ -392,30 +392,34 @@ void uart_ignore_char(char c) {
 
 void uart0_write_char(char c) {
     if(&Serial != NULL && Serial.isTxEnabled()) {
-        if(c == '\n') {
-            Serial.write('\r');
+        if(Serial.availableForWrite() > 0) {
+            if(c == '\n') {
+                Serial.write('\r');
+            }
+            Serial.write(c);
+            return;
         }
-        Serial.write(c);
-    } else {
-        if(c == '\n') {
-            USF(0) = '\r';
-        }
-        USF(0) = c;
     }
+    if(c == '\n') {
+        USF(0) = '\r';
+    }
+    USF(0) = c;
 }
 
 void uart1_write_char(char c) {
     if(&Serial1 != NULL && Serial1.isTxEnabled()) {
-        if(c == '\n') {
-            Serial1.write('\r');
+        if(Serial1.availableForWrite() > 0) {
+            if(c == '\n') {
+                Serial1.write('\r');
+            }
+            Serial1.write(c);
+            return;
         }
-        Serial1.write(c);
-    } else {
-        if(c == '\n') {
-            USF(1) = '\r';
-        }
-        USF(1) = c;
     }
+    if(c == '\n') {
+        USF(1) = '\r';
+    }
+    USF(1) = c;
 }
 
 static int s_uart_debug_nr = UART0;