 Hey guys, here is a new lab in our STM32H5 MOOC. So this is a more advanced lab. We will be creating a USB application using USB-X middleware. We will use USB-CDC class for virtual comport application. So let's get started. So here is a little recap about the Azure Arthos middleware offering. So in term of middleware, so here is what we have. First of all, the Azure Arthos FredX. So this is a real-time operating system. Then we also have the offering of the Azure Arthos FileX. So this is for file system, folder rent. And then we also have for TCPIP stacks, the Azure Arthos NetX Duo. And finally, the Azure Arthos USB-X. So this is the USB stack for host and device. So this is what we'll be using today during this lab. So here are the objectives of this lab. First, we're going to build a USB virtual comport application. So this is the main purpose of this lab. So we will use virtual comport to print a hello world message on a terminal when the user button is pressed. So this project will include the following middleware. So the USB stack. So like we said, we're going to use the Azure Arthos USB-X. And for the class, we'll use the CDC or ACM class. So CDC stands for Communication Device Class and ACM Abstract Control Model. We'll also use for Arthos the Azure Arthos FredX. So we're going to see how to create this application. For this lab, we're going to create a new project. So File, New, STM-42 Project. For the part number, we'll use the STM-42 H563Z86 and select it from the menu here. OK, so this is the one. So that's the one on your nuclear war. And then press Next. Give a name to your project. USB CDC, for example. And then click Finish. So same as before. For this lab also, we can ignore these warnings. So click Yes. The first configuration we're going to do is to enable the EXTI on PC-13. So this is the one, the IO that is connected to the user button on your nuclear world. So let's do this configuration. OK, so look for PC-13. So we'll type it here. OK, right there. Select GPIO EXTI-13. And then we'll go to GPIO. That make sure it's external interrupt mode with rising edge. That's great. Same as before, we'll enable the interrupt. Done. Good, so our configuration for PC-13 is done. Now we're going to enable the USB interface. So for this, in PNOT configuration tab, under Connectivity, select USB. And then for the mode, click on Device Only. All right. So Connectivity, USB, so the last one. And we're going to select Device Only. We will enable the USB interrupt. In the NVIC, enable the interrupt. For the middleware configurations, there is two parts. First, we're going to enable the FedEx part of the configuration. Middleware, so scroll down. Look for middleware. And like we said, FedEx first. And we'll enable the core. The second part of the configuration of the middleware is going to be the USB-X. So select USB-X. And then we'll enable the core system. And the UX device FS, so FS for full speed, we will select the device core stack FS, device controller FS. And then for the device class, we will select the CDC ACM. So this is for virtual comport. Select USB-X. Enable the core system. Now expand UX device FS. Like we said, first enable the core stack. Then the device controller. And for the device class, we will select the CDC ACM. Now we're going to configure the memory pool and stack size for USB-X. So this is where this is going to be done. So first we select the pool size here with this amount. And then the stack size with this amount. So the values that we have chosen here will allow the correct operations of the USB-X stack and application. OK, so we scroll down. We're going to the memory configuration. First, we're going to change the size of the pool size. And we'll use the value that we defined before. And now we'll take care of the stack size. So for this, we go here. Instead of 512, we'll use the value we defined before. Press Enter. And that's it. For the clock configuration, we will run at the maximum speed from HSI. So HSI with PLL to reach 250 megahertz, which is the maximum speed of the STM32H5. Clock configuration tab. So we're going to select the HSI first. OK. And then, oh, sorry, so we'll have the HSI from here. PLL. And now we're going to have 250 megahertz. It's going to find a configuration for us for the USB. So we need to basically provide 48 megahertz. So to do this, we have like a MUX here. And so we will select the HSI 48. So this is a clock that will be dedicated for the USB. 250 megahertz. That's perfect. Now let's look for the USB. So USB clock is here. And so we'll run from HSI 48. So it should be already selected. But if not, select HSI 48. Now it's time to select the time base source. So this is for the hal timer as a time base source. So basically, usually by default, the time base source is the sys stick. But here, we are using the Fredx RTOS. So in this case, we need to change the time base source to be a timer. So a timer inside the SMAH5 instead of using the sys stick. So you can use any timer you want. So of course, as long as they don't interfere or conflict, have a conflict with other functions. So make sure you're using a timer that is not being used by your application. OK, we go back to the pinout configuration tab. And let's look for the system. So it's on top, right there, under system core sys. Instead of sys stick, we'll use timer one. We will now enable the CRS. So the CRS sync is basically the clock recovery system. So we select the CRS sync source USB. So this will allow the auto calibration of the HSI48 clock using the USB SOF for start of frame packet. So as a synchronization source, so this is needed to guarantee the correct USB clock accuracy. All right, so let's go to RCC and select CRS sync. And we select CRS sync source USB. Done. One last configuration is needed. To do this, we're going to go to the project manager and then click on advanced setting. We're going to change. So this is what we're going to change. We're going to click on this to do not generate the function call. And then we'll also remove, you know, this check to remove visibility. So why do we do this? Because the MX USB PCD init, so this function here will be called from the USB application user code instead of main.c. Click on project manager tab, then advanced settings. So we said, so look for MX USB PCD init. So do not generate the function and then we'll remove the visibility. Okay, perfect. Now we can generate the code. So remember, so we can just actually save the project. So that will also generate the code and click yes. Change the perspective. Now we're going to add some code. So user code. Yes, again. So open the file called app underscore USB X underscore device.c. So this is located right here in your project. Let's go usbx app app underscore usbx underscore device.c. And in this file, we're going to add multiple things. So first we'll add an include of main.h. Then we'll add some variables. And at this point, so under, you know, like a user code section, we'll also add some code, you know, to create the thread for the CDC rights. Again, so the code to be added can be found in the description of this video. So like we said, we're going to open usbx and then app and we'll open this file. So app underscore usbx underscore device.c. The first thing we're going to do is to add the include. So for doing this, so we can put that for example here, this location. So put include for main.h. Now we're going to add the variables. So to do this, so in this section, user code begin PV and user code end PV. We'll add, you know, these variables that we'll be using. Now we're going to add the code, you know, to create the thread for the CDC rights. So we're going to scroll down and so the proper location to put it is right here in this section, right there. So this is the user code begin for mx underscore usbx underscore device underscore init one. So add the code like this. So this is the one that will create the thread for CDC rights. In the same file, we are going to add some other code. So right here in this function, so app underscore UX underscore device underscore Fred underscore entry. We'll add some code to do the hardware configuration of the USB IP to do a USB stack level, driver initisation to do usb D plus pull up enable. And then we'll also create like put a semaphore when the user button is pressed with this function. So that will be a callback function that will be adding scroll down. So we're going to go to a different area. So this is the place right here. So instead of this code, we're going to add this code. So this is where we're going to configure the hardware of the USB X and the other init that we are talking about. Okay, scroll down. We're going to add the callback function now. So in a user code here at the end of the file, so user code one section. We'll add the callback function you know for the EXTI. So PC 13 and set a semaphore right here. Now we're going to do a modification to another file called UX underscore device underscore CDC underscore ACM.C. So this is where we're going to add some includes. We're going to add some parameters you know for the CDC virtual comports so like the broad rates, the number of stop beats, the parity and also the number of bits you know for the data. We'll add the buffer that we want to send so the hello world and then we'll have also some code for the setting of the virtual comports with default parameters on activation. Okay, so this is the file. So open this one. So ACM.C. And first, we'll add the includes. So two includes to be added. Now we're going to add some code in the PV section. So right here and now so we're going to add some code in this function. So what you can do is replace this code by this code. So this is for the activation. So we're going to continue adding code in the same file. So ACM.C. We're going to add some code to handle the CDC class request and then we'll add some code also to wait for the same afford then transmit the message on usb. Okay, so the code we're going to be adding is inside this function. So that's a big portion of code that we're adding here to handle the class, the CDC class request. And then last part of the code to be added about the same afford waiting for the same afford and then transmit over usb. So this is this function to be added right here. And we are done with this file. Okay, so to open the file at one. So this is the one. We're going to open this file. So the dot H, right? ACM dot H and we're going to add for example, let's see in this section EFP and we're going to add you know this code right here and we are done basically with the code to be added. Now you can build your project. So this little icon will do the job. Okay, so the compilation process is pretty long you know, because we have so many source code you know to compile for fredex but also usb x so this is a pretty long process so it can take some time. When the end you should have zero errors zero warning. Okay, so to debug this code and to test you know our lab what we're going to do is two things. So remember so far we're only using this connector you know to connect to the ST link and then the ST link connected through swd and also actually we know that there are some like GPIOs that can be used you know for virtual comports but now what you're going to use is also this connector right there. So CN13 and this connector is actually connected to the usb peripheral of the sm32h5 that we have configured you know like doing this lab. So same thing we'll use also the same button and we want to see that every time we press the button we'll see the hello world you know message that we sent over usb so virtual comports so basically it will be seen as a comports you know on your device manager and that's what we want to see and you know in the debug session so for this lab you will connect this cable to power you know the microcontroller and of course to be able to debug and load the code and then this also you're going to need the second you know usb cable so another type C on this side in order you know to connect to the usb peripheral inside usb m32 h5 so do this connect these two to your machine and then we'll enter debug session so connect first you know the st link part so the usb cable for the st link number one and then the second one so second usb connected also to your machine so you need ok let me check so you have another port right here ok great now we have the two usb cables connected we can start the debug session so here is another way to enter debug session so you can do that and then debug as and stm32 and c++ application we'll do that ok change perspective so switch and now we can execute the code as you can see now our virtual comports so cdc class device has been enumerated and from on my hand it's a com 47 that has been associated to my virtual comport device so com 47 let's remember that so to find out what comport has been associated to your device you can go you know to your device manager or you know like if you have other ways on otherwise but for windows you go to your device manager and as you can see this is the device so that we have created so our you know virtual comport device so cdc acm and as you can see com 47 in my case now we're going to open a terminal you know for our stm32 virtual comport device so same as before remember the little icon here you're going to click on command shell console and we are going to select serial ports but now we are basically going to have a new connection select so do not you know use the previous one because the other one is connected to the stlink virtual comports so here we're going to create a new one with the new comports you know associated so you're going to select the encoding same as before the 88 59 dash 1 and click on new give a name to your connection so for example me I'm calling it stm32 underscore cdc then select the proper comports associated to your virtual comport device for the stm32 not you know the stlink so make sure you select the proper one so in my case that's com 47 the rest of the configuration is the same so we because remember in the settings we had 115 thousand 200 for the board rates we had 8 bytes for the data we had no parity and then we had one bit for stop so then you can click on finish and press ok now you have your virtual comports you know like window right here so the console created so now let's try to press on the user button and every time I'm pressing on the user button I'm sending my message the hello world so it's working very well perfect this is great alright and that's it so this is it that's the end of this lab so we can now terminate great ok perfect so now you know how to create a usb device you know using our tool and our bolts thank you