How to add Suspend and Resume support in USB device stack[KSDK 1.3]

Document created by Kan_Li Employee on Mar 28, 2016Last modified by Kan_Li Employee on Mar 29, 2016
Version 1Show Document
  • View in full screen mode

The USB stack in ksdk 1.3 has provided a macro of “USBCFG_DEV_ADVANCED_SUSPEND_RESUME” , while the RM says “suspend/resume is not implemented yet.”, but if you looks into the source code, you may find APIs like USB_Suspend_Service() which is reserved for further implementation, so users may use these APIs as a starting point to add suspend and resume feature in ksdk 1.3.

The test is based on FRDM-KL27Z, dev_hid_mouse_bm demo. And before we modify the device stack, we have to clone a copy of this demo.

 

  1. Set USBCFG_DEV_ADVANCED_SUSPEND_RESUME to 1 to releases APIs related with suspend and resume features.

 

 

But we will have the following errors after compile, that is because remote resume function is not supported by KL27.

so we should disable it for this device, with the help of #if USBCFG_DEV_ADVANCED_SUSPEND_RESUME == 1 && FSL_FEATURE_USB_KHCI_HOST_ENABLED == 1. For example, like below:

 

2.Further implement USB_Suspend_Service() and USB_Resume_Service() to let them notify the upper layer application on the event of suspend/resume. With reference of USB_Error_Service()

 

and Don’t forget to add two more events for suspend and resume.

 

3.USB module has two types of resume interrupt , one is sync resume , issued by the bit of ISTAT[RESUME] bit, the other is async resume, issued by the bit of TRC0[USB_RESUME_INT], so we have to monitor these two interrupt status flag in the ISR, as below:

 

And so we have modify the resume and sleep interrupt services for supporting the async resume interrupt as below:

 

4.Modify application and add code to handle suspend and resume event, in this case, such kind of events are handled in USB_App_Device_Callback()

 

5.Verify the hid_mouse demo with USB 2 CV tool from usb.org

 

Select the FS device with VID 15a2 from the list.

 

Test passed!

 

if you print some messages on the event of suspend and resume, you will see something like below from the Terminal channel.

 

 

6. Now we can add low power mode switch function in the main application code to meet the suspend current limitation specified by USB spec, with reference of power_manager_hal_demo for frdmkl27z, and for this case, add code in USB_App_Device_Callback() of mouse.c to tell the main application when to enter low power mode.

Please also note Don’t enter low power mode in this callback function, as it is called by the interrupt service, so if the device enter low power mode during interrupt, that would prevent the following resume interrupt happen, so the device never wake up!

 

For more details on the implementation, please refer to the attached mouse.c

 

7.Test low power mode current:

  Before test, please do the following modification to the FRDM-KL27Z board:

 

Please note, if your board has the following jumpers, no need to remove R7,R21 and R83, just keep J19 and J22 open during the test.

 

Select the low power mode you are going to test from the Ternimal:

Leave KL27 USB port (J10) open and power the board with USB SDA port, that would make the device enter suspend interrupt out of reset. And you may also see the current vary with power mode switch with the help of USB 2 CV tool. I have tested wait, stop and vlps modes, the current measured under these modes are shown as below:

 

Wait:

 

Stop:

VLPS:

 

So it is recommend using VLPS mode during USB suspend mode as it far below the suspend current specified in USB spec (500uA).

 

8. Patch and demo code

Please replace the following files with the attached one, as well as the mouse.c in dev_hid_mouse_bm demo. Recompile the stack and application code.

C:\Freescale\KSDK_1.3.0\usb\usb_core\device\include\MKL27Z644

usb_device_config.h

C:\Freescale\KSDK_1.3.0\usb\usb_core\device\sources\controller

usb_dev.h

usb_dev.c

usb_framework.c

C:\Freescale\KSDK_1.3.0\usb\usb_core\device\sources\controller\khci

device_khci_interface.c

khci_dev.c

khci_dev.h

C:\Freescale\KSDK_1.3.0\usb\usb_core\device\include

usb_device_stack_interface.h

C:\Freescale\KSDK_1.3.0\examples\frdmkl27z\demo_apps\usb\device\hid\hid_mouse

mouse.c

Attachments

Outcomes