+/rtos

git clone https://git.julia.blue/+/rtos

Joseph RyanRe-add usblib files83915e3

master
7.1 KiB204 linesraw
1//*****************************************************************************
2//
3// usbtick.c - Functions related to USB stack tick timer handling.
4//
5// Copyright (c) 2008-2017 Texas Instruments Incorporated.  All rights reserved.
6// Software License Agreement
7//
8// Texas Instruments (TI) is supplying this software for use solely and
9// exclusively on TI's microcontroller products. The software is owned by
10// TI and/or its suppliers, and is protected under applicable copyright
11// laws. You may not combine this software with "viral" open-source
12// software in order to form a larger program.
13//
14// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
15// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
16// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
18// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
19// DAMAGES, FOR ANY REASON WHATSOEVER.
20//
21// This is part of revision 2.1.4.178 of the Tiva USB Library.
22//
23//*****************************************************************************
24
25#include "tivaware/hw_types.h"
26#include "usblib/usblib.h"
27#include "usblib/usblibpriv.h"
28#include <stdbool.h>
29#include <stdint.h>
30
31//*****************************************************************************
32//
33//! \addtogroup general_usblib_api
34//! @{
35//
36//*****************************************************************************
37
38//*****************************************************************************
39//
40// These are the internal timer tick handlers used by the USB stack.  Handlers
41// in g_pfnTickHandlers are called in the context of the USB SOF interrupt
42// every USB_SOF_TICK_DIVIDE milliseconds.
43//
44//*****************************************************************************
45tUSBTickHandler g_pfnTickHandlers[MAX_USB_TICK_HANDLERS];
46void* g_pvTickInstance[MAX_USB_TICK_HANDLERS];
47
48//*****************************************************************************
49//
50// Flag to indicate whether or not we have been initialized.
51//
52//*****************************************************************************
53bool g_bUSBTimerInitialized = false;
54
55//*****************************************************************************
56//
57// This is the current tick value in ms for the system.  This is used for all
58// instances of USB controllers and for all timer tick handlers.
59//
60//*****************************************************************************
61uint32_t g_ui32CurrentUSBTick = 0;
62
63//*****************************************************************************
64//
65// This is the total number of SOF interrupts received since the system
66// booted.  The value is incremented by the low level device- or host-interrupt
67// handler functions.
68//
69//*****************************************************************************
70uint32_t g_ui32USBSOFCount = 0;
71
72//*****************************************************************************
73//
74// This internal function initializes the variables used in processing timer
75// ticks.
76//
77// This function should only be called from within the USB library.  It is set
78// up to ensure that it can be called multiple times if necessary without
79// the previous configuration being erased (to cater for OTG mode switching).
80//
81// \return None.
82//
83//*****************************************************************************
84void InternalUSBTickInit(void) {
85    uint32_t ui32Loop;
86
87    if (!g_bUSBTimerInitialized) {
88        for (ui32Loop = 0; ui32Loop < MAX_USB_TICK_HANDLERS; ui32Loop++) {
89            g_pfnTickHandlers[ui32Loop] = (tUSBTickHandler)0;
90            g_pvTickInstance[ui32Loop] = 0;
91        }
92
93        g_bUSBTimerInitialized = true;
94    }
95}
96
97//*****************************************************************************
98//
99// This internal function resets the USB tick handler.
100//
101// This function should only be called from within the USB library.  It will
102// clear out the tick handler state and should be called to allow the tick
103// handlers to be initialized once USBDCDInit() function is called.
104//
105// \return None.
106//
107//*****************************************************************************
108void InternalUSBTickReset(void) {
109    //
110    // Reset the initialized flag so that the next time InternalUSBTickInit()
111    // is called.
112    //
113    g_bUSBTimerInitialized = 0;
114}
115
116//*****************************************************************************
117//
118// This internal function handles registering OTG, Host, or Device SOF timer
119// handler functions.
120//
121// \param pfHandler specifies the handler to call for the given type of
122// handler.
123// \param pvInstance is the instance pointer that will be returned to the
124// function provided in the \e pfHandler function.
125//
126// This function should only be called inside the USB library and only as a
127// result to a call to reinitialize the stack in a new mode.  Currently the
128// following 3 types of timer tick handlers can be registered:
129// TICK_HANDLER_OTG, TICK_HANDLER_HOST, or TICK_HANDLER_DEVICE.  Handlers
130// registered via this function are called in the context of the SOF interrupt.
131//
132// \return A value of zero means that the tick handler was registered and any
133// other value indicates an error.
134//
135//*****************************************************************************
136int32_t InternalUSBRegisterTickHandler(tUSBTickHandler pfHandler,
137                                       void* pvInstance) {
138    int32_t i32Idx;
139
140    for (i32Idx = 0; i32Idx < MAX_USB_TICK_HANDLERS; i32Idx++) {
141        if (g_pfnTickHandlers[i32Idx] == 0) {
142            //
143            // Save the handler.
144            //
145            g_pfnTickHandlers[i32Idx] = pfHandler;
146
147            //
148            // Save the instance data.
149            //
150            g_pvTickInstance[i32Idx] = pvInstance;
151
152            break;
153        }
154    }
155
156    if (i32Idx == MAX_USB_TICK_HANDLERS) {
157        return (-1);
158    }
159    return (0);
160}
161
162//*****************************************************************************
163//
164//! \internal
165//!
166//! Calls internal handlers in response to a tick based on the start of frame
167//! interrupt.
168//!
169//! \param ui32TicksmS specifies how many milliseconds have passed since the
170//! last call to this function.
171//!
172//! This function is called every 5mS in the context of the Start of Frame
173//! (SOF) interrupt.  It is used to call any registered internal tick
174//! functions.
175//!
176//! This function should only be called from within the USB library.
177//!
178//! \return None.
179//
180//*****************************************************************************
181void InternalUSBStartOfFrameTick(uint32_t ui32TicksmS) {
182    int32_t i32Idx;
183
184    //
185    // Advance time.
186    //
187    g_ui32CurrentUSBTick += ui32TicksmS;
188
189    //
190    // Call any registered SOF tick handlers.
191    //
192    for (i32Idx = 0; i32Idx < MAX_USB_TICK_HANDLERS; i32Idx++) {
193        if (g_pfnTickHandlers[i32Idx]) {
194            g_pfnTickHandlers[i32Idx](g_pvTickInstance[i32Idx], ui32TicksmS);
195        }
196    }
197}
198
199//*****************************************************************************
200//
201// Close the Doxygen group.
202//! @}
203//
204//*****************************************************************************