cesanta / mongoose-os

Mongoose OS - an IoT Firmware Development Framework. Supported microcontrollers: ESP32, ESP8266, CC3220, CC3200, STM32F4, STM32L4, STM32F7. Amazon AWS IoT, Microsoft Azure, Google IoT Core integrated. Code in C or JavaScript.
https://mongoose-os.com
Other
2.49k stars 430 forks source link

Option to use REF_TICK for uart clock source on ESP32 (corrupted baud rate with DFS) #511

Closed pwiner99 closed 3 years ago

pwiner99 commented 4 years ago

When dynamic frequency scaling (DFS) is enabled on ESP32 the APB clock frequency feeding the uart can change resulting in incorrect baud rate. This problem can be seen by setting CONFIG_PM_ENABLE=y CONFIG_PM_DFS_INIT_AUTO=y in the build vars for ESP_IDF_SDKCONFIG_OPTS.

Suggest this line https://github.com/cesanta/mongoose-os/blob/787ac38aae90231b9db9c71cf19dfc617ca58166/platforms/esp32/src/esp32_uart.c#L407

be modified to something like:

uint32_t conf0 = cfg->dev.use_ref_tick ? 0 : UART_TICK_REF_ALWAYS_ON;

where use_ref_tick is added to the esp hal and exposed with a config-schema.

rojer commented 4 years ago

REF_TICK is only 1 MHz though, and UART required at least 1x divider, so it's only suitable for low baud rates

Harvie commented 3 years ago

in deps/modules/mongoose-os/platforms/esp32/src/esp32_uart.c i changed on line 429:

uint32_t conf0 = UART_TICK_REF_ALWAYS_ON; to uint32_t conf0 = 1;

Now the baudrate is incorrect, so mos console shows only whitespaces.

Perhaps something else has to be reconfigured as well...

rojer commented 3 years ago

fixed in https://github.com/cesanta/mongoose-os/commit/5b937f05a949c6227b6a17267533f490f4b8c661. baud rates up to 460800 verified to be working with ref_tick, for higher rates we fall back to APB.

Harvie commented 3 years ago

Perfect! Thank you :-)

Harvie commented 3 years ago

I've just tested and it fixed the problem. Here is easy way to test it in any mongoose project:

curl 'https://raw.githubusercontent.com/cesanta/mongoose-os/master/platforms/esp32/src/esp32_uart.c' > ./deps/modules/mongoose-os/platforms/esp32/src/esp32_uart.c

#revert back to 2.18
curl 'https://raw.githubusercontent.com/cesanta/mongoose-os/2.18.0/platforms/esp32/src/esp32_uart.c' > ./deps/modules/mongoose-os/platforms/esp32/src/esp32_uart.c