Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PeriphDrivers): Adding MAX32657 clock measurement function. #1308

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */
Loading