Stm32 Peripheral Drivers from Scartch : GPIO Programming Part 1

okay so now we're gonna see how to configure the porch first and foremost so then we can get an idea in how to write our peripheral driver so we're gonna start a new project well first and foremost you do want to have your datasheet open okay cuz we'll be going back and forth to this Thank You doke so let's start up a new project and I went over how to do this in a different video so I'm not gonna restate any of that here I just call it GPIO driver look for chip stm32 f103 c8 here it is I'm gonna get necessary files the start up file and the CMC's core okay I'm gonna pre-compile this even though it won't really build but I hope you wouldn't get some dependencies here okay I'm gonna rename this for this is just personal preference I'm gonna right click here on target add group which gives me another folder rename that to user libraries or drivers whatever here I'm gonna add a new item to the group on under user application C file this is gonna be where my main file is gonna be I'm gonna call it main but you can call it anything you want it doesn't have to be called me just because the main function is gonna be there okay so I have a main dot C I'm gonna include this header right there which is yes TM 32 f10 X dot H now I can make my main remember to add some empty spaces after this otherwise you'll get a warning I'm going to build the target zero errors or warnings okay and I'm gonna configure my programmer the st-link go to debug st-link settings um what does this okay no that's fine trace changes just to eight because the crystal is at least on my specific board is eight change this to 1 because it's 64 not 128 yet dad erase we saying run okay I'm gonna change my seat to see 99 because I like it change this to 8 also output page user sees linker don't even like that yep and we're good to go now if i plug in my programmer i should be able to compile and upload just without a problem yep so that works alright so let's see um the first thing we're gonna do is we are going to initialize the clock for the kind of hard to talk and type at the same time initial eyes the clock so the port that we're using and we're gonna be using port c because on port C on this specific board there's an LED on pin 13 so we're basically gonna do the whole LED blink hello world thing just to get just to learn how to you know how to program the port so let's open up our here our datasheet to enable the clock we're gonna go to this peripheral the RCC which is a registered clock control I guess or whatever we're gonna go down to the RCC registers and I know that GPIO pins are on the APB to bus and how do I know this because it says it right here GPIO a b c d e and f are on this bus right here the APB to bus okay so that's how i know that and you really gotta go through your datasheet and just I guess learn basic things from it so where we are at the RCC peripheral which is this handles basically the clock for all the peripherals on the microcontroller because when you first turn on the microcontroller everything is turned off for power consumption it keeps the power consumption low so that obviously if you don't if you're not using new word or I squared C or SPI then you leave it off and you only turn on what you need okay so we're gonna go to that APB to enable register which is right here the APB to powerful and able register just scroll down to look at it and here it is and we can see that IO POC which is input output port C enable is on bit four so we're going to shift a four into this and this is before if you read down here and bit four it says IO port C o'clock and evil zero disables the clock a 1 enables it so we're going to shift one into that fourth bit of this this register another thing is to know what you have to write in your code you're gonna first start typing this which is the name of your peripheral then you're gonna dereference it okay which is going to be basically a dash and a right arrow and then you're gonna start typing this and this is the actual register inside the peripheral right so it's gonna look like this RCC right dereference it the structure and it's a PB 2 enable enable register right there so the APB to enable registers we're gonna or one into the fourth bit you know one into the fourth bit and that's it that initialize the clock for the port and it's good to go so now we're gonna configure pin three on port C okay and I call these things port but in the datasheet they're called GPIO C I'm just so used to calling them ports from the Arduino days so yeah GPIO C so it just just renamed what the hell did I do okay let's go back to the datasheet let's go to our general purpose and we're gonna look for the control higher register okay so we're now at our GPIO peripheral again he's the name of the peripheral you're gonna replace the X with the port or the one that you're using in our case we're gonna be using C so we're gonna so we're gonna start typing GPIO C we're going to dereference it and type in the name of the register which is to control high register again we're using the control high register because we're using pin 13 which is basically on the high register because if you recall the low register only handles pins 0 through 7 okay so if we're gonna use 13 we get you have to use this register and you see it starts from 8 all the way up to 15 and 13 is right here these two sections so for the mode section for my example for no particular reason really I could have chosen any of these 3 but I'm gonna set it as an output mode at 50 megahertz okay so I'm gonna have to shift a 1 into here and here okay if I wanted the 10 megahertz I would put a 0 here in the one on one bit 20 and since I'm using output mode then the output options are gonna apply for the configuration bits which are 22 and 23 and all I'm gonna do there is just shift two zeros in there to set it as general-purpose output okay it's okay so let's start doing that so again like I like I said what we do is we start typing the name of the peripheral with changed the X to the port that we're using in our cases see you reference it and then type in the name of the register so that looks something like this GPIO see right dereference it control high register which is CRH and then we're gonna or equals a I forgot what it was it was a 1 1 this is the mode okay 1 1 into the mode so it's gonna be a 1 in the 20th bit and a 1 into the 21st bit so we're going to do to me double double things here 1 into the 20th bit 1 to the 21st bit okay I just use these extra parentheses just to enclose the entire thing it doesn't really matter just whatever ok so that we configured it as this right now makes it into output at 50 maker it's correct okay so now let's go to the configuration bits configuration bits right here since we're an output mode these apply and I want to shift to zeros into there okay the way we're gonna do this is we're gonna ant not it so GPIO see oops dereference control high register and not or not not but compliments one into the 22nd bit or one into the 23rd bit heroes oops not with this general general-purpose output I know you gotta love my typing okay so yeah what is 2023 that's check yep 22 and 23 we shifted the desert over there and if you don't understand this right here and and the complements and Orion etc I think I'll write a different series on tutorials on basic things that you need for programming embedded see because I took a course at school on C and they didn't cover any of VZ any bitwise maths so yeah or you can just look online and look for courses on embedded C which will teach this stuff okay so now we've got our pin 13 as output in general purpose yeah general purpose output at fifty megahertz now that's all there is to it as far as the configuration grows and now we just have to actually use it so let's just make a while loop right so we're gonna make a while loop and let's just use a I guess a one of those inappropriate for loops as a delay correct equals zero now if you're using different versions of see I like to use the c99 standard and it allows me to define I in here otherwise you would have to define it outside of the for loop could you you wouldn't be able to define it in there but I like that so furniture I equals 0 is less than equal to 5 million itay okay so that's a silly delay now to UM set and reset the pins go back to our data sheet we're gonna use the bits set and reset register if you read the previous post and you should have an idea how this works so since we're using pin 13 I want to first set the pin 13 right oops I'm gonna outline everything so first I want to set pin 13 so bit set their team which corresponds with bit 13 so the mapping right there is 1 to 1 so let's set it so GPIO see correct the reference said we are under bit set reset register psrr and this one we don't have to use any bitwise operation so we just equals just straight up and shift the 1 into bit 13 boom on button set pin 13 high okay now that's at another retarded delay here okay my tabbing is off okay now we're gonna reset it okay now I don't even have to look at my data sheet okay because I know that the set bit is 13 right to set the pin high and I know that the reset pin is my pin number which is 13 plus 16 so GPIO see what the hell is wrong with my tabs GPIO oops io see you reference it yes r equals the one shifted into 13 plus 16 oops what the excuse my language let's just get rid of this one so I'm shifting a1 into 13 which is my pin number which is also my set bit which corresponds to my pin number you know those are map perfectly one-to-one but these are not mapped one-to-one but they are mapped in a way that is very predictable and always true which is your pin number plus 16 so you're set bit equals your pin number your reset bit is equal to your pin number plus 16 right reset in other words drive it low and we add another inappropriate delay and I say inappropriate because these are not good ways to know I don't need another one I'm sorry I won at the top these are not good ways to make to make delays so if I were to okay last time I press this is my second time doing this video and I pressed f7 and it messed up my recording so you have to just press the button zero errors or warnings load this on to the board and unfortunately I don't have video but it's blinking okay so trust me this is good to go so this is all that it really takes to UM to operate the the general purpose input output peripheral which you know which is basically depends and as you can see it's very easy and you just gotta use your datasheet and you know look at it and see what you need to set so I hope this helps and in the next video we're gonna actually start we're gonna make the files that we need to start writing our drivers so we can take all of this and I'd have to worry about shifting this and shifting that because if you were to come a couple of months from now and just read this this is not really reader friendly so we're gonna make this look a lot nicer and much more I guess friendly you can call it where it looks something like like this where you say GPIO mode output type general purpose feed port C pin 13 and then finally enable C that's a lot more that makes sense to the eye you can read that okay I can see what I'm doing as opposed to this where you just shifting bits into registers you know like okay what the hell is this you know what am I doing here so that's that guys thank you for watching

