summaryrefslogtreecommitdiff
path: root/bsp/stm32f10x-HAL/drivers/drv_iwg.c
blob: 1d170b2df054655564ca36dbbf0b032775048618 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * File      : drv_iwg.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2015, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author            Notes
 * 2017-11-08     ZYH            the first version
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>


#ifdef RT_USING_WDT
IWDG_HandleTypeDef hiwdg;
static rt_err_t drv_init(rt_watchdog_t *wdt)
{
    hiwdg.Instance = IWDG;
    hiwdg.Init.Prescaler = IWDG_PRESCALER_16; //1s
    hiwdg.Init.Reload = 4095;
    if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
    {
        RT_ASSERT(0);
    }
    return RT_EOK;
}

static rt_err_t drv_control(rt_watchdog_t *wdt, int cmd, void *arg)
{
    switch (cmd)
    {
    case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
        hiwdg.Init.Reload = (rt_uint32_t)arg;
        if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
        {
            return RT_ERROR;
        }
        break;
    case RT_DEVICE_CTRL_WDT_KEEPALIVE:
        HAL_IWDG_Refresh(&hiwdg);
        break;
    default:
        return RT_ERROR;
    }
    return RT_EOK;
}

static struct rt_watchdog_ops _ops =
    {
        drv_init,
        drv_control
    };

static rt_watchdog_t _iwg =
    {
        .ops = &_ops
    };

int rt_iwg_init(void)
{
    return rt_hw_watchdog_register(&_iwg, "iwg", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL);
}
INIT_BOARD_EXPORT(rt_iwg_init);
#endif