Skip to content

Commit

Permalink
feat(PeriphDrivers): Add MAX32657 clock measurement function (#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-gillespie authored Dec 19, 2024
1 parent 506501c commit d3a80c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "mxc_device.h"
#include "gcr_regs.h"
#include "fcr_regs.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -115,6 +116,13 @@ typedef enum {
MXC_SYS_CLOCK_DIV_128 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV128
} mxc_sys_system_clock_div_t;

/** @brief Compare clock enumeration. Used in MXC_SYS_ClockMeasure function. */
typedef enum {
MXC_SYS_COMPARE_CLOCK_RTC = MXC_S_FCR_FRQCNTCTRL_CMP_CLKSEL_RTC,
MXC_SYS_COMPARE_CLOCK_EXT_GPIO = MXC_S_FCR_FRQCNTCTRL_CMP_CLKSEL_EXT_GPIO,
MXC_SYS_COMPARE_CLOCK_INRO = MXC_S_FCR_FRQCNTCTRL_CMP_CLKSEL_INRO
} mxc_sys_compare_clock_t;

#define MXC_SYS_USN_CHECKSUM_LEN 16 // Length of the USN + padding for checksum compute
#define MXC_SYS_USN_CSUM_FIELD_LEN 2 // Size of the checksum field in the USN
#define MXC_SYS_USN_LEN 13 // Size of the USN including the checksum
Expand Down Expand Up @@ -348,6 +356,18 @@ uint32_t MXC_SYS_RiscVClockRate(void);
*/
int MXC_SYS_LockDAP_Permanent(void);

/**
* @brief Measure the clock frequency.
*
* @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.
*
* @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);

#ifdef __cplusplus
}
#endif
Expand Down
32 changes: 32 additions & 0 deletions Libraries/PeriphDrivers/Source/SYS/sys_me30.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,36 @@ int MXC_SYS_LockDAP_Permanent(void)
}
#endif

/* ************************************************************************** */
uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClockTicks)
{
/* Assuming that both clocks are already enabled */

/* Setup the comparison clock */
MXC_FCR->frqcntctrl = (MXC_FCR->frqcntctrl & ~(MXC_F_FCR_FRQCNTCTRL_CMP_CLKSEL)) | clock;

/* Set ticks of the comparison clock */
MXC_FCR->frqcntcmp = compareClockTicks;

/*
* Enable interrupt, note that we don't see the flag if we leave
* this disabled.
*/
MXC_FCR->inten |= MXC_F_FCR_INTFL_FRQCNT;

/* Clear the interrupt flag */
MXC_FCR->intfl = MXC_F_FCR_INTFL_FRQCNT;

/* 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)) {}

/* Calculate the frequency */
uint64_t freq = (uint64_t)ERFO_FREQ * (uint64_t)MXC_FCR->cmpclk / (uint64_t)MXC_FCR->refclk;

return (uint32_t)freq;
}

/**@} end of mxc_sys */

0 comments on commit d3a80c4

Please sign in to comment.