 Hello and welcome to the STM32L4 MOOC online training. My name is André Barata and this is the second part of the USB dedicated session. USB session was split in two. The first one regarding the Human Interference class or HAD and this one will be about the USB Communications Device class or CSD class. In case you want to have access to the theory introduction about USB, I would recommend you to start on the first USB session. In this session we will learn how to use the USB SDC class which will allow us to exchange communication message between our Discovery L4 board and another USB device, which in our case will be a computer. The objective of this hands-on is to learn how to properly set up the USB CDC class on STM32QMX. We will also learn how to properly set up the USB clock as the peripheral uses the MSI clock source at 48 MHz which lacks in accuracy to improve it we will use the LSE to trim it. The main goal of this session is to create a bi-directional connection using the virtual COM port on the STM32L4 Discovery board and then exchange message between the board and the computer. As always we will configure all peripherals and generate code with the STM32QMX. Let's now open the STM32QMX to get started on our hands-on. As a first step we will click on new project and on a new window we will type our part number STM32L476VG. We will double click on our part and we will start a new project. On the peripheral tree we will scroll down and on the USB OTGFS we will change the mode to device only. As a result two pins will light up in green PA11 and PA12 which are the communication lines. Then we will scroll up to the middle or sub-3 and we will look for the USB device which changed the class to communication device class which refers to the virtual COM port. To finalize this step we will go to the RCC and we will enable and select crystal ceramic resonator. As a result two other pins will become green PC13 and PC14. Now we will change to the clock tab. To be in line with the recent configurations we will manually change the MSI clock from 4 to 48 MHz and then in the USB clock tree we will select MSI as clock source. Now we proceed to the configuration tab and we will select USB. We will go to the VBUS sensing feature and as this one is not implemented on the L4 discovery board it needs to be disabled. Then we open the RCC tab and we make sure that the MSI auto calibration is enabled. We press OK and all the changes will be set. At this point we are ready to save our project and generate our code. To never forget to select the system workbench for STM32 as your chosen IDE. We will press OK, we generate our code and we will open now the system workbench for STM32. After our project is generated we will open it and we will go to our main.c file. As a first step we need to create two buffers one to transmit data and another to receive data from a new USB device. So for transmitting data we create a TX user buffer with 2048 bytes which is the maximum supported length by this STM32 L4 to virtual comport. We fill the buffer with a test message which in our case will be the ASCII representation for STM32. The second buffer will be the Rx user buffer with the same length and we will leave it empty as we are expecting to receive data from a USB device or in this case from our computer. Now we will scroll down to the wild one loop. Inside of it we will implement pre-autical transmissions of data. For this purpose we will use the hl function uspd-cdc-set-tx buffer which the arguments are. The first one is the USB device handler, the second is the user tx buffer and the third is the length of the buffer which in our case will be 6. After the configuration is preformed we will call the transmission function which is the uspd-cdc-transmit-packet. The argument of this function will be solely the USB device handler. Now we will include some delay to prevent the overflow of that on the receiving side. So this means that we will have an implementation that sends 6 bytes over the course of half a second. To implement data reception we will go to the uspd.cdc.it.c file and to increase the visibility of the user rx buffer we will need to retype it here as extern. Now we go to the function which is the handler for the receiving messages interrupt. In this function we will make a copy of the receiving content to the user rx buffer in order to be able to see it on debug mode. To do so we will use the string copy function to copy the content from the local buffer to the global one. We will build the project and enter debug mode. We cannot forget to connect the usp micro cable to the port located in the lower part of the port. After entering debug mode we need to open a serial terminal and we will press resume on our application. With the serial terminal open we will select the usp protocol and we will start receiving on our computer the test message sent by the L4 discovery board. And now to test the usp reception we will type a test message in our terminal and in this case we will type the word test. Now we press to send. We will add the user rx buffer to our monitoring expressions and after pausing the execution of the program if we inspect the variable we are supposed to see the test word received by the buffer. Thank you for watching and I hope you enjoy this hands-on.