STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 34 - USB MSC Flash Drive

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Mohammad Jakub and it's time of the week again for another stm32 tutorial video and today's tutorial I'm going to show you how to use how to connect USB flash drive to these TM 34 discovery board and for any wasty on boards generally anyone comes with the on-the-go USB all right so in today's tutorial i'll show you the basics of how to open a file how to connect USB flash drive how to write to a text file and how to read from a text file using the flash drive so let's get straight to the point in open qmix and set up a mass storage composite project and on cuba max open click on your project and select the right board it's the HTM of for discovery that I'm using so discovery board of type of for and it's this one right and first thing we do in here as usual is the hull create clear all the pins enable them myself first thing I wanna enable is I'll enable my LEDs because I'm gonna use them in this demo so BD 12 13 14 and PD 15 and I would like to give them a label as well just to ease the programming so this is LED 1 and will appear in my main as LED 1 this is led to is quite useful then this is led 4 and I'm also gonna enable my push button on PA 0 and set this to GPIO input and I'm also going to give it a label label I'll call it button ok and the next thing on my guideline my tutorial steps is a value to enable the USB on-the-go host only under the V bus so needs to enable USB on-the-go and set the mode to host only and we need to enable the V bus this is for applying voltage to the terminal because we are using host so we uh plug in the USB flash drive in the USB flash drive doesn't have a power so we need to provide it with a power and that's why we need to enable the V boss in this case alright next thing is that we need to - USB host or middleware section then class 4 FS select Mass Storage host class and then on fat FS take the USB disk so that we enable file system for USB disk alright and the next thing we need to do is that we need to well enable the external oscillator to provide a very stable clock for the USB so go to RCC expand it and select the ceramic resonator and this will enable the external clock and then we need to there's one more thing we need to do is that if you go to the HTM of for discover user manual and look at the USP schematics so scroll down towards the end and look for USP schematics this one on figure 13 you'll see that in order to enable power to the v Passman that will enable that will provide three volt to the USB mistake we need to set pc 0 this is a pen on the SDM and this goes to the enable of the power switch to provide power to the USB terminal and this is active low pen so we need to enable PC 0 and set it to low in order to enable power to the USB so let's do that so please use your own e to be enabled and set to DVI to output and let's go to configuration and just make sure GPIO is on low state and sure enough its level is low by default so it will enable power now alright and back to my steps need to increase the fat 4s parameter exactly so that will be when you go to the fatty phase middleware configuration section scroll down until you find the maximum sector size make it to the maximal we have minimum to maximum so that we allow a larger sector size okay and then back to the clock configuration where we need to set our clocks since we selected the external oscillator we need to enable it use it as a source and then I'll set the system clock to 84 megahertz and should automatically sort out all the other ones perfect and the most important one for this demo or for this application is the 48 megahertz clock because that's the one that USB uses okay and then on my steps that's everything on cuba mix so we are ready to generate the project so click on generate source code icon and give the project a name I'll first select the project location I've really copied my path where I want to store it so I'm gonna store to this location and I'll call it USB flashdrive tutorial and select the right idea that you want to use I want to use mdk-arm v5 or kilo 5 and then select your middleware or firmware package I prefer to use 1.16 this is a very stable one where I do most of my coding on and I really like it never had a problem with it unlike some of the latest versions where I do struggle there seems to be more bugs not sure if they fixed a lot of them now so I will also include this down in the description of the video firmware 1.16 some people seems to request it for my latest tutorials so I'll add it to my description too right so that's everything here click on OK and this will generate the source code and while this is loading for for any of my business contacts you can get in touch with me through Munich's embedded comm and you can send me a request there but for the time being let's wait for this one to generate the source code and then we'll go to to Kiel and start programming alright source code is generated successfully so click on open project and this will take you to your IDE ok until the first thing to do here is to open the main and I'll implement a simple code to test the USB status update so what I'll do right a shortcode that turns the green LED on whenever you plug the USB flash drive and turns it off when you disconnect it so to do that simple thing you need to go need to open USB host at sea and we need to copy this application state a variable to our main as external so that we can see it in the main so and private variables perhaps set it as an external variable because it's already defined somewhere else and I'll do a simple state machine into my infinite wire load and by the way this function max USB m x USB host process this one that does the status update of the USB so things like when the USB is idle really blog disconnected and start and so on so we'll do the switch case and we are doing it for this variable application state and we'll implement all the cases so case 1 is 1 this 1 is equal to idle for example let's try to go out the differential of this well I need to compile the code so let me first compile it and then I'll get back to this and do the switch case all right so let's leave a combining for a few minutes alright could compile with on the arrows now let's carry on with our tutorial so I said I want to see all the status all the possible switch cases so go on this application idle and go to the finishing this will give you all the in ohms so this one is an application type to phenom and has got all these data so idle start ready disconnect so I'm gonna implement to all of these cases so for case 1 and then break case 2 is 1 the USB is on start and then when it's ready so that's when it's completely plugged and ready for anything you're an implement with it and finally when you disconnect it it will go to this state all right and basically what I want to do here as a starting point is that whenever USB is plugged it will directly jump to the start state so here I want to turn green LED on so using the harlotry key value function so how do you like your rice Ben and that's my LED one and the reason why I get the important pin labels is because I did label them as LED ones in Cuba mix that's why they appear on here if you haven't labeled them then you would just have to stick with the GPIO port and pin numbers as they are so LED one is led one GPIO port and the second parameter is LED one then right and then the state is so if you've got your pen sit or turn the LED on and then when you disconnect when you remove the USB flash drive I'm gonna turn the LED off all right so the state will be reset at this case and that's it so let's compile the code and test it on the board right plug into your discovery board and upload the code all right perfect as you saw on the short video I did work when I plug in my USB flashdrive LED turns on when I disconnected LED turns off right next thing I'll show you in my tutorial is that next one is we want to mount the USB flash drive so this would be as we plug it in we want to mount it by software so link it to the fat-faced library and to do that we first need to add a variable the fat Affairs filesystem variable so I'll call it my USB fat face and let's need to be defined somewhere outside the main so I'll define it in here I'll call it my fatty phase variables basically this variable is of type fatty phase and I'll call it my USB fatty phase variable anything really and I also need to says my earth mount function the function that mounts the USB uses the fat voice variable and also uses the USB path this is the logical path defined in the libraries you'll find it in the fact if s dot C and I need to define it in the main as external variable to make use of it so open the fattest of C file and you'll find it here at the top just copy it over to the main but with the extend thing so this one is that they use pH use be logical path so at extent at the beginning and this variable now will be seen in the main under routine is very simple so at the application start when you first plug the USB we just want to mount it up so we lose this function and they will turn the LED on of the mounting is successful so let's do that so the function is f mount and we'll do it in the start so instead of just turning the LED on will first do the mounting so f-mount I'll factory takes two parameters the first one is the fatty phase variable and a pointer to that so we put the ampersand sign and the second parameter is the path variable I believe yes it is so we need we also need to typecast this to T character constant because that's the type that a function expects when you go to differentiate the function expect this type variable so we need to typecast it to the similar type so this one and then we add the path you use pH path well and then the last parameter is whether you're mounted immediately or whenever straily I'll just put 0 so that will be mounted whenever it's ready and then if this one is equal to okay then I want to turn the LED on all right so we'll put this one inside the condition block perfect so I'll be very similar to what we have previously now when you plug the USB this will be mounted and if it's successful then I will turn the LED on when you disconnect it it turns it off so let's compile it load it to the board and do another test great it did work as well oui Mon tonight we get led on we disconnected LED turns off right next thing I'll show you is that I'll show you how to write a text file to the USB flash and how to read a text file from the USB flash and to do that we first need to define some file i/o variables so there are variables that you need to define so that you open a file and then see the result of the file operations like if open if close enough read and there are the bytes retina and bytes rate for the write and read functions and this is a a temporary buffer to store your data to be brought into the text file and the data read from the text file so I'll first defined this variable at the top of my main so that I can use them for the next operations so I'll just copy them and over so that I don't waste more time and I'll add this text file down in the description of my video so that you can follow me step by step and you can pause the video if you if you think I'm going really quick so I'll copy this variable over to my main and I'll add them here at the top alright very basic variables and then let's implement the file write function before we be able to do the new spirit function or the fixed via write function of the USB flash drive we first need to include two library headers the string library and the boring library because we're going to make use of them and the following so let's add them into my main in the include section suburb and then now we need to define the function prototype for you speed test right and we'll define it at the top in our main so this should go into the private functions section prototypes okay that's for my write function and let me also do my read function on the go so and that's for my read function okay so the write function is very simplistic doesn't take any parameters this is just a test ride and this is a test read and it retains boolean variable so when the right is successful it will return one and if there's any error overturn zero so you can check for the status and then accordingly swap your led all right now let's go ahead and write the function body of the test right Frank sure let's do that I need to copy the venture prototype down in begin number four where I'm gonna go ahead and detail the body of the function as in my steps in guideline the first thing we need to do is that we need to open a file for right okay so to do that we gonna set this in here remove the semicolon so open or create file for writing so that we need to use a function called if open part with the fattest middleware and the first parameter it takes is the file pointer so we have to find a variable file our variables generally at the top and this one requires a file pointer so my file I call this and a pointer to that and the second parameter is the path as the name of the file so maybe test or test two since I think I have a file already stored in my USB flash drive called test so this to dot txt and then what's the operation for opening so it's gonna be writing so if I write or create always of the file doesn't exist perfect and I want to check if this one is okay then I'll carry on if it's not then I'll return there are all right so let me sit in f'q induction here if this one is not successful so the exclamation mark in equal which mean not this one is not okay then I want to return to the function with an error and not continue with the rest and if everything is okay then I carry on okay so we'll carry on and that would be next step is that I'm going to copy some test string to my variable I called using spring turf so let me show you how it's done so now Coby just fixed I know that's very difficult to my temporary read/write buffer that I defined at the top initially so the file I will variables part of them is a temporary buffer so that's the one this is the read/write buffer okay I'm gonna use it now to add text to it and we use this printer for that there's a string function sergeant letter storing in this variable and I'll say hello for misty and 32a for discovery maybe you can write anything you want all right and then I know we can write it to the text file that we just opened so write to text file using F right Richard okay and this function takes the first parameter is similar it takes a file pointer and then the second parameter it takes is a pointer to the data variable and I think the type this mention expects is so we've got to do finish with this it expects a constant void pointer so we'll do a constant void so constant void pointer and we pass this variable and the next parameter is the length of the data that you wanna send so that will be the length of a string so using making use of string length function to count the number of characters in this string and then next parameter is last last one as a pointer to the apply certain variable so this will store the number of bytes that are written successfully so that we can keep track of them for debugging and I already defined a variable for this that I called by certain of the type u and and that's just the type that the function expects if you look in there so it's UN data type of variable the function expects so and I want to check if this one is successful I carry on if it's not then I return an error of the function so this one is not if our okay then I'm gonna return with an error certain an error flag of zero if everything is okay then I carry on all right and then to carry on and also part of the check is no wait I'm sorry so that should take a result first because I'm also checking the right and the same time I need to check the number of bytes I need to do two checks that's why I'll make use of my result variable that I defined a part of the file IO variables is the F result one that stores the result status of any file i/o operation so not that quick I will just to store the status and then and then if resort is not okay and or vice-versa 10 is equal to 0 let me just put brackets just to control the operations and also of ice for 10 are equal to zero so if nothing is written then I know there is an error so our return an error otherwise I'll continue normally okay so now having written them successfully I know I can close the file and return success flag okay so this could be over so if closed as a function that closes the open file and that takes the my file point r2 with the unperson sign and then I can return one the happy flag all right does that mean success all right that's it so let's go ahead and test it in our main so copy the function and call it over and there and I want to call it in my ready state so when everything is ready it's started mounted I want to set the condition on my push button if my bush return is pressed so if hull GPIO read Ben I wanna read my push button status since I label this I can just write button and it will automatically tell me the board and then my button bin if this is a call to GPIO and sit FS equal to high then I wanna write to my text file do my test write function also a delay of one second just not to do the override again while I'm pressing a button and I want to test if this one is equal to okay then I'll turn my LED on so this is okay then I will go right Ben I set my second LED perhaps so led to or let's say LED for the blue one okay then I'll set my blue LED to hi if this is successful otherwise I'll set the red LED indicating there is an error and the red LED is a third one LED three a last one I label it in here three which is B D 14 so let's just double check with our guidelines so that's the way to implement it yep yep yep let's go ahead and test it so let's compile the code load it to the board and we'll do a test or the board so what we should expect is that when you press the up user post pattern we expect the blue LED to come on if the operation is successful and then we can plug the USB flash to my to your computer and see if the file test the - the Teague's - is rotten what does content inside okay okay compiled with our oath now let's loaded to the board unless they sit with the board all right perfect seems like a successful according to the led now let's plug the USB flash drive to our PC and see if the file is retained all right so that's news B flash drive open exactly we've got test dot just food at sixty let's open it sure enough hello from stm32f4discovery just like what we've written in here all right superb now let's unplug it again now next thing I'll show you is how to read a text file that's very straightforward very similar steps so let's continue with the tutorial I'll show you how to do the USB test read function body ok so we have defined the prototype at the top let's copy it over and start writing the function definition alright very similar to the function to the right function first thing is that we need to open the file but now for reading instead so let me just copy this to save time now we're opening the file for oh that's an M should be an end sorry so opening file for reading this time so you don't need the F any right you don't need to create always you need to if a read okay you can so the text just so the text protects is you click on control space and I will show you there your prediction so I want if a read and then so if this one if the file is opened successfully then we continue if there's an error then we return an error flag all right and then we can do if a read to read the content of the text file so very straightforward according to my guideline I but I'm doing a special read type so I'm reading according to the length of my buffer so since my perform a temporary buffer length hundred bytes I said it 200 bytes of lymph then what I do with the read function is I'm reading the file byte by byte until I get to the null character which which marks the end of the string then I stop reading so let me copy this thing over and I'll explain you step-by-step okay so what this does is a reads from a text file till the end of a tell the null character which marks the end of the string so if a read every function is very similar to a Freud it takes a similar parameter my file and then it takes the buffer to the pointer in which you want to read data to and then the bytes okay so I'm going byte by byte so index by index from index 0 to index 99 and I am reading to the temporary buffer first starting with index 0 and then incrementing according to the for loop with an I so a will each byte overwrite it to the next element in the array and it will is doing by trade so the F read function let's just make it very very simple it takes a pointer to it to where you want to read data to and then the size of data and I'm reading byte by byte and I'm checking each byte I read I read if it's equal to 0 which means the null character in a string then I will stop I will break the loop and I will keep track of the number of bytes right which is equal to the eye of the for loop if you didn't get that don't worry you can you can always just copy the code from here and then spend spend your time try to understand what it does you can experiment yourself is very straightforward no difficult really and then if the by thread are equal to 0 then we know there is an error we haven't read anything and this will return an error otherwise everything's successful and we can return one that marks success but before that we need to close file of course so we need to close the file all right my closing file is very similar to what we did with the rise function now we're 2ne1 - mark success so that's everything for the read function let's compile the code and let's first also copy it to the main so that we can implement it so we'll do it in a very very similar way we'll do it in the ready state when we when you press the push button instead of writing the text file I will no have came in the USB test read function and it will turn the blue LED on when the read operation is successful but we're also going to get to debugging mode and read the temporary buffer and see if the text is read successfully that's stored in that text file alright compiled without any rules now let's load it to the board and we'll do a quick test with the board right amazing looks like is successful because we got the blue LED on now let me let's go to the debugging mode and see the data that are read so navigate to debugging mode while keeping the USB flash drive connected in everything okay I don't debugging mode I want to add my temporary buffer variable to watch memory so that I can see the content so highlight it and right-click and add to watch one and expand this a little bit so that I can see the content that will appear in here oh that's everything let's make sure there are not affecting divide there are no breakpoints so kill all breakpoints and run the code and when I press my blue post pattern sure enough I give the hello from stm32f4discovery and that's just the text stored in my test file text file alright that's it that's everything I want to show you at this video that brings me to the end of the tutorial thank you very much for watching and as always if you found it helpful don't forget to Like and subscribe and maybe recommend it to a friend and for any of my business contacts you can get in touch with me through mutex embedded comm thanks for watching and I'll see you in the next vid
Info
Channel: Mutex Embedded - Education
Views: 14,129
Rating: undefined out of 5
Keywords: STM32, MCU, Embedded, Microcontroller, Programming, USB, Cortex-M
Id: XycS9_jmYn0
Channel Id: undefined
Length: 31min 48sec (1908 seconds)
Published: Sat Apr 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.