Commit 0b2e474d authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Improve ESP flasher baud rate setting

Use old baud rate and autodetected divider to calculate new divider.

This works better with higher baud rates and drifting clocks.

CL: mos: Improve ESP flasher baud rate setting

PUBLISHED_FROM=7761e756c3ae7cc76e3b9a4fe3e40d06a779b2e8
parent 95a36b17
......@@ -25,7 +25,7 @@
#if defined(ESP8266)
#include "eagle_soc.h"
#include "ets_sys.h"
#include "../../../miniz.c"
#include "miniz.c"
#elif defined(ESP32)
#include "rom/efuse.h"
#include "rom/miniz.h"
......@@ -38,7 +38,7 @@
#include "uart.h"
/* Param: baud rate. */
uint32_t params[1] __attribute__((section(".params")));
uint32_t params[2] __attribute__((section(".params")));
#define FLASH_BLOCK_SIZE 65536
#define FLASH_SECTOR_SIZE 4096
......@@ -477,8 +477,7 @@ uint8_t cmd_loop(void) {
}
void stub_main1(void) {
uint32_t baud_rate = params[0];
uint32_t greeting = 0x4941484f; /* OHAI */
uint32_t old_baud_rate = params[0], new_baud_rate = params[1];
uint8_t last_cmd;
/* This points at us right now, reset for next boot. */
......@@ -487,6 +486,7 @@ void stub_main1(void) {
/* Selects SPI functions for flash pins. */
#if defined(ESP8266)
SelectSpiFunction();
spi_flash_attach();
SET_PERI_REG_MASK(0x3FF00014, 1); /* Switch to 160 MHz */
#elif defined(ESP32)
esp_rom_spiflash_attach(ets_efuse_get_spiconfig(), 0 /* legacy */);
......@@ -496,9 +496,10 @@ void stub_main1(void) {
0 /* deviceId */, 16 * 1024 * 1024 /* chip_size */, FLASH_BLOCK_SIZE,
FLASH_SECTOR_SIZE, FLASH_PAGE_SIZE, 0xffff /* status_mask */);
if (baud_rate > 0) {
uint32_t old_div = 0;
if (new_baud_rate > 0) {
ets_delay_us(10000);
set_baud_rate(0, baud_rate);
old_div = set_baud_rate(0, old_baud_rate, new_baud_rate);
}
/* Give host time to get ready too. */
......@@ -509,7 +510,7 @@ void stub_main1(void) {
WRITE_PERI_REG(UART_FIFO_REG(0), 0x55);
}
#else
SLIP_send(&greeting, 4);
SLIP_send(&old_div, 4);
#endif
last_cmd = cmd_loop();
......@@ -525,7 +526,7 @@ void stub_main1(void) {
* then jumps to 0x4000108a, then checks strapping bits again (which will
* not have changed), and then proceeds to 0x400010a8.
*/
volatile uint32_t *sp = &baud_rate;
volatile uint32_t *sp = &old_baud_rate;
while (*sp != (uint32_t) 0x40001100) sp++;
*sp = 0x400010a8;
/*
......
......@@ -42,5 +42,10 @@ SECTIONS {
INCLUDE "components/esp32/ld/esp32.rom.ld"
INCLUDE "components/esp32/ld/esp32.rom.spiram_incompatible_fns.ld"
PROVIDE(ets_isr_mask = 0x400067fc);
PROVIDE(ets_isr_unmask = 0x40006808);
PROVIDE(MD5Init = 0x4005da7c);
PROVIDE(MD5Update = 0x4005da9c);
PROVIDE(MD5Final = 0x4005db1c);
PROVIDE(esp_rom_spiflash_attach = 0x40062a6c);
PROVIDE(esp_rom_spiflash_config_clk = 0x40062bc8);
......@@ -19,9 +19,13 @@
#include "rom_functions.h"
void set_baud_rate(uint32_t uart_no, uint32_t baud_rate) {
uint32_t master_freq = ets_get_detected_xtal_freq() << 4;
master_freq += (baud_rate / 2);
uint32_t div = master_freq / baud_rate;
uart_div_modify(uart_no, div);
uint32_t set_baud_rate(uint32_t uart_no, uint32_t old_baud_rate,
uint32_t new_baud_rate) {
uint32_t uart_reg = REG_READ(UART_CLKDIV_REG(uart_no));
uint32_t uart_div = uart_reg & UART_CLKDIV_M;
uint32_t fraction = (uart_reg >> UART_CLKDIV_FRAG_S) & UART_CLKDIV_FRAG_V;
uart_div = (uart_div << 4) + fraction;
uint32_t master_freq = uart_div * old_baud_rate;
uart_div_modify(uart_no, master_freq / new_baud_rate);
return uart_div;
}
......@@ -20,6 +20,7 @@
#include <stdint.h>
void set_baud_rate(uint32_t uart_no, uint32_t baud_rate);
uint32_t set_baud_rate(uint32_t uart_no, uint32_t old_baud_rate,
uint32_t new_baud_rate);
#endif /* CS_COMMON_PLATFORMS_ESP32_STUBS_UART_H_ */
......@@ -23,13 +23,13 @@ all: $(STUB_ELF)
$(STUB_ELF): $(STUB) $(LIBS)
@echo " CC $^ -> $@"
@[ -d $(BUILD_DIR) ] || mkdir $(BUILD_DIR)
@docker run --rm -i -v $(CURDIR)/../../../..:/src $(SDK) //bin/bash -c \
docker run --rm -i -v $(CURDIR)/../../../..:/src $(SDK) //bin/bash -c \
"cd /src/common/platforms/esp8266/stubs && \
$(XT_CC) -std=c99 -Wall -Werror -Os -DESP8266 \
-mtext-section-literals -mlongcalls -nostdlib -fno-builtin \
-I. -I/src/common/platforms/esp \
-I. -I/src/common/platforms/esp -I/src/common/platforms/esp8266 \
-I/opt/Espressif/ESP8266_SDK \
-Wl,-static -ffunction-sections -Wl,--gc-sections \
-Wl,-static -ffunction-sections -lgcc -Wl,--gc-sections \
-Tstub.ld $(CFLAGS) -o $@ $^"
wrap: $(STUB_JSON)
......
......@@ -41,3 +41,4 @@ INCLUDE "eagle.rom.addr.v6.ld"
PROVIDE(SPIFlashModeConfig = 0x40004568);
PROVIDE(SPI_erase_sector = 0x400040c0);
PROVIDE(SPI_erase_block = 0x40004120);
PROVIDE(uart_div_modify = 0x400039d8);
......@@ -18,9 +18,15 @@
#include "uart.h"
#include "ets_sys.h"
#define UART_CLKDIV_26MHZ(B) (52000000 + B / 2) / B
#define UART_CLKDIV_M (UART_CLKDIV_CNT << UART_CLKDIV_S)
void set_baud_rate(uint32_t uart_no, uint32_t baud_rate) {
uint32_t div = UART_CLKDIV_26MHZ(baud_rate);
WRITE_PERI_REG(UART_CLKDIV_REG(uart_no), div & 0xfffff);
void uart_div_modify(uint8_t uart_no, uint32_t div);
uint32_t set_baud_rate(uint32_t uart_no, uint32_t old_baud_rate,
uint32_t new_baud_rate) {
uint32_t uart_reg = READ_PERI_REG(UART_CLKDIV_REG(uart_no));
uint32_t uart_div = uart_reg & UART_CLKDIV_M;
uint32_t master_freq = uart_div * old_baud_rate;
uart_div_modify(uart_no, master_freq / new_baud_rate);
return uart_div;
}
......@@ -20,9 +20,11 @@
#include <stdint.h>
void set_baud_rate(uint32_t uart_no, uint32_t baud_rate);
#include "uart_register.h"
uint32_t set_baud_rate(uint32_t uart_no, uint32_t old_baud_rate,
uint32_t new_baud_rate);
#define REG_UART_BASE(i) (0x60000000 + (i) *0xf00)
#define UART_FIFO_REG(i) (REG_UART_BASE(i) + 0x0)
#define UART_CONF1_REG(i) (REG_UART_BASE(i) + 0x24)
#define UART_RX_TOUT_EN (BIT(31))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment