 Hello and welcome everyone to the STM32L4 MOOC online training. This session will be about USB and will be split in two parts. One covering the HDD class and the second covering CDC class. USB stands for universal serial bus. This protocol was developed to create a standard connection between computer peripherals as for example, electronic mouse, keyboards, printers or more recently to connect with smartphones. From the electrical point of view, the USB cable consists of four connectors. Two for applying Vbus and ground reference and two additional for differential signal cables for communication. The first exercise will make use of the HDD USB class which will allow our discovery board to operate as if it was a computer mouse. The implementation of the USB protocol on the LFAR discovery board embeds operating frozen related components. It uses the 48 MHz MSI clock source embedded in the MCU. As MSI does not fulfill the accuracy needs, to achieve the proper accuracy we will use the LSE to trim and calibrate the internal MSI clock source. No external connection or pull-up resistors are needed as everything is featured on the board to communicate with a USB. In this session we will learn how to use the HDD class to communicate with a computer. We will learn how to properly set up the USB clock as the peripheral uses MSI at 48 MHz. As this source is an inaccurate source to improve its accuracy, we will use LSE to trim it. Additionally, we will configure the joystick as input pins and each pin will correspond to a mouse coordinate. We will use STM32 to configure the peripherals and generate code for them. So the overall goal is to develop a new USB-HAT computer mouse application which cursor moves according to the joystick usage. Let's now open STM32 to start generating our code. As a first step we will click on new project. On the new window that opened we will type our part number STM32L476VG. We will select the desired part and we will double click on it. On the peripheral tree we will scroll down and on the USB OTG FS we will change the default mode to device only. As a result two pins will light up in green. Pins PA11 and PA12 which are the communication lines for a USB. We will now scroll up and on the middleware sub-tree we will look for the USB device where we will change the class to human interface device class. As the next step we will enable the LSE to trim the internal clock source. We will open the RCC. We will look for the LSE enabling and we will select Crystal Ceramic Resonator. As a result the LSE pins will become green PC13 and PC14. As a last step in the pinout configuration we need to enable the pins connected to the joystick which are PA1, PA2, PA3 and PA5. We will configure them as inputs and to do so we need to select them individually as inputs. Let's now switch to the clock configuration tab and as the message displayed on the screen suggests the default configuration of the clock domain is not aligned with a recent peripheral configuration. STM32 has a mechanism of self-repair but for demonstration purpose we will manually select the parameters so we will select no. On the multi-speed internal clock the default frequency is 4 MHz and we will change it for 48 MHz which is the necessary frequency to use with USB. The next step will be to switch the USB clock source to MSI and we will manage to solve all issues identified by the STM32 Cubemix. To finalize the initialization we need to go to the configuration tab we will select USB and we will check the Viba sensing feature as this one is not implemented on the Alpha Discovery board it needs to be disabled and on the GPIO peripheral we will set all the joystick pins to pull down. As the pins are floating on VDD level when a key is pressed the value will go to the logic 0. Then we open the RCC tab and we make sure the MSI auto calibration feature is enabled. After everything is configured we just need to save our project with an appropriate name, select system workbench as our IDE and we are ready to generate the code. After the code is generated by the STM32 Cubemix we will enter our main.c file. When implementing a new SPHED mouse program it is important to note that the transmitter, the mouse will send periodically to a computer the receiver data which in a HED class context will be called reports. These reports contain four items the first item contains the status of the buttons, the second the status of the x-axis, the third the status of the aps on axis and the fourth the screw wheel information. In order to be in line with a protocol we will create a buffer which will store all the status to be reported to the computer. After initialization of the buffer we scroll down and on the infinite loop we will monitor all the status of each joystick button. After checking if a button was pressed we will take action and send the report to the computer. So for this purpose we will use the HAL readpin to check the status being the arguments GPIOA and PIN1. We will write an if condition and in case a button was pressed the function will return true. Then we need to fill a report to be sent to the computer and we expect the cursor to move accordingly to the key which was pressed. So we will make use of the HAL function usb-htt-sint-report which uses as arguments the handler of the USB device, the buffer and the length of the buffer. We will apply the very same approach to the rest of the buttons using copy paste. Please don't forget to change the PIN numbers to PA2, PA3 and PA5 and please adjust the coordinates accordingly. To finalize we will build our project and we will enter in debug mode. After pressing the resume button our STM32L for discovery board will be ready to control our computer's mouse and it should move accordingly to the keys we are pressing.