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





/* Includes ------------------------------------------------------------------*/
// Must be included if using STM32F4 Discovery board or processor
#include "stm32f4xx.h"

// Must be included if using GPIO (General purpose I/O) peripheral
#include "stm32f4xx_gpio.h"

// Must be included to setup general purpose I/O
// Some peripherals require the setup of RCC (Reset and clock controller)
#include "stm32f4xx_rcc.h"


void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}

/**********************************************************************************
 *
 * The GPIO_InitTypeDef is a structure defined in the stm32f4xx_gpio.h file.
 * This is a simple way to use ST's library to configure the I/O.
 *
 * This is a common setup for the other peripherals as well.  Each peripheral will have
 * different properties which can be found in the header files and source files.
 *
**********************************************************************************/

GPIO_InitTypeDef  GPIO_InitStruct;


int main(void)
{

        unsigned int _btn_count = 0;
    /**********************************************************************************
     *
     * This enables the peripheral clock to the GPIOD module.  This is stated in
     * the beginning of the stm32f4xx.gpio.c source file.
     *
    **********************************************************************************/

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    /**********************************************************************************
     *
     * This block of code defines the properties of the GPIO port.
     * The different options of each item can be found in the stm32f4xx_gpio.h header file
     * Every GPIO configuration should have the following initialized
     *
     * GPIO_InitStruct.GPIO_Pin
     * GPIO_InitStruct.GPIO_Mode
     * GPIO_InitStruct.GPIO_Speed
     * GPIO_InitStruct.GPIO_OType
     * GPIO_InitStruct.GPIO_PuPd
     * GPIO_Init(GPIOx, &GPIO_InitStruct); (x represents port - A, B, C, D, E, F, G, H, I)
     *
    **********************************************************************************/

    //GPIO_InitStruct.GPIO_Pin configures the pins that will be used.
    //In this case we will use the LED's off of the discovery board which are on
    //PortD pins 12, 13, 14 and 15
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12;

    //PIO_InitStruct.GPIO_Mode configures the pin mode the options are as follows
    // GPIO_Mode_IN (Input Mode)
    // GPIO_Mode_OUT (Output Mode)
    // GPIO_Mode_AF (Alternate Function)
    // GPIO_Mode_AN (Analog Mode)
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

    //GPIO_InitStruct.GPIO_Speed configures the clock speed, options are as follows
    // GPIO_Speed_2MHz
    // GPIO_Speed_25MHz
    // GPIO_Speed_50MHz
    // GPIO_Speed_100MHz
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    //GPIO_InitStruct.GPIO_OType configures the pin type, options are as follows
    // GPIO_OType_PP (Push/Pull)
    // GPIO_OType_OD (Open Drain)
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

    //Configures pullup / pulldown resistors on pin, options are as follows
    // GPIO_PuPd_NOPULL (Disables internal pullup and pulldown resistors)
    // GPIO_PuPd_UP (Enables internal pullup resistors)
    // GPIO_PuPd_DOWN (Enables internal pulldown resistors)
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

    //This finally passes all the values to the GPIO_Init function
    //which takes care of setting the corresponding bits.
    GPIO_Init(GPIOD, &GPIO_InitStruct);

    /**********************************************************************************
     *
     * This enables the peripheral clock to the GPIOA module.  This is stated in
     * the beginning of the stm32f4xx.gpio.c source file.
     *
    **********************************************************************************/
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    /**********************************************************************************
     *
     * This block of code defines the properties of the GPIOA port.
     * We are defining Pin 0 as a digital input with a pulldown resistor
     * to detect a high level.  Pin 0 is connected to the 3.3V source
     *
   **********************************************************************************/
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    /**********************************************************************************
     *
     * This block of code blinks all four LED's on initial startup
     *
     * **********************************************************************************/
    GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
    Delay(0xFFFFF);
    GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);

    while(1)
    {

        //Reads the status of the push-button on the Discovery board
        //If button is pressed blue LED is toggled

        if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 1)
        {
            _btn_count++;
                if (_btn_count > 0xFFFF)
                {
                    GPIO_ToggleBits(GPIOD, GPIO_Pin_15);
                    _btn_count = 0;
                }
        }

    } //End of while(1) loop
} //End of main loop








6 comments:

  1. Hi There.
    Many thanks for the post. Helped me get beyond a blank screen in CooCox.

    ReplyDelete
  2. Thanks,
    A real great and simple Quick Start.

    ReplyDelete
  3. Thank you Mike! I needed an example like this to start from the very scratch

    ReplyDelete
  4. Standard library download link

    http://www.st.com/web/en/catalog/tools/PF257901

    Download button is present at the bottom, under "Sample and Buy"

    or

    Google for "STSW-STM32065" which the library part number.

    ReplyDelete