stm32l4xx + HSI48 Topic is solved

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
geoffrey.brown
Posts: 87
Joined: Thu May 07, 2015 9:47 pm
Has thanked: 3 times
Been thanked: 15 times

stm32l4xx + HSI48  Topic is solved

Postby geoffrey.brown » Tue May 23, 2017 2:44 am

The support for using the HSI48 with USB is slightly broken (there's also no support for CRS, but that's not a big deal). The problem is that in the hal_lld.h file, there's no way to select the HSI48 clock as the source for STM32_48CLK -- here's the relevant code:

Code: Select all

/**
 * @brief   48MHz clock frequency.
 */
#if (STM32_CLK48SEL == STM32_CLK48SEL_NOCLK) || defined(__DOXYGEN__)
#define STM32_48CLK                 0
#elif STM32_CLK48SEL == STM32_CLK48SEL_PLLSAI1
#define STM32_48CLK                 (STM32_PLLSAI1VCO / STM32_PLLSAI1Q_VALUE)
#elif STM32_CLK48SEL == STM32_CLK48SEL_PLL
#define STM32_48CLK                 (STM32_PLLVCO / STM32_PLLQ_VALUE)
#elif STM32_CLK48SEL == STM32_CLK48SEL_MSI
#define STM32_48CLK                 STM32_MSICLK
#else
#error "invalid source selected for 48CLK clock"
#endif
#define STM32_USBCLK                STM32_48CLK


..........
In contrast, the code hal code for the stm32f0xx does provide such a method:

Code: Select all

/**
 * @brief   USB frequency.
 */
#if (STM32_USBSW == STM32_USBSW_HSI48) || defined(__DOXYGEN__)
#define STM32_USBCLK                STM32_HSI48CLK
#elif STM32_USBSW == STM32_USBSW_PCLK
#define STM32_USBCLK                STM32_PLLCLKOUT
#else
#error "invalid source selected for USB clock"
#endif


...........

I wouldn't mind, but I couldn't find a workaround short of hacking the hal_lld.h file which I hate to do. BTW, with a little work, the CRS + USB combo works quite well on the STM32L432.

Geoffrey Brown

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: stm32l4xx + HSI48

Postby Giovanni » Tue May 23, 2017 8:45 am

Hi,

Moving this to "bug reports", if you have a patch please post it here.

Giovanni

geoffrey.brown
Posts: 87
Joined: Thu May 07, 2015 9:47 pm
Has thanked: 3 times
Been thanked: 15 times

Re: stm32l4xx + HSI48

Postby geoffrey.brown » Tue May 23, 2017 2:08 pm

Here is a patch that does two things:

1) Adds the necessary definitions to STM32L4xx/hal_lld.h
2) If defined, initialized the HSI48 clock in STM32L4xx/hal_lld.c

I didn't attempt to add the CRS code -- it has a number of possible configurations.

