usbtick.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //*****************************************************************************
  2. //
  3. // usbtick.c - Functions related to USB stack tick timer handling.
  4. //
  5. // Copyright (c) 2008-2010 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 AM1808 Sitaraware USB Library and reused from revision 6288
  22. // of the Stellaris USB Library.
  23. //
  24. //*****************************************************************************
  25. #include "hw_types.h"
  26. #include "debug.h"
  27. #include "usblib.h"
  28. #include "usblibpriv.h"
  29. //*****************************************************************************
  30. //
  31. //! \addtogroup general_usblib_api
  32. //! @{
  33. //
  34. //*****************************************************************************
  35. //*****************************************************************************
  36. //
  37. // These are the internal timer tick handlers used by the USB stack. Handlers
  38. // in g_pfTickHandlers are called in the context of the USB SOF interrupt
  39. // every USB_SOF_TICK_DIVIDE milliseconds.
  40. //
  41. //*****************************************************************************
  42. tUSBTickHandler g_pfTickHandlers[USB_TICK_HANDLER_NUM];
  43. void *g_pvTickInstance[USB_TICK_HANDLER_NUM];
  44. //*****************************************************************************
  45. //
  46. // Flag to indicate whether or not we have been initialized.
  47. //
  48. //*****************************************************************************
  49. tBoolean g_bUSBTimerInitialized = false;
  50. //*****************************************************************************
  51. //
  52. // This is the current tick value in ms for the system. This is used for all
  53. // instances of USB controllers and for all timer tick handlers.
  54. //
  55. //*****************************************************************************
  56. unsigned int g_ulCurrentUSBTick = 0;
  57. //*****************************************************************************
  58. //
  59. // This is the total number of SOF interrupts received since the system
  60. // booted. The value is incremented by the low level device- or host-interrupt
  61. // handler functions.
  62. //
  63. //*****************************************************************************
  64. unsigned int g_ulUSBSOFCount = 0;
  65. //*****************************************************************************
  66. //
  67. // This internal function initializes the variables used in processing timer
  68. // ticks.
  69. //
  70. // This function should only be called from within the USB library. It is set
  71. // up to ensure that it can be called multiple times if necessary without
  72. // the previous configuration being erased (to cater for OTG mode switching).
  73. //
  74. // \return None.
  75. //
  76. //*****************************************************************************
  77. void
  78. InternalUSBTickInit(void)
  79. {
  80. unsigned int ulLoop;
  81. if(!g_bUSBTimerInitialized)
  82. {
  83. for(ulLoop = 0; ulLoop < USB_TICK_HANDLER_NUM; ulLoop++)
  84. {
  85. g_pfTickHandlers[ulLoop] = (tUSBTickHandler)0;
  86. g_pvTickInstance[ulLoop] = 0;
  87. }
  88. g_bUSBTimerInitialized = true;
  89. }
  90. }
  91. //*****************************************************************************
  92. //
  93. // This internal function handles registering OTG, Host, or Device SOF timer
  94. // handler functions.
  95. //
  96. // \param ulHandler specifies which type of handler to register.
  97. // \param pfHandler specifies the handler to call for the given type of
  98. // handler.
  99. //
  100. // This function should only be called inside the USB library and only as a
  101. // result to a call to reinitialize the stack in a new mode. Currently the
  102. // following 3 types of timer tick handlers can be registered:
  103. // TICK_HANDLER_OTG, TICK_HANDLER_HOST, or TICK_HANDLER_DEVICE. Handlers
  104. // registered via this function are called in the context of the SOF interrupt.
  105. //
  106. // \return None.
  107. //
  108. //*****************************************************************************
  109. void
  110. InternalUSBRegisterTickHandler(unsigned int ulHandler,
  111. tUSBTickHandler pfHandler,
  112. void *pvInstance)
  113. {
  114. ASSERT(ulHandler < USB_TICK_HANDLER_NUM);
  115. //
  116. // Save the handler.
  117. //
  118. g_pfTickHandlers[ulHandler] = pfHandler;
  119. //
  120. // Save the instance data.
  121. //
  122. g_pvTickInstance[ulHandler] = pvInstance;
  123. }
  124. //*****************************************************************************
  125. //
  126. //! \internal
  127. //!
  128. //! Calls internal handlers in response to a tick based on the start of frame
  129. //! interrupt.
  130. //!
  131. //! \param ulTicksmS specifies how many milliseconds have passed since the last
  132. //! call to this function.
  133. //!
  134. //! This function is called every 5mS in the context of the Start of Frame
  135. //! (SOF) interrupt. It is used to call any registered internal tick
  136. //! functions.
  137. //!
  138. //! This function should only be called from within the USB library.
  139. //!
  140. //! \return None.
  141. //
  142. //*****************************************************************************
  143. void InternalUSBStartOfFrameTick(unsigned int ulTicksmS,
  144. unsigned int ulIndex)
  145. {
  146. int iIdx;
  147. //
  148. // Advance time.
  149. //
  150. g_ulCurrentUSBTick += ulTicksmS;
  151. //
  152. // Call any registered SOF tick handlers.
  153. //
  154. for(iIdx = 0; iIdx < USB_TICK_HANDLER_NUM; iIdx++)
  155. {
  156. if(g_pfTickHandlers[iIdx])
  157. {
  158. g_pfTickHandlers[iIdx](g_pvTickInstance[iIdx], ulTicksmS, ulIndex);
  159. }
  160. }
  161. }
  162. //*****************************************************************************
  163. //
  164. // Close the Doxygen group.
  165. //! @}
  166. //
  167. //*****************************************************************************