drv_wdt.c 2.3 KB
Newer Older
S
Sherman 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-08-20     Mr.Tiger     first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <rthw.h>
#include <drv_common.h>
#include <drv_config.h>
#include <hal_data.h>

#ifdef RT_USING_WDT

//#define DRV_DEBUG
#define LOG_TAG             "drv.wdt"
#include <rtdbg.h>

static struct rt_watchdog_device ra_wdt_dev;
static struct rt_watchdog_ops ops;

static rt_err_t wdt_init(rt_watchdog_t *wdt)
{
    return RT_EOK;
}

static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
{
    struct st_wdt_timeout_values *wdt_value = {0};
    switch (cmd)
    {
S
Sherman 已提交
37
    /* feed the watchdog */
S
Sherman 已提交
38
    case RT_DEVICE_CTRL_WDT_KEEPALIVE:
S
Sherman 已提交
39
        if (R_WDT_Refresh(&g_wdt_ctrl) != FSP_SUCCESS)
S
Sherman 已提交
40 41 42 43
        {
            LOG_E("watch dog keepalive fail.");
        }
        break;
S
Sherman 已提交
44
    /* set watchdog timeout */
S
Sherman 已提交
45 46 47 48 49 50
    case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
        /**< set*/
//        g_wdt_cfg.timeout = *(rt_uint32_t *)arg;
        break;
    case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
        wdt_value = (struct st_wdt_timeout_values *)arg;
S
Sherman 已提交
51
        if (R_WDT_TimeoutGet(&g_wdt_ctrl, wdt_value) != FSP_SUCCESS)
S
Sherman 已提交
52 53 54 55 56 57
        {
            LOG_E("wdt get timeout failed.");
            return -RT_ERROR;
        }
        break;
    case RT_DEVICE_CTRL_WDT_START:
S
Sherman 已提交
58
        if (R_WDT_Open(&g_wdt_ctrl, &g_wdt_cfg) == FSP_SUCCESS)
S
Sherman 已提交
59
        {
S
Sherman 已提交
60
            if (R_WDT_Refresh(&g_wdt_ctrl) != FSP_SUCCESS)
S
Sherman 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            {
                LOG_E("wdt start failed.");
                return -RT_ERROR;
            }
        }
        else
        {
            LOG_E("wdt start failed.");
            return -RT_ERROR;
        }
        break;
    default:
        LOG_W("This command is not supported.");
        return -RT_ERROR;
    }
    return RT_EOK;
}

int rt_wdt_init(void)
{
    ops.init = &wdt_init;
    ops.control = &wdt_control;
    ra_wdt_dev.ops = &ops;
    /* register watchdog device */
    if (rt_hw_watchdog_register(&ra_wdt_dev, "wdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
    {
        LOG_E("wdt device register failed.");
        return -RT_ERROR;
    }
    LOG_D("wdt device register success.");
    return RT_EOK;
}
INIT_BOARD_EXPORT(rt_wdt_init);

#endif /* RT_USING_WDT */