From 0adc37422f48e9468d980a1d98dcc65bd7ab3043 Mon Sep 17 00:00:00 2001 From: Megan Wachs Date: Tue, 31 Jan 2017 20:25:38 -0800 Subject: add a 'driver' common PRCI functions. --- bsp/drivers/fe300prci/fe300prci_driver.h | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 bsp/drivers/fe300prci/fe300prci_driver.h (limited to 'bsp/drivers/fe300prci/fe300prci_driver.h') diff --git a/bsp/drivers/fe300prci/fe300prci_driver.h b/bsp/drivers/fe300prci/fe300prci_driver.h new file mode 100644 index 0000000..052edcb --- /dev/null +++ b/bsp/drivers/fe300prci/fe300prci_driver.h @@ -0,0 +1,54 @@ +// See LICENSE file for license details + +#ifndef _FE300PRCI_DRIVER_H_ +#define _FE300PRCI_DRIVER_H_ + +#include + + +/* Measure and return the approximate frequency of the + * CPU, as given by measuring the mcycle counter against + * the mtime ticks. + */ +static uint32_t PRCI_measure_mcycle_freq(uint32_t mtime_ticks, uint32_t mtime_freq); + +/* Safely switch over to the HFROSC using the given div + * and trim settings. + */ +static void PRCI_use_hfrosc(int div, int trim); + +/* Safely switch over to the PLL using the given + * settings. + * + * Note that not all combinations of the inputs are actually + * legal, and this function does not check for their + * legality ("safely" means that this function won't turn off + * or glitch the clock the CPU is actually running off, but + * doesn't protect against you making it too fast or slow.) + */ + +static void PRCI_use_pll(int refsel, int bypass, + int r, int f, int q, int finaldiv, + int hfroscdiv, int hfrosctrim); + +/* Use the default clocks configured at reset. + * This is ~16Mhz HFROSC and turns off the LFROSC + * (on the current FE310 Dev Platforms, an external LFROSC is + * used as it is more power efficient). + */ +static void PRCI_use_default_clocks(); + +/* This routine will adjust the HFROSC trim + * as the PLL clock source, measure the resulting + * frequency, and attempt to get close to the requested + * frequency. It returns the actual measured frequency. + * + * Note that the requested frequency must be within the + * range supported by the PLL so not all values are + * achievable with this function, and not all + * are guaranteed to actually work. + */ +uint32_t PRCI_set_hfrosctrim_for_f_cpu(uint32_t f_cpu); + +#endif + -- cgit v1.2.3 From 08a72bb956a25fd94d965fb4474f3ddcca1ed1e6 Mon Sep 17 00:00:00 2001 From: Megan Wachs Date: Wed, 1 Feb 2017 16:39:38 -0800 Subject: Additional PRCI driver support. Make the code usable by C++ programs as well. --- bsp/drivers/fe300prci/fe300prci_driver.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'bsp/drivers/fe300prci/fe300prci_driver.h') diff --git a/bsp/drivers/fe300prci/fe300prci_driver.h b/bsp/drivers/fe300prci/fe300prci_driver.h index 052edcb..75dcf00 100644 --- a/bsp/drivers/fe300prci/fe300prci_driver.h +++ b/bsp/drivers/fe300prci/fe300prci_driver.h @@ -3,19 +3,26 @@ #ifndef _FE300PRCI_DRIVER_H_ #define _FE300PRCI_DRIVER_H_ -#include +__BEGIN_DECLS +#include /* Measure and return the approximate frequency of the * CPU, as given by measuring the mcycle counter against * the mtime ticks. */ -static uint32_t PRCI_measure_mcycle_freq(uint32_t mtime_ticks, uint32_t mtime_freq); +uint32_t PRCI_measure_mcycle_freq(uint32_t mtime_ticks, uint32_t mtime_freq); /* Safely switch over to the HFROSC using the given div * and trim settings. */ -static void PRCI_use_hfrosc(int div, int trim); +void PRCI_use_hfrosc(int div, int trim); + +/* Safely switch over to the 16MHz HFXOSC, + * applying the finaldiv clock divider (1 is the lowest + * legal value). + */ +void PRCI_use_hfxosc(uint32_t finaldiv); /* Safely switch over to the PLL using the given * settings. @@ -27,7 +34,7 @@ static void PRCI_use_hfrosc(int div, int trim); * doesn't protect against you making it too fast or slow.) */ -static void PRCI_use_pll(int refsel, int bypass, +void PRCI_use_pll(int refsel, int bypass, int r, int f, int q, int finaldiv, int hfroscdiv, int hfrosctrim); @@ -36,7 +43,7 @@ static void PRCI_use_pll(int refsel, int bypass, * (on the current FE310 Dev Platforms, an external LFROSC is * used as it is more power efficient). */ -static void PRCI_use_default_clocks(); +void PRCI_use_default_clocks(); /* This routine will adjust the HFROSC trim * as the PLL clock source, measure the resulting @@ -50,5 +57,7 @@ static void PRCI_use_default_clocks(); */ uint32_t PRCI_set_hfrosctrim_for_f_cpu(uint32_t f_cpu); +__END_DECLS + #endif -- cgit v1.2.3 From b003c52b0f5460c6e0e15cb29171164530490e08 Mon Sep 17 00:00:00 2001 From: Megan Wachs Date: Wed, 1 Feb 2017 18:03:37 -0800 Subject: PRCI: add control for the target CPU frequency (to allow a minimum, maximum, or closest match) --- bsp/drivers/fe300prci/fe300prci_driver.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'bsp/drivers/fe300prci/fe300prci_driver.h') diff --git a/bsp/drivers/fe300prci/fe300prci_driver.h b/bsp/drivers/fe300prci/fe300prci_driver.h index 75dcf00..243e6de 100644 --- a/bsp/drivers/fe300prci/fe300prci_driver.h +++ b/bsp/drivers/fe300prci/fe300prci_driver.h @@ -7,6 +7,14 @@ __BEGIN_DECLS #include +typedef enum prci_freq_target { + + PRCI_FREQ_OVERSHOOT, + PRCI_FREQ_CLOSEST, + PRCI_FREQ_UNDERSHOOT + +} PRCI_freq_target; + /* Measure and return the approximate frequency of the * CPU, as given by measuring the mcycle counter against * the mtime ticks. @@ -55,7 +63,7 @@ void PRCI_use_default_clocks(); * achievable with this function, and not all * are guaranteed to actually work. */ -uint32_t PRCI_set_hfrosctrim_for_f_cpu(uint32_t f_cpu); +uint32_t PRCI_set_hfrosctrim_for_f_cpu(uint32_t f_cpu, PRCI_freq_target target); __END_DECLS -- cgit v1.2.3 From 2579554bb17038dffa9af57649c1e83e614efbc6 Mon Sep 17 00:00:00 2001 From: Megan Wachs Date: Thu, 2 Feb 2017 09:56:52 -0800 Subject: prci: Simplify the targeted f_cpu function by measuring HFROSC frequency first, then using it as the PLL source. This is also safer as it is less risk of using PLL out of its range. --- bsp/drivers/fe300prci/fe300prci_driver.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'bsp/drivers/fe300prci/fe300prci_driver.h') diff --git a/bsp/drivers/fe300prci/fe300prci_driver.h b/bsp/drivers/fe300prci/fe300prci_driver.h index 243e6de..7100f46 100644 --- a/bsp/drivers/fe300prci/fe300prci_driver.h +++ b/bsp/drivers/fe300prci/fe300prci_driver.h @@ -54,15 +54,23 @@ void PRCI_use_pll(int refsel, int bypass, void PRCI_use_default_clocks(); /* This routine will adjust the HFROSC trim - * as the PLL clock source, measure the resulting - * frequency, and attempt to get close to the requested - * frequency. It returns the actual measured frequency. + * while using HFROSC as the clock source, + * measure the resulting frequency, then + * use it as the PLL clock source, + * in an attempt to get over, under, or close to the + * requested frequency. It returns the actual measured + * frequency. * * Note that the requested frequency must be within the * range supported by the PLL so not all values are * achievable with this function, and not all - * are guaranteed to actually work. + * are guaranteed to actually work. The PLL + * is rated higher than the hardware. + * + * There is no check on the desired f_cpu frequency, it + * is up to the user to specify something reasonable. */ + uint32_t PRCI_set_hfrosctrim_for_f_cpu(uint32_t f_cpu, PRCI_freq_target target); __END_DECLS -- cgit v1.2.3