Skip to content

Commit

Permalink
feat(PeriphDrivers): Add MAX32657 non-blocking clock measurement (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-gillespie authored Jan 16, 2025
1 parent c9a3d80 commit f45a535
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
23 changes: 21 additions & 2 deletions Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,36 @@ uint32_t MXC_SYS_RiscVClockRate(void);
int MXC_SYS_LockDAP_Permanent(void);

/**
* @brief Measure the clock frequency.
* @brief Measure the clock frequency, blocking.
*
* @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.
* @return clock frequency
*/
uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks);

/**
* @brief Start clock frequency measurement, non blocking.
*
* @param clock Enumeration for which clock to measure.
* @param compareClockTicks Number of ticks of the comparison clock to use for measurement.
*/
void MXC_SYS_StartClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks);

/**
* @brief Get clock frequency measurement after calling start, non blocking.
*
* @param clock Enumeration for which clock to measure.
* @param compareClockTicks Number of ticks of the comparison clock to use for measurement.
* @return clock frequency, 0 if measurement is unfinished.
*/
uint32_t MXC_SYS_GetClockMeasure(void);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions Libraries/PeriphDrivers/Source/LP/lp_me30.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ void MXC_LP_EnterStandbyMode(void)

/* Go into Standby mode and wait for an interrupt to wake the processor */
__WFI();

/* Clear SLEEPDEEP bit to prevent WFI from entering deep sleep */
CLR_SLEEPDEEP();
}

void MXC_LP_EnterBackupMode(void)
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 f45a535

Please sign in to comment.