usb_interrupt.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /***********************************************************************************
  2. Filename: usb_interrupt.h
  3. Description: USB library interrupt initialisation and ISR.
  4. ***********************************************************************************/
  5. #ifndef USBINTERRUPT_H
  6. #define USBINTERRUPT_H
  7. /** \addtogroup module_usb_interrupt USB Interrupt (usbirq)
  8. * \brief This module contains the USB interrupt handler, which converts USB interrupts to USBIRQ events.
  9. *
  10. * This module contains two interrupt service routines:
  11. * \li P0 ISR
  12. * \li P2 ISR
  13. * Both these are used by the USB part of the MCU. Hence it is recommended to only use P1 for
  14. * interrupts from any peripherals connected to the MCU. Unless running low on GPIO pins, one should
  15. * generally avoid using the P0 and P2 pins at all.
  16. *
  17. * The MCU contains three interrupt flag registers, USBCIE, USBIIE and USBOIE, which are all cleared
  18. * upon read access. The \ref module_usb_interrupt module encapsulates the USB interrupts, and saves the
  19. * three flag registers in a single 16-bit word. By doing that it becomes possible to process
  20. * high-priority events in the interrupt context, and low-priority events in the main loop.
  21. *
  22. * \section section_usbirq_initialization Initialization
  23. * After initializing the \ref module_usb_framework module, \c main() must call \ref usbirqInit(). The
  24. * \c irqMask parameter of this function shall contain all \c USBIRQ_EVENT bits that will be handled
  25. * either in the interrupt or in \c main(). Note, however, that the event reporting may not always be
  26. * necessary. For instance, there is usually no reason to enable \c USBIRQ_EVENT_EPxIN or
  27. * \c USBIRQ_EVENT_EPxOUT events when handling low-priority transfers in the \c main() loop. In these
  28. * cases it is simpler and more efficient to check the arming condition of the endpoint in question.
  29. *
  30. * The following example enables the setup and reset events (which must always be enabled!), and turns on
  31. * global interrupts:
  32. * \code
  33. * void main(void) {
  34. *
  35. * ... Initialize the crystal oscillator and USB framework first ...
  36. *
  37. * // Initialize the USB Interrupt module
  38. * usbirqInit(USBIRQ_EVENT_RESET | USBIRQ_EVENT_SUSPEND | USBIRQ_EVENT_RESUME | USBIRQ_EVENT_SETUP);
  39. *
  40. * // Turn on interrupts
  41. * INT_GLOBAL_ENABLE();
  42. *
  43. * // Main loop
  44. * while (1) {
  45. * ...
  46. * }
  47. * \endcode
  48. *
  49. * \section section_usbirq_event_processing Event Processing
  50. * Regardless of whether the interrupt event is processed in the interrupt or in the main loop, the code
  51. * piece for doing it is the same (this example illustrates the processing of \ref USBIRQ_EVENT_RESET
  52. * events):
  53. * \code
  54. * // Let the framework handle reset events :)
  55. * if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_RESET) {
  56. * USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_RESET);
  57. * usbfwResetHandler();
  58. * }
  59. * \endcode
  60. *
  61. * \section Hooks
  62. * The following hook is called from the USB interrupt, and allows for event processing in the interrupt
  63. * context:
  64. * \code
  65. * void usbirqHookProcessEvents(void) {
  66. * // Process high-priority events here, or simply return if there are none
  67. * }
  68. * \endcode
  69. * @{
  70. */
  71. #include "usb_framework_structs.h"
  72. #ifdef EXTERN
  73. #undef EXTERN
  74. #endif
  75. #ifdef USBINTERRUPT_C
  76. #define EXTERN ///< Definition used only for usb_interrupt.c to declare variable
  77. #else
  78. #define EXTERN extern ///< Definition used in other source files to declare external
  79. #endif
  80. //-------------------------------------------------------------------------------------------------------
  81. /// USBIRQ internal module data
  82. /*typedef struct {
  83. uint16 eventMask; ///< Bit mask containing all pending events (see the \c USBIRQ_EVENT definitions)
  84. BOOL inSuspend; ///< Is currently in suspend?
  85. uint16 irqMask; ///< USB interrupts to be enabled
  86. } USBIRQ_DATA;*/
  87. #ifdef USBIRQ_DATA_ADDR
  88. EXTERN __no_init __data USBIRQ_DATA usbirqData @ USBIRQ_DATA_ADDR; ///< USBIRQ internal module data at fixed address
  89. #else
  90. EXTERN USBIRQ_DATA __data usbirqData; ///< USBIRQ internal module data
  91. #endif
  92. //-------------------------------------------------------------------------------------------------------
  93. //-------------------------------------------------------------------------------------------------------
  94. /// \name USB Interrupt Events
  95. //@{
  96. /// Suspend signaling detected on the USB bus
  97. /// Note that the chip should not enter PM1 while still in the interrupt routine (inside the usbirqHookProcessEvents() function )
  98. #define USBIRQ_EVENT_SUSPEND 0x0001
  99. /// Resume signaling detected on the USB bus
  100. #define USBIRQ_EVENT_RESUME 0x0002
  101. /// Reset signaling detected on the USB bus (call \ref usbfwResetHandler() for processing)
  102. #define USBIRQ_EVENT_RESET 0x0004
  103. /// Start of frame token received (synthesized by hardware when the next SOF token is expected, so that missing or corrupted tokens have no effect)
  104. #define USBIRQ_EVENT_START_OF_FRAME 0x0008
  105. /// Endpoint 0 IN/OUT setup/data transfer complete / stall sent / premature completion (call \ref usbfwSetupHandler() for processing)
  106. #define USBIRQ_EVENT_SETUP 0x0010
  107. /// Endpoint 1 IN data successfully transmitted to host (FIFO disarmed) / FIFO flushed / stall sent
  108. #define USBIRQ_EVENT_EP1IN 0x0020
  109. /// Endpoint 2 IN data successfully transmitted to host (FIFO disarmed) / FIFO flushed / stall sent
  110. #define USBIRQ_EVENT_EP2IN 0x0040
  111. /// Endpoint 3 IN data successfully transmitted to host (FIFO disarmed) / FIFO flushed / stall sent
  112. #define USBIRQ_EVENT_EP3IN 0x0080
  113. /// Endpoint 4 IN data successfully transmitted to host (FIFO disarmed) / FIFO flushed / stall sent
  114. #define USBIRQ_EVENT_EP4IN 0x0100
  115. /// Endpoint 5 IN data successfully transmitted to host (FIFO disarmed) / FIFO flushed / stall sent
  116. #define USBIRQ_EVENT_EP5IN 0x0200
  117. /// Endpoint 1 OUT data received from host (FIFO disarmed) / stall sent
  118. #define USBIRQ_EVENT_EP1OUT 0x0400
  119. /// Endpoint 2 OUT data received from host (FIFO disarmed) / stall sent
  120. #define USBIRQ_EVENT_EP2OUT 0x0800
  121. /// Endpoint 3 OUT data received from host (FIFO disarmed) / stall sent
  122. #define USBIRQ_EVENT_EP3OUT 0x1000
  123. /// Endpoint 4 OUT data received from host (FIFO disarmed) / stall sent
  124. #define USBIRQ_EVENT_EP4OUT 0x2000
  125. /// Endpoint 5 OUT data received from host (FIFO disarmed) / stall sent
  126. #define USBIRQ_EVENT_EP5OUT 0x4000
  127. //@}
  128. //-------------------------------------------------------------------------------------------------------
  129. //-------------------------------------------------------------------------------------------------------
  130. /// \name Interrupt Mask Access Macros
  131. //@{
  132. /// Clears one or more events (use one or more \c USBIRQ_EVENT bits OR'ed together)
  133. #define USBIRQ_CLEAR_EVENTS(mask) (usbirqData.eventMask &= ~(mask))
  134. /// Get the bit mask containing all pending events
  135. #define USBIRQ_GET_EVENT_MASK() (usbirqData.eventMask)
  136. //@}
  137. //-------------------------------------------------------------------------------------------------------
  138. //-------------------------------------------------------------------------------------------------------
  139. /// \name Interrupt Event Hooks
  140. //@{
  141. /// Called upon all USB interrupts for high-priority event processing
  142. void usbirqHookProcessEvents(void);
  143. //@}
  144. //-------------------------------------------------------------------------------------------------------
  145. //-------------------------------------------------------------------------------------------------------
  146. // Function prototypes
  147. void usbirqInit(uint16 irqMask);
  148. #if defined HAL_SB_BOOT_CODE
  149. void usbirqHandler(void);
  150. #else
  151. __interrupt void usbirqHandler(void);
  152. #endif
  153. //-------------------------------------------------------------------------------------------------------
  154. //@}
  155. /*
  156. +------------------------------------------------------------------------------
  157. | Copyright 2004-2007 Texas Instruments Incorporated. All rights reserved.
  158. |
  159. | IMPORTANT: Your use of this Software is limited to those specific rights
  160. | granted under the terms of a software license agreement between the user who
  161. | downloaded the software, his/her employer (which must be your employer) and
  162. | Texas Instruments Incorporated (the "License"). You may not use this Software
  163. | unless you agree to abide by the terms of the License. The License limits
  164. | your use, and you acknowledge, that the Software may not be modified, copied
  165. | or distributed unless embedded on a Texas Instruments microcontroller or used
  166. | solely and exclusively in conjunction with a Texas Instruments radio
  167. | frequency transceiver, which is integrated into your product. Other than for
  168. | the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  169. | works of, modify, distribute, perform, display or sell this Software and/or
  170. | its documentation for any purpose.
  171. |
  172. | YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  173. | PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  174. | INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  175. | NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  176. | TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  177. | NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  178. | LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING
  179. | BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
  180. | CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
  181. | SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  182. | (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  183. |
  184. | Should you have any questions regarding your right to use this Software,
  185. | contact Texas Instruments Incorporated at www.TI.com.
  186. |
  187. +------------------------------------------------------------------------------
  188. */
  189. #endif