//############################################################################# // FILE: usbtick.c // TITLE: Functions related to USB stack tick timer handling //############################################################################# //! //! Copyright: Copyright (C) 2023 Texas Instruments Incorporated - //! All rights reserved not granted herein. //! Limited License. //! //! Texas Instruments Incorporated grants a world-wide, royalty-free, //! non-exclusive license under copyrights and patents it now or hereafter //! owns or controls to make, have made, use, import, offer to sell and sell //! ("Utilize") this software subject to the terms herein. With respect to the //! foregoing patent license, such license is granted solely to the extent that //! any such patent is necessary to Utilize the software alone. The patent //! license shall not apply to any combinations which include this software, //! other than combinations with devices manufactured by or for TI //! ("TI Devices"). //! No hardware patent is licensed hereunder. //! //! Redistributions must preserve existing copyright notices and reproduce this //! license (including the above copyright notice and the disclaimer and //! (if applicable) source code license limitations below) in the documentation //! and/or other materials provided with the distribution. //! //! Redistribution and use in binary form, without modification, are permitted //! provided that the following conditions are met: //! //! * No reverse engineering, decompilation, or disassembly of this software is //! permitted with respect to any software provided in binary form. //! * Any redistribution and use are licensed by TI for use only //! with TI Devices. //! * Nothing shall obligate TI to provide you with source code for the //! software licensed and provided to you in object code. //! //! If software source code is provided to you, modification and redistribution //! of the source code are permitted provided that the following conditions //! are met: //! //! * any redistribution and use of the source code, including any resulting //! derivative works, are licensed by TI for use only with TI Devices. //! * any redistribution and use of any object code compiled from the source //! code and any resulting derivative works, are licensed by TI for use //! only with TI Devices. //! //! Neither the name of Texas Instruments Incorporated nor the names of its //! suppliers may be used to endorse or promote products derived from this //! software without specific prior written permission. //############################################################################# #include #include #include "inc/hw_types.h" #include "debug.h" #include "include/usblib.h" #include "include/usblibpriv.h" //***************************************************************************** // //! \addtogroup general_usblib_api //! @{ // //***************************************************************************** //***************************************************************************** // // These are the internal timer tick handlers used by the USB stack. Handlers // in g_pfnTickHandlers are called in the context of the USB SOF interrupt // every USB_SOF_TICK_DIVIDE milliseconds. // //***************************************************************************** tUSBTickHandler g_pfnTickHandlers[MAX_USB_TICK_HANDLERS]; void *g_pvTickInstance[MAX_USB_TICK_HANDLERS]; //***************************************************************************** // // Flag to indicate whether or not we have been initialized. // //***************************************************************************** bool g_bUSBTimerInitialized = false; //***************************************************************************** // // This is the current tick value in ms for the system. This is used for all // instances of USB controllers and for all timer tick handlers. // //***************************************************************************** uint32_t g_ui32CurrentUSBTick = 0; //***************************************************************************** // // This is the total number of SOF interrupts received since the system // booted. The value is incremented by the low level device- or host-interrupt // handler functions. // //***************************************************************************** uint32_t g_ui32USBSOFCount = 0; //***************************************************************************** // // This internal function initializes the variables used in processing timer // ticks. // // This function should only be called from within the USB library. It is set // up to ensure that it can be called multiple times if necessary without // the previous configuration being erased (to cater for OTG mode switching). // // \return None. // //***************************************************************************** void InternalUSBTickInit(void) { uint32_t ui32Loop; if(!g_bUSBTimerInitialized) { for(ui32Loop = 0; ui32Loop < MAX_USB_TICK_HANDLERS; ui32Loop++) { g_pfnTickHandlers[ui32Loop] = (tUSBTickHandler)0; g_pvTickInstance[ui32Loop] = 0; } g_bUSBTimerInitialized = true; } } //***************************************************************************** // // This internal function resets the USB tick handler. // // This function should only be called from within the USB library. It will // clear out the tick handler state and should be called to allow the tick // handlers to be initialized once USBDCDInit() function is called. // // \return None. // //***************************************************************************** void InternalUSBTickReset(void) { // // Reset the initialized flag so that the next time InternalUSBTickInit() // is called. // g_bUSBTimerInitialized = 0; } //***************************************************************************** // // This internal function handles registering OTG, Host, or Device SOF timer // handler functions. // // \param pfHandler specifies the handler to call for the given type of // handler. // \param pvInstance is the instance pointer that will be returned to the // function provided in the \e pfHandler function. // // This function should only be called inside the USB library and only as a // result to a call to reinitialize the stack in a new mode. Currently the // following 3 types of timer tick handlers can be registered: // TICK_HANDLER_OTG, TICK_HANDLER_HOST, or TICK_HANDLER_DEVICE. Handlers // registered via this function are called in the context of the SOF interrupt. // // \return A value of zero means that the tick handler was registered and any // other value indicates an error. // //***************************************************************************** int32_t InternalUSBRegisterTickHandler(tUSBTickHandler pfHandler, void *pvInstance) { int32_t i32Idx; for(i32Idx = 0; i32Idx < MAX_USB_TICK_HANDLERS; i32Idx++) { if(g_pfnTickHandlers[i32Idx] == 0) { // // Save the handler. // g_pfnTickHandlers[i32Idx] = pfHandler; // // Save the instance data. // g_pvTickInstance[i32Idx] = pvInstance; break; } } if(i32Idx == MAX_USB_TICK_HANDLERS) { return(-1); } return(0); } //***************************************************************************** // //! \internal //! //! Calls internal handlers in response to a tick based on the start of frame //! interrupt. //! //! \param ui32TicksmS specifies how many milliseconds have passed since the //! last call to this function. //! //! This function is called every 5mS in the context of the Start of Frame //! (SOF) interrupt. It is used to call any registered internal tick //! functions. //! //! This function should only be called from within the USB library. //! //! \return None. // //***************************************************************************** void InternalUSBStartOfFrameTick(uint32_t ui32TicksmS) { int32_t i32Idx; // // Advance time. // g_ui32CurrentUSBTick += ui32TicksmS; // // Call any registered SOF tick handlers. // for(i32Idx = 0; i32Idx < MAX_USB_TICK_HANDLERS; i32Idx++) { if(g_pfnTickHandlers[i32Idx]) { g_pfnTickHandlers[i32Idx](g_pvTickInstance[i32Idx], ui32TicksmS); } } } //***************************************************************************** // // Close the Doxygen group. //! @} // //*****************************************************************************