From 43191db63e9f4132a29c10160a32742d8165ec76 Mon Sep 17 00:00:00 2001
From: Deomid Ryabkov <rojer@cesanta.com>
Date: Wed, 11 Apr 2018 18:31:26 +0100
Subject: [PATCH] Fix boot loader UART baud rate on soft reboot

Also remove `-O3` optimization flasg, it negates `-Os`. Saves ~400 bytes.

CL: none

PUBLISHED_FROM=f40f42e4f54a8e0e003ab65ec8f5838c2d932876
---
 .../platforms/esp8266/rboot/rboot/rboot.c     | 20 ++++++++++++++++---
 .../platforms/esp8266/rboot/rboot/rboot.mk    |  2 +-
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/common/platforms/esp8266/rboot/rboot/rboot.c b/src/common/platforms/esp8266/rboot/rboot/rboot.c
index 4ac5df18b..6f7521423 100644
--- a/src/common/platforms/esp8266/rboot/rboot/rboot.c
+++ b/src/common/platforms/esp8266/rboot/rboot/rboot.c
@@ -160,7 +160,8 @@ static uint8 calc_chksum(uint8 *start, uint8 *end) {
 }
 #endif
 
-#define UART_CLKDIV_26MHZ(B) (52000000 + B / 2) / B
+#define UART_CLKDIV_80MHZ(B) (80000000 + B / 2) / B
+#define UART_CLKDIV_52MHZ(B) (52000000 + B / 2) / B
 
 // prevent this function being placed inline with main
 // to keep main's stack size as small as possible
@@ -182,10 +183,23 @@ uint32 NOINLINE find_image(void) {
 	// delay to slow boot (help see messages when debugging)
 	//ets_delay_us(2000000);
 
-	uart_div_modify(0, UART_CLKDIV_26MHZ(115200));
+	/*
+	 * UART divider depends on the APB frequency. Cold boot starts with 52MHz APB,
+	 * SDK configures PLL, sets it to 80 and it persists across soft reset
+	 * so on soft reset a different divider should be used. What we really want to
+	 * know is if the PLL is running but since that is completely undocumented
+	 * we use CPU frequency bit as a workaround: mos sets CPU to 160 MHz on startup
+	 * so we assume that if frequency is preserved, then it's soft reset, PLL is
+	 * running and APB is at 80 MHz.
+	 */
+	if (READ_PERI_REG(0x3ff00014) & 1) {
+		uart_div_modify(0, UART_CLKDIV_80MHZ(115200));
+	} else {
+		uart_div_modify(0, UART_CLKDIV_52MHZ(115200));
+	}
 	ets_delay_us(1000);
 
-	ets_printf("\r\nrBoot v1.2.1 - richardaburton@gmail.com\r\n");
+	ets_printf("\r\nrBoot v1.2.1-cesanta1 - richardaburton@gmail.com\r\n");
 
 	// read rom header
 	SPIRead(0, header, sizeof(rom_header));
diff --git a/src/common/platforms/esp8266/rboot/rboot/rboot.mk b/src/common/platforms/esp8266/rboot/rboot/rboot.mk
index f7f398784..419ba5a8d 100644
--- a/src/common/platforms/esp8266/rboot/rboot/rboot.mk
+++ b/src/common/platforms/esp8266/rboot/rboot/rboot.mk
@@ -23,7 +23,7 @@ LD := $(addprefix $(XTENSA_BINDIR)/,xtensa-lx106-elf-gcc)
 endif
 CC_WRAPPER ?=
 
-CFLAGS    = -Os -O3 -Wpointer-arith -Wundef -Werror -Wl,-EL \
+CFLAGS    = -Os -Wpointer-arith -Wundef -Werror -Wl,-EL \
             -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals \
             -D__ets__ -DIRAM='__attribute__((section(".fast.text")))' \
             -DNOINSTR='__attribute__((no_instrument_function))' \
-- 
GitLab