#ifndef __BSP_TIM_H__
#define __BSP_TIM_H__


#include "CH58x_common.h"
#include "CONFIG.h"

uint32_t BSP_Get_Tick(void);


typedef uint64_t (*PlatformTicksFunction_t)(void);

typedef struct MultiTimerHandle MultiTimer;

typedef void (*MultiTimerCallback)(MultiTimer* timer, void* userData);

struct MultiTimerHandle
{
    MultiTimer* next;
    uint64_t deadline;
    MultiTimerCallback callback;
    void* userData;
};

/**
 * @brief Platform ticks function.
 *
 * @param ticksFunc ticks function.
 * @return int 0 on success, -1 on error.
 */
int MultiTimerInstall(PlatformTicksFunction_t ticksFunc);

/**
 * @brief Start the timer work, add the handle into work list.
 *
 * @param timer target handle strcut.
 * @param timing Set the start time.
 * @param callback deadline callback.
 * @param userData user data.
 * @return int 0: success, -1: fail.
 */
int MultiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback callback, void* userData);

/**
 * @brief Stop the timer work, remove the handle off work list.
 *
 * @param timer target handle strcut.
 * @return int 0: success, -1: fail.
 */
int MultiTimerStop(MultiTimer* timer);

/**
 * @brief Check the timer expried and call callback.
 *
 * @return int The next timer expires.
 */
int MultiTimerYield(void);

#endif //!@__BSP_TIM_H__

