From 0a5d07c3914045e7ca8554250b7ad14deffa1256 Mon Sep 17 00:00:00 2001 From: Dylan Laduranty Date: Thu, 6 Jul 2023 15:41:34 +0200 Subject: [PATCH 1/2] boards/nrfxx: update LFCLK selection to be more generic Signed-off-by: Dylan Laduranty --- boards/common/e104-bt50xxa-tb/include/periph_conf.h | 7 ++++++- boards/common/nrf51/include/cfg_clock_16_0.h | 8 +++++--- boards/common/nrf51/include/cfg_clock_16_1.h | 8 +++++--- boards/common/nrf52/include/cfg_clock_32_0.h | 8 +++++--- boards/common/nrf52/include/cfg_clock_32_1.h | 8 +++++--- boards/nrf5340dk-app/include/board.h | 9 +++++++-- boards/nrf9160dk/include/board.h | 6 ++++-- 7 files changed, 37 insertions(+), 17 deletions(-) diff --git a/boards/common/e104-bt50xxa-tb/include/periph_conf.h b/boards/common/e104-bt50xxa-tb/include/periph_conf.h index 3a6a4b1ca961..4dde7a8e2274 100644 --- a/boards/common/e104-bt50xxa-tb/include/periph_conf.h +++ b/boards/common/e104-bt50xxa-tb/include/periph_conf.h @@ -34,7 +34,12 @@ extern "C" { * @{ */ #define CLOCK_HFCLK (1) /* external crystal */ -#define CLOCK_LFCLK (0) /* internal RC oscillator */ + +/* LFCLK Source clock selection:*/ +/* - CLOCK_LFCLKSRC_SRC_RC: internal RC oscillator + * - CLOCK_LFCLKSRC_SRC_Xtal: 32.768 kHz crystal + * - CLOCK_LFCLKSRC_SRC_Synth: derived from HFCLK */ +#define CLOCK_LFCLK (CLOCK_LFCLKSRC_SRC_RC) /**< LFCLK Source */ /** @} */ /** diff --git a/boards/common/nrf51/include/cfg_clock_16_0.h b/boards/common/nrf51/include/cfg_clock_16_0.h index 28afb15eecaa..1f3fad603568 100644 --- a/boards/common/nrf51/include/cfg_clock_16_0.h +++ b/boards/common/nrf51/include/cfg_clock_16_0.h @@ -35,9 +35,11 @@ #define CLOCK_HFCLK (16U) /* set to 0: internal RC oscillator 16: 16MHz crystal 32: 32MHz crystal */ -#define CLOCK_LFCLK (0) /* set to 0: internal RC oscillator - * 1: 32.768 kHz crystal - * 2: derived from HFCLK */ +/* LFCLK Source clock selection:*/ +/* - CLOCK_LFCLKSRC_SRC_RC: internal RC oscillator + * - CLOCK_LFCLKSRC_SRC_Xtal: 32.768 kHz crystal + * - CLOCK_LFCLKSRC_SRC_Synth: derived from HFCLK */ +#define CLOCK_LFCLK (CLOCK_LFCLKSRC_SRC_RC) /**< LFCLK Source */ /** @} */ #ifdef __cplusplus diff --git a/boards/common/nrf51/include/cfg_clock_16_1.h b/boards/common/nrf51/include/cfg_clock_16_1.h index 340e682a4968..b888c44acc83 100644 --- a/boards/common/nrf51/include/cfg_clock_16_1.h +++ b/boards/common/nrf51/include/cfg_clock_16_1.h @@ -35,9 +35,11 @@ #define CLOCK_HFCLK (16U) /* set to 0: internal RC oscillator 16: 16MHz crystal 32: 32MHz crystal */ -#define CLOCK_LFCLK (1) /* set to 0: internal RC oscillator - * 1: 32.768 kHz crystal - * 2: derived from HFCLK */ +/* LFCLK Source clock selection:*/ +/* - CLOCK_LFCLKSRC_SRC_RC: internal RC oscillator + * - CLOCK_LFCLKSRC_SRC_Xtal: 32.768 kHz crystal + * - CLOCK_LFCLKSRC_SRC_Synth: derived from HFCLK */ +#define CLOCK_LFCLK (CLOCK_LFCLKSRC_SRC_Xtal) /**< LFCLK Source */ /** @} */ #ifdef __cplusplus diff --git a/boards/common/nrf52/include/cfg_clock_32_0.h b/boards/common/nrf52/include/cfg_clock_32_0.h index e1278f2ff441..292f8dd3f98b 100644 --- a/boards/common/nrf52/include/cfg_clock_32_0.h +++ b/boards/common/nrf52/include/cfg_clock_32_0.h @@ -33,9 +33,11 @@ extern "C" { */ #define CLOCK_HFCLK (32U) /* set to 0: internal RC oscillator * 32: 32MHz crystal */ -#define CLOCK_LFCLK (0) /* set to 0: internal RC oscillator - * 1: 32.768 kHz crystal - * 2: derived from HFCLK */ +/* LFCLK Source clock selection:*/ +/* - CLOCK_LFCLKSRC_SRC_RC: internal RC oscillator + * - CLOCK_LFCLKSRC_SRC_Xtal: 32.768 kHz crystal + * - CLOCK_LFCLKSRC_SRC_Synth: derived from HFCLK */ +#define CLOCK_LFCLK (CLOCK_LFCLKSRC_SRC_RC) /**< LFCLK Source */ /** @} */ #ifdef __cplusplus diff --git a/boards/common/nrf52/include/cfg_clock_32_1.h b/boards/common/nrf52/include/cfg_clock_32_1.h index 5ad7f2c5e86d..73b73814d320 100644 --- a/boards/common/nrf52/include/cfg_clock_32_1.h +++ b/boards/common/nrf52/include/cfg_clock_32_1.h @@ -33,9 +33,11 @@ extern "C" { */ #define CLOCK_HFCLK (32U) /* set to 0: internal RC oscillator * 32: 32MHz crystal */ -#define CLOCK_LFCLK (1) /* set to 0: internal RC oscillator - * 1: 32.768 kHz crystal - * 2: derived from HFCLK */ +/* LFCLK Source clock selection:*/ +/* - CLOCK_LFCLKSRC_SRC_RC: internal RC oscillator + * - CLOCK_LFCLKSRC_SRC_Xtal: 32.768 kHz crystal + * - CLOCK_LFCLKSRC_SRC_Synth: derived from HFCLK */ +#define CLOCK_LFCLK (CLOCK_LFCLKSRC_SRC_Xtal) /**< LFCLK Source */ /** @} */ #ifdef __cplusplus diff --git a/boards/nrf5340dk-app/include/board.h b/boards/nrf5340dk-app/include/board.h index 445946e24868..199b25182ed2 100644 --- a/boards/nrf5340dk-app/include/board.h +++ b/boards/nrf5340dk-app/include/board.h @@ -31,7 +31,7 @@ extern "C" { #define CLOCK_CORECLOCK MHZ(128) /** - * @name HF Clock configuration + * @name Clock configuration * * * @{ @@ -40,7 +40,12 @@ extern "C" { /* CLOCK_HFCLKSRC_SRC_HFXO to use external 32MHz crystal * CLOCK_HFCLKSRC_SRC_HFINT to use internal crystal */ #define CLOCK_HFCLK (CLOCK_HFCLKSRC_SRC_HFXO) /**< HFCLK Source selection */ -#define CLOCK_LFCLK (3) /**< LFCLK Source selection */ + +/* LFCLK Source clock selection:*/ +/* - CLOCK_LFCLKSRC_SRC_LFRC: 32.768 kHz RC oscillator + * - CLOCK_LFCLKSRC_SRC_LFXO: 32.768 kHz crystal oscillator + * - CLOCK_LFCLKSRC_SRC_LFSYNT: 32.768 kHz synthesized from HFCLK*/ +#define CLOCK_LFCLK (CLOCK_LFCLKSRC_SRC_LFXO) /**< LFCLK Source */ /** @} */ /** diff --git a/boards/nrf9160dk/include/board.h b/boards/nrf9160dk/include/board.h index d6d56cee9771..8010a4deeb45 100644 --- a/boards/nrf9160dk/include/board.h +++ b/boards/nrf9160dk/include/board.h @@ -34,8 +34,10 @@ extern "C" { */ #define CLOCK_HFCLK (32U) /**< set to 0: internal RC oscillator * 32: 32MHz crystal */ -#define CLOCK_LFCLK (3) /**< set to 0: internal RC oscillator - * 3: High Accuracy oscillator */ +/* LFCLK Source clock selection:*/ +/* - CLOCK_LFCLKSRC_SRC_LFRC: 32.768 kHz RC oscillator + * - CLOCK_LFCLKSRC_SRC_LFXO: 32.768 kHz crystal oscillator */ +#define CLOCK_LFCLK (CLOCK_LFCLKSRC_SRC_LFXO) /**< LFCLK Source */ /** @} */ /** From 1d0177dc311d44729380ad889278f1df2f92c558 Mon Sep 17 00:00:00 2001 From: Dylan Laduranty Date: Thu, 6 Jul 2023 15:47:22 +0200 Subject: [PATCH 2/2] cpu/nrfxx: simplify LFCLK source selection Signed-off-by: Dylan Laduranty --- cpu/nrf51/cpu.c | 9 +++++++++ cpu/nrf52/cpu.c | 9 +++++++++ cpu/nrf53/cpu.c | 9 +++++++++ cpu/nrf5x_common/clock.c | 23 ++++------------------- cpu/nrf9160/cpu.c | 8 ++++++++ 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/cpu/nrf51/cpu.c b/cpu/nrf51/cpu.c index aa6f4cadd3b9..8b91680b3cf6 100644 --- a/cpu/nrf51/cpu.c +++ b/cpu/nrf51/cpu.c @@ -25,6 +25,15 @@ #include "periph/init.h" #include "stdio_base.h" +/** + * @brief LFCLK Clock selection configuration guard +*/ +#if ((CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_RC) && \ + (CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_Xtal) && \ + (CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_Synth)) +#error "LFCLK init: CLOCK_LFCLK has invalid value" +#endif + /** * @brief Initialize the CPU, set IRQ priorities */ diff --git a/cpu/nrf52/cpu.c b/cpu/nrf52/cpu.c index 79e894de54f0..7f34a4d231ef 100644 --- a/cpu/nrf52/cpu.c +++ b/cpu/nrf52/cpu.c @@ -35,6 +35,15 @@ static bool ftpan_32(void); static bool ftpan_37(void); static bool ftpan_36(void); +/** + * @brief LFCLK Clock selection configuration guard +*/ +#if ((CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_RC) && \ + (CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_Xtal) && \ + (CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_Synth)) +#error "LFCLK init: CLOCK_LFCLK has invalid value" +#endif + /** * @brief Initialize the CPU, set IRQ priorities */ diff --git a/cpu/nrf53/cpu.c b/cpu/nrf53/cpu.c index 221807f59cea..77b8b6a74204 100644 --- a/cpu/nrf53/cpu.c +++ b/cpu/nrf53/cpu.c @@ -27,6 +27,15 @@ #include "stdio_base.h" #include "board.h" +/** + * @brief LFCLK Clock selection configuration guard +*/ +#if ((CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_LFRC) && \ + (CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_LFXO) && \ + (CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_LFSYNT)) +#error "LFCLK init: CLOCK_LFCLK has invalid value" +#endif + /** * @brief Initialize the CPU, set IRQ priorities */ diff --git a/cpu/nrf5x_common/clock.c b/cpu/nrf5x_common/clock.c index 63a340c8d9f6..8347b06101b9 100644 --- a/cpu/nrf5x_common/clock.c +++ b/cpu/nrf5x_common/clock.c @@ -23,23 +23,16 @@ #include "nrf_clock.h" #include "periph_conf.h" -/* make sure both clocks are configured */ +/* make HFCLK clock is configured */ #ifndef CLOCK_HFCLK #error "Clock init: CLOCK_HFCLK is not defined by your board!" #endif -#ifndef CLOCK_LFCLK -#error "Clock init: CLOCK_LFCLK is not defined by your board!" -#endif /* Add compatibility wrapper defines for nRF families with Cortex-M33 core */ #ifdef NRF_CLOCK_S #define NRF_CLOCK NRF_CLOCK_S #endif -#ifdef CLOCK_LFCLKSRC_SRC_LFRC -#define CLOCK_LFCLKSRC_SRC_RC CLOCK_LFCLKSRC_SRC_LFRC -#endif - static unsigned _hfxo_requests = 0; void clock_init_hf(void) @@ -92,17 +85,9 @@ void clock_start_lf(void) return; } -#if (CLOCK_LFCLK == 0) - NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC); -#elif (CLOCK_LFCLK == 1) - NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal); -#elif (CLOCK_LFCLK == 2) - NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Synth); -#elif (CLOCK_LFCLK == 3) - NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_LFXO); -#else -#error "LFCLK init: CLOCK_LFCLK has invalid value" -#endif + /* Select LFCLK source */ + NRF_CLOCK->LFCLKSRC = CLOCK_LFCLK; + /* enable LF clock */ NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; NRF_CLOCK->TASKS_LFCLKSTART = 1; diff --git a/cpu/nrf9160/cpu.c b/cpu/nrf9160/cpu.c index e32b7b272e69..a61d54c8d17d 100644 --- a/cpu/nrf9160/cpu.c +++ b/cpu/nrf9160/cpu.c @@ -25,6 +25,14 @@ #include "periph/init.h" #include "stdio_base.h" +/** + * @brief LFCLK Clock selection configuration guard +*/ +#if ((CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_LFRC) && \ + (CLOCK_LFCLK != CLOCK_LFCLKSRC_SRC_LFXO)) +#error "LFCLK init: CLOCK_LFCLK has invalid value" +#endif + /** * @brief Initialize the CPU, set IRQ priorities */