STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 8 UART - Updated Dec 2017

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Mohammed a cube and today's video is going to be about you art or serial communication of this team board this is in fact an updated version for the video I created about a year ago so in today's video I'm going to show you how to do this with cuba mix in the first part and then towards the end of the video we will set the project up from scratch without the help of cuba rice the second part is particle useful for students who want to learn more about the low level of the STM board your art has got two lines TX and rx that you have to cross connect between the two devices so you connect TX of the first device to rx of the second device and vice versa and you also must have a common ground between the two devices okay I think that's enough introduction so let's make a start let's open qmx and select new project and we've got to choose the right board stm32 or four or seven in my case vgt this one and so on and on here the first name got to do is to scroll down to we found UART and we've got to enable one of the UART lines we're gonna use you are two at this case and select a synchronous mode and thus enable PA two as a TX line and PA three as an Rx line and that's how we enable UART I'm also going to enable a couple of LEDs I might use them now let's go to the configuration window you are - and here we can set the baud rate I'm going to leave it the same and I'm also going to enable ad main for the RX line so receiving via DMA is the most effective way you can receive using the polling method but you'd have to wait and have a timeout wait for the data to arrive but with DMA they will happen as an event so whenever you receive a data that will be stored receive via the DMA you don't have to have a timeout receiving data can come at any time it's not a deterministic process so that's why I prefer to is DMA for it so when Abel teammate for the arcs line and we already know so click OK and that's everything we need to do so we're ready to generate the code click on this icon and give the project a name gonna call it UART video going store to this location and select the right IDE mdk-arm v5 and hit okay now once the source code is generated hit on open project an on call macrovision expand the project folder and expand application user and open domain and you can see the main human directed all the low-level files for us and now all we've got to do is to start transmitting or receiving via UART so in my case today I've got a TTL to USB converter that connects my the output of my esteem board you are to the PC via USB so I connect TX and rx to these lines and a common ground and connect this to my PC and I'll act as a serial comport so you'll need one of those okay now let me do a very quick transmit test this function called Hal UART transmit this function takes a couple of parameters you are tan Delta live which is it you are two as defined by Q mix at the top here is just to UART handle type death and the second parameter is the data buffer so I need to define a data buffer air as a character array I'm gonna call it TX data let's say 30 and I'm gonna send just how the ward with a new line so I've got the Buster's as a parameter to the function but I would need to typecast it - unsigned integer eight because the function expect an unsigned integer a variable or a pointer and the size is the length of that string can do this with the string length function in the string library that I would need to include first so include string detach and now I can use string length to cover the length of that thing and then I would need to pass the last parameter as a timeout I'm gonna give it 10 milliseconds of a timeout and that's all I've got about a delay of how second and last everything so I'm gonna compile the code load it to the board connect this via that TTL to be converted to my PC and open a terminal real term in my case and I expect he'll award to be streamed every half a second so let me first compile the code perfect compiled successfully without any URLs not limited to the board and now I'm going to open my terminal return at this case and go to port should become port nine another company celebre sir whatever and click change you must have the right grade first so baud rate was this as I set it in Cuba makes and hit change and now when I reset my steam board I expect he'll award to appear in here right very good ok now let me show you how to receive data video art as I said we're gonna do this with MA and what you need to do is you need to call in a function at the start called how you receive in the DMA here takes your parameter the first one is the UART handle type def as usual and the next parameter is a alex buffer so I'm going to define an arcs buffer here are X data I'll call it I'm gonna give that size of 30 maximum and I need to typecast it to unsigned integer 8 a pointer and I'm receiving 10 bytes at a time so whenever I receive a full 10 bytes it will interrupt and jump into a certain callback that I'm gonna define in a minute that's all four parameters for this function I believe unless there's something I missed so what this is saying I'm not sorry this is 8 not 16 right and now let me define the receive callback function and you can find it in the functions tab and UART so an you are dirty and should be a function called receive complete callback a lot so an arse complete callback this function is defined as we can here part we need to redefine it in the main without that weak object so I'm gonna redefine down here begin number four and in here whenever I receive fold 10 bytes I want to I'm gonna echo them back to the transmitter let me copy transmit functional from the top I'm going to submit the RX data that I received not the x1 it's undefined here because I define it locally in domain let me define it clearly the arcs buff only so I need to find this outside the main to become a global variable and then it will be seen by the crack function so in here would be good and not all should have been gunnel alright perfect so now so now I'm ready so let me compile the code okay you haven't got any arrows now let's load it aboard and do a quick test all right let's go back to realtor so on here I'm gonna open that 14 so click open a same boat right gonna clear the window now when I reset my steam board and when I sent 10 bytes of data let's say hello this is 5 and then MOH this is 6 7 8 with a carriage return these are two more bites now when I send them I expect to receive them back right superb I'm gonna enable the half duplex mode just so that you can differentiate between that were I sent and what I receive so the thing I send will appear in green and the thing I receive will appear in yellow so I'm sending this one don't receive any more oh yeah sorry I think this has got to do with the D may not being circular so maybe I did it forget that in my cue mix but it's not too late I can still change it in here so let me go back to my project so MSP file scroll down in here here are the email settings the mood is apparently and the normal exactly it's like why I expected and this should be circular so when I right-click go to the definition okay so you can see here so I'm going to change it to the circular mode instead no it should walk so let me compile it again unload back again to the board a knowledge test again so the port apparently is still opened go back to the sent restart my steam board since this I'll get it back ascend it again I'll get it back perfect I was good all right cool I've just shown you how to use UART of these team 32 discovery board with the help of cuba mix and if that's all you're looking for you may still the blue here but i'm gonna carry on i'm going to show you how to do the same thing without cuba mix until do this without connects click on project and renew Microvision project and select a location to store your files in i'm gonna store them here i'm going to call my project UART no queue and click Save no need to select the board steam 30 or 407 VGT so I so on and click OK and here we need to enable the software components that we can use so first sim says score and go to device and enable the startup and cube framework API we need to enable the classic one and this require other software's that we can enable by just hitting resolve not expand esteem 32 how cube hull and I need to enable you our software component which is this one and the DMA as well and that's all I need to enable so click ok as you can see karmic revision generated those library files I need to include the main manually so right click add a new item as a C file and we must call it main and click OK and on my empty main I first need to include the challah bread here vials also include a scene 32 4 X exhale detach no need to define the main function so enter main parameters void no parameters I need to define the infinite while loop at the bottom arrow at the very start of the main to go practice to call the hull and it functions or how initialization and the function is called hull in it okay so for my peripherals what I need to do is I need to enable been to be a 2 and B a 3 and set them to UART mode because there are the parents for TX and rx and I also need to configure my UART and enable the DMA device and I'm going to do these in two separate functions the first one is just for you art so you aren't config for the pins and for the art configuration and the second one is for DMA setup and something's gonna call it D meg config and the parameter is void it doesn't take any parameters okay and in the definition of the first function you aren't config the first thing I'm gonna do is to enable the clocks so port a clock and you are to clock using these two functions and I'm going to set up my pens PA 2 and be a three set them through art and through this aunty - aunty Pio and it type diff to initialize those pen I'm gonna call it my UART at this I know it's not the best name and we use this type diff to configure the pen sir why you aren't difficult pen let's get out so do you by open to ante by open three and I'm gonna set the mood to alternate function because this one is not analog nor digital input and output so we have to use the alternate function and then we don't have normal ups and the speed is very high and next we're gonna set the alternate function to the sortable alternate function multiplexer so alternate GPIO AF and we can look at the alternate function mapping of this team 30 or 4 we can just Google this and you can see alternate function number seven is the one that connect UART so it's eight of seven to start the world and it's UART one sorry X you are two for PA to LV a three so that's what we need to sit on the alternate and now finally I need to call you by your NH function to implement those initialization implement them on port a and I can past any target for this case and I need to pass a pointer to it and that's all for the pin configurations i no need to configure the UART peripheral so converter things like the baud rate and other settings and first I need to define a UART handle type ship type this Abani to define it globally because we're gonna use it in the main and I call it my you are tundle and we use this to set up the UART peripheral so you are dot instance - you are - so this is just to select the UART instance and this need to be used art with an S and then we do some initialization so the first one perhaps is the baud rate and I'm going to set this to 1 1 5 200 and then the mood to TX and rx so UART boot to TX and rx I know some other initializations award lengths to 8 bits 1 byte and stop it to just a single one and over sampling to 16 now we need to call Holly art in it to implement those initialization and we pass a pointer to view art handle and that's all and one final thing I want to do here is it's not quite related to art is to enable the systick interrupt so we need to enable systick interrupt available to use he'll delay in our main so I'm gonna do in here and I would also need to define the systick handler out there to link the system systick handler to the holodeck or cystic irq handler this is usually defined by cue mix in a separate file but we've got to do it manually ourselves here all right Anala configure our DNA so deem a UART config and the first thing to do at this function is to enable DMA 1 o'clock because DMA one is the one that connects from UART to to the system or to the ROM memory to be very specific and to be able to configure the DMA we need to define a DMA hundl type death and I'm going to find it globally again and I called it my team a you are to handle and we're gonna use it to set up our DMA so but the first thing is sodium a dot instance and it's gonna be deemed a one stream v and the reason for this is that when you look at da1 map of these team board you can just google this you will find this table you can see that DMA one stream 5 is the one that connects to you our to our X so that's one we need to use and next we need to do some initialization and I think we should start with the channel which is also defined on the table that we just look at so let's go back to the table and the DMA channel is channel 4 that connects the stream 5 3r to the sodium channel and it's 4 and now we're going to do a series of initialization so setting the direction to peripheral from a to memory disabling the peripheral increment address but we are incrementing the memory address a perfect data alignment to bite same for the memory and the more to circular they only made a mistake in killing spot so we ensured a circular here and a priority to low v remote to disable and if you think I'm going too fast don't worry I'm going to the main dot C file in my description of the video I no need to call Hal da init function to implement those initializations and we need to pass the team a hundred octave and a pointer to that and next we need to link our teammate to our UART a peripheral so the first parameter is a pointer to the UART tundle type dev and second parameter is HD and our X because the sinorix line and then the third parameter is the d800 Optive and finally we need to enable deem a stream v interrupt using these two functions and we also need to define a similar thing to what we did with the systick interrupt so we define the systick handler we also need to deem a one stream 500 as well outside of the function here so do you mainstream v just to link the RQ handler to the hull library handler alright that's all here so let's scroll up and write the main code so on the main we need to call in the functions that we defined so I need to call in your art config and DMA config to implement their configurations and yeah now we are at the same stage as the project generated by Q max I'm not going to include the other parts that we did in Q max I'm just going to copy and paste them here very quickly so the first one is to define a TX and rx buffer and then transmit a the TX data every half a second we also need to include the string library at the top to be able to use strength length function so can include it in here and that's it for the you are transmitted so we're gonna test the transmitted in its own and then we're gonna come back and finish off the code for the receive so first let's compile it and make sure that we don't have any errors okay I have one error another thing I know why is it says undefined symbol DMA config thank the finishing of the yeah-ah so on so definition of team a config I did team at UART config and that should be give me a config now let me compile it again right super errors gun and all is loaded to the board and tests and real term all right okay that's another one it doesn't upload his fully because we need to select the right debugger cubics was setting this up for us but now we have to do it manually so go to options for target and go to debug and in here select stealing debugger I also go to settings and go to the trace window and enable trace and set the core clock to 16 megahertz and click OK not should load find so hit the load button again are perfect so let's go to real term and on real time we select the right board I think it's 49 and the right baud rate 1 1 5 200 and click change and now when I reset my Steam board I expect the strength to be streamed here alright very good I know for the are for the receive functions I'm going to do the changes very quick here so I need to comment the transmit one because I don't want to transmit anymore in here and I need to start the DMA receiving 10 bytes of data and the arcs complete callback at the bottom so here's the one so whenever I receive with 10 bytes I'm going to send them back and this should be the right the newly named UART and I'll just copy this from Cuba next one ok so let's compile the code ok no arrows load it to the board and back to real term ok a non real term opened that port again and we're gonna clear the window I'm also going to enable half duplex so that I can see the data I send in green color and the data I receive in yellow so let's go to send and the steam board is expecting 10 bytes so I'm gonna say the same one hello move with the courage returned two extra bytes so when I send this I expect the data to be returned back but I first need to reset my steam border ok so I send this and I get it back perfect and again alright that's good and that's how to set up a you art project for this team board without the help of qmx under Springs me to the end of my tutorial today thanks for watching and as always if you found this helpful don't forget to hit like and subscribe thanks and I'll see you next time bye
Info
Channel: Mutex Embedded - Education
Views: 50,046
Rating: undefined out of 5
Keywords:
Id: RQNT0-_Iq-o
Channel Id: undefined
Length: 20min 26sec (1226 seconds)
Published: Wed Dec 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.