diff --git a/os/hal/ports/STM32/STM32L4xx/hal_lld.c b/os/hal/ports/STM32/STM32L4xx/hal_lld.c
index f02d2a3..3196a49 100644
--- a/os/hal/ports/STM32/STM32L4xx/hal_lld.c
+++ b/os/hal/ports/STM32/STM32L4xx/hal_lld.c
@@ -188,6 +188,14 @@ void stm32_clock_init(void) {
; /* Wait until HSI is stable. */
#endif

+#if STM32_HSI48_ENABLED
+ /* HSI48 activation.*/
+ RCC->CCIPR |= RCC_CCIPR_CLK48SEL;
+ RCC->CRRCR |= RCC_CRRCR_HSI48ON;
+ while (!(RCC->CRRCR & RCC_CRRCR_HSI48RDY))
+ ; /* Waits until HSI48 is stable. */
+#endif
+
#if STM32_HSE_ENABLED
#if defined(STM32_HSE_BYPASS)
/* HSE Bypass.*/
diff --git a/os/hal/ports/STM32/STM32L4xx/hal_lld.h b/os/hal/ports/STM32/STM32L4xx/hal_lld.h
index d78b157..5c4701f 100644
--- a/os/hal/ports/STM32/STM32L4xx/hal_lld.h
+++ b/os/hal/ports/STM32/STM32L4xx/hal_lld.h
@@ -72,6 +72,7 @@
* @{
*/
#define STM32_HSI16CLK 16000000 /**< High speed internal clock. */
+#define STM32_HSI48CLK 48000000 /**< 48MHz speed internal clock.*/
#define STM32_LSICLK 32000 /**< Low speed internal clock. */
/** @} */

@@ -362,6 +363,13 @@
#endif

/**
+ * @brief Enables or disables the HSI48 clock source.
+ */
+#if !defined(STM32_HSI48_ENABLED) || defined(__DOXYGEN__)
+#define STM32_HSI48_ENABLED FALSE
+#endif
+
+/**
* @brief Enables or disables the LSI clock source.
*/
#if !defined(STM32_LSI_ENABLED) || defined(__DOXYGEN__)
@@ -1963,8 +1971,13 @@
/**
* @brief 48MHz clock frequency.
*/
+
#if (STM32_CLK48SEL == STM32_CLK48SEL_NOCLK) || defined(__DOXYGEN__)
+#if (STM32_HSI48_ENABLED == TRUE)
+#define STM32_48CLK STM32_HSI48CLK
+#else
#define STM32_48CLK 0
+#endif
#elif STM32_CLK48SEL == STM32_CLK48SEL_PLLSAI1
#define STM32_48CLK (STM32_PLLSAI1VCO / STM32_PLLSAI1Q_VALUE)
#elif STM32_CLK48SEL == STM32_CLK48SEL_PLL

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: stm32l4xx + HSI48

Postby Giovanni » Mon May 29, 2017 10:46 am

bump

User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Location: Munich (Germany)
Has thanked: 83 times
Been thanked: 67 times
Contact:

Re: stm32l4xx + HSI48

Postby RoccoMarco » Fri Jun 02, 2017 10:49 pm

Hi,
are you working on trunk?

STM32L4 has not something called HSI48 but MSI48. It is supported is currently used in USB-CDC demo as USB clock source. I reworked whole clock three six months ago with Giovanni.

This is the mcuconf os chibios_trunk\teshal\STM32\STM32L4xx\USB-CDC

/*
* HAL driver system settings.
*/

Code: Select all

#define STM32_NO_INIT                       FALSE
#define STM32_VOS                           STM32_VOS_RANGE1
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0
#define STM32_HSI16_ENABLED                 TRUE
#define STM32_LSI_ENABLED                   TRUE
#define STM32_HSE_ENABLED                   FALSE
#define STM32_LSE_ENABLED                   TRUE
#define STM32_MSIPLL_ENABLED                TRUE
#define STM32_ADC_CLOCK_ENABLED             TRUE
#define STM32_USB_CLOCK_ENABLED             TRUE
#define STM32_SAI1_CLOCK_ENABLED            TRUE
#define STM32_SAI2_CLOCK_ENABLED            TRUE
#define STM32_MSIRANGE                      STM32_MSIRANGE_48M
#define STM32_MSISRANGE                     STM32_MSISRANGE_4M
#define STM32_SW                            STM32_SW_PLL
#define STM32_PLLSRC                        STM32_PLLSRC_HSI16
#define STM32_PLLM_VALUE                    4
#define STM32_PLLN_VALUE                    80
#define STM32_PLLP_VALUE                    7
#define STM32_PLLQ_VALUE                    6
#define STM32_PLLR_VALUE                    4
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE1                         STM32_PPRE1_DIV1
#define STM32_PPRE2                         STM32_PPRE2_DIV1
#define STM32_STOPWUCK                      STM32_STOPWUCK_MSI
#define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
#define STM32_MCOPRE                        STM32_MCOPRE_DIV1
#define STM32_LSCOSEL                       STM32_LSCOSEL_NOCLOCK
#define STM32_PLLSAI1N_VALUE                72
#define STM32_PLLSAI1P_VALUE                7
#define STM32_PLLSAI1Q_VALUE                6
#define STM32_PLLSAI1R_VALUE                6
#define STM32_PLLSAI2N_VALUE                72
#define STM32_PLLSAI2P_VALUE                7
#define STM32_PLLSAI2R_VALUE                6
#define STM32_USART1SEL                     STM32_USART1SEL_SYSCLK
#define STM32_USART2SEL                     STM32_USART2SEL_SYSCLK
#define STM32_USART3SEL                     STM32_USART3SEL_SYSCLK
#define STM32_UART4SEL                      STM32_UART4SEL_SYSCLK
#define STM32_UART5SEL                      STM32_UART5SEL_SYSCLK
#define STM32_LPUART1SEL                    STM32_LPUART1SEL_SYSCLK
#define STM32_I2C1SEL                       STM32_I2C1SEL_SYSCLK
#define STM32_I2C2SEL                       STM32_I2C2SEL_SYSCLK
#define STM32_I2C3SEL                       STM32_I2C3SEL_SYSCLK
#define STM32_LPTIM1SEL                     STM32_LPTIM1SEL_PCLK1
#define STM32_LPTIM2SEL                     STM32_LPTIM2SEL_PCLK1
#define STM32_SAI1SEL                       STM32_SAI1SEL_OFF
#define STM32_SAI2SEL                       STM32_SAI2SEL_OFF
#define STM32_CLK48SEL                      STM32_CLK48SEL_MSI
#define STM32_ADCSEL                        STM32_ADCSEL_SYSCLK
#define STM32_SWPMI1SEL                     STM32_SWPMI1SEL_PCLK1
#define STM32_DFSDMSEL                      STM32_DFSDMSEL_PCLK1
#define STM32_RTCSEL                        STM32_RTCSEL_LSI


This is guide to switch on trunk and update it:
How to switch to the development version of ChibiOS.

I am going to mark this as solved but feel free to reply for any concern...
Ciao,
RM

geoffrey.brown
Posts: 87
Joined: Thu May 07, 2015 9:47 pm
Has thanked: 3 times
Been thanked: 15 times

Re: stm32l4xx + HSI48

Postby geoffrey.brown » Mon Jun 05, 2017 1:06 pm

I think you may have missed the fundamental point -- the code that enables the selection of the USB clock source is incomplete -- it does not allow selection of the dedicated internal 48Mhz oscillator (whatever you've called it in the code).

Geoffrey

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: stm32l4xx + HSI48

Postby Giovanni » Mon Jun 05, 2017 2:03 pm

Reopened...

Giovanni

User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Location: Munich (Germany)
Has thanked: 83 times
Been thanked: 67 times
Contact:

Re: stm32l4xx + HSI48

Postby RoccoMarco » Wed Jun 07, 2017 9:28 pm

Hi,
to clock the USB you just have to enable MSI and set MSI range as 48 MHz, then select CKL48 source as MSI
2017-06-07 22_23_30-Reference manual STM32L4x6 - PDF-XChange Editor.png


in mcuconf this is done in this way:

Code: Select all

#define STM32_MSIRANGE                      STM32_MSIRANGE_48M

+

Code: Select all

#define STM32_CLK48SEL                      STM32_CLK48SEL_MSI


am I missing something?
Ciao,
RM

User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Location: Munich (Germany)
Has thanked: 83 times
Been thanked: 67 times
Contact:

Re: stm32l4xx + HSI48

Postby RoccoMarco » Thu Jun 08, 2017 6:46 am

Ok,
I have figure out the problem. You are talking about STM32L4x2 which is not completely supported.

I was looking at STM32L4x7 RM which doesn't has HSI48.

We will add support for this new device.
Ciao,
RM

rvense
Posts: 2
Joined: Wed Sep 02, 2015 1:09 pm

Re: stm32l4xx + HSI48

Postby rvense » Tue Jun 27, 2017 5:29 pm

Is availability of hardware a limiting factor in this? I have some self-made boards with STM32L433CC and would be very happy to send one to Italy at my expense. USB powered, some LEDs, and most other I/O broken out - Kicad files here. The 48-pin F042 and L433 are compatible and I have a simple LED blinking example running on boards populated with an L433, but would like to see more complete support for this chip, but am not very familiar with ChibiOS internals.


Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 32 guests