Skip to content

drivers: watchdog: bugfix for ambiq wdt clk select #92958

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 27 additions & 7 deletions drivers/watchdog/wdt_ambiq.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef void (*ambiq_wdt_cfg_func_t)(void);
struct wdt_ambiq_config {
uint32_t base;
uint32_t irq_num;
uint8_t clk_freq;
uint32_t clk_freq;
ambiq_wdt_cfg_func_t cfg_func;
};

Expand All @@ -30,6 +30,13 @@ struct wdt_ambiq_data {
bool reset;
};

uint32_t wdt_ambiq_clk_select[] =
#if defined(CONFIG_SOC_SERIES_APOLLO3X) || defined(CONFIG_SOC_SERIES_APOLLO4X)
{128, 16, 1};
#else
{128, 16, 1, 32768, 16384};
#endif

static void wdt_ambiq_isr(void *arg)
{
const struct device *dev = (const struct device *)arg;
Expand All @@ -39,7 +46,7 @@ static void wdt_ambiq_isr(void *arg)
am_hal_wdt_int_clear();
#else
uint32_t status;
am_hal_wdt_interrupt_status_get(AM_HAL_WDT_MCU, &status, false);
am_hal_wdt_interrupt_status_get(AM_HAL_WDT_MCU, &status, true);
am_hal_wdt_interrupt_clear(AM_HAL_WDT_MCU, status);
#endif

Expand Down Expand Up @@ -79,7 +86,15 @@ static int wdt_ambiq_setup(const struct device *dev, uint8_t options)
cfg.eClockSource = AM_HAL_WDT_16HZ;
} else if (dev_cfg->clk_freq == 1) {
cfg.eClockSource = AM_HAL_WDT_1HZ;
#if defined(CONFIG_SOC_SERIES_APOLLO5X)
} else if (dev_cfg->clk_freq == 32768) {
cfg.eClockSource = AM_HAL_WDT_XTAL_HS;
} else if (dev_cfg->clk_freq == 16384) {
cfg.eClockSource = AM_HAL_WDT_XTAL_HS_DIV2;
}
#else
}
#endif

cfg.bInterruptEnable = true;
cfg.ui32InterruptValue = data->timeout;
Expand Down Expand Up @@ -116,7 +131,7 @@ static int wdt_ambiq_install_timeout(const struct device *dev, const struct wdt_
return -EINVAL;
}

data->timeout = cfg->window.max / 1000 * dev_cfg->clk_freq;
data->timeout = cfg->window.max * dev_cfg->clk_freq / 1000;
data->callback = cfg->callback;

switch (cfg->flags) {
Expand Down Expand Up @@ -145,17 +160,22 @@ static int wdt_ambiq_feed(const struct device *dev, int channel_id)
#else
am_hal_wdt_restart(AM_HAL_WDT_MCU);
#endif
LOG_DBG("Fed the watchdog");

return 0;
}

static int wdt_ambiq_init(const struct device *dev)
{
const struct wdt_ambiq_config *dev_cfg = dev->config;

if (dev_cfg->clk_freq != 128 && dev_cfg->clk_freq != 16 && dev_cfg->clk_freq != 1) {
return -ENOTSUP;
uint8_t clk_index = sizeof(wdt_ambiq_clk_select) / sizeof(uint32_t);

for (uint8_t i = 0; i < clk_index; i++) {
if (dev_cfg->clk_freq == wdt_ambiq_clk_select[i]) {
break;
}
if (i == clk_index - 1) {
return -ENOTSUP;
}
}

NVIC_ClearPendingIRQ(dev_cfg->irq_num);
Expand Down
Loading