 We are now at the last hands-on of the day which is going to introduce to you the Cubamex and all of the libraries. The goals of this session are to become even more familiar with the STM32, F0 and its ecosystems. So we've already had a play around twice now with the previous hands-on with the F0. So we're now going to expand on the ecosystems part. The method that we're going to use in this section is the Cubamex tool which will graphically help us create our project and workspace and link all the libraries in. We're still using the same technique, polling, from our application software flows. The steps that we're going to do are create our Cubamex project. Enable the configuration of the peripherals that we have chosen for our Fermo regulator project. Use the Cubamex tool to generate all our kyle startup files. So this time we won't be actually using the kyle new project function. We'll be letting the Cubamex tool do that for us. Then we'll add in the application source code as we've done with the previous two examples. We'll start from a template and we'll paste in the main part of the application source code. Then we'll compile and debug the application as we did in the previous hands-on using the kyle environment. And then we'll set our LED using the same principles as before where we trigger our threshold for our LED on the target bot. So the block diagram for this hands-on is exactly the same as the previous hands-on. So we've still got the temperature sensor and the control button as our two inputs into our MCU. We're still driving the same two outputs which is the LED to represent our switch relay out to our heater. And we're going to link in the communications back to our external system which will be our PC via the virtual comport. So the same four peripherals are still needed as we did in the previous example. On our nuclear board everything stays the same as we had in the previous example. So we're still using the green LED, the blue push button, the external temperature sensor inside the SDM32. And in this hands-on we are actually going to connect to the UART this time so that our PA2 and PA3 from our SDM32 will link up to the section you can see highlighted in purple on the top half of the board, section 4, which will then generate our virtual comport back to our PC. Schematic diagrams for all of these peripherals. We've already seen them in the previous hands-on. So it's still PA5 for the LED, PC13 for the push button and PA2 and PA3 for the UART. Our flow diagram stays exactly the same. We're now going to cover all these blue boxes. So the three blue boxes at the bottom which is our push button and our UART are now also going to get included inside this part of the hands-on. The section now highlighted in blue is what we're going to do with the Cubamex tool. So this is what we're going to now start and configure and lay out the pins to match our hardware schematics you've just previously seen so that we can generate our template for building our code. So now we're going to launch our Cubamex tool and we are going to start configuring our application using the graphical interface inside the Cubamex tool. So we now need to launch Cubamex from our start menu. When you open Cubamex you'll be presented with the landing page. This is where you can either start a new project or load an existing project. You can also go and check to make sure that your library pack is installed correctly by clicking on the help button on the top menu bar and selecting manage embedded software packages. So here if you expand the F0 family you should hopefully have a green dot in the latest SDM32F0 library pack which means we're now ready to start our application. So we can close that screen and we now need to click on new project in the middle of the screen. This opens a new window which is our MCU selector. This is exactly the same screen as you get with the MCU Finder where you've got your parameter selection on the left hand side and your results at the bottom half of the right hand side of the screen. So there are different ways where you can select a product. You can either key in the part number and search that way. You can use the slider bars on the left hand side of the screen to select different memory sizes, different pin IO counts, different frequencies and then select which peripheral mix you want or you also have the ability to select a board level design. So you can select any of these particular boards as your starting point for the Cubimx tool. For what we want to do though, we will just key in our part number which is the SDM32F072RB and if you notice then on the right hand side as you were typing that part number in the list on the right hand side was getting smaller and smaller until we have only two parts left and if I move my screen width a bit wider you can say that we've got a BGA variant and a LQFP variant. So it's the LQFP variant we need to select. Now you've noticed on the top of this screen that the page has been populated. This is where you can now link to the datasheet, various documentations or the block diagram. But we don't need to do any of those. We're just going to double click on my blue highlighted line which is the RBTX and that will then go and generate our project which is set out for a 64 pin QFP to match what we have on our Nucleo board and you should hopefully now get presented with the pinout diagram. We now need to start adding our peripherals to our pinout diagram. So we'll go around each peripheral one by one. The first peripheral we will add is the temperature sensor which is part of the ATD converter. Now this doesn't have a physical pin, it's internal connection but we still have to select it in the ADC. So if we expand the ADC you will see you have the 16 external channels and then the three internal channels that are connected to the ADC. And we need to put a tick into the temperature sensor channel. The next peripheral we need to add is Usart number 2 which is going to generate our virtual comport back to our PC. So if we scroll down on the left hand side we will get Usart number 2. If we expand Usart number 2 we need to select asynchronous mode and you should notice on your pinout diagram that PA2 and PA3 have now gone green. This means that those two pins have now been assigned to the Usart. One of the nice features of the Cubamex tool is that most of the smaller digital peripherals have the ability to sit in different locations on the pinout diagram. And you can find out if that is possible by hovering your mouse over PA2 as I am doing there and it says control click to show alternatives. So I will hold my control key down and now left click PA2 and you can see in the top right hand corner PA14 is flashing as blue every time I click my button. So this means that UR2TX can either sit at PA2 or PA14 on the pinout diagram. If you want to move UR2 to PA14 all you do is hold down the control key, your left mouse key and then you just drag and release onto the PA14 pin that was highlighted blue. We are happy with this default configuration because in our Schematics it is PA2 and PA3 that are connected to the virtual comport at the top half of the nuclear board so we don't want to change the configuration that we have currently on the screen. The next feature we need to enable is our serial wire debug. So our serial wire debug is part of our system function so we need to expand SYS on the left hand side and we need to put a tick box in debug serial wire and this should now have sent two more pins green on your pinout diagram which is PA14 and PA13 for our serial wire clock and serial wire data. But you might now have noticed on the left hand side of the screen we have a yellow caution and a red line appeared. So this is telling you the yellow caution will tell you that you have lost some parts of a peripheral and the red line will be the feature that you have actually lost. If you want to find out what has caused the problem you can hover over the caution and you can see which peripheral is now causing the conflict and the same goes if you hover over the red line you can see which peripheral is causing the conflict. We don't need that particular feature that is there which is wake up number three or wake up number four, sorry. So we are not too concerned that that feature has now gone red. As you populate your pinout diagram you will see more and more of these cautions and red features to let you know that a peripheral is either partially not available or completely not available. Now we also need to enable our two pins so we need to enable our PA5 for our LED and we also need to enable PC13 for our push button. On this particular pin diagram it's not too bad to find pins. You can quickly scan around the package and find the various pins that you are looking for. But if you are looking for PA5 on a 208 pin package it might take some time so we have a search box where you can type in port A so you can now see all of port A flashing and if you finish typing in PA5 you can now see PA5 flashing. So we have a quick way of finding all of the GPIOs that you need. So we now need to left click on PA5 and we need to set that to be our GPIO output. So this is for our LED so we just put a tick in that box and it has now been changed to green and the definition is GPIO output. We now need to do the same for our push button so our blue push button which was on PC13 again left click and that one is a GPIO input that has now changed that pin to a green pin. If you want to use labels from this diagram you can also do that. All you need to do is right click on the button you have pressed and you can click enter user label and that label will get ported into your C code so you can use labels within your C code when you come to write the rest of your software. So now we should hopefully have six green pins highlighted on our pin out diagram and that means we have assigned all the peripherals that we need for our thermo regulator example. If we move across to the next tab which is the clock configuration tab we can have a look at the clock tree that is available in the STM32F0. Now if we had assigned external pins for our crystals on the pin out diagram then the two items on the far left hand side of the screen would be blue and you would then have the ability to type in the frequency of your crystal that you have chosen. Because we're using the internal RC we didn't assign any pins externally for crystals therefore we're going to use the HSIRC as our main clock source. Because we're only toggling an LED we don't need high performance so we're going to leave the clock settings as their default which is 8 megahertz. If you wanted to change the frequency of your clock source then you would type in HCLK so H-Clock in the middle of the screen where it currently says 8 you can tap in 48 which is the largest available and the system will automatically calculate the required PLL to get your 48 megahertz. If it's not available from the current clock source then it'll prompt you to say it needs to change the clock source and then it'll go and recalculate again using one of the other clock sources available on the chip. Also down in the bottom right hand corner of the screen you can see USART number 2 clock walks. At the moment the default setting is going to use P clock number 1 which is the peripheral clock to run at 8 megahertz. If we wanted to use this device in low power mode say stop mode where the clock tree actually gets stopped then we would need to change that clock walks to be LSE and we would have had to have provided a low speed external clock to the device and then USART number 2 would have the ability to wait the device from stop mode because it'll be running on a clock source that has not been stopped. But we don't need to change anything here so we can move across to the next tab which is the configuration tab. In this configuration view this is where we can now add peripherals that have no physical pins things like the CRC the watchdogs timers 6 and 7 which are used to synchronize the DAX and it's also a place where you can add the middlewares items like the freeRTOS or FATFS if you were running a file system on the device. Also in these middlewares if we had selected the peripheral of USB then the USB libraries would have appeared up there in the middlewares so that you can configure the USB libraries. But we need to now go and configure our USART so that we can connect it to our PC externally. So if we click on our USART number 2 we want to select a board rate of 9600 board so you can just click on the number and type in your 9600 and we want our word length to be 8 bits. So this one is just on a dropdown where we select 8 bits including parity. And that's all we need to set for the UART. So we can now click on apply in the bottom and then OK. So that is our USART now configured for what we want to do. The next peripheral we need to apply some settings to is the ADC. So inside the ADC there are just as many settings as there were with the UART. And for this one so that we can read our temperature sensor we're going to slow our clock source down. So our clock prescaler we need to put to be divided by number 4. Now there's one other item that we need to set inside our ADC and that is our sampling time. Now the sampling time has to be calculated based on the information from the external analog device that we have connected or in this case the internal analog device we have connected. So if we look at our data sheet in chapter 6 section 3.19 we have all the parameters for our temperature sensor. Now one of the parameters is the minimum sampling time required when reading the temperature sensor in the ADC. For us that means we now need to calculate this value. So if you remember from our clock tree we had a system clock of 8 MHz. We have just set our ADC clock to divide by 4. Therefore we have 2 MHz entering our ADC sampling unit. This means we have 0.5 microseconds as our sampling time. We need a minimum of 4 microseconds therefore we need a sampling time of 8 cycles. Now if we hit our drop down list in our CubeMx tool for the sampling time there is no option for 8 cycles. So we have 7.5 or 13.5. 7.5 is not long enough we won't get a clean enough reading or a reliable reading so therefore we will need to select 13.5 cycles as our sampling time. And just as before we need to click apply and OK. So that's all we need to do in the configuration. If we just have a look at the GPIO and click on PA5 you can see it's already configured as output push pull and as we highlighted earlier the reset conditions of all the other features which are output level low no pull up pull down low are suitable for what we need so we do not need to change anything in this GPIO configuration so we can cancel that screen. Now we need to save our project and generate our code. To do that we need to go into the projects section up on the top toolbar and go into settings so in here we give our project a name and a location so I will go and find myself a location so I will generate a new folder just as we did before call it lab 3 and then say open so that's now giving me a project path of hands on lab 3 and I will also give it a name lab 3 again to follow the naming I have used for the rest of my projects now I need to set the toolchain so we are using the kyle microvision which has many other names and the name it refers to inside here is mdkarmv5 so that is the toolchain that links to our kyle microvision that we have been using in all the other hands on so we can select that and we need to do this for low layer libraries if you remember from the presentation kybermex defaults to how so we need to go on to advanced settings and we need to change all our how libraries to ll so this is done via drop down boxes so you have to do it for each of the peripherals and then you can click ok once you have completed that task with your settings so now we are ready to generate our code so we go project and generate code so this now goes off to the repository finds all the library files for the periphery that we have selected in our graphical interface pull those into our project folder which is lab number 3 it also then goes and pulls out the kyle template and loads all that into lab 3 so that we can now just click on the button in the middle of the screen which is open project so if we open the project kyle will go and launch and in this particular case it's also launched the pack installer which means it's telling me that something is wrong and it's telling me that there is a missing library pack in this particular instance that is actually wrong so there is a problem inside the kyle tool on version 5.24 of kyle so we can cancel that it tells me now that there is no f072rb so we can ok that we can close the pack installer as well and we now need to go to options for target so if we click on our options for target we need to go into our device tab now and we need to do exactly as we did with the first two hands on this morning and type in stm32f072 rb and then click on our tx and then say ok so for some reason in the kyle tool only on version 5.24 there was an error for some reason it does not see the library pack when the library pack is installed in the device if you are missing the library pack then you will need to install it but I know for a fact on my laptop the library pack was there so if I expand all our folders here just so I can talk you through what has been pulled in if I make that a little wider so here you can see it's pulled in the start-up file which is the initialization part in our application and user section we have our main.c and our interrupt subroutine.c then we have all the howl drivers or low layer drivers of the cases for what we've selected to cover all the periphery that we might use with inside our project and then we have the cmsys system file which is part of the ARM configurations that is needed so all these files have been brought in the rest of the peripheral drivers have not been brought in because we have not selected them so if I open our main.c we have quite a structured main.c so there's quite a few comments there at the top we have our includes which will include main.h so if you had any labels defined from your cuba mechs tool then they would come from the main.h then we have our private function prototypes there is one for our GPIO peripheral one for our ADC peripheral and one for our USART peripheral then there are two more generic ones for the low layer library initialization and the system clock configuration as we carry on scrolling down this screen we will get to our main routine at line 74 and in main you can see the low layer libraries get initialized then the system clock configuration gets initialized this system clock configuration is what you saw in the clock screen inside the cuba mechs tool and then you get your free peripheral initializations so these were the free sections in the configuration part of the cuba mechs tool that are getting called to load our configurations for each of those peripherals we have selected and then we have our empty while one loop you might also notice as we scroll down this screen there are lots of these user code begin and user code end comments dotted all the way down this file there is quite a lot of them actually in the main file these are the equivalent of compiler directives for the cuba mechs tool if you type your code between a user code begin and a user code end so line 101 as my screen is showing here if you then chose to re-run the cuba mechs tool any code you've written between the begin and end would be kept if you wrote some code at line say 103 which is between an end and the begin then that bit of code would be lost when you re-run the cuba mechs tool and it would get replaced by the standard template again so we've put plenty of these user code begin and end in the software so that we don't restrict your coding styles and techniques and it also gives you the flexibility of writing the code wherever you want inside that main.c file if we carry on down the code you can now see the information that was brought in for the project so we have the low layer library initialization which is setting interrupt priorities then we have our system clock configuration in which is the information from the clock screen inside the cuba mechs tool we didn't change anything here so most of this will be in its reset state but you can say the hsi set calibration trimming is there usark clock is set tp clock 1 so everything there is from the clock diagram then we have our ADC initialization so there's one of the parameters that we changed was to get set our clock divide by 4 and there is the other parameter that we changed which is our sampling time of 13.5 cycles and then down in the use art section you will find that we've set a board rate of 9600 and we've set our data width to be 8 bits so this is all our configuration setting for the use art you can also see there the alternate function has been enabled for pins 2 and 3 on port A so that was our use art pins and then right at the bottom we'll have our GPIO configuration for our pin 13 which is input and pin 5 which is output so we've now done these top 2 boxes so we've now got to put our application code in to cover our loop for our ADC and our question is it above or below the threshold and then our transmission part which is has the bottom being pressed yes or no as then send our data over the use art so we're going to do exactly as we did in the first 2 hands on we'll go to our template which we saved or extracted from our user disk this morning and we now need to expand folder number 7 developing an 8 bit application which is for our hands on number 3 and go into the template folder and we need to open our main template ll.c and this time because our main.c is already structured we can't do a control a select everything and paste it directly into our main.c this time we've got to do it section by section so the first section we need to move in is our variables so that is these top 3 lines there so you want to control C then you go back to your kyle environment and go all the way back to the top to line number 50 which is our private variables section and then control V so that puts our variables between a user code begin and a user code end then we go back to our template again and we have a section for user code begin and end number 2 so we'll take those 3 lines copy those back to our kyle environment or micro revision environment and this was user code begin and end number 2 so this is now going to line 103 exactly only 2 lines when the word wrap comes out of the equation from our text file and then the last section goes in our user code begin and end number 3 all of those lines remember not to take the user code begin and end number 3s those are already in our main.c file in our kyle environment control C and paste those into user code begin at line 116 so that's our code now pasted in to our correct sections within our template that has been generated by our cube mx software as before there are some lines of code that need editing so we need to edit line 103 116 118 128 and 134 so if we look at our code what actually 6 in user code begin and end number 2 is the enabling of the ADC then the first section of user code begin number 3 is the start of the ADC and reading the results then we have our ADC threshold question to enable or disable our LED then we have our question is the push button pressed at line 132 and then line 134 is our transmission of our message across the UART so we now need to start completing some of these question marks so at line 103 we need to enable our ADC now we can try using the help which is control space find out what commands we've got but in the low level library there are quite a lot of commands so you do need to start giving it a bit more of a hint on what the commands should be we can also go and look in the user manual what the libraries so if you remember if you remember we had our um1785 which was our description of all our howl and low layer commands it's a large document 1,320 pages and we need to go and find the lladc section so in here we have the commands which tell you what's available inside the lladc now I know for a fact the command that I'm after is on 658 and that is lladc enable so we need to start typing enable and then now if I can go control space to bring up my help there's the command and that is the first command entered for what we need to do the next section where we need some editing is at line 116 and our comment there is start the adc regular conversion now this is where you need to know a bit more about the adc this is why we say low level commands are more for the expert mode inside the adc we have two modes we have regular conversions and we have injected conversions so you would need to have known that to decide that this one needs to be a regular we've already put the reg for you in our tip screen there but if you didn't know the adc well then you would have not known that there is a regular and injected version so so we need to start the adc in regular conversion mode I'll go with the obvious st for start control space start conversion again another fairly easy one to find now we need to read the regular conversion results we need to start and read a regular conversion result so go for reg underscore read so we've got a couple of answers now that might be correct read conversion data 10 12 or 32 if you remember we are on a 12 bit 8d converter here so conversion data 12 is what we need to do here the adc is configurable so you can set the adc to be a 10 bit adc or a 6 or 8 bit adc as well so we want 12 for our particular case here so I'll let you have a try now with the remaining items another location where you can find information is from the .h files as we've not built the project yet we don't have the dependencies under each of these .c files so I will hit the build I will get errors because there are 3 red crosses still there on my screen it will then populate my dependencies the next section I'm looking for so if I look in the gpio.h file again you can find the various commands for GPIOs so there's get pin mode set output type set pin speed get pin speed so there's your first command there which is set output pin so you can also find these commands in these .h's in that case I will copy that part into my main .c scroll a bit further down you'll find the second command you need for the GPIO there which is reset output pin so you have one more left to find now which is the use art and we want to transmit over the use art so I will transmit and we set it to be data 8 and now my final caution has gone so I can now build my project so I now get 0 errors and 0 warnings because we've generated the project without going through the normal kyle startup functions we still need to check to make sure that the development tool is still correct so we still want to talk to the ST link not the U link which is the default in the kyle environment so to do that we need to go into options for target and the debug tab and we need to change our U link to be an ST link debuger then we need to go and check the settings to make sure our flash downloader is correct 128k and we can also tick the box for reset and run when we just do the download function and we can say ok if you had a problem with your linker on the first two examples then you might also need to go into the linker and make sure the tick box is there for use memory layout from target dialog once you've done that you can select ok and we can now end the debug before we actually run the code we need to go and put the watch window in place for our variable which was ADC value so double click on ADC value then right click and add ADC value to watch one while we're on the same screen we might as well do threshold as well so double click to highlight in blue then right click and add threshold to watch one I prefer decimal values so I will change both of mine to decimal by right clicking and removing the tick box and now I can go and run my code and I can see that when my ADC value is cold so my finger is not touching the chip I'm at 1644 and when I warm the chip up I drop down to about 1628 so therefore I will need a threshold of about 1635 just type in there 1635 hit enter so when my chip is above my threshold of 1635 my green LED is on when I warm my chip up my green LED goes off before we proceed to the next part I will save that 1635 into my code so I need to stop debugging terminate my debug go back to my main.c file all the way up to my variables at line 50 and type in 1635 rebuild the project re-enter debug run the code again just to make sure everything works so now we need to run the terminal window so that we can press our blue button and see the information appearing in our PC environment so I will use TerraTerm now need to set up our serial port which was com 30 for me 9600 board and 8 bit data so now I am currently sat with the temperature at ambient which means my green LED is on so when I press my blue button I will receive 1's on my screen so I will press my blue button now and there we go I get a packet of 1's there is no debounce software integrated into our application so I get quite a lot of 1's and now if I warm my device up my LED has gone off my ADC value you can see on the screen is below my threshold I will get a packet of 0's go through wait for the board to warm up my green LED comes on as it passes the threshold and now when I press my button again I should get a packet of 1's you can see there is 1 random 0 in that packet when you catch the board on it's threshold like that you will see that the ADC as the temperature is going to open down the ADC will give you so many 0's and so many 1's results because the live update you are seeing on the PC is quite slow in comparison to the speed and microcontroller is looping around that loop taking those ADC readings all the time so we now have a complete project which is sending information back to our target board we can also now use the HAL libraries so if we go back to our project we will terminate the debug environment we will go back to our cube environment tool and we will change our settings so I will go to project and settings and change all of these commands now to HAL OK and go project generate code again so it's now going to go and pull in all the HAL libraries this time because my HAL project is already open I will just say close to that screen and go back to the HAL environment where it will ask me would I like to reload so I will say yes to all of these yes's I will still get the same error where the device pack has not been recognised I will wait for that I will cancel that I will get rid of my pack installer I will go into my options for target and key in my device again STM32F072RB that's the TX and HEAD OK and now we see that we have pulled in HAL drivers and we have actually pulled in a few more drivers now because we have got the extended functions and the default functions and you have got a lot more information that is part of the HAL software so there is all these extra nice cities as I said that have been pulled in as well into this project code wise not that much has to change our definition of variable stays the same they are pure C nothing to do with libraries so we don't have to change anything there what we had in user code beginning and end number 2 is no longer needed so we can delete lines 106 and 107 and now what we need to put in user code begin number 3 is these few lines here so I will copy this across and overwrite what we had in user code begin number 3 to and including that bracket there which looks fairly similar I will get the spelling correct so we can see that we start the ADC we are pulling for the conversion we are getting the value loaded into our variable called ADC value testing it against the threshold and then writing to the pin pin set and writing to the pin again pin reset command and then we are doing our transmit of one block of data to UART number 2 with a timeout value of 1000 so now we are going to run the code again so it is now building with the HAL libraries you need to check our options to make sure our debug tool is set for sdlink we need to check our settings for the flash downloader are correct we can enter debug run the code I am going to need to change my threshold now because my ambience is now at 1689 I need to set it to be about 1680 I can now launch my terra term again press my blue button I get my bank of ones warm the chip up press my blue button again I now get my bank of zeros wait for it to fall down again press my button again I get my bank of ones so you can see there is not a great deal of difference between the code from the low layer libraries to the HAL libraries the start part has been moved from user code begin to into that one statement there so two lines have now become one but most of the other statements are roughly the same amount of lines just the syntax of each line has now changed to match the different formatting of the libraries so what have you learned in this section so you have now had a play with the kubimx tool to understand how the kubimx tool can offer you what it can do for you you have had a play around with the low layer libraries and the HAL libraries so you have seen the differences between what you do in the kial environments compared to direct register access to working with the libraries that are generated by the kubimx and you have had yet more practice with this version toolchain hopefully you enjoyed the hands-on and thank you for taking part in the migrating from 8 to 32 bit workshop