Thursday, May 30, 2013

Working with STM32F4 Discovery and CooCox

Working with STM32F4 Discovery and CooCox

 So this is the beginning of opening the package to the STM32F4 Discovery board and working with the CooCox software.  To load the IDE go to the following link, download the CoIDE and follow the instructions for linking to the GCC compiler and downloading the necessary drivers.

http://www.coocox.org/Index.html
  
Once you have loaded all of the necessary components you can start your first project.  I will try to include as many screen shots as possible to make it easier to follow.

Creating the 1st Project

Open CooCox CoIDE and in the start-up screen click on "Create a New Project".  I'm going to name the first project "Blog_Example_1".  Going forward I will have new project names for each Blog, but the existing code will be carried over.  My intent is to keep adding on and modifying to create one large working project in the end.

 


The next screen will appear and select "Chip" and press the Next button.  Finally select the microprocessor you are working with.  In my case I am using the discovery board so I will select the STM32F407VG chip.  Then click the finish button.



Once we hit the finish button, a new tab pops up labeled "Repository".  This is where you can select the files for the available peripherals.  This will automatically bring in the source files and header files for the peripherals you select.  To start with, we are going to do a simple LED control.  To do this we need to use the GPIO Peripheral, so check the box next to GPIO.  When you do this there will be other boxes that are automatically checked as well.  These include the RCC,  CMSIS BOOT for STM32F4xx series and CMSIS core for Cortex M4. 



Once you have selected GPIO, you will see the files that have been loaded under your Project tab.  The actually copies the files and folders in your Project folder.  Once this is complete, you can either close the "Repository" tab or double click on the main.c file under the Project tab.  We are ready to start the code on our first project.







Writing the Code

I will definitely not take credit for writing the following application, like most people I had to do some "Google" research and study the STM32F4 guides to initially start my first project.  They also have some sample projects in CoIDE which help do simple tasks using the peripherals.

In this project, I just intend on enabling the GPIO, reading the push-button input (User button on Discovery board) and toggling the blue LED on the Discovery board.  The push-button is tied to PortA Pin0 and the blue LED is tied to PortD Pin15.  In my code I will enable all of the LED's because I do a quick blink at the beginning of the code.  These are tied to PortD Pin12, Pin13 and Pin14.

Note:  If you are looking STM32F4 Standard Peripheral Library, I had a difficult time finding it online.  I ended up finding this under my IAR Embedded Workbench folder.  If someone has a link to the library please provide this.  I was expecting a .pdf file but it is actually a .chm file.

I am going to include the complete code at the bottom and explain each piece of the code in order.
  1. We need to include the necessary header files.
  2. Write a short delay function to blink the LED's on initial start-up.
  3. We need to create a label for the GPIO structure definition.  This will be used to define the necessary parameters to configure the GPIO. 
  4. We need to setup the GPIOD module
    • We need to enable the peripheral clock to the GPIOD module.  This is typically required for most peripherals available on microprocessor.  It will typically state the specific clock tied to the peripheral in the peripheral source file or you can find this in the Standard Peripheral Library.
    • We need to define the properties of the GPIOD module.  Down in the code I explain in more detail the different properties and the options. 
  5. We need to setup the GPIOA module
    • We need to enable the peripheral clock to the GPIOA module.
    •  We need to define the properties of the GPIOD module.
  6. Write a short piece of code to blink all four LED's and then start the main loop.
  7. In the main loop read the push-button input, if held for a period of time toggle the blue LED.


In my next post I will configure a timer to blink the green LED..........

Code