Skip to content

Commit b5caedb

Browse files
committed
tests/drivers/clock_control: stm32u5: Add a _devices test
Add a stm32u5_devices test which aims at testing devices clock control configuration on stm32u5 targets Signed-off-by: Erwan Gouriou <[email protected]>
1 parent 012bc18 commit b5caedb

File tree

9 files changed

+315
-0
lines changed

9 files changed

+315
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(stm32_clock_configuration_u5_devices)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2021 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* Warning: This overlay clears clocks back to a state equivalent to what could
9+
* be found in stm32h7.dtsi
10+
*/
11+
12+
13+
/* Clocks clean up config
14+
* Aim is to avoid conflict with specific default board configuration
15+
*/
16+
17+
&clk_hse {
18+
status = "disabled";
19+
/delete-property/ clock-frequency;
20+
/delete-property/ hse-bypass;
21+
};
22+
23+
&clk_hsi {
24+
status = "disabled";
25+
};
26+
27+
&clk_lse {
28+
status = "disabled";
29+
};
30+
31+
&clk_msis {
32+
status = "disabled";
33+
/delete-property/ msi-range;
34+
/delete-property/ msi-pll-mode;
35+
};
36+
37+
&clk_msik {
38+
status = "disabled";
39+
/delete-property/ msi-range;
40+
/delete-property/ msi-pll-mode;
41+
};
42+
43+
&pll1 {
44+
/delete-property/ div-m;
45+
/delete-property/ mul-n;
46+
/delete-property/ div-q;
47+
/delete-property/ div-r;
48+
/delete-property/ clocks;
49+
status = "disabled";
50+
};
51+
52+
&rcc {
53+
/delete-property/ clocks;
54+
/delete-property/ clock-frequency;
55+
/delete-property/ ahb-prescaler;
56+
/delete-property/ apb1-prescaler;
57+
/delete-property/ apb2-prescaler;
58+
/delete-property/ apb3-prescaler;
59+
};
60+
61+
&spi1 {
62+
pinctrl-0 = <&spi1_nss_pe12 &spi1_sck_pe13
63+
&spi1_miso_pe14 &spi1_mosi_pe15>;
64+
pinctrl-names = "default";
65+
status = "disabled";
66+
};
67+
68+
/* Core set up
69+
* Aim of this part is to provide a base working clock config
70+
*/
71+
72+
&clk_lse {
73+
status = "okay";
74+
};
75+
76+
&clk_msis {
77+
status = "okay";
78+
msi-range = <4>;
79+
msi-pll-mode;
80+
};
81+
82+
&pll1 {
83+
div-m = <1>;
84+
mul-n = <80>;
85+
div-q = <2>;
86+
div-r = <2>;
87+
clocks = <&clk_msis>;
88+
status = "okay";
89+
};
90+
91+
&rcc {
92+
clocks = <&pll1>;
93+
clock-frequency = <DT_FREQ_M(160)>;
94+
ahb-prescaler = <1>;
95+
apb1-prescaler = <1>;
96+
apb2-prescaler = <1>;
97+
apb3-prescaler = <1>;
98+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2021 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* Warning: This overlay performs configuration from clean sheet.
9+
* It is assumed that it is applied after core_init.overlay file.
10+
*/
11+
12+
&clk_hsi {
13+
status = "okay";
14+
};
15+
16+
&spi1 {
17+
/delete-property/ clocks;
18+
clocks = <&rcc STM32_CLOCK_BUS_APB2 0x00001000>,
19+
<&rcc STM32_SRC_HSI16 SPI1_SEL(2)>;
20+
status = "okay";
21+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2021 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* Warning: This overlay performs configuration from clean sheet.
9+
* It is assumed that it is applied after core_init.overlay file.
10+
*/
11+
12+
&clk_msik {
13+
msi-range = <4>;
14+
status = "okay";
15+
};
16+
17+
&spi1 {
18+
/delete-property/ clocks;
19+
clocks = <&rcc STM32_CLOCK_BUS_APB2 0x00001000>,
20+
<&rcc STM32_SRC_MSIK SPI1_SEL(3)>;
21+
status = "okay";
22+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2021 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* Warning: This overlay performs configuration from clean sheet.
9+
* It is assumed that it is applied after core_init.overlay file.
10+
*/
11+
12+
&spi1 {
13+
/delete-property/ clocks;
14+
clocks = <&rcc STM32_CLOCK_BUS_APB2 0x00001000>;
15+
status = "okay";
16+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2021 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* Warning: This overlay performs configuration from clean sheet.
9+
* It is assumed that it is applied after core_init.overlay file.
10+
*/
11+
12+
&spi1 {
13+
/delete-property/ clocks;
14+
clocks = <&rcc STM32_CLOCK_BUS_APB2 0x00001000>,
15+
<&rcc STM32_SRC_SYSCLK SPI1_SEL(1)>;
16+
status = "okay";
17+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2022 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ztest.h>
8+
#include <soc.h>
9+
#include <drivers/clock_control.h>
10+
#include <drivers/clock_control/stm32_clock_control.h>
11+
#include <logging/log.h>
12+
LOG_MODULE_REGISTER(test);
13+
14+
#define DT_DRV_COMPAT st_stm32_spi
15+
16+
#if STM32_DT_INST_DEV_OPT_CLOCK_SUPPORT
17+
#define STM32_SPI_OPT_CLOCK_SUPPORT 1
18+
#else
19+
#define STM32_SPI_OPT_CLOCK_SUPPORT 0
20+
#endif
21+
22+
#define DT_NO_CLOCK 0xFFFFU
23+
24+
/* Not device related, but keep it to ensure core clock config is correct */
25+
static void test_sysclk_freq(void)
26+
{
27+
uint32_t soc_sys_clk_freq;
28+
29+
soc_sys_clk_freq = HAL_RCC_GetSysClockFreq();
30+
31+
zassert_equal(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC, soc_sys_clk_freq,
32+
"Expected sysclockfreq: %d. Actual sysclockfreq: %d",
33+
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC, soc_sys_clk_freq);
34+
}
35+
36+
static void test_spi_clk_config(void)
37+
{
38+
static const struct stm32_pclken pclken[] = STM32_DT_CLOCKS(spi1);
39+
40+
uint32_t spi1_actual_clk_src;
41+
uint32_t spi1_dt_clk_freq, spi1_actual_clk_freq;
42+
int r;
43+
44+
/* Test clock_on(reg_clk) */
45+
r = clock_control_on(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
46+
(clock_control_subsys_t) &pclken[0]);
47+
zassert_true((r == 0), "Could not enable SPI gating clock");
48+
49+
zassert_true(__HAL_RCC_SPI1_IS_CLK_ENABLED(), "SPI1 gating clock should be on");
50+
TC_PRINT("SPI1 gating clock on\n");
51+
52+
if (IS_ENABLED(STM32_SPI_OPT_CLOCK_SUPPORT) && DT_NUM_CLOCKS(DT_NODELABEL(spi1)) > 1) {
53+
/* Test clock_on(alt source) */
54+
r = clock_control_configure(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
55+
(clock_control_subsys_t) &pclken[1],
56+
NULL);
57+
zassert_true((r == 0), "Could not configure SPI source clk");
58+
TC_PRINT("SPI1 clk source configured\n");
59+
60+
/* Test clk source */
61+
spi1_actual_clk_src = __HAL_RCC_GET_SPI1_SOURCE();
62+
63+
if (pclken[1].bus == STM32_SRC_HSI16) {
64+
zassert_equal(spi1_actual_clk_src, RCC_SPI1CLKSOURCE_HSI,
65+
"Expected SPI src: HSI (%d). Actual SPI src: %d",
66+
RCC_SPI1CLKSOURCE_HSI, spi1_actual_clk_src);
67+
} else if (pclken[1].bus == STM32_SRC_SYSCLK) {
68+
zassert_equal(spi1_actual_clk_src, RCC_SPI1CLKSOURCE_SYSCLK,
69+
"Expected SPI src: SYSCLK (%d). Actual SPI src: %d",
70+
RCC_SPI1CLKSOURCE_SYSCLK, spi1_actual_clk_src);
71+
} else {
72+
zassert_true(1, "Unexpected clk src(%d)", spi1_actual_clk_src);
73+
}
74+
75+
/* Test get_rate(source clk) */
76+
r = clock_control_get_rate(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
77+
(clock_control_subsys_t) &pclken[1],
78+
&spi1_dt_clk_freq);
79+
zassert_true((r == 0), "Could not get SPI clk freq");
80+
81+
spi1_actual_clk_freq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI1);
82+
zassert_equal(spi1_dt_clk_freq, spi1_actual_clk_freq,
83+
"Expected SPI clk: (%d). Actual SPI clk: %d",
84+
spi1_dt_clk_freq, spi1_actual_clk_freq);
85+
} else {
86+
/* No alt clock available, get rate from gating clock */
87+
88+
/* Test get_rate(gating clock) */
89+
r = clock_control_get_rate(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
90+
(clock_control_subsys_t) &pclken[0],
91+
&spi1_dt_clk_freq);
92+
zassert_true((r == 0), "Could not get SPI pclk freq");
93+
94+
spi1_actual_clk_freq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SPI1);
95+
zassert_equal(spi1_dt_clk_freq, spi1_actual_clk_freq,
96+
"Expected SPI clk: (%d). Actual SPI clk: %d",
97+
spi1_dt_clk_freq, spi1_actual_clk_freq);
98+
}
99+
100+
/* Test clock_off(gating clock) */
101+
r = clock_control_off(DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE),
102+
(clock_control_subsys_t) &pclken[0]);
103+
zassert_true((r == 0), "Could not disable SPI reg_clk");
104+
105+
zassert_true(!__HAL_RCC_SPI1_IS_CLK_ENABLED(), "SPI1 gating clock should be off");
106+
TC_PRINT("SPI1 gating clock off\n");
107+
108+
/* Test clock_off(source clk) */
109+
/* Not supported today */
110+
}
111+
112+
void test_main(void)
113+
{
114+
ztest_test_suite(test_stm32u5_devices_clocks,
115+
ztest_unit_test(test_sysclk_freq),
116+
ztest_unit_test(test_spi_clk_config)
117+
);
118+
ztest_run_test_suite(test_stm32u5_devices_clocks);
119+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
common:
2+
timeout: 5
3+
platform_allow: b_u585i_iot02a
4+
tests:
5+
drivers.stm32_clock_configuration.dev_u5.spi1_pclk2:
6+
extra_args: DTC_OVERLAY_FILE="boards/core_init.overlay;boards/spi1_pclk2.overlay"
7+
drivers.stm32_clock_configuration.dev_u5.spi1_hsi_16:
8+
extra_args: DTC_OVERLAY_FILE="boards/core_init.overlay;boards/spi1_hsi_16.overlay"
9+
drivers.stm32_clock_configuration.dev_u5.spi1_msik:
10+
extra_args: DTC_OVERLAY_FILE="boards/core_init.overlay;boards/spi1_msik.overlay"
11+
drivers.stm32_clock_configuration.dev_u5.spi1_sysclk:
12+
extra_args: DTC_OVERLAY_FILE="boards/core_init.overlay;boards/spi1_sysclk.overlay"

0 commit comments

Comments
 (0)