Skip to content

Commit

Permalink
Add function to select 32K clock source for RTC, WUT, Timers and system
Browse files Browse the repository at this point in the history
RTC, WUTs, Timers and system can use a different input for 32KHz clock
based on a setting in MCR.CTRL register. Add a function to select the
input for 32KHz clock.

Signed-off-by: Tahsin Mutlugun <[email protected]>
  • Loading branch information
ttmut committed Jan 13, 2025
1 parent 0fe8628 commit 07b66bd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 52 deletions.
35 changes: 19 additions & 16 deletions Libraries/PeriphDrivers/Include/MAX32657/mxc_sys.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
*
* Copyright (C) 2024 Analog Devices, Inc.
* Copyright (C) 2024-2025 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,6 +27,7 @@
#include "mxc_device.h"
#include "gcr_regs.h"
#include "fcr_regs.h"
#include "mcr_regs.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -88,7 +89,6 @@ typedef enum {
} mxc_sys_periph_clock_t;

/** @brief Enumeration to select System Clock source */
// TODO(ME30): CLKCTRL sysclk_sel values seem incorrect.
typedef enum {
MXC_SYS_CLOCK_IPO =
MXC_V_GCR_CLKCTRL_SYSCLK_SEL_IPO, /**< Select the Internal Primary Oscillator (IPO) */
Expand Down Expand Up @@ -116,6 +116,17 @@ typedef enum {
MXC_SYS_CLOCK_DIV_128 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV128
} mxc_sys_system_clock_div_t;

/** @brief Enumeration to select 32KHz Clock source used by the RTC, the timers, the wakeup timers,
* and the system clock */
typedef enum {
MXC_SYS_32K_CLOCK_ERTCO =
MXC_V_MCR_CTRL_CLKSEL_ERTCO, /**< Select the External Real-Time Clock Oscillator (ERTCO) */
MXC_SYS_32K_CLOCK_INRO =
MXC_V_MCR_CTRL_CLKSEL_INRO_DIV4, /**< Select the Internal Baud Rate Oscillator (INRO) */
MXC_SYS_32K_CLOCK_RTC_IN =
MXC_V_MCR_CTRL_CLKSEL_RTC_IN_DIV8, /**< Select the External clock input (RTC_CLK_IN) */
} mxc_sys_32k_clock_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,
Expand Down Expand Up @@ -270,22 +281,14 @@ void MXC_SYS_RTCClockEnable(void);
int MXC_SYS_RTCClockDisable(void);

/**
* @brief Enables the 32kHz oscillator to be powered down when not in use.
* Only available for ME17 Rev. B and older chips. This has no effect on ME17
* Rev. A chips.
*
* @returns E_NO_ERROR if everything is successful
*/
void MXC_SYS_RTCClockPowerDownEn(void);

/**
* @brief Disables the 32kHz oscillator from being powered down when not in use.
* Only available for ME17 Rev. B and older chips. This has no effect on ME17
* Rev. A chips.
* @brief Selects the 32KHz clock source used by the RTC, the timers, the wakeup timers,
* and the system clock.
*
* @returns E_NO_ERROR if everything is successful
* @param clock Clock source to use. See @ref mxc_sys_32k_clock_t for options.
* @return E_NO_ERROR if everything is successful
* E_BAD_PARAM if the clock is not valid
*/
void MXC_SYS_RTCClockPowerDownDis(void);
int MXC_SYS_Select32KClockSource(mxc_sys_32k_clock_t clock);

/**
* @brief Enable System Clock Source without switching to it
Expand Down
22 changes: 10 additions & 12 deletions Libraries/PeriphDrivers/Source/SYS/sys_me30.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
*
* Copyright (C) 2024 Analog Devices, Inc.
* Copyright (C) 2024-2025 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -198,19 +198,17 @@ int MXC_SYS_RTCClockDisable(void)
}
}

#if TARGET_NUM == 32655
/******************************************************************************/
void MXC_SYS_RTCClockPowerDownEn(void)
int MXC_SYS_Select32KClockSource(mxc_sys_32k_clock_t clock)
{
MXC_MCR->ctrl |= MXC_F_MCR_CTRL_32KOSC_EN;
}
if (clock > MXC_SYS_32K_CLOCK_RTC_IN) {
return E_BAD_PARAM;
}

/******************************************************************************/
void MXC_SYS_RTCClockPowerDownDis(void)
{
MXC_MCR->ctrl &= ~MXC_F_MCR_CTRL_32KOSC_EN;
MXC_MCR->ctrl &= ~MXC_F_MCR_CTRL_CLKSEL;
MXC_MCR->ctrl |= (clock << MXC_F_MCR_CTRL_CLKSEL_POS);

return E_NO_ERROR;
}
#endif //TARGET_NUM == 32655

/******************************************************************************/
int MXC_SYS_ClockSourceEnable(mxc_sys_system_clock_t clock)
Expand All @@ -232,7 +230,7 @@ int MXC_SYS_ClockSourceEnable(mxc_sys_system_clock_t clock)
break;

case MXC_SYS_CLOCK_INRO:
// The 80k clock is always enabled
// The 131k clock is always enabled
return MXC_SYS_Clock_Timeout(MXC_F_GCR_CLKCTRL_INRO_RDY);
break;

Expand Down
27 changes: 4 additions & 23 deletions Libraries/zephyr/MAX/Include/wrap_max32_rtc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
*
* Copyright (C) 2024 Analog Devices, Inc.
* Copyright (C) 2024-2025 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,39 +21,20 @@

/***** Includes *****/
#include <rtc.h>
#include <mxc_sys.h>
#include "wrap_max32_sys.h"

#ifdef __cplusplus
extern "C" {
#endif

#if defined(CONFIG_SOC_MAX32657)

static inline mxc_rtc_clock_t wrap_get_clock_source_instance(uint8_t clock_source)
{
mxc_rtc_clock_t clk_src;

switch (clock_source) {
case 4: // ADI_MAX32_PRPH_CLK_SRC_ERTCO
clk_src = MXC_RTC_ERTCO_CLK;
break;
case 5: // ADI_MAX32_PRPH_CLK_SRC_INRO
clk_src = MXC_RTC_INRO_CLK;
break;
default:
return -1;
}

return clk_src;
}

static inline int Wrap_MXC_RTC_Init(uint32_t sec, uint16_t ssec, uint8_t clock_source)
{
int ret;
mxc_rtc_clock_t clk_src;

clk_src = wrap_get_clock_source_instance(clock_source);

if (MXC_RTC_SetClockSource(clk_src) == E_NO_ERROR) {
if (Wrap_MXC_SYS_Select32KClockSource(clock_source) == E_NO_ERROR) {
ret = MXC_RTC_Init(sec, ssec);
} else {
ret = E_BAD_PARAM;
Expand Down
37 changes: 36 additions & 1 deletion Libraries/zephyr/MAX/Include/wrap_max32_sys.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
*
* Copyright (C) 2023 Analog Devices, Inc.
* Copyright (C) 2023-2025 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,6 +51,11 @@ static inline void Wrap_MXC_SYS_SetClockDiv(int div)
MXC_SYS_Clock_Div((mxc_sys_system_div_t)div);
}

static inline int Wrap_MXC_SYS_Select32KClockSource(int clock)
{
return 0;
}

/*
* MAX32690, MAX32655 related mapping
*/
Expand Down Expand Up @@ -83,6 +88,36 @@ static inline void Wrap_MXC_SYS_SetClockDiv(int div)
MXC_SYS_SetClockDiv((mxc_sys_system_clock_div_t)div);
}

#if defined(CONFIG_SOC_MAX32657)
static inline mxc_sys_32k_clock_t wrap_get_32k_clock_source_instance(uint8_t clock_source)
{
mxc_sys_32k_clock_t clk_src;

switch (clock_source) {
case 4: // ADI_MAX32_PRPH_CLK_SRC_ERTCO
clk_src = MXC_SYS_32K_CLOCK_ERTCO;
break;
case 5: // ADI_MAX32_PRPH_CLK_SRC_INRO
clk_src = MXC_SYS_32K_CLOCK_INRO;
break;
default:
return -1;
}

return clk_src;
}

static inline int Wrap_MXC_SYS_Select32KClockSource(int clock)
{
return MXC_SYS_Select32KClockSource(wrap_get_32k_clock_source_instance(clock));
}
#else
static inline int Wrap_MXC_SYS_Select32KClockSource(int clock)
{
return 0;
}
#endif

#endif // part number

#ifdef __cplusplus
Expand Down

0 comments on commit 07b66bd

Please sign in to comment.