STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 33 - CAN Bus

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Mohammad yaqoob hope everybody's having a very nice weekend and it's time again for another stm32 tutorial and today's tutorial is going to be a very interesting one so today I'm gonna show you how to use canvas and I believe many people have written down in the comments of my videos that they want to see canvas tutorial so here it is let me give you a very brief introduction about canvas before we get started so can bus protocol looks like this you have a an ID so that's the canvas device ID and you have the data length and the data that you want to send and you can send up to 8 bytes I believe with canvas and then come to CRC field and so on but what the stm32 can't peripheral you don't have to do any of this protocol by yourself all you need is to give the STM what your ideas and what the control bytes in the data and it will do the rest it will calculate the CRC and I will manage the transmission and collision and all of that details alright and what you need for today's demo I'm going to communicate between two STM boards to send and receive data over between the two boards and what you would need you need to canvass transceivers I'm using the c j MCU 1051 module why i believe you can use any kind bus module as long as it works the same so for the connections you need to connect the context of the SDM to the CTX of the module and similarly for the RX the same for the other board TX t TX and RX to RX and most importantly you need to connect VCC to 5 volt I think if we connect it to 3 volt I don't think it will work it needs a higher voltage alright and you need to connect the can high and can low all together with a twisted pair preferably but I think for short distances it would work with a normal jumper wire just like what I have in here and mine demo today would be to synchronize two LEDs or the two is T on board that's basically it's actually one of the st examples that they provide and I'm gonna show you how to make it from scratch so the final working application will look something like this in the video where I have to steam board and I press the button here the two LEDs synchronize together and same thing when I do it with the other board the first board gets synchronized - and it's all with canvas okay so let's get started and setup a project in cuba max on cuba makes first click on your project and then select the right board so i'll go to board selector and it's the discovery of for myself for discovery that i'm using i'm using two of them okay and on the pan out as usual i'm gonna clear everything out and then set it up from scratch myself so first thing I'll do is I'll enable my LEDs because I need them in my application so PD 12 13 and 14 and 15 for my LEDs and I'll enable the push-button because it's part of the demo too and now I can enable the canvas peripheral so on the peripherals go to can one that's the master canvas and enable it and this will get mapped out to PA 2 11 and PA 12 and I don't wanna use ba 11 and 12 because I think those are part of the USB on-the-go and the circuit gets a little bit messy so I want to map them out to a different pen i want to map them off to bb-8 and vb9 that's where my wiring is done on so what you need to do is click on ctrl on your keyboard control and the left click of the mouse and i will show you what other alternatives will drag it over to bb-8 okay the same thing for the TX ctrl on your keyboard and will show you what other alternatives and I want to connect it to be benign okay that's as simple as this and if you don't know how to do this you can do it differently so you can just go straight to bb-8 and enable can and enable can RX and it will automatically cut calm here same thing for the TX okay that's very simple next thing I wanna enable is own enable the external oscillator it's much more stable so go to high-speed clock and select the ceramic resonator and now let's go to the clock configuration window our sir I will select the external oscillator and I'll set the frequency to 84 megahertz and it will do the rest automatically next go to the configuration tab and I need to configure my canvas so on parameter setting I'll set the time quanta bit segment 1 to 4 and Sigma 2 to 2 so these timings are describing details and the reference manual of the MCU and some of them are application dependent but for application in foreign to will do the job and then I'll leave everything else as default the operating mode need to be normal just like what it is and the other modes are for testing so loopback silent are for testing not for normal operating ok and then let's go to the interrupts and I need to enable the rx0 interrupt and that's it click OK and we are now ready to generate the code so click on generate source code icon and I'll call the project can video perhaps or can tutorial and I'll store it at this location and select your correct IDE I'm using MDK rv5 and one more thing I want to use a different firmware package but you don't have to do this but I like to use version 1.1 8 or sort of 1.2 18 I think it's one of the most to stable firmware for canvas but you don't have to do this I've got all of them installed in my repository so I'll select one point 18 and most importantly has to be there for and click OK but as I said you don't have to do this yourself and while this is loading for any of my professional business contacts you can get in touch with me through mutex embedded the calm all right I know let's just wait for this to finish generating the source code all right the code is generated successfully click on open project and this will take you to your IDE okay and until the first thing we will do is that will open the main and we'll do basic initialization to the TX and rx message so I've got this in my steps of making this demo so after setting up cuba mix we need to declare TX and rx message type divs and set them and they initialize with canvas so it's just something like this so you go to private variable section and I'm gonna define two variables this is TX and rx message structure perhaps structure or type live it's actually a structure type def type static can rx message type live and ultimately came out and I'll call it my rx message and same thing for the TX so can TX message type if and I'll call it my TX message this is where all the RX and TX message data are stored so things like I show you on the canvas frame so this variable when you navigate to the definition of it but I don't think it will go because code is not compiled yet let me leave a compiling while I'm doing the tutorial so this variable acts as a structure and it stores all of these variables so the ID at the top of the ID with our standard or extended and then the data length and the data bytes and things like that so that's what it is for and after defining it in here you need to initialize it as part of the canvas initialization so scroll down till you find canvas initialize this one and you need to add it as bar to the field here so H can 1 dot PTX is not coming out so let me just wait for us to finish compiling and then we'll carry on with writing this code all right finish go bollocks so so what we need to do is as part of the initialize we need to write a PT X and set the TX message here so we define a variable for TX message that will store all the parameters so let me copy it to here as a pointer so you need to what the unpleasant sign and similarly we need to do the same for the RX because this will store all the TX and rx parameter in this variable okay so and if I go to the definition of those you will see what parameter those things hold so go to the finishing you see what this hold it holds the standard ID we the rights and the ID type data length and data just like why I show you in here okay so that's what this variable is for okay having done this we done with the initialization function next step is to do the can filter config function so that's to configure the canvas filter of unify our certified ease as necessary so I'll create a function for it function prototype will look something like this void my can filter config oh actually I don't need the mic can here okay unless doesn't take any parameters and it will initialize the and will configure the can't filter and I'll do the function definition at bigger number for as usual and the function definition looks something like this you need to define a can't filter config type def that will store all the filter configurations so that then you can pass it to the config filter function okay so we need to define a can sorry so can filter config controlspace if you wanna see the options so can filter config type diff is the one we're looking for and I'll call it a can or just filter config and then what you need to do is that you need to set all of those fields so bank number filter activation filter FIFO assignment filter ID high filter ID low and all of them so I'm gonna go over all of them now first and then I'll write their parameters and filter mask ID law this is for the extended only another mode and hopefully finally no yeah faulty number sorry and finally filter scale first bank number is 14 and then filter activation is all you need to enable the filter so set this to enable and then filter fly through assignment to I think 0 something like that yeah we're using 5 4 0 of things like that and then filter ID hi or set those all the filter parameter to 0 because we're not I'll show you later on how to enable filter now I'll keep filter disables the own except so I want to accept all can traffic I don't have filter out anything for now I'll show later on how to filter out certain IDs or perhaps how to allow certain ID and filter the rest and the filter mode is filter mode is ID mask so there is something called ID list an ID mask and details are described in one of the ISTE forums if you know more details about it so this is can filter mode ID mask okay last one and the filter number is filter number is zero okay the filter scale is 32-bit because there are 16-bit and 32-bit for the filter scale and there is a purpose behind it and the finally I need to call in my Hulk an config filter function so how can filter config our config filter function rather others function takes two parameters it takes the can't handle and it takes the filter config type def so it can one as a pointer and then the filter config has appointed to alright that's it and I can do the error or check as well this is not called okay then I can do the or all but I think this one will always work I believe so alright so this is the can filter config and what we need to do is that we need to call it at the start of the main to implement those filter configurations so filter config we need to call it in here alright the next thing is the I'll define LED switch function as you saw in the demo when I click the botton of the first esteem board and I keep it there it will switch between LEDs and that's what this function is for you give a number from 1 to 4 and it will switch between LEDs and this is a very straightforward function so I don't want to spend a lot of time all right I'll just copy and paste it I'll go I bought this txt file down at the description of the video as well if you want to copy over the steps so I'll copy it I said LED switch I first need to define the function of prototype in here so this is a Leedy switch and then I need to do the function definition in bigger number 4 so just copy it over and bigger number four this is my LED switch function very straightforward all is doing is a switching off all the LEDs at the beginning and then based on the number it will turn the right LED on okay and the next we need to do the main initialize functions so I already did that for the can't filter so you call the filter config at the beginning of the main to implement the filter configuration and then we need to start they can receive interrupt before we do anything because this will just start listening for any can traffic and when anything arrived it will jump into the can't receive callback that I will show you in a moment so let's first start can IT receive and the function looks like this so how can receive I T go in here and this is basically start listening for can traffic with interrupts so how can receive is interrupt infection takes two parameters it takes the whole text it can handle a pointer to that and it takes it can each other.what five for you and you is we've got two five foods you can choose any of them I'll go at five four zero but I would have to go at five four zero because I have enabled our X zero interrupt if you have noticed no arcs one so that's why I need to do here alright and next I need to do the transmission can parameters before I do that let me define two variables for the canvas IDs as I'll show you you need to define we can ID for the device as part of the as part of the packet and because I've got two STM boards so I need to have two IDs for the my own board and for the remote board and I can keep them the same as well so let me define two variables for them so my own and removed IDs and signed integer 8o so out of 16 I'll call it my own ID and that's equal to 0 X 1 2 3 for example and then they remote ID for the rate for the other board for simplicity I'll set it to the same for now so that I can flush the same code into both boards and it will work and I'll also demonstrate to you the case where we have different IDs and all you have to do for different IDs is that only flashes in the thing to the first board flashes with this order of IDs and when you flush it in the second board just to swap the IDS because now the second one will be the own and the first one will be the remote and so on I hope you got that so that's it and then now I need to set the transmission parameters before I transmit any things so it will be in that big in number 2 again set transmit parameters so that will be the canvas protocol parameters and for that we need to use the can handle and go to the PTX message the one we set at the beginning as a pointer so I'll first set the type of the ID so we are using a standard ID not extended so you need to do this can ID it's a standard and then next parameter is I'll set the standard ID itself to my own because I'm transmitting so I'll set the ID to my own ID and next would be let me set the data the RTR so the type of data I'm sending this is a data because there are two things either data or command things like that but I'm sending data so just use this one and then what need to do with the data length of course I'm sending two bytes but you can do just one bite or you can do one to eight bytes I believe with canvas so data length I'm sending two bytes one for my LED status and one just a dummy data I'm sending an over all right that's for the transmit parameters so and now I'm ready to transmit data and for the can transmit we'll do that in my infinite wire loop I'll check the push button status at the bottom of the button is breast then I will increment the data and send them over with canvas let me show you how it's done so in here F my button is pressed so if i'll GBI who read been reading my push button which is on port a and pin number zero and if this one is equal to CP i-- on6 this is high then I wanna transmit a can bucket okay and I'll say a short delay at the end or flips a hundred milliseconds yet 200 throws alright and to transmit who I firstborn I'll have a variable that stores my led status from 1 to 4 I'll call it data variable you can call anything so let me send unsigned integer 8 I'll call that my LED well variable may be anything okay and I'll initialize it to 0 and what I need to do here is I need to set it to my LED function thing so LED switch at the beginning when I press the button I also update my own LEDs and then there are motor board LEDs through canvas just like what you saw in the video so I will increment this parameter link remitted by one because has to be from 1 to 4 not from 0 to 3 or 0 to 4 and the reason and for that I will check if this variable is equal to 4 then well I don't need the hex anyway so this is equal to 4 then I will initialize it back to 0 and what this increment will do is that since I'm gonna has an X to 0 whenever it gets to 4 and there's always increment 1 this one will always be from 1 to 4 if this confusing don't worry just write it down you will get it you'll get what it does it's very straightforward alright and next I can just set the data to be transmitted and transmit them with canvas so it would be H can 1 dot the TX message and I need to set data 0 to my LED switch so my aleady variable will go on as a first parameter and the second data any dummy data because I'm sending two bytes so I gotta give it another data so let's say so x12 for example anything I know I'm ready to transmit via canvas so Hull can transmit and this takes the can handle and it takes the just a timeout so 10 milliseconds or 20 millisecond whatever you want as long as it gets transmitted successfully and you can also check for an error if it didn't if you didn't get through you can go to the error handler well I don't want to I don't want to jump into the error Honda I want to try again so I'll leave it as this let's see if there's anything we haven't done yeah sure enough we also need to do the can't receive callback so this is the transmitted code whenever you press the button it will transmit a can packet but also whenever it receives a packet it will jump into the interrupt call back so now let's go ahead together and do the receive callback for the receive callback go to and stabbed and killed and expand the can the can library and you'll find a receive callback function and receive complete call that and that's what we need let's copy it into our main without the weak object and that needs to go and pick a number for the user functions and in here we will implement the callback routine so whenever will receive a message we just want to first double check that the ID is correspond to our expected remote ID and then we'll go ahead and read the data and update our LED status and start the receipt all over again so here is what you need to do I first need to check that the receive data are actually coming from the specified remote ID not from other devices on the canvas network so I need to I'll use the edge you can hear and the prx because the received message and if the data of zero sorry not the data if the standard ID is equal to our remote ID that we specified then I will accept the data update my variable and then update my LED status so this is my LED variable and I'll read whatever data read by the con and set it to this variable so we'll do the H can PR eggs and then the data of zero because this is the data of LED I don't update my LED variable just like when we did for the transmitted data of index 0 are the LED and that will be the same sent by the other board as well because we're gonna flash the same code in both boards alright and now I need to update my LED switch and pass this variable to there all right and then at the end of the callback I will start listening again for another canned packet okay and I will be the Hulk and receive intervention that will take similar parameter the H can one and they can five four zero all right and I believe that's everything for the very basic demo now all right so let's compile the code let me just go over it really quickly to make sure nothing is missing all right and I'll do for simplicity is that I'll leave I'll set the owner remote address this is the same so that I can flash the same code to both boards or I compile with our new rules now let's flash it to let me plug one of my boards and flash the code over to now I'll unplug the first one I'd like the second one there is an easy way easier way but I like to do with the simplest way now flashing the second board right now I'll plug in both of them to power and reset both of them and let's see if that is working and sure enough working exactly as we expected now next step is that I said I want to show you how to use the filter thing so how to filter out IDs so for the filter function let me first change the own and remote IDs so my own ID is one two three four and the remote is one two three or one two four you can use any IDs as long as there are 11 bits in width and I'll use the filter I know that my my remote ID is 0 X 1 2 4 and to enable filter set the ID hi to whatever I do you want to filter or you want to allow to a bus and then shift by 5 so 0 X 1 2 4 and they shifted above by 5 because this filter ID hi is used by the standard ID and the extended and for the standard is the shifted by 5 spaces and the register you'll find more details about this in the reference manual of the STM and also in this HT forum so you'll see a lot more details about why this is the case I don't want to get in too much details of it because it can be very confusing for some so the most important thing is that you need to set the ID filter so I want to allow one to four to bus and in order to get to make this correct I need to set the mask ID high to to a 2-0 X F F 0 and the reason for that is because the ID is 11 bits so this is 8 8 bits or a byte and this is three more bits so 8 plus 3 is 11 and the filter mechanism work in a very simple way so you do an end of a damask so if mask and which is a a bit masking operation with the idea that you sit here the filter ID which is 0 X 1 to 4 in this case sorry so this is equal to the ID itself 0 X 1 to 4 then then it will allow it to pass will allow this packet to pass for 1 to 1 packet filter allow so this mask is always 1 so 0 X if is just once and when you end once with anything you will get the same thing so this will only allow one to 4 however I hope that makes sense [Music] and it can allow other ones to I believe for more details about filter implementation get to this s/t forums I will include the link down the description of the video I don't want to get more details because filters can be very complex you can implement tons of filter ways but this is a simple one so I'm only allowing one to four let me show you how it works so I said I want to allow only one to four so I'll flush this to one of my boards because I already flush one of them and now this one the own will be the same but the remote ID is different so I believe I need to flash both of them in fact so let's compile the code and flush it to the first steam board so that we'll have an own ID this and a remote idea of 0 X 1 to 4 well in fact instead of using 1 to 4 in here let me use the remote ID because I'll change it for the second board so this will only accept the remote ID thing so compile again and flash it to the first steam board now for the second one like the second one and unplug the first because keel can get confused need to swap the IDS now the remote will be unknown in the own world via remote compile the code and flush it again alright now I expect it to work exactly the same because the filter will always allow the remote ID to get in so let's just do a quick test reset both boards and run them unsure enough it works exactly the same now let's do a very basic test to one of the boards so I know the remote the idea of this board the second one I flashed was 0x123 let me change the filter to a different ID now it shouldn't walk so if I did Eero X 1 to 5 for example now one of the boards will stop working alright okay so I think I have I've confused the board so I flashed first a second so let's do that all over again with the filter very quickly so I'll just reflect both of them the same first so this one use the remote ID and then the first board is 1 to 4 and the second world is 1 to 3 as a remote so flashing the first one with this so I'm just getting back one more step what the filter being the same one remote ID I'm not trying anything else with the filter so I've lost the first one now let me flush the second one with the ID swab ok now compile the code again and load it all right and now since this is my second one I want to make this change so I want to filter out the wrong I want to allow the wrong idea now I don't expect the second one to receive anything but it will always transmitted successfully and get received by the other board so if I say this one twenty five now and flash it to the second board I just flashed I expect one of them to transmit and receive and the other one just to transmit and not receiving successfully all right let's plug it in and do the test exactly one of them transmit and receive and the second one only transmit and doesn't receive successfully because the filter is not working it's actually blocking the right one all right let me see if there's anything I haven't sure know that several things so basically this brings me to the end of my canvas tutorial today thanks for watching and as always if you found it helpful don't forget to Like and subscribe and for any of my business contacts you can always get in touch with me through a mutex and be ready to come thanks for watching I'll see you in the next video
Info
Channel: Mutex Embedded - Education
Views: 41,669
Rating: undefined out of 5
Keywords: STM32, MCU, Microcontroller, Software, Embedded, Automotive, programming
Id: ar3I38lCLT4
Channel Id: undefined
Length: 35min 16sec (2116 seconds)
Published: Sat Apr 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.