cpu/nrf5x: allow to request/release HFXO clk src

This commit is contained in:
Hauke Petersen 2021-01-19 11:06:13 +01:00
parent 1f085cfd03
commit 9d1692c45a
2 changed files with 55 additions and 5 deletions

View File

@ -18,6 +18,9 @@
*/ */
#include "cpu.h" #include "cpu.h"
#include "irq.h"
#include "assert.h"
#include "nrf_clock.h"
#include "periph_conf.h" #include "periph_conf.h"
/* make sure both clocks are configured */ /* make sure both clocks are configured */
@ -28,6 +31,8 @@
#error "Clock init: CLOCK_LFCLK is not defined by your board!" #error "Clock init: CLOCK_LFCLK is not defined by your board!"
#endif #endif
static unsigned _hfxo_requests = 0;
void clock_init_hf(void) void clock_init_hf(void)
{ {
/* for the nRF51 we can chose the XTAL frequency */ /* for the nRF51 we can chose the XTAL frequency */
@ -39,12 +44,36 @@ void clock_init_hf(void)
#endif #endif
#endif #endif
#if CLOCK_HFCLK /* allow to always enable the HFXO as non-default option */
/* start the HF clock */ #if CLOCK_HFXO_ONBOOT
clock_hfxo_request();
#endif
}
void clock_hfxo_request(void)
{
unsigned state = irq_disable();
++_hfxo_requests;
if (_hfxo_requests == 1) {
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
NRF_CLOCK->TASKS_HFCLKSTART = 1; NRF_CLOCK->TASKS_HFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {} while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {}
#endif }
irq_restore(state);
}
void clock_hfxo_release(void)
{
/* if this function is called while the counter is zero the state in a
* driver requesting the HFXO is broken! */
assert(_hfxo_requests);
unsigned state = irq_disable();
--_hfxo_requests;
if (_hfxo_requests == 0) {
NRF_CLOCK->TASKS_HFCLKSTOP = 1;
}
irq_restore(state);
} }
void clock_start_lf(void) void clock_start_lf(void)

View File

@ -23,12 +23,33 @@
extern "C" { extern "C" {
#endif #endif
/**
* @brief The high frequency clock (HFCLK) uses the internal oscillator per
* default. Setting this define to 1 will enable the HFXO clock source
* on boot so it will always be active.
*/
#ifndef CLOCK_HFXO_ONBOOT
#define CLOCK_HFXO_ONBOOT 0
#endif
/** /**
* @brief Initialize the high frequency clock (HFCLK) as configured in the * @brief Initialize the high frequency clock (HFCLK) as configured in the
* board's periph_conf.h * board's periph_conf.h
*/ */
void clock_init_hf(void); void clock_init_hf(void);
/**
* @brief Request the external high frequency crystal (HFXO) as HF clock
* source. If this is the first request, the HFXO will be enabled.
*/
void clock_hfxo_request(void);
/**
* @brief Release the use of the HFXO. If this was the last active request,
* the HFXO will be disabled
*/
void clock_hfxo_release(void);
/** /**
* @brief Start the low frequency clock (LFCLK) as configured in the board's * @brief Start the low frequency clock (LFCLK) as configured in the board's
* periph_conf. * periph_conf.