From 361928f42983bed4d10443668d8eb3db69ade80f Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 23 Jul 2018 22:32:21 +0700 Subject: rename timeout_timer.h to tusb_timeout.h --- src/common/tusb_timeout.h | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/common/tusb_timeout.h (limited to 'src/common/tusb_timeout.h') diff --git a/src/common/tusb_timeout.h b/src/common/tusb_timeout.h new file mode 100644 index 000000000..400434708 --- /dev/null +++ b/src/common/tusb_timeout.h @@ -0,0 +1,85 @@ +/**************************************************************************/ +/*! + @file tusb_timeout.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2013, hathach (tinyusb.org) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + This file is part of the tinyusb stack. +*/ +/**************************************************************************/ + +/** \ingroup Group_Common Common Files + * \defgroup Group_TimeoutTimer timeout timer + * @{ */ + + +#ifndef _TUSB_TIMEOUT_H_ +#define _TUSB_TIMEOUT_H_ + +#include "tusb_compiler.h" +#include "tusb_hal.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + uint32_t start; + uint32_t interval; +}timeout_timer_t; + +static inline void timeout_set(timeout_timer_t* tt, uint32_t msec) +{ + tt->interval = msec; + tt->start = tusb_hal_millis(); +} + +static inline bool timeout_expired(timeout_timer_t* tt) +{ + return ( tusb_hal_millis() - tt->start ) >= tt->interval; +} + +static inline void timeout_blocking_wait(uint32_t msec) +{ + timeout_timer_t tt; + timeout_set(&tt, msec); + + // blocking delay + while ( !timeout_expired(&tt) ) { } +} + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_TIMEOUT_H_ */ + +/** @} */ -- cgit v1.3.1 From 51903a60c51bd1006bec9266dbe1bfb35e282675 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 23 Jul 2018 22:36:29 +0700 Subject: rename timeout_ API to tu_timeout API --- src/class/hid/hid_device.c | 6 +++--- src/common/tusb_timeout.h | 14 +++++++------- src/host/ehci/ehci.c | 8 ++++---- src/portable/nxp/lpc43xx_lpc18xx/hal_lpc43xx.c | 8 ++++---- 4 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src/common/tusb_timeout.h') diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 4ce927725..82c7e3d3f 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -142,14 +142,14 @@ bool tud_hid_keyboard_send_string(const char* str, uint32_t interval_ms) tud_hid_keyboard_send_char(ch); // Blocking delay - timeout_blocking_wait(interval_ms); + tu_timeout_wait(interval_ms); /* Only need to empty report if the next character is NULL or the same with * the current one, else no need to send */ if ( lookahead == ch || lookahead == 0 ) { tud_hid_keyboard_send_report(NULL); - timeout_blocking_wait(interval_ms); + tu_timeout_wait(interval_ms); } } @@ -170,7 +170,7 @@ bool tud_hid_mouse_busy(void) bool tud_hid_mouse_send(hid_mouse_report_t const *p_report) { - VERIFY( tud_mounted() && !tud_hid_mouse_is_busy() ); + VERIFY( tud_mounted() && !tud_hid_mouse_busy() ); hidd_interface_t * p_hid = &_mse_itf; memcpy(p_hid->report_buf, p_report, sizeof(hid_mouse_report_t)); diff --git a/src/common/tusb_timeout.h b/src/common/tusb_timeout.h index 400434708..7c51ff36f 100644 --- a/src/common/tusb_timeout.h +++ b/src/common/tusb_timeout.h @@ -54,26 +54,26 @@ extern "C" { typedef struct { uint32_t start; uint32_t interval; -}timeout_timer_t; +}tu_timeout_t; -static inline void timeout_set(timeout_timer_t* tt, uint32_t msec) +static inline void tu_timeout_set(tu_timeout_t* tt, uint32_t msec) { tt->interval = msec; tt->start = tusb_hal_millis(); } -static inline bool timeout_expired(timeout_timer_t* tt) +static inline bool tu_timeout_expired(tu_timeout_t* tt) { return ( tusb_hal_millis() - tt->start ) >= tt->interval; } -static inline void timeout_blocking_wait(uint32_t msec) +static inline void tu_timeout_wait(uint32_t msec) { - timeout_timer_t tt; - timeout_set(&tt, msec); + tu_timeout_t tt; + tu_timeout_set(&tt, msec); // blocking delay - while ( !timeout_expired(&tt) ) { } + while ( !tu_timeout_expired(&tt) ) { } } #ifdef __cplusplus diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index 9e326ec53..ba23319ca 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -268,14 +268,14 @@ static tusb_error_t hcd_controller_init(uint8_t hostid) static tusb_error_t hcd_controller_stop(uint8_t hostid) { ehci_registers_t* const regs = get_operational_register(hostid); - timeout_timer_t timeout; + tu_timeout_t timeout; regs->usb_cmd_bit.run_stop = 0; - timeout_set(&timeout, 2); // USB Spec: controller has to stop within 16 uframe = 2 frames - while( regs->usb_sts_bit.hc_halted == 0 && !timeout_expired(&timeout)) {} + tu_timeout_set(&timeout, 2); // USB Spec: controller has to stop within 16 uframe = 2 frames + while( regs->usb_sts_bit.hc_halted == 0 && !tu_timeout_expired(&timeout)) {} - return timeout_expired(&timeout) ? TUSB_ERROR_OSAL_TIMEOUT : TUSB_ERROR_NONE; + return tu_timeout_expired(&timeout) ? TUSB_ERROR_OSAL_TIMEOUT : TUSB_ERROR_NONE; } //--------------------------------------------------------------------+ diff --git a/src/portable/nxp/lpc43xx_lpc18xx/hal_lpc43xx.c b/src/portable/nxp/lpc43xx_lpc18xx/hal_lpc43xx.c index 92f9f1cf0..18745d03e 100644 --- a/src/portable/nxp/lpc43xx_lpc18xx/hal_lpc43xx.c +++ b/src/portable/nxp/lpc43xx_lpc18xx/hal_lpc43xx.c @@ -72,11 +72,11 @@ static void hal_controller_reset(uint8_t rhport) // NXP chip powered with non-host mode --> sts bit is not correctly reflected (*p_reg_usbcmd) |= BIT_(1); -// timeout_timer_t timeout; -// timeout_set(&timeout, 2); // should not take longer the time to stop controller - while( ((*p_reg_usbcmd) & BIT_(1)) /*&& !timeout_expired(&timeout)*/) {} +// tu_timeout_t timeout; +// tu_timeout_set(&timeout, 2); // should not take longer the time to stop controller + while( ((*p_reg_usbcmd) & BIT_(1)) /*&& !tu_timeout_expired(&timeout)*/) {} // -// return timeout_expired(&timeout) ? TUSB_ERROR_OSAL_TIMEOUT : TUSB_ERROR_NONE; +// return tu_timeout_expired(&timeout) ? TUSB_ERROR_OSAL_TIMEOUT : TUSB_ERROR_NONE; } bool tusb_hal_init(void) -- cgit v1.3.1 From 5f6cd4903101b0486771efdb2bb435ddf6cf8e7a Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 23 Jul 2018 23:41:14 +0700 Subject: clean up include --- src/class/hid/hid_device.c | 1 - src/common/tusb_common.h | 6 ++---- src/common/tusb_timeout.h | 15 ++++++++++++++- src/host/ehci/ehci.c | 1 - src/host/ohci/ohci.c | 1 - src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c | 1 - src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c | 1 - 7 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src/common/tusb_timeout.h') diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 82c7e3d3f..7aca8ef8e 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -45,7 +45,6 @@ // INCLUDE //--------------------------------------------------------------------+ #include "common/tusb_common.h" -#include "common/tusb_timeout.h" #include "hid_device.h" #include "device/usbd_pvt.h" diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index ddfceabdb..44f31ffe0 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -61,15 +61,13 @@ //------------- TUSB Option Header -------------// #include "tusb_option.h" -//------------- General Header -------------// +//------------- Common Header -------------// #include "tusb_compiler.h" #include "tusb_verify.h" #include "binary.h" #include "tusb_error.h" -#include "tusb_hal.h" #include "tusb_fifo.h" - -//------------- TUSB Header -------------// +#include "tusb_timeout.h" #include "tusb_types.h" //--------------------------------------------------------------------+ diff --git a/src/common/tusb_timeout.h b/src/common/tusb_timeout.h index 7c51ff36f..9d80f5fe7 100644 --- a/src/common/tusb_timeout.h +++ b/src/common/tusb_timeout.h @@ -45,7 +45,6 @@ #define _TUSB_TIMEOUT_H_ #include "tusb_compiler.h" -#include "tusb_hal.h" #ifdef __cplusplus extern "C" { @@ -56,6 +55,8 @@ typedef struct { uint32_t interval; }tu_timeout_t; +extern uint32_t tusb_hal_millis(void); + static inline void tu_timeout_set(tu_timeout_t* tt, uint32_t msec) { tt->interval = msec; @@ -67,6 +68,18 @@ static inline bool tu_timeout_expired(tu_timeout_t* tt) return ( tusb_hal_millis() - tt->start ) >= tt->interval; } +// 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(); +} + + static inline void tu_timeout_wait(uint32_t msec) { tu_timeout_t tt; diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index ba23319ca..09f2f573a 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -44,7 +44,6 @@ //--------------------------------------------------------------------+ #include "hal/hal.h" #include "osal/osal.h" -#include "common/tusb_timeout.h" #include "../hcd.h" #include "../usbh_hcd.h" diff --git a/src/host/ohci/ohci.c b/src/host/ohci/ohci.c index 73e9d887e..644dbfa0f 100644 --- a/src/host/ohci/ohci.c +++ b/src/host/ohci/ohci.c @@ -44,7 +44,6 @@ //--------------------------------------------------------------------+ #include "hal/hal.h" #include "osal/osal.h" -#include "common/tusb_timeout.h" #include "../hcd.h" #include "../usbh_hcd.h" diff --git a/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c b/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c index 0bb79090e..68eef47e1 100644 --- a/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c +++ b/src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c @@ -51,7 +51,6 @@ #include "common/tusb_common.h" #include "hal/hal.h" #include "osal/osal.h" -#include "common/tusb_timeout.h" #include "device/dcd.h" #include "usbd_dcd.h" diff --git a/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c b/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c index 6c076baa8..0b48ad1e6 100644 --- a/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c +++ b/src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c @@ -46,7 +46,6 @@ #include "common/tusb_common.h" #include "tusb_hal.h" #include "osal/osal.h" -#include "common/tusb_timeout.h" #include "device/dcd.h" #include "dcd_lpc43xx.h" -- cgit v1.3.1