STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 9 SPI - Updated Nov 2017

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone that's Mohammed and this video is going to be an update for my SPI tutorial to make some improvements and to add some extra content in my video today I'm going to show you how to use this bi of the HTM board with and without cuba mix the cuba mix method is very useful for someone who wanna proceed really fast with his project and using her libraries without cuba mix is very beneficial for learning purposes specifically for students before i start i would like to give you a very brief introduction about spi but i assume that majority of you have some background about it already but for those people who know nothing about this beyond interface the most important thing i would like you to know is that spi is a three-wire interface has got serial o'clock line mozi and mezu serial clock is provided by the master device which is usually the microcontroller itself mozi stand for master output slave in both and mezzo is master in both lay output in addition to that it has got a chip select line to activate a certain slave because you can only activate a certain slave at a time and this is usually active low you set it to zero to activate use it at high to deactivate allow should be enough introduction okay so am i doing today I'm using the esteemed board accelerometer this chip is already on the board and is connected to one of the SPI alarms and according to these schematics this is connected to this accelerometer device is connected to SPI line number one so the clock is connected to be a five mosey on PA seven and meter is on PA six the slave select is on P III just a digital output on this TM and the interrupt date or D line is on PE 0 because in my demo today I'm gonna show you how to use enter update or OD as well this is very useful for majority of measurement devices and instead of you going and reading data continuously from the device you can just wait for the data or the interrupt to trigger and then read the data from the device I'm going to demo it without the data interrupt and with the interrupt and I'm sure you will get to appreciate why it is useful ok I think that's enough information let's start our demo on Cuba click on new project and select the right board stm32 of 407 3 GT for the panel to first need to enable spi 1 because that's where that storms the device is connected to and to enable it scroll down till you find SPI 1 and select the mode to be full duplex master I don't see be a 5 6 & 7 enabled the next thing we need to enable PE 3 and set it to digital output for the slave select and I need to enable PE 0 asserted to external interrupt because this is where the data or the interrupt line is connected to I'm also going to enable one of my LEDs and thus everything for the pin out next go to configuration and the first thing I want to do is I wanna enable the external interrupt line you see xti lines view this needs to be enabled for the interrupter work and go to GPIO and I'm going to select set the slave select too high at the default state because it's an active low pin and go to SPI we've got to set the parameters here the most important one is perhaps the prescaler I'm gonna set it to 16 because the device is capable of 1 megabits per second I believe and that's it click ok we're ready to generate the code give the project a name I'm gonna call it this bi one and select the right IDE mdk-arm v5 and click OK and once the source code is generated click on open project and this will take you to Carl Microvision and then here open the main and the first thing I want to do is I want to enable two variables 2 buffers TX buffer and rx buffer of size 2 so T X buffer is the before I used to store the data and and then pass it to the transmit function to transmit it to the device and ORIX buffaloes to store the receive data into it I'm gonna give it an exercise too as well now I'll show you how to transmit data via SPI there are three main steps you need to bring the slave select line to low transmit register plus data and then bring the slave select line high again and to bring the slave decline to low we use healthy by all right pan function as some port eight and three and this will bring the slave select line to low to activate the slave device then to transmit data vspii where i first write the data and the spi TX bottle so first we write the original address which is 0 X 24 the control register number 4 and then the data we're gonna write to that register and it's gonna be 0 X 1 1 in this case to enable the accelerometers x-axis and to set the data output data rate to 3 Hertz then you call Holly SPI transmit function and this function takes few parameters it take the SPI handle type def which is it is pi-1 defined by cuba max then the TX buffer address which is just the array name and the data size to 2 bytes and the time out I'm just gonna give it 50 milliseconds and this would write 0 x11 to register 0 X 2 0 now I need to bring the slave select line high again now let me show you how to do SPI receive I'm gonna read it from register 0 X 20 to verify that the data has been written successfully and to do this there are 4 steps for SPI receive first you bring the slave select line to low then transmit register plus 0 X 80 this is to set the most significant bit too high to enable read mode and then you read data via receive function I finally bring the slave select line high okay so use a similar action braying slaves like I love the trans bridges Terra plus 80 so it's gonna be released in about 20 but I'm gonna or it will throw X 80 to set its most significant bit too high and enable read mode and then I'm gonna transmit the to the slave device using the transmit function but I'm only sending one byte this time so I'd set the size to one as I received the I need to use SPI or C function now this function all mistakes similar parameter the first parameter is this bi handle type def then the rx buffer so SPI rx buffer and then the size you want to read I'm reading only one bite because this is 1 byte register as you can see in here and the datasheet this is only a single byte register and another SPI devices registers might have 2 bytes or 3 or 4 bytes but in this case it's only 1 byte so the size is 1 and the time art is 50 finally we need to pin the slave select line hi again so I hope they started to make sense to transmit data we bring the slave select low transmit register followed by data then bringing a slave select I for receive is a similar thing but when we transmit the register address we set the most significant too high to enable read mode I've learnt followed by receive function to read data and finally bring slave select our gain and this is true for almost all SPI devices so let me compile the code load it to the board and get into debugging mode to verify that this is working and on debugging mode first thing to do is to add arcs buffer to the watch memory so that we can see the value in there now let's run the code perfect so we get 10 0 X 1 1 this is the value that we written to register 20 and we are reading it successfully now let's do continuous reading of the x axis acceleration raw data and display this and esteem studio I'm going to do this in the while loop and this one is one register 29 according to the datasheet I'm gonna put a small delay so let's compile the code load it to the board and go to steam studio to read the values and on esteems to it you will need to navigate to the project folder click on this and go to the folder and where you store your project files in and a hair go to mdk-arm and go to spi one and you will see with a excel file you need to select this and this will contain all the variables in your project and I want to read only a single variable which is the rx buffer import and close and you'll see this under here right click select this right click and send to variable viewer number one and I want to select the mood to table run this and reset your esteem for I can see this is changing as I leaned my accelerometer right and left this is just the raw data perfect now I've just shown you how to read measurement data and falling method so I'm reading continuously every 300 milliseconds this is not efficient because I'm continuously reading data from the device however I can make this more systematic by using interrupt I can program the device for the output data rate to be 3 Hertz or six Hertz and it will interrupt at this rate whenever a new data is really this part is not a purely speculation but small of the kind of things you'll be doing when you're dealing with a measurement spi device so in this case I want to enable the interrupt and what I've got to do is I first need to enable interrupts from the slave device then do the interrupt handling routine on this TM board and to do this I need to enable the data really interrupt interrupt line number one all right so the polarity too high so I'm going to write to register number 23 controller number three and the data is c8 to enable the interrupt now I need to redefine the external interrupt callback in my main you might already have seen this in my external interrupt étoile so go to GPIO this function call xD I call back and I need to copy this to my main and without the weak object and this one will be called whenever the data of the interrupt has got a rising arising age and I've got to copy the reading data code to that callback so whenever this will be called this would get the data from the x axis acceleration register I'm also gonna blink in a lady so that we can see this on the board that's it let me compile the code load it to the board and we'll have a look first at the board and look at the blinking LED and then we'll we'll look at the data and esteem story oh okay very good and as I increase the output data rate to let's say 12 Hertz I expect led to blink even faster so I'm gonna change this so this 2 3 compile it and load it to the board and you should see the LED blinking faster let's have a look at the board and then to his team studio to read the raw data okay so you saw the LED blinking faster no let's have a look at the raw data again so here are the raw data as I tilt it right back to middle we see the raw data changing okay so this is how to use SPI on this team board with the help of Q Max and I'm showing you how to transmit receive data and how to read measurement data and both polling method and an enter outer third okay if cubics method is all you're looking for you may stop the video here and as always if you found the video helpful don't forget to Like and subscribe but I'm gonna carry on and I'm gonna show you how to set this project up without qmx there's a particular useful for students I know many students who want to learn how to do this are not supposed to use the q-max if you're one of those you will find a second of our values fall and through this without kuba mix we have to do everything and call Micro vision so go to project click on you micro vision project I need to select a location to store the folders and I'm gonna store them here and give your project a name gonna call it SP I know cube and click Save now when you select the board is team 30 or 4 or 7 VGT unselect the software components first onee to enable is the course of their component then go to device and startup expand s DM cube framework and enable classic classic require other software components we can add them by clicking resolve a no expand hull under need to add the SBI one and this recorded EMA I need to add it and thus everything so click OK ok so as you can see Carl macrovision generated few files mainly the hull drivers but I didn't a domain so I need to add it manually right click add a new item to the group and its a main file it's a C file I call it main and on this main will first need to include the Halal regular file then defined the main function well there are infinite while look at the end and we need to do two things we need to configure the bends the SPI burns LED pen and the chip select an interrupt depend I'm gonna define them and I'm gonna configure them and a separate function which I'll call GPIO config and the second thing we need to do is to setup the SPI parameters I'll do this and another function I'm gonna call SPI config and for the body of the first function I first need to enable the clocks enable SPI clock T by up or today that has got the LEDs on it port a the SPI one port a the one that has got the interrupt and the chip select line then I need to enable a bio I need to define value in a type def it's like a structure to configure the parameters I'm gonna call it my pin in it and first I'm gonna do the SPI once SPI pins so the pins are 5 6 & 7 on port a 5 is o'clock 6 at mizzou and 7 is a boozy line and then the mood to alternate and the speed to very high and the ball to noble and the alternate function the alternate function is SBI one and now I just need to call hull T value in it to implement those configuration for port a and four points five six and seven for the SPI bins now I need to do a similar thing for the led configuration so very similar things so the pins are 12 13 14 and 15 the mood is output push bold and the speed is low and I call in this and its own port D and similarly for the chip select Bend it's a pin 3 Anacin 40 but similar setting to LEDs so that's why we didn't have those middle bit and finally the interrupt pin which is an external interrupt arising gauge and it support a pin 0 either need to enable the external interrupt and the systick interrupt this is going to be used by the spi functions and this is for the external interrupt line no need to define the SBR SPI config function but first I'm going to define a sb900 type def is going to be used inside the config function by need to define it global because it's going to be used in the main as well so defining this bi config function I need to use my spi handle to set the instance to SPI 1 and then in it initialize firstly the baud rate or prescaler 2 to 16 to bring the speed down to 1 megahertz or when megabits per second and then clock face to age 1 which means that data will be updated on the rising edge and then general other settings like polarity to low disable conc calculation CRC data size to 8-bit direction two lines send and receive first bit as the most significant bit the mood to be on master and disable slave selected so to software T I moved to disabled and then I need to call an SPI init function to implement those initialization bypassing the SPI handles are different here and thus everything force behind config function and one last thing I like to add at the bottom is our cue handle this is usually defined by cubics in a separate file called hull / IT but we don't have that folder that's why we need to add it manually so what this does is is gonna link the define device external interrupt handle to the hull handle irq handle and same thing for the Cystic handle you don't have to know much about this I explained them in details in one of my videos and now let's get back to the main I don't need to call in these functions in here the first one is hull init function and then my config functions and now we are in a similar page to kill the mix setup we are ready to add all the code we added in Cuba mix in here now I'm not gonna do with line by line I'm just gonna copy it over and this is it and just to convince you that it works the same let me compile it and load it to the board and see if it's gonna work on the board and also on STM studio I'm too loaded to the board successfully you need to select the right debugger so I need to go to options for target debug and select the stealing debugger and also go to settings and enable trace I set the clock to 16 megahertz and click OK nor should load fine to the world perfect now let's have a look at the LED on the board and then to steams told you I'm the only seems to do just click run I know play around with your board tilt it to the right to the left back you can see the value is changing all right and this brings me to the end of my tutorial today I've shown you how to use SPI on the esteem board thanks for watching and if you found it helpful as always don't forget to Like and subscribe thank you and I'll see you next time
Info
Channel: Mutex Embedded - Education
Views: 49,241
Rating: undefined out of 5
Keywords: STM32F4, ARM Cortex, Microcontroller, Embedded
Id: rg25-zU3R-E
Channel Id: undefined
Length: 19min 21sec (1161 seconds)
Published: Sat Nov 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.