From bae760ccffc12235bf5057b3f18632f01f3e07a9 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sat, 15 May 2021 13:39:41 +0000 Subject: [PATCH] Voltage regulator polling added, reorganization in hal.h. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@14377 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/hal/include/hal.h | 264 +++++++++++++------------ os/hal/ports/STM32/STM32G4xx/hal_lld.c | 16 +- 2 files changed, 149 insertions(+), 131 deletions(-) diff --git a/os/hal/include/hal.h b/os/hal/include/hal.h index 31d2918d7..08f92169d 100644 --- a/os/hal/include/hal.h +++ b/os/hal/include/hal.h @@ -29,6 +29,76 @@ #include "board.h" #include "halconf.h" +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/** + * @brief ChibiOS/HAL identification macro. + */ +#define __CHIBIOS_HAL__ + +/** + * @brief Stable release flag. + */ +#define CH_HAL_STABLE 0 + +/** + * @name ChibiOS/HAL version identification + * @{ + */ +/** + * @brief HAL version string. + */ +#define HAL_VERSION "7.2.0" + +/** + * @brief HAL version major number. + */ +#define CH_HAL_MAJOR 7 + +/** + * @brief HAL version minor number. + */ +#define CH_HAL_MINOR 2 + +/** + * @brief HAL version patch number. + */ +#define CH_HAL_PATCH 0 +/** @} */ + +/** + * @name Return codes + * @{ + */ +/** + * @brief HAL operation success. + */ +#define HAL_SUCCESS false +/** + * @brief HAL operation failed. + */ +#define HAL_FAILED true +/** @} */ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/* Configuration file checks.*/ +#if !defined(_CHIBIOS_HAL_CONF_) +#error "invalid configuration file" +#endif + +#if !defined(_CHIBIOS_HAL_CONF_VER_7_1_) +#error "obsolete or unknown configuration file" +#endif + /* Error checks on the configuration header file.*/ #if !defined(HAL_USE_PAL) #define HAL_USE_PAL FALSE @@ -118,136 +188,6 @@ #define HAL_USE_WSPI FALSE #endif -/* Low Level HAL support.*/ -#include "hal_lld.h" - -/* Abstract interfaces.*/ -#include "hal_objects.h" -#include "hal_streams.h" -#include "hal_channels.h" -#include "hal_files.h" -#include "hal_ioblock.h" -#include "hal_mmcsd.h" -#include "hal_persistent.h" -#include "hal_flash.h" - -/* Shared headers.*/ -#include "hal_buffers.h" -#include "hal_queues.h" - -/* Normal drivers.*/ -#include "hal_pal.h" -#include "hal_adc.h" -#include "hal_can.h" -#include "hal_crypto.h" -#include "hal_dac.h" -#include "hal_efl.h" -#include "hal_gpt.h" -#include "hal_i2c.h" -#include "hal_i2s.h" -#include "hal_icu.h" -#include "hal_mac.h" -#include "hal_pwm.h" -#include "hal_rtc.h" -#include "hal_serial.h" -#include "hal_sdc.h" -#include "hal_sio.h" -#include "hal_spi.h" -#include "hal_trng.h" -#include "hal_uart.h" -#include "hal_usb.h" -#include "hal_wdg.h" -#include "hal_wspi.h" - -/* - * The ST driver is a special case, it is only included if the OSAL is - * configured to require it. - */ -#if OSAL_ST_MODE != OSAL_ST_MODE_NONE -#include "hal_st.h" -#endif - -/* Complex drivers.*/ -#include "hal_mmc_spi.h" -#include "hal_serial_usb.h" - -/* Community drivers.*/ -#if defined(HAL_USE_COMMUNITY) || defined(__DOXYGEN__) -#if (HAL_USE_COMMUNITY == TRUE) || defined(__DOXYGEN__) -#include "hal_community.h" -#endif -#endif - -/*===========================================================================*/ -/* Driver constants. */ -/*===========================================================================*/ - -/** - * @brief ChibiOS/HAL identification macro. - */ -#define __CHIBIOS_HAL__ - -/** - * @brief Stable release flag. - */ -#define CH_HAL_STABLE 0 - -/** - * @name ChibiOS/HAL version identification - * @{ - */ -/** - * @brief HAL version string. - */ -#define HAL_VERSION "7.1.0" - -/** - * @brief HAL version major number. - */ -#define CH_HAL_MAJOR 7 - -/** - * @brief HAL version minor number. - */ -#define CH_HAL_MINOR 1 - -/** - * @brief HAL version patch number. - */ -#define CH_HAL_PATCH 0 -/** @} */ - -/** - * @name Return codes - * @{ - */ -/** - * @brief HAL operation success. - */ -#define HAL_SUCCESS false -/** - * @brief HAL operation failed. - */ -#define HAL_FAILED true -/** @} */ - -/*===========================================================================*/ -/* Driver pre-compile time settings. */ -/*===========================================================================*/ - -/*===========================================================================*/ -/* Derived constants and error checks. */ -/*===========================================================================*/ - -/* Configuration file checks.*/ -#if !defined(_CHIBIOS_HAL_CONF_) -#error "invalid configuration file" -#endif - -#if !defined(_CHIBIOS_HAL_CONF_VER_7_1_) -#error "obsolete or unknown configuration file" -#endif - /*===========================================================================*/ /* Driver data structures and types. */ /*===========================================================================*/ @@ -308,6 +248,70 @@ static inline halfreq_t halClockGetPointX(halclkpt_t clkpt) { } #endif /* defined(HAL_LLD_USE_CLOCK_MANAGEMENT) */ +/*===========================================================================*/ +/* Driver late inclusions. */ +/*===========================================================================*/ + +/* Low Level HAL support.*/ +#include "hal_lld.h" + +/* Abstract interfaces.*/ +#include "hal_objects.h" +#include "hal_streams.h" +#include "hal_channels.h" +#include "hal_files.h" +#include "hal_ioblock.h" +#include "hal_mmcsd.h" +#include "hal_persistent.h" +#include "hal_flash.h" + +/* Shared headers.*/ +#include "hal_buffers.h" +#include "hal_queues.h" + +/* Normal drivers.*/ +#include "hal_pal.h" +#include "hal_adc.h" +#include "hal_can.h" +#include "hal_crypto.h" +#include "hal_dac.h" +#include "hal_efl.h" +#include "hal_gpt.h" +#include "hal_i2c.h" +#include "hal_i2s.h" +#include "hal_icu.h" +#include "hal_mac.h" +#include "hal_pwm.h" +#include "hal_rtc.h" +#include "hal_serial.h" +#include "hal_sdc.h" +#include "hal_sio.h" +#include "hal_spi.h" +#include "hal_trng.h" +#include "hal_uart.h" +#include "hal_usb.h" +#include "hal_wdg.h" +#include "hal_wspi.h" + +/* + * The ST driver is a special case, it is only included if the OSAL is + * configured to require it. + */ +#if OSAL_ST_MODE != OSAL_ST_MODE_NONE +#include "hal_st.h" +#endif + +/* Complex drivers.*/ +#include "hal_mmc_spi.h" +#include "hal_serial_usb.h" + +/* Community drivers.*/ +#if defined(HAL_USE_COMMUNITY) || defined(__DOXYGEN__) +#if (HAL_USE_COMMUNITY == TRUE) || defined(__DOXYGEN__) +#include "hal_community.h" +#endif +#endif + #endif /* HAL_H */ /** @} */ diff --git a/os/hal/ports/STM32/STM32G4xx/hal_lld.c b/os/hal/ports/STM32/STM32G4xx/hal_lld.c index 809e036f2..bfa583317 100644 --- a/os/hal/ports/STM32/STM32G4xx/hal_lld.c +++ b/os/hal/ports/STM32/STM32G4xx/hal_lld.c @@ -436,9 +436,16 @@ bool hal_lld_clock_raw_switch(const halclkcfg_t *ccp) { PWR->CR3 = PWR_CR3_EIWF; PWR->CR4 = 0U; - /* Making sure HSI is activated.*/ + /* Waiting for all regulator status bits to be cleared, this means that + power levels are stable.*/ + while ((PWR->SR2 & (PWR_SR2_VOSF | PWR_SR2_REGLPF)) != 0U) { + /* Waiting for the regulator to be ready.*/ + } + + /* Making sure HSI16 is activated.*/ RCC->CR |= RCC_CR_HSION; while ((RCC->CR & RCC_CR_HSIRDY) == 0U) { + /* Waiting for HSI16 activation.*/ } /* Disabling boost mode.*/ @@ -490,6 +497,13 @@ bool hal_lld_clock_raw_switch(const halclkcfg_t *ccp) { PWR->CR4 = ccp->pwr_cr4; PWR->CR5 = ccp->pwr_cr5; + /* Wait on LPR bit clear.*/ + if ((ccp->pwr_cr1 & PWR_CR1_LPR) == 0U) { + while ((PWR->SR2 & PWR_SR2_REGLPF) != 0U) { + /* Waiting for the regulator to be ready.*/ + } + } + /* Switching to the final clock source.*/ RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_SW_Msk) | (ccp->rcc_cfgr & RCC_CFGR_SW_Msk); while ((RCC->CFGR & RCC_CFGR_SWS) != ((ccp->rcc_cfgr & RCC_CFGR_SW_Msk) << RCC_CFGR_SWS_Pos)) {