- //*****************************************************************************
- //File Name: kb_machblue_core_timer.c
- //
- //Description: timer function
- //
- // used by Machblue to access the platform's timer
- //
- //Author: steven
- //
- //Date: 2006.12.29
- //
- //Version: v1.0
- //*****************************************************************************
- #include "timer.h"
- #include "machblue_defines.h"
- #include "machblue_porting_core.h"
- static UINT32 mbTimer=KB_TIMER_INVALID_ID;
- static BOOL mbTimerInit=FALSE;
- /**
- * init the machblue timer
- * @return MB_SUCCESS on success, MB_FAILURE on failure.
- */
- mb_error_t mb_timer_init(void)
- {
- if(mbTimerInit)
- return MB_SUCCESS;
- mbTimer=KB_TimerCreate(KB_TIMER_REPEAT,NULL,NULL);
- if(mbTimer==KB_TIMER_INVALID_ID)
- {
- mb_printf("n[Machblue]:Timer create error.");
- return MB_FAILURE;
- }
- mbTimerInit=TRUE;
- return MB_SUCCESS;
- }
- /**
- * delete the machblue timer
- * @return MB_SUCCESS on success, MB_FAILURE on failure.
- */
- mb_error_t mb_timer_delete(void)
- {
- INT32 ret;
- if(mbTimerInit==FALSE)
- return MB_SUCCESS;
- ret=KB_TimerDel(mbTimer);
- if(ret!=RETOK)
- {
- mb_printf("n[Machblue]:Timer delete error.");
- return MB_FAILURE;
- }
- mbTimerInit=FALSE;
- return MB_SUCCESS;
- }
- /**
- * Sets Machblue's timer. The timer callback function should be invoked when
- * the specified delay expires. Machblue will only schedule one timer at a time.
- * delay < timer delay in milliseconds >
- * callback_f < timer callback function >
- * cb_client_data < timer callback function client data >
- * @return MB_SUCCESS on success, MB_FAILURE on failure.
- */
- mb_error_t mb_timer_set(long delay,mb_timer_callback_f *callback_f,void *cb_client_data)
- {
- T_TIMER_st *timer;
- INT32 ret;
- if(mbTimer==KB_TIMER_INVALID_ID)
- {
- mb_printf("n[Machblue]:Timer set invalid.");
- return MB_FAILURE;
- }
- //the lib call delay time maybe 0
- if(delay<=0)
- delay=1;
- timer=(T_TIMER_st *)mbTimer;
- timer->entry=(KB_TIMER_FUNC_POINTER)callback_f;
- timer->param=cb_client_data;
- ret=KB_TimerEnable(mbTimer,delay);
- if(ret!=RETOK)
- {
- mb_printf("n[Machblue]:Timer set error.");
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }
- /**
- * Cancels Machblue's timer request.
- * This function cancels Machblue's timer request.
- */
- mb_error_t mb_timer_cancel(void)
- {
- INT32 ret;
- if(mbTimer==KB_TIMER_INVALID_ID)
- {
- mb_printf("n[Machblue]:Timer cancel invalid.");
- return MB_FAILURE;
- }
- ret=KB_TimerDisable(mbTimer);
- if(ret!=RETOK)
- {
- mb_printf("n[Machblue]:Timer cancel error.");
- return MB_FAILURE;
- }
- return MB_SUCCESS;
- }