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

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

#ifndef _DRV_TIMER_H_
#define _DRV_TIMER_H_

#include <drv/common.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct csi_timer csi_timer_t;

struct csi_timer {
    csi_dev_t        dev;
    void            (*callback)(csi_timer_t *timer, void *arg);
    void            *arg;
    void            *priv;
} ;

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

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

/**
  \brief       Start timer
  \param[in]   timer         handle timer handle to operate
  \param[in]   timeout_us    the timeout for timer
  \return      error code \ref csi_error_t
*/
csi_error_t csi_timer_start(csi_timer_t *timer, uint32_t timeout_us);

/**
  \brief       Stop timer
  \param[in]   timer    handle timer handle to operate
  \return      None
*/
void csi_timer_stop(csi_timer_t *timer);

/**
  \brief       Get timer remaining value
  \param[in]   timer    handle timer handle to operate
  \return      timer    remaining value
*/
uint32_t csi_timer_get_remaining_value(csi_timer_t *timer);

/**
  \brief       Get timer load value
  \param[in]   timer    handle timer handle to operate
  \return      timer    load value
*/
uint32_t csi_timer_get_load_value(csi_timer_t *timer);

/**
  \brief       Check timer is running
  \param[in]   timer    handle timer handle to operate
  \return      true->running, false->stopped
*/
bool csi_timer_is_running(csi_timer_t *timer);

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

/**
  \brief       Detach the callback handler
  \param[in]   timer    operate handle.
*/
void csi_timer_detach_callback(csi_timer_t *timer);

/**
  \brief       Enable timer power manage
  \param[in]   timer    timer handle to operate.
  \return      error code \ref csi_error_t
*/
csi_error_t csi_timer_enable_pm(csi_timer_t *timer);

/**
  \brief       Disable timer power manage
  \param[in]   timer    timer handle to operate.
*/
void csi_timer_disable_pm(csi_timer_t *timer);

#ifdef __cplusplus
}
#endif

#endif /* _DRV_TIMER_H_ */
