2019-03-20 16:11:42 +07:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018, hathach (tinyusb.org)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
2014-03-12 14:43:58 +07:00
|
|
|
|
2014-03-24 12:35:44 +07:00
|
|
|
/** \ingroup Group_Common Common Files
|
2014-03-16 22:20:33 +07:00
|
|
|
* \defgroup Group_TimeoutTimer timeout timer
|
|
|
|
* @{ */
|
|
|
|
|
2014-03-12 14:43:58 +07:00
|
|
|
|
2018-07-23 22:32:21 +07:00
|
|
|
#ifndef _TUSB_TIMEOUT_H_
|
|
|
|
#define _TUSB_TIMEOUT_H_
|
2014-03-12 14:43:58 +07:00
|
|
|
|
2018-03-28 13:38:17 +07:00
|
|
|
#include "tusb_compiler.h"
|
2014-03-12 14:43:58 +07:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t start;
|
|
|
|
uint32_t interval;
|
2018-07-23 22:36:29 +07:00
|
|
|
}tu_timeout_t;
|
2014-03-12 14:43:58 +07:00
|
|
|
|
2018-07-23 23:41:14 +07:00
|
|
|
extern uint32_t tusb_hal_millis(void);
|
|
|
|
|
2018-07-23 22:36:29 +07:00
|
|
|
static inline void tu_timeout_set(tu_timeout_t* tt, uint32_t msec)
|
2014-03-12 14:43:58 +07:00
|
|
|
{
|
2018-03-29 18:03:04 +07:00
|
|
|
tt->interval = msec;
|
2018-03-30 15:33:36 +07:00
|
|
|
tt->start = tusb_hal_millis();
|
2014-03-12 14:43:58 +07:00
|
|
|
}
|
|
|
|
|
2018-07-23 22:36:29 +07:00
|
|
|
static inline bool tu_timeout_expired(tu_timeout_t* tt)
|
2014-03-12 14:43:58 +07:00
|
|
|
{
|
2018-03-30 15:33:36 +07:00
|
|
|
return ( tusb_hal_millis() - tt->start ) >= tt->interval;
|
2014-03-12 14:43:58 +07:00
|
|
|
}
|
|
|
|
|
2018-07-23 23:41:14 +07:00
|
|
|
// For used with periodic event to prevent drift
|
|
|
|
static inline void tu_timeout_reset(tu_timeout_t* tt)
|
|
|
|
{
|
|
|
|
tt->start += tt->interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void tu_timeout_restart(tu_timeout_t* tt)
|
|
|
|
{
|
|
|
|
tt->start = tusb_hal_millis();
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:43:58 +07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-23 22:32:21 +07:00
|
|
|
#endif /* _TUSB_TIMEOUT_H_ */
|
2014-03-12 14:43:58 +07:00
|
|
|
|
|
|
|
/** @} */
|