Explorar el Código

Autobaudrate detection by default -- to reduce problems with speed change (#1215)

* Auto autobaud detection by default

* Added stopping the autobaud if explicitly configured

* Add note about auto-baud to the uart.md
Philip Gladstone hace 8 años
padre
commit
5b4b1e0388
Se han modificado 5 ficheros con 50 adiciones y 2 borrados
  1. 3 0
      README.md
  2. 39 0
      app/driver/uart.c
  3. 4 2
      app/include/user_config.h
  4. 2 0
      docs/en/modules/uart.md
  5. 2 0
      sdk-overrides/include/osapi.h

+ 3 - 0
README.md

@@ -96,6 +96,9 @@ editing `BIT_RATE_DEFAULT` in `app/include/user_config.h`:
 #define BIT_RATE_DEFAULT BIT_RATE_115200
 ```
 
+Note that, by default, the firmware runs an auto-baudrate detection algorithm so that typing a few characters at boot time will cause
+the firmware to lock onto that baud rate (between 1200 and 230400). 
+
 ### Debugging
 
 To enable runtime debug messages to serial console edit `app/include/user_config.h`

+ 39 - 0
app/driver/uart.c

@@ -15,6 +15,7 @@
 #include "task/task.h"
 #include "user_config.h"
 #include "user_interface.h"
+#include "osapi.h"
 
 #define UART0   0
 #define UART1   1
@@ -33,6 +34,8 @@ static task_handle_t sig = 0;
 // UartDev is defined and initialized in rom code.
 extern UartDevice UartDev;
 
+static os_timer_t autobaud_timer;
+
 LOCAL void ICACHE_RAM_ATTR
 uart0_rx_intr_handler(void *para);
 
@@ -271,6 +274,36 @@ uart0_rx_intr_handler(void *para)
       task_post_low (sig, false);
 }
 
+static void 
+uart_autobaud_timeout(void *timer_arg)
+{
+  uint32_t uart_no = (uint32_t) timer_arg;
+  uint32_t divisor = uart_baudrate_detect(uart_no, 1);
+  static int called_count = 0;
+
+  // Shut off after two minutes to stop wasting CPU cycles if insufficient input received
+  if (called_count++ > 10 * 60 * 2 || divisor) {
+    os_timer_disarm(&autobaud_timer);
+  }
+
+  if (divisor) {
+    uart_div_modify(uart_no, divisor);
+  }
+}
+
+static void 
+uart_init_autobaud(uint32_t uart_no)
+{
+  os_timer_setfn(&autobaud_timer, uart_autobaud_timeout, (void *) uart_no);
+  os_timer_arm(&autobaud_timer, 100, TRUE);
+}
+
+static void 
+uart_stop_autobaud()
+{
+  os_timer_disarm(&autobaud_timer);
+}
+
 /******************************************************************************
  * FunctionName : uart_init
  * Description  : user interface for init uart
@@ -291,11 +324,17 @@ uart_init(UartBautRate uart0_br, UartBautRate uart1_br, os_signal_t sig_input)
     UartDev.baut_rate = uart1_br;
     uart_config(UART1);
     ETS_UART_INTR_ENABLE();
+#ifdef BIT_RATE_AUTOBAUD
+    uart_init_autobaud(0);
+#endif
 }
 
 void ICACHE_FLASH_ATTR
 uart_setup(uint8 uart_no)
 {
+#ifdef BIT_RATE_AUTOBAUD
+    uart_stop_autobaud();
+#endif
     ETS_UART_INTR_DISABLE();
     uart_config(uart_no);
     ETS_UART_INTR_ENABLE();

+ 4 - 2
app/include/user_config.h

@@ -34,10 +34,12 @@ extern void luaL_assertfail(const char *file, int line, const char *message);
 #ifdef DEVELOP_VERSION
 #define NODE_DEBUG
 #define COAP_DEBUG
-#else
-#define BIT_RATE_DEFAULT BIT_RATE_115200
 #endif /* DEVELOP_VERSION */
 
+#define BIT_RATE_DEFAULT BIT_RATE_115200
+
+// This enables automatic baud rate detection at startup
+#define BIT_RATE_AUTOBAUD
 
 #define NODE_ERROR
 

+ 2 - 0
docs/en/modules/uart.md

@@ -5,6 +5,8 @@
 
 The [UART](https://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter) (Universal asynchronous receiver/transmitter) module allows configuration of and communication over the UART serial port.
 
+The default setup for the uart is controlled by build-time settings. The default rate is 115,200 bps. In addition, auto-baudrate detection is enabled for the first two minutes
+after platform boot. This will cause a switch to the correct baud rate once a few characters are received. Auto-baudrate detection is disabled when `uart.setup` is called.
 ## uart.alt()
 Change UART pin assignment.
 

+ 2 - 0
sdk-overrides/include/osapi.h

@@ -8,6 +8,8 @@ int atoi(const char *nptr);
 int os_printf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
 int os_printf_plus(const char *format, ...)  __attribute__ ((format (printf, 1, 2)));
 
+unsigned int uart_baudrate_detect(unsigned int uart_no, unsigned int async);
+
 void NmiTimSetFunc(void (*func)(void));
 
 #include_next "osapi.h"