/*
 * Copyright (C) 2017-2020 Alibaba Group Holding Limited
 */

/******************************************************************************
 * @file       wdt.h
 * @brief      header file for wdt driver
 * @version    V1.0
 * @date       28. Apr 2020
 * @model      wdt
 ******************************************************************************/

#ifndef _DRV_WDT_H_
#define _DRV_WDT_H_


#include <drv/common.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct csi_wdt csi_wdt_t;

struct csi_wdt {
    csi_dev_t       dev;
    void            (*callback)(csi_wdt_t *wdt,  void *arg);
    void            *arg;
    void            *priv;
};

/**
  \brief       Initialize WDT Interface. Initializes the resources needed for the WDT interface 
  \param[in]   wdt    wdt handle to operate
  \param[in]   idx    wdt index
  \return      error code \ref csi_error_t
*/
csi_error_t csi_wdt_init(csi_wdt_t *wdt, uint32_t idx);

/**
  \brief       De-initialize WDT Interface. stops operation and releases the software resources used by the interface
  \param[in]   wdt    handle to operate
  \return      None
*/
void csi_wdt_uninit(csi_wdt_t *wdt);

/**
  \brief       Set the WDT value
  \param[in]   wdt    handle to operate
  \param[in]   ms     the timeout value(ms)
  \return      error code \ref csi_error_t
*/
csi_error_t csi_wdt_set_timeout(csi_wdt_t *wdt, uint32_t ms);

/**
  \brief       Start the WDT
  \param[in]   wdt    handle to operate
  \return      error code \ref csi_error_t
*/
csi_error_t csi_wdt_start(csi_wdt_t *wdt);

/**
  \brief       Stop the WDT
  \param[in]   wdt    handle to operate
  \return      None
*/
void csi_wdt_stop(csi_wdt_t *wdt);

/**
  \brief       Feed the WDT
  \param[in]   wdt    handle to operate
  \return      error code \ref csi_error_t
*/
csi_error_t csi_wdt_feed(csi_wdt_t *wdt);

/**
  \brief       Get the remaining time to timeout
  \param[in]   wdt    handle to operate
  \return      the remaining time of wdt(ms)
*/
uint32_t csi_wdt_get_remaining_time(csi_wdt_t *wdt);
    
/**
  \brief       Check wdt is running
  \param[in]   wdt    handle wdt handle to operate
  \return      true->running, false->stopped
*/
bool csi_wdt_is_running(csi_wdt_t *wdt);

/**
  \brief       Attach the callback handler to wdt
  \param[in]   wdt         operate handle
  \param[in]   callback    callback function
  \param[in]   arg         callback's param
  \return      error code \ref csi_error_t
*/
csi_error_t csi_wdt_attach_callback(csi_wdt_t *wdt, void *callback, void *arg);

/**
  \brief       Detach the callback handler
  \param[in]   wdt    operate handle
  \return      None
*/
void csi_wdt_detach_callback(csi_wdt_t *wdt);

/**
  \brief       Enable wdt power manage
  \param[in]   wdt    wdt handle to operate
  \return      error code \ref csi_error_t
*/
csi_error_t csi_wdt_enable_pm(csi_wdt_t *wdt);

/**
  \brief       Disable wdt power manage
  \param[in]   wdt    wdt handle to operate
  \return      None
*/
void csi_wdt_disable_pm(csi_wdt_t *wdt);

#ifdef __cplusplus
}
#endif

#endif /* _DRV_WDT_H_ */
