From 51297d710a312a803d56cee06aa6bcb66e6f27af Mon Sep 17 00:00:00 2001 From: Kevin Gillespie Date: Tue, 14 Jan 2025 14:45:00 -0600 Subject: [PATCH] Adding non-blocking clock measurement. --- .../PeriphDrivers/Include/MAX32657/mxc_sys.h | 5 +++- Libraries/PeriphDrivers/Source/SYS/sys_me30.c | 24 ++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h b/Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h index 902b103e97..92ccdd886b 100644 --- a/Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h +++ b/Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h @@ -364,12 +364,15 @@ int MXC_SYS_LockDAP_Permanent(void); * * @details Assumes that measurement clock and ERFO are enabled. * Increasing compareClockTicks will provide a more accurate measurement, - * but there are limits that could cause overflow. + * but there are limits that could cause overflow. Start and Get function + * are used for non-blocking implementations. * * @param clock Enumeration for which clock to measure. * @param compareClockTicks Number of ticks of the comparison clock to use for measurement. */ uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks); +void MXC_SYS_StartClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks); +uint32_t MXC_SYS_GetClockMeasure(void); #ifdef __cplusplus } diff --git a/Libraries/PeriphDrivers/Source/SYS/sys_me30.c b/Libraries/PeriphDrivers/Source/SYS/sys_me30.c index 5b30e17a87..9a0eaa9780 100644 --- a/Libraries/PeriphDrivers/Source/SYS/sys_me30.c +++ b/Libraries/PeriphDrivers/Source/SYS/sys_me30.c @@ -542,7 +542,7 @@ int MXC_SYS_LockDAP_Permanent(void) #endif /* ************************************************************************** */ -uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks) +void MXC_SYS_StartClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks) { /* Assuming that both clocks are already enabled */ @@ -563,9 +563,15 @@ uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClo /* Start the procedure */ MXC_FCR->frqcntctrl |= MXC_F_FCR_FRQCNTCTRL_START; +} - /* Wait for the procedure to finish */ - while (!(MXC_FCR->intfl & MXC_F_FCR_INTFL_FRQCNT)) {} +/* ************************************************************************** */ +uint32_t MXC_SYS_GetClockMeasure(void) +{ + /* Return 0 if the procedure is incomplete */ + if(!(MXC_FCR->intfl & MXC_F_FCR_INTFL_FRQCNT)) { + return 0; + } /* Calculate the frequency */ uint64_t freq = (uint64_t)ERFO_FREQ * (uint64_t)MXC_FCR->cmpclk / (uint64_t)MXC_FCR->refclk; @@ -573,4 +579,16 @@ uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClo return (uint32_t)freq; } +/* ************************************************************************** */ +uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks) +{ + /* Assuming that both clocks are already enabled */ + MXC_SYS_StartClockMeasure(clock, compareClockTicks); + + /* Wait for the procedure to finish */ + while (!(MXC_FCR->intfl & MXC_F_FCR_INTFL_FRQCNT)) {} + + return MXC_SYS_GetClockMeasure(); +} + /**@} end of mxc_sys */