usb_suspend.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /***********************************************************************************
  2. Filename: usb_suspend.c
  3. Description: USB library common functionality.
  4. ***********************************************************************************/
  5. /// \addtogroup module_usb_suspend
  6. /// @{
  7. #include "usb_firmware_library_headers.h"
  8. #include "hal_board.h"
  9. #include "hal_mcu.h"
  10. #include "hal_led.h"
  11. __xdata VFPTR pFnSuspendEnterHook= NULL;
  12. __xdata VFPTR pFnSuspendExitHook= NULL;
  13. #if HAL_UART_USB_SUSPEND
  14. extern void halEnterPowerMode(void);
  15. /** \brief Puts the chip into power mode 1 during USB suspend.
  16. *
  17. * This function must be called from main (i.e. not from interrupt context) upon the reception of a
  18. * \ref USBIRQ_EVENT_SUSPEND event. To comply with the USB specification, this must happen within 10 ms
  19. * after the event occurs. The chip will stay in power mode 1 until a USB resume or USB reset is detected
  20. * on the USB bus, or remote wakeup is used. During this period, the MCU can only run code from
  21. * interrupt context.
  22. */
  23. void usbsuspEnter(void)
  24. {
  25. if (pFnSuspendEnterHook!=NULL)
  26. pFnSuspendEnterHook();
  27. HAL_USB_INT_CLEAR();
  28. HAL_USB_INT_ENABLE();
  29. // Disable USB clock (PLL) before entering PM1
  30. HAL_USB_PLL_DISABLE();
  31. HAL_LED_CLR_1();
  32. do {
  33. // Enter PM1, in prescribed manner as explained in CC253x User's Guide
  34. SLEEPCMD = 0x05;
  35. halEnterPowerMode();
  36. } while ( usbirqData.inSuspend );
  37. // .... we are now up and running again
  38. // First make sure that the crystal oscillator is stable
  39. while (!CC2530_IS_XOSC_STABLE());
  40. // Restart the USB clock (PLL)
  41. HAL_USB_ENABLE();
  42. if (pFnSuspendExitHook!=NULL)
  43. pFnSuspendExitHook();
  44. } // usbsuspEnter
  45. #endif
  46. /** \brief Attempts USB remote wakeup.
  47. *
  48. * This function can be called from interrupt context while the USB device is suspend mode. If the device
  49. * is privileged to do so (see \c usbfwData.remoteWakeup and the \ref USBSR_EVENT_REMOTE_WAKEUP_ENABLED
  50. * and \ref USBSR_EVENT_REMOTE_WAKEUP_DISABLED events), remote wakeup will be performed. Note that this
  51. * function will block for 10 ms while the resume signal is set on the bus. Note: This function can only
  52. * be called when the 48 MHz XOSC is stable.
  53. *
  54. * \return
  55. * \c TRUE if the remote wakeup was performed (the privilege had been granted), otherwise \c FALSE
  56. * (the device is still in suspend mode).
  57. */
  58. uint8 usbsuspDoRemoteWakeup(void)
  59. {
  60. extern void halMcuWaitMs(uint16 msec);
  61. halIntState_t intState;
  62. // Make sure that it's OK
  63. if (!usbfwData.remoteWakeup) return FALSE;
  64. HAL_ENTER_CRITICAL_SECTION(intState);
  65. // Make sure that the suspend loop does not power down the chip again
  66. usbirqData.inSuspend = FALSE;
  67. // Perform remote wakeup by holding the USB resume signal for 10 ms
  68. USBPOW |= USBPOW_RESUME;
  69. halMcuWaitMs(10);
  70. USBPOW &= ~USBPOW_RESUME;
  71. // Clear the interrupt flag
  72. HAL_USB_INT_CLEAR();
  73. HAL_EXIT_CRITICAL_SECTION(intState);
  74. return TRUE;
  75. } // usbsuspDoRemoteWakeup
  76. //@}
  77. /*
  78. +------------------------------------------------------------------------------
  79. | Copyright 2004-2009 Texas Instruments Incorporated. All rights reserved.
  80. |
  81. | IMPORTANT: Your use of this Software is limited to those specific rights
  82. | granted under the terms of a software license agreement between the user who
  83. | downloaded the software, his/her employer (which must be your employer) and
  84. | Texas Instruments Incorporated (the "License"). You may not use this Software
  85. | unless you agree to abide by the terms of the License. The License limits
  86. | your use, and you acknowledge, that the Software may not be modified, copied
  87. | or distributed unless embedded on a Texas Instruments microcontroller or used
  88. | solely and exclusively in conjunction with a Texas Instruments radio
  89. | frequency transceiver, which is integrated into your product. Other than for
  90. | the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  91. | works of, modify, distribute, perform, display or sell this Software and/or
  92. | its documentation for any purpose.
  93. |
  94. | YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  95. | PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  96. | INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  97. | NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  98. | TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  99. | NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  100. | LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING
  101. | BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
  102. | CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
  103. | SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  104. | (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  105. |
  106. | Should you have any questions regarding your right to use this Software,
  107. | contact Texas Instruments Incorporated at www.TI.com.
  108. |
  109. +------------------------------------------------------------------------------
  110. */