 In this demo I will show you how to work with the low power features of the STM32 USB peripherals in this particular case connected to HID device functionality. So the main point here is the demonstration how to set the low power mode on the USB and what to expect from its usage. So also few more details about the USB. Suspense and the remote wake up. And at the end I will show you what to expect from the VBUS Sensing and why it may be important for us to use it. So as we already told you there is the USB specification feature for the suspend. This means that the host decides to not send the start of RAM anymore and it's on the device to shrink its power consumption from the bus under maximum 10 mA. Even this is not strictly checked by the host that the power consumption of the device is lower than this maximum value. The device still should consume less than this value. So for this purpose it's set like that that the USB peripherals are almost deactivated and the microcontroller is put in the sleep mode. But while in the sleep mode the USB peripherals are still able to wake up the microcontroller upon detection of the wake up push from the host and also is able to generate the wake up event on the bus if needed. Here this hands-on will be a little bit different from the other one. I won't show you the programming step-by-step because it would be quite long and also the testing of this project will be on different PC as I will operate there with the suspend of the PC. So let's start with the theory. The generation of the project is very same like for the other one. So first we create a new project for our microcontroller. Here it's a F446 nuclear board. Activate the OTG full speed also with the VBUS option which will be discussed later. So we have also PA9 usage as you already passed through the hardware section there is more described the obstacles with PA9 usage. So if you are not aware please take a look there. As a standard way we also use the high speed external. Use the PC13 as the GPIO for remote wake up. Configure LEDs and we will use the HID class for the mouse which is quite normal to use with the low power and the remote wake up. I believe you already have such experience when you put your laptop to sleep. In such case the USB bus is suspended and you are able to wake up the PC by the power button and then also the mouse is again activated or it's also possible to wake up the PC by the click of the mouse or some action from the mouse. But for this the remote wake up option needs to be enabled by the PC and by the port where the mouse is connected. So few more configurations we need to do in QPMX. So we need to go into the USB full speed configuration and enable their low power. This will enable the putting the microcontroller into stop mode when the suspend is detected and then we need to little bit change the priorities as there is used a half delay inside of the remote wake up event. So when we press the button there is the activation of remote wake up on the bus and after 10 millisecond the remote wake up is stop. So we need to have higher priority on the cystic compared to USB or to the XT. So in more cleaner way different approach than a whole delay should be used as it's called from interrupt but for the simplicity here all delay is used. And also we want to enable the wake up event of the USB later than it's configured. So now we can generate the project again for the through studio and we can move directly through code. So in the code we will more or less reuse the configuration from the mouse example. So again add there the include and define a buffer. And into the while loop we put the mice movement compared to the previous example with the mouse. We want to use here the button so the movement will be permanent. The button of the nuclear board will be spared for the remote wake up functionality. So now to the more important functionality of this example. So this is a callback of the button so the remote wake up. First we will check that the XT comes from the pin connected to the button. Then the microcontroller is in the correct state so that the remote wake up is enabled and currently the USB is suspended and on top of that the low power is enabled. So if we do this we need to go out from the stop mode and configure again the clock. And after that we can activate the remote wake up using the whole PCD function. This wake up should be on the bus 10 millisecond so delay. And they activate the remote wake up so using these two functions USB host will be waken up from the suspend mode and the bus will be activated again. And according to that we will change the states. On top of that we will need also to modify the IRQ handler of the wake up interrupt which is not together into one interrupt with other USB functionalities. So it has dedicated number for the wake up. And the reason why we need to modify this handler is that without this modification the system clock setting would be called multiple times and now it's implemented in this way that the PL is not configured exactly correctly so it would get stuck as it would try to reconfigure already used PL. So that's the reason why we need to do this handling here. And now with this we are ready to test. So right now I have it connected to a different PC. So I will enable the USB analyzer so you will be able to see at least some stuff in the video but more or less it's intended to follow this description and try it on your side. So now I connect the device so you can see there are some frames for the start of frame and knock transaction and useful transaction. So some data are sent. Now I put the laptop to the sleep mode so no frames, no useful transaction. So now the bus is suspended and when I press the power button of the laptop it's functional again and the LED is toggling again. So let's go to the sleep mode again. So the LED stop with the toggling and again the communication is stopped. So let's press the button and LED is toggling again and the communication is running. So this is quite simple how to make the low power running. The low power on the USB can be used with any kind of cars but the USB peripheral full functionality can be made only with the run mode because in the sleep mode the peripheral is still clocked but the stack itself is triggered by the core so the peripheral can be active for both a horse or device but still you need to be in the run mode to either trigger the host request or to handle the interrupt caused by the device peripheral. So let's move to the second part to demonstration of the VBUS sensing. So as it was already explained VBUS sensing is in the USB specification from two reasons. First one is that without detecting of the VBUS there should be no voltage on the data lines. So if you don't detect the voltage from the host there should be not connected the pull up resistor on either D plus or D minus but in the practice this is not strictly followed or checked but it's in the specification. Second reason more important why to have the VBUS is mainly for the self-powered device that means that the device is powered from the battery and is not dependent only on the power from the bus and without the VBUS sensing the device is not able to differentiate the disconnection and the suspend state because when there is a suspend there is no activity on the bus for the 3ms no start of frames nothing and the device may somehow realize that this is exactly the same like the disconnection so no activity on the bus and for some correct it may be important and also if the device detects the disconnection but still drain more power from the VBUS than it's allowed it may be completely disconnected by the host and then the communication cannot be restored after the wake up from the host So for this we will need to modify the suspend callback So first thing we have in the callback is a delay again this is not a correct approach and it's here more or less for the simplicity because as there is quite large capacitor on the nuclear board it takes a long time to discharge its capacitor so then the peripheral can measure the VBUS and realize whether there is still the connection to the host or not So first the delay then we will check in the registers if there is still the connection to the host and if the connection is still there and the suspend is activated we will toggle the LED and use the suspend mode of the USB peripheral so again the device will be sent to stop on the other hand if the suspend is not used we know that the device was disconnected so we will toggle the LED to make it visible and go to this disconnection event in the library So when we will test the example for you it won't be unfortunately not visible in this video when we detect the suspend, LED free toggle and on this connection, LED1 will toggle and LED2 is toggling why we are in the run mode So let's go to the project here it's already prepared what I discussed you will have it in attachment and in the USB configuration is defined for VBUS test So now you want to enable this defined to test the suspend and disconnection and load it into the microcontroller So I will disable again as we will use it in the stop mode and show one more time the analyzer So let's restart and connect So again we can see the activity and communication and let's disconnect So now LED1 is active LED2 is still blinking and we can see there is no traffic that's exactly the same so like when we put it into the sleep mode and that show us that there is no difference between suspend and disconnection for the USB without the VBUS So we will connect and now it's again active so let's put the PC into sleep and again there is no activity in the end and if we would disable the VBUS Sensing then we would not be able to recognize the disconnection and both disconnection and suspend here would be recognized as a suspend only