apache / mynewt-core

An OS to build, deploy and securely manage billions of devices
https://mynewt.apache.org/
Apache License 2.0
817 stars 366 forks source link

Wrong SystemCoreClock value in b-l072z-lrwan1 bsp #1829

Closed wnts closed 5 years ago

wnts commented 5 years ago

The bsp package for the b-l072z-lrwan1 board uses an incorrect value for the SystemCoreClock value. https://github.com/apache/mynewt-core/blob/a2f5fd1837c33fd088f66391ce047d82e495b901/hw/bsp/b-l072z-lrwan1/src/system_stm32l0xx.c#L126

The SystemCoreClock variable is used by the hal to set the baud rate: https://github.com/apache/mynewt-core/blob/a2f5fd1837c33fd088f66391ce047d82e495b901/hw/mcu/stm/stm32_common/src/hal_uart.c#L477-L485 This causes UART communication to fail, resulting in framing errors. I believe the https://github.com/apache/mynewt-core/blob/a2f5fd1837c33fd088f66391ce047d82e495b901/hw/bsp/b-l072z-lrwan1/src/system_stm32l0xx.c file originates in the STM32CubeL0 firmware package that is made available by ST. The newest version of the STM32CubeL0 package contains a template (STM32Cube_FW_L0_V1.11.2/Projects/STM32L073Z-EVAL/Templates/Src/system_stm32l0xx.c) for the system32_stm32l0xx.c file, containing:

uint32_t SystemCoreClock = 2097152U; /* 32.768 kHz * 2^6 */

The comment seems to be wrong though, but the value is consistent with the device's reference manual. From the reference manual of the STM32L0x2 microcontroller (STM DocID025941 Rev 5), section 7.2.3, "MSI Clock", p. 178:

The MSI clock signal is generated from an internal RC oscillator. Its frequency range can be adjusted by software by using the MSIRANGE[2:0] bits in the RCC_ICSCR register (see Section 7.3.2: Internal clock sources calibration register (RCC_ICSCR)). Seven frequency ranges are available: 65.536 kHz, 131.072 kHz, 262.144 kHz, 524.288 kHz, 1.048 MHz, 2.097 MHz (default value) and 4.194 MHz. The MSI clock is always used as system clock after restart from Reset and wake-up from Standby. After wake-up from Stop mode, the MSI clock can be selected as system clock instead of HSI16 (or HSI16/4).

I tried the console application from the blinky tutorial with the correct SystemCoreClock value, and the UART no longer glitches or displays framing errors.

A final remark: it seems to me that the hw/bsp/b-l072z-lrwan1/src/system_stm32l0xx.c file should not be under the bsp package in the source tree, since it contains no board specific code, but MCU specific code. I believe it should be at hw/mcu/stm/stm32l0xx/

utzig commented 5 years ago

Nice catch, thanks, I had framing errors as well, especially at 115200bps. I will see into updating STM32Cube L0 next week.

it seems to me that the hw/bsp/b-l072z-lrwan1/src/system_stm32l0xx.c file should not be under the bsp package in the source tree, since it contains no board specific code, but MCU specific code. I believe it should be at hw/mcu/stm/stm32l0xx

Sounds like an improvement, I'm gonna try it.