Skip to content

Commit

Permalink
mcu/nrf5340: Use Low Frequency Clock source setting API in startup
Browse files Browse the repository at this point in the history
Code from function hal_system_clock_start was moved into new API,
so now we can call that API with proper argument to avoid code
repetition.
  • Loading branch information
m-gorecki committed May 11, 2023
1 parent bfc7fa2 commit 9aad62f
Showing 1 changed file with 3 additions and 44 deletions.
47 changes: 3 additions & 44 deletions hw/mcu/nordic/nrf5340/src/hal_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ void
hal_system_clock_start(void)
{
#if MYNEWT_VAL(MCU_LFCLK_SOURCE)
uint32_t regmsk;
uint32_t regval;
uint32_t clksrc;

regmsk = CLOCK_LFCLKSTAT_STATE_Msk | CLOCK_LFCLKSTAT_SRC_Msk;
regval = CLOCK_LFCLKSTAT_STATE_Running << CLOCK_LFCLKSTAT_STATE_Pos;

#if MYNEWT_VAL_CHOICE(MCU_LFCLK_SOURCE, LFXO)

Expand All @@ -107,50 +101,15 @@ hal_system_clock_start(void)
NRF_P0->PIN_CNF[0] = GPIO_PIN_CNF_MCUSEL_Peripheral << GPIO_PIN_CNF_MCUSEL_Pos;
NRF_P0->PIN_CNF[1] = GPIO_PIN_CNF_MCUSEL_Peripheral << GPIO_PIN_CNF_MCUSEL_Pos;
#endif
regval |= CLOCK_LFCLKSTAT_SRC_LFXO << CLOCK_LFCLKSTAT_SRC_Pos;
clksrc = CLOCK_LFCLKSTAT_SRC_LFXO;
nrf5340_set_lf_clock_source(CLOCK_LFCLKSTAT_SRC_LFXO);
#elif MYNEWT_VAL_CHOICE(MCU_LFCLK_SOURCE, LFSYNTH)
regval |= CLOCK_LFCLKSTAT_SRC_LFSYNT << CLOCK_LFCLKSTAT_SRC_Pos;
clksrc = CLOCK_LFCLKSTAT_SRC_LFSYNT;
nrf5340_set_lf_clock_source(CLOCK_LFCLKSTAT_SRC_LFSYNT);
#elif MYNEWT_VAL_CHOICE(MCU_LFCLK_SOURCE, LFRC)
regval |= CLOCK_LFCLKSTAT_SRC_LFRC << CLOCK_LFCLKSTAT_SRC_Pos;
clksrc = CLOCK_LFCLKSTAT_SRC_LFRC;
nrf5340_set_lf_clock_source(CLOCK_LFCLKSTAT_SRC_LFRC);
#else
#error Unknown LFCLK source selected
#endif

#if MYNEWT_VAL_CHOICE(MCU_LFCLK_SOURCE, LFSYNTH)
/* Must turn on HFLCK for synthesized 32768 crystal */
if ((NRF_CLOCK->HFCLKSTAT & CLOCK_HFCLKSTAT_STATE_Msk) !=
(CLOCK_HFCLKSTAT_STATE_Running << CLOCK_HFCLKSTAT_STATE_Pos)) {
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
nrf5340_clock_hfxo_request();
while (1) {
if ((NRF_CLOCK->EVENTS_HFCLKSTARTED) != 0) {
break;
}
}
} else {
nrf5340_clock_hfxo_request();
}
#endif

/* Check if this clock source is already running */
if ((NRF_CLOCK->LFCLKSTAT & regmsk) != regval) {
NRF_CLOCK->TASKS_LFCLKSTOP = 1;
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK->LFCLKSRC = clksrc;
NRF_CLOCK->TASKS_LFCLKSTART = 1;

/* Wait here till started! */
while (1) {
if (NRF_CLOCK->EVENTS_LFCLKSTARTED) {
if ((NRF_CLOCK->LFCLKSTAT & regmsk) == regval) {
break;
}
}
}
}
#endif
if (MYNEWT_VAL(MCU_HFCLCK192_DIV) == 1) {
NRF_CLOCK->HFCLK192MCTRL = 0;
Expand Down

0 comments on commit 9aad62f

Please sign in to comment.