Embedded Systems Programming Lesson 4: Blinking the LED

welcome to the embedded systems programming course my name is Miro Samek and in this lesson I'm going to show you how to blink the LED on the stellaris launchpad board for this lesson I highly recommend that you download the user manual of the board from the link I providing the class notes for this video if you don't have the board you can still follow along in the simulator but your debugger views will be slightly different and of course you won't see the LED blink the manual describes how to connect the board to your computer via the provided USB cable for today you are mostly interested in the very nice red green blue user LED located at the right edge of the board the manual explains how the components on the board are connected to the microcontroller among others you can see that the user LED is connected to GPIO which means general purpose input output at the end of the manual you can find the schematics of the board on the first page of the schematics you can see that the are G and P components of the user LED are powered by transistors which are controlled by outputs LED are led G and LED B respectively you can see these outputs again higher up in the schematics where they connect to the microcontroller pins labeled pin f1 pin f2 and pin f3 the letter F in this case stands for GPIO F so now that you have some idea how the LED is connected let's prepare the IR project for this lesson as usual let's start with making a copy of the previous lesson 3 project and renaming it to lesson 4 if you don't have the lesson 3 project you can get it online from state-machine.

com/quickstart get inside the new lesson 4 directory and double-click on the workspace file to open the iar toolset if you don't have the iar toolset go back to lesson 0 if you have the launchpad board you should connect it to your pc now and make sure that the debugger is configured for the TI stellaris interface and that the download tab has they used flash loader option checked if you don't have the board configure the debugger for simulator and follow along also if you are using the board please click on the TI stellaris menu and check the reset will do system reset option so that the board will always start from clean reset finally please rebuilt the project entirely to prevent the IAR from pulling in the main dot C file from the previous lesson three project now let's go to the debugger and review quickly how the processor uses various addresses first at the low addresses starting with 0 you can see the machine instructions this is the compiled code of your program which is stored permanently inside the microcontroller this means that the low addresses starting from 0 are mapped to the flash memory you also already know that the addresses starting from hex – I used for variables such as the counter variable that means that the Android hex – followed by all zeros marks the start of the random access memory RAM indeed you can see a hole in the address right before the beginning of RAM which simply means that the debugger could not read anything from these addresses most likely because they are unused when you probe further at the address hex – 0 0 0 8 0 0 0 you can see that the RAM ends so it is an island extending for hex eight zero zero zero addresses which is 32 kilobyte in decimal this means that the microcontroller has 32 kilobytes of RAM this is as much as you know about the addresses at this point however in order to blink the LED which is your goal in this lesson you need to learn more ideally you need to know their map of all the various continents and islands such as the RAM island in the address space the document that describes the memory map of your microcontroller and much more in excruciating detail is called the datasheet I highly recommend that you download the datasheet for the specific lm4f microcontroller on your launchpad board from the link I provide in the class notes but I need to warn you right away the data sheets tend to be huge this particular one is over 1, 200 pages long is still relatively short as the data sheets go luckily these documents are not intended to be read cover to cover in fact a large part of being an embedded systems engineer consists of knowing how to find your way around the data sheet so that you can find the information you need quickly I hope that you pick up these scales gradually as you participate in this course so for example to find the memory map of your microcontroller simply search the datasheet for this drink memory map so here it is the memory map of a typical modern ARM Cortex and micro controller this is a very nice and simple linear address space without any segment or memory banks if you work with other microcontrollers especially the older 8 bitters I hope you appreciate the simplicity of a linear 32-bit address space I hope you recognize the first island on this map called on-chip flash from address 0 to 3 ffff which corresponds to 256 kilobytes of flash memory you should also recognize the familiar Ram Island starting at 2 hex followed by all zeros which is named here bit banded on chip SRAM whereas SRAM stands for static Ram in the peripherals continent you should note the GPIO ports these islands are interesting because you are looking for GPIO port F to control your LED keep looking further down the memory map Hey here is GPIO poor deaf that you've been looking for copy the starting address to the clipboard and go back to the IAR debugger paste the starting address of gpiof to the memory view and watch what comes up whoops this address range of gpiof advertised in the datasheet appears empty if this happens to you down despair the typical reason is that the hardware block is switched off by default to save power the technique of blocking the clock signal to certain parts of the chip called clock gating is a very common practice in modern microcontrollers so first you need to discover how to turn the GPIO F block on which means you need to go back to the datasheet go back to the beginning of the document and search for the string clock gating oh here is something interesting let's go to that page let's search for the string gpio in this section oh here it is the gpio clock gating control register let's take a closer look at the register description because this is a very typical format commonly used in datasheet the register is shown as a block of bits always numbered from 0 the type of each bit is indicated whereas our o means read only RW means read write and wo would mean write only the logically related groups of bits are documented below the register block picture starting from the most significant bit for you the most interesting is the description of bit 5 because this bit enables the clock to GPIO port F this confirms that this register is exactly what you've been looking for copy the base address of the register to the clipboard and notice that the additional offset hex six away needs to be added to the base to get the complete register address go back to the debugger and open the additional memory view called symbolic memory to set the bit 5 in the clock gating register while simultaneously watching the gpiof start address in the original memory view paste the clock gating register base address from the datasheet to the symbolic memory view and don't forget to add the 608 offset to it now go to the highlighted clock gating register and edit its value to set bit 5 which as you remember from lesson 1 about counting is 20 hexadecimal hit enter hey the gpiof hardware block wakes up you are getting really close to being able to blink the LED but you are not quite there yet as you should do it in gpio section of the datasheet you still need to configure the gpiof bits 1 2 & 3 which drive red blue and red colors respectively as digital outputs to set the pins direction as output scroll down within the GPIO F address block to the address hex four zero zero two five four zero zero and set the bits one two and three two one which corresponds to one one one zero in binary and E in hex additionally to set the function of the pins as digital output scroll further down within the GPIO F address block to the address four zero zero two five five 1c and again set the bits one two and three two one so finally you can try to control the LED you do this through the gpiof data register located at hex four zero zero two five 3fc scroll up to this register and first just set the bit 1 by writing hex two to the lowest nibble again remembering that the bits are always counted from zero hey it worked the red LED lights up you should give yourself a big pat on the back try to turn the LED off by writing zero cool how about setting bit two by writing hex for Wow the LED turned blue you are rockin how about setting bit three by writing X 8 you got it too the heavy-lifting is really over because coding this all up in C will be a piece of cake all there is to controlling the LED boils down to writing numbers to specific memory addresses and this you already know how to do with pointers specifically you are going to use the pointer hack that I showed you at the end of lesson 3 because this technique allows you to write any number to any memory address you choose clean the code up to leave only the pointer stuff actually you won't even need the separate pointer variable because you can dereference the pointer cost as you will see in a minute in lesson 3 you used pointers to end but arm registers are unsigned so change the pointer type to unsigned int replace the fabricated address from less than 3 with the address of the clock gating system register that you use to turn the GPIO f-block on and close the whole pointer cast in parentheses and notice that this whole thing is a pointer to unsigned int if so then you should be able to dereference this pointer by means of the star operator just as you see in the line below now you can write to the pointer as you recall from the experiment in the debugger you need to set a bit 5 that is hex 20 into this register the value should be unsigned which you indicate by the used suffix get rid of the no longer used P end pointer and check if the compiler likes your code by pressing f7 all right so now you can press on with the next register which is the gpiof pin Direction register where you need to set bits 1 2 & 3 by writing hex e finally the GPIO e configuration requires also setting bits 1 2 & 3 in the digital function register with this done you can turn the red color LED on and off by setting and clearing bit one in the gpiof data register actually if you want to really blink the LED you can turn it on and off just once but you need to keep doing this forever to achieve this you can wrap a while loop around the code for turning the LED on and off the constant one in place of the condition means that the condition is always true so the loop runs forever when you compile this code you get a warning that the return statement downstream from the endless while it is unreachable which is true let's test this code on the launchpad board note that setting the clock gating register wakes up the gpiof block as expected here you can see that the red LED lights up and now it goes dark again the endless loop also seems to be working well if everything looks so good let's run the code at full speed by pressing the Go button what the heck the LED stays on all the time let's break into the code by pressing the break button and single step again this time everything is fine yet when you run at full speed the blinking stops do you know what's the problem here yes you are right the program just runs too fast for a human eye to notice the rapid blinking of the LED we need to slow the program down for this you can use the counting while loop that you learned in Lesson two and look like this wastes a lot of CPU cycles but the delay can be controlled by setting the upper limit in the condition of the while note that you need a delay both after turning the LED on and after turning it off again all right so let's give it a try let's delete the breakpoint and run the program well still seems too fast okay go back and increase the limit by another three orders of magnitude try again and hi that appears to be just right that LED keeps blinking this concludes this lesson about blinking an LED even though it might not seem like much this is a very significant milestone in your embedded career so congratulations in the next lesson you will learn how to improve the blinky program by using the preprocessor and the volatile keyword if you like this channel please subscribe to stay tuned you can also visit state-machine.

com/quickstart for the class notes and project file downloads you.

Leave a Reply