Skip to content

Commit

Permalink
Adding non-blocking clock measurement.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-gillespie committed Jan 14, 2025
1 parent c9a3d80 commit 51297d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 21 additions & 3 deletions Libraries/PeriphDrivers/Source/SYS/sys_me30.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -563,14 +563,32 @@ 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;

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 */

0 comments on commit 51297d7

Please sign in to comment.