20 thoughts on “Stm32 Peripheral Drivers from Scartch : GPIO Programming Part 1

  1. // GPIOC->BSRR |= GPIOC->ODR & (0b1 << 13) ? (0b1 << 29) : (0b1 << 13);
    GPIOC->ODR ^= (0b1 << 13);

    I found this to be a faster toggle…. and uses the dreaded ODR register

  2. havent watched this yet but ive added it to my watch later. i can tell this is gonna be useful as F. and in case you're wondering – that F stands for FUN! ..nah actually it stands for fuck.

  3. Eddie you are awesome !!! the way you describe those things is kinda genius !!!! thank you so much !!!

  4. Few tutorials actually show how to write directly to registers which is what high performance embedded controllers need (to be fast and predictable). I don't want to abstract any more
    than this. Operations like the -> operator are poorly understood by most C programmers but are used extensively in header files. Thanks for showing how it's done. Great job. Do more.

  5. Hi, Thanks for your great video 🙂 , Is there any chance that you could send me this data sheet that you are using in this video ? I couldn't find it any where ! and I really need it:(.

  6. at least a sendible intro … to someone who does not want java like abstractions what are in the stdlib

  7. hello sir, the link that you putted on the description for your personnal page is not working, do you have another one that's working ?!! thank you in advance

  8. Can you make video how to do same stuff with EmBitz? What is best and free for stm32F development anyway?

  9. hello sir can we do the programming for reading switch input without using drivers normal coding by refering refernce sheet

  10. Why do you keep clicking your tongue? Where do your tutorials start . this is number one and is already in the pond

  11. Hello Eddie, Great help with this tutorial!
    Have you ever considered choosing another IDE for your projects like AC6 SW4stm32 which can be combined with STM32 CubeX software to define automatically your peripheral firmware for you. What's actually your preference? Thanks
    I'm actually learning with SW4stm32 but still it seems that your typing by deference the structure of the periph are preatty handy on your IDE!

  12. Thank you very much for this tutorial. Now I am able to program my STM32 with uVision Keil environment. Only one thing – it did not worked at first, because Boot jumper remains in positin '1', and it should be in position '0'. Out there are many people like me, who had no or little experience with this board.

  13. Hi Eddie. Thanks for this tutorial. One of the best I could find! I read on about drivers for SPI, I2C and USART. Will you offer these tutorials also? Looking forward to them.   I am looking for someone who can help with Lwip drivers, any advice? Kind regards from UK. Wim

Leave a Reply

Your email address will not be published. Required fields are marked *