Setting up Eclipse with STM32 Discovery Boards + BlinkLED

Setting up Eclipse with STM32 Discovery Boards + BlinkLED

Tutorial

This tutorial shows you how to set up your STM32 Discovery Board in a free Eclipse programming environment. The write-up is partially based on a Tutorial for STM32F4-boards and adapted it for the STM32L152 board. I am setting this up on Windows 8 and the board that I will be using is the STM32L-Discovery board.

Where to Buy

The STM32 boards can be purchased at Amazon:

  • Amazon US Link






    Advertisement


    Installing Prerequisite Software

    1. First download and install the Java JRE (Java Runtime Environment) which can be downloaded here: http://www.java.com/en/download/index.jsp . Jave is needed for running Eclipse.
    2. The next step is to download the Eclipse Helios IDE for C and C++, which can be downloaded at: http://www.eclipse.org/downloads/packages/release/helios/sr1 . First select “Eclipse IDE for C/C++ Developers” in the middle column and on the next page select your Windows version (in my case Windows x64) from the “Download Links”. The download is a .zip file, which I extracted to “C:\Eclipse\” . The Eclipse Software is now installed and you can start it via eclipse.exe
    3. When you start eclipse.exe you will be asked for a workspace, which will contain all of you projects. I selected the folder “C:\stm32-discovery-projects\workspace”. The “C:\stm32-discovery-projects” will be called the project folder throughout the tutorial.

    4. This step will have you download the Software and drivers that are specific to the STM32L Discovery Board. If you have a STM32F or similar you should search the website for drivers for your device. Go to http://www.st.com/web/en/catalog/tools/PF257908# and under “Sample and Buy” click the red download button next to STSW-STM32072. Download and extract and install the STM32L1 Discovery Firmware Pack. Make note of the installation folder where you download it.

      1. Navigate to the installation folder (my default: C:\Program Files (x86)\STMicroelectronics\STM32L1_Discovery_Firmware_Pack_V1.0.3) and copy the three folders in the “Libraries” Directory to C:\stm32-discovery-projects\ The three folders to be copied are:
        • CMSIS
        • STM32L1xx_StdPeriph_Driver
        • STMTouch_Driver (may not be needed?)
      2. Go to http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1734/PF257913# and download the STM32L1xx Standard Peripherals Library, which contains 80 code examples on the use of STM32L1 evaluation boards. We will need a folder that contains drivers specific to the evaluation board. Download the STSW-STM32072 file at the bottom of the page and unzip it.
      3. From the Utilities folder of the unzipped file copy STM32_EVAL into your project directory.
      4. Your project folder should look like this now:

    5. Download and install Code Sourcery Lite ARM GNU Tool chain, which is an embedded C/C++ development environment on ARM and other architectures. We will be using the IA32 Windows installer from this link.
      1. Install the file with the default options by clicking next. Select the “Typical” installation at this dialog.

      2. Navigate to the installation directory for CodeSourcey (my default “C:\Program Files (x86)\CodeSourcery\Sourcery G++ Lite\bin”) and copy “cs-rm.exe” and paste it in the same folder. You will get a file called “cs-rm – Copy.exe”. Rename it to “rm.exe”
      3. Note: You may need to restart Windows after this step to update the PATH variable. Press [Windows Key] + [r] and type cmd to bring up a command prompt. Type in path and press [enter] to view the PATH variable. The CodeSourcery directory should be in there after installation. If it is not, try rebooting your computer.
    6. Now start eclipse with eclipse.exe and you will see the following screen:

      1. Select Help->Install New Software… and then click the blue link “Available Software Sites” and then type “cdt” and click the checkmark to get:

      2. Click “OK” and the “Work with:” should have changed to :http://download.eclipse.org/tools/cdt/releases/helios. If it did not, enter the link manually.
      3. Click the arrow by “CDT Optional Features” and select the check-mark next to “C/C++ Cross Compiler Support” to get:

      4. Click next and the download should begin. After the installation click next and then check “I accept the license agreement” and then Finish, which should start the installation. You will be prompted to restart Eclipse, so select Restart Now.

    Creating a Template File

    1. The template file will contain all the necessary information and can be re-used for each of the STM32 based projects. In Eclipse, select File->New->C++ Project.
      1. In the “C++ Project”-window enter “Template” for the project name and select “Cross-Compile Project” and the Cross GCC. Your dialog should look like:

      2. Press next and enter the Command Prefix arm-none-eabi- exactly as it appears and point the command path to the CodeSourcery \bin directory as shown in the following dialog:

      3. Select Finish and you will be returned to the Eclipse window after some installation. Press the “Workbench”-button to go to the workbench:


        You will then be taken to the Eclipse C++ development environment:

      4. Right-click on the Template folder in the “Project Explorer” and select Properties.
      5. The “Properties for Template”-window should now open. In the sidebar expand C/C++ Build and select Tool Chain Editor to get the following dialog:

        Press the Select Tools… button to edit the tools that will be used for compile and link the projects.
      6. In the “Select Tools” window select GCC Assembler on the left and click Add Tool–> and the press OK

        [tut14-toolchain_edit2.png]
      7. You must also select the proper builder in the “ToolChain Editor” window. Select CDT Internal Builder from the drop down menu (red box in image above)
      8. Now we need to change the properties of the GCC Assembler that has been added. You should now be in the “Properties for Template” window. In the sidebar select Settings under C/C++ Build and scroll down until you select GCC Assembler the dialog should look something like that:

      9. Change the Command entry to arm-none-eabi-as (see red box in above image). This file corresponds to a file in the CodeSourcery /bin/ directory.
      10. Click on General under the GCC Assembler and add the following flag: -mthumb -mcpu=cortex-m3

      11. In the same way, select GCC Assembler->Miscallaneous add the flag: -c -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd instead of the default -c -fmessage-length=0
      12. Then select Cross G++ Compiler->Miscallaneous add the flag: -c -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd instead of -c -fmessage-length=0
      13. Now select Cross G++ Linker->Miscallaneous add the flag: -T “${ProjDirPath}\startup_src\stm32_flash.ld” -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd -Wl,-Map=linker.map -Wl,-cref -Wl,–gc-sections
      14. For a line-by-line breakdown of each entry, take a look at the page from the following tutorial Press OK to close the Template properties.
    2. Create two folders in your Template project. Right click in the Project Explorer, select New->Folder and create two folders, src for your porgamming files and startup_src for initialization files related to the board.

      1. Copy the file startup_stm32l1xx_md.s from C:\stm32-discovery-projects\CMSIS\Device\ST\STM32L1xx\Source\Templates\TrueSTUDIO to the startup_src folder. You can do this by dragging and dropping it into the folder in eclipse. You should also rename the file to startup_stm32l1xx_md.S (note: capital S), because of potential problems with eclipse file extensions.
      2. Now copy the file stm32_flash.ld from the C:\Program Files (x86)\STMicroelectronics\STM32L1_Discovery_Firmware_Pack_V1.0.3\Projects\AN3964-Temperature_sensor\TrueSTUDIO\STM32L152RB(STM32L-Discovery) folder into the startup_src directory.
    3. In this step you will set up the src folder for the libraries.
      1. Copy the file lcd_log_conf_template.h from the project folder STM32_EVAL/Common/ into the src folder and rename it to lcd_log_conf.h.
        1. Edit the file lcd_log_conf.h line 38 to say: #include “stm32l152_eval_lcd.h
      2. Navigate to the folder: C:\Program Files (x86)\STMicroelectronics\STM32L1_Discovery_Firmware_Pack_V1.0.3\Projects\AN3964-Temperature_sensor\inc and copy stm32l1xx_conf.h and stm32l1xx_it.h into the src folder.
      3. Navigate to the folder: C:\Program Files (x86)\STMicroelectronics\STM32L1_Discovery_Firmware_Pack_V1.0.3\Projects\AN3964-Temperature_sensor\src and copy system_stm32l1xx.c and stm32l1xx_it.c into the src folder.
      4. Download the blink LED example main.c file from this link and place it into the src directory.
      5. Now your src folder should contain these files:

    4. In this step you will add the libraries and source files to link and compile the libraries. Right click on Template, select Properties. Then go to C/C++ General->Path and Symbols in the sidebar. Select the Source Location tab.
      1. Click Add Folder and add the src and the startup_src directories. It should look like:

      2. Select Link folder and make sure to check the “Link to folder in file system”.

        Note: that I have been told that there are potential issues with errors at this step. If you get a The folder ‘src’ already exists-error, then change the name in Folder name to something else, as shown in the above image. Select Browse and navigate to the following folders:
      • C:\stm32-discovery-projects\CMSIS\Include
      • C:\stm32-discovery-projects\STM32L1xx_StdPeriph_Driver\src
      • C:\stm32-discovery-projects\STM32_EVAL\STM32L152_EVAL
      1. Select the Includes-tab and GNU C. Click the Add… button and check the “Is a workspace path” and add the following path:

        The ${ProjName} variable will change its value so that you can easily copy the template elsewhere.
      2. Now also add the following folders (not all of them may be necessary?):
      • C:/stm32-discovery-projects/CMSIS/Device/ST/STM32L1xx/Include
      • C:/stm32-discovery-projects/CMSIS/Include
      • C:/stm32-discovery-projects/STM32L1xx_StdPeriph_Driver/inc
      • C:/stm32-discovery-projects/STM32_EVAL/STM32L152_EVAL
      • C:/stm32-discovery-projects/STM32_EVAL/Common
      1. Your include tab should look like:

    5. Add some Environment Variable next. In the “Template Property”-window go to C/C++ Build->Settings->Cross GCC Compiler->Symbols and add the following defined variables by clicking Add next to the defined variables:
      • USE_STM32L152_EVAL
      • USE_DEFAULT_CALLBACK
      • USE_STDPERIPH_DRIVER
    6. In the “Template Property”-window go to C/C++ Build->Settings->Build Steps and add the following commands in the Post-build steps Command exactly as it appears arm-none-eabi-objcopy -S -O binary “${ProjName}” “${ProjName}.bin”
      This command should create a binary file, which can be uploaded via the STLink Utility.
      1. Close the “Template Property” window by pressing OK. When prompted click Yes to rebuild.
    7. In the next steps you will have to edit the stm32l152_eval.h file from the stm32-discovery-projects\STM32_EVAL\STM32L152_EVAL directory to select the proper LED pins. In this tutorial the pins will be for the STM32L152 board. You can find out the proper pins from the manual (page 20).
    8. Open stm32l152_eval.h and scroll down to line 121, which starts with #define LEDn 4. The STM32 board have 4 pin ports which are designated A,B,C,D and each port has 15 pins or so. The user LEDs 3 and 4 on the STM32L152 are connected to PB7 (port B, pin 7) and PB6 (port B, pin 6), respectively.
      1. Change #define LED3_PIN to say GPIO_Pin_7
      2. Change #define LED3_GPIO_PORT to say GPIOB
      3. Change #define LED3_GPIO_CLK to say ‘RCC_AHBPeriph_GPIOB
      4. Do the same thing for the LED4_ entries, except change the pin to GPIO_Pin_6
      5. Your stm32l152_eval.h LED entries should look like:

      6. Note: The used-buttons and any other definitions in stm32l152_eval.h may not be properly set up for your board. The manual will tell you which pins, the buttons and other peripherals are connected to. If the pin defines are wrong, your program may not work.

    Build and Upload

    1. Download the STLink Utility from http://www.st.com/web/en/catalog/tools/PF258168
      Install it with the default settings. This program will be used to communicate with the board over the USB cable.
    2. From the Eclipse main menubar click Run->External Tools->External Tools Configurations
      1. Select Program from the sidebar and click the New button. In the next dialog click Browse to navigate to the STM32STLinkUtility.exe on your computer. Give the entry a meaningful name (like “STLink Utility”) and then click Apply

      2. Click on the Build tab and deselect Build before launch to stop the project being rebuilt every time STLink utility is launched
      3. You can test if it was properly linked by clicking Run, which should launch STLink Utility. Otherwise click Close
    3. Right click the Template folder and click Build Project. If the build was successful, the Console output should look something like:

      STM32L152_EVAL\stm32l152_eval_glass_lcd.o
      STM32L152_EVAL\stm32l152_eval.o
      arm-none-eabi-objcopy -S -O binary Template Template.bin
      Build complete for project Template
    4. Build errors could fall into three categories:
      1. Problems with the executable paths: these problems stem from incorrectly entered compile/build options or if there are problems with reaching the .exe files in the CodeSourcery directory.
      2. Problems with include/source paths: The console output will usually tell you which ‘.h file cannot be reached and which should be added in the include directories. A little bit of tinkering with the folders can fix these errors.
      3. Problems with Functions not Found: these problems could stem from the .c files not being included as source. Or from incorrectly defined definitions in the Cross GCC Compiler->Symbols area.
    5. Once the files have been correctly built, run the STLink Utility from Run->StLink Utility

    6. The following STLink Interface will open:

    7. Connect your STMDiscovery board through via the USB cable.
    8. Click File->Open File…” and navigate to the workspace/Template/Debug folder and open Template.bin. If the file is not there, then the project was not built correctly, or there is a problem set the Post-build steps Command”’.
    9. Once the file is loaded and with your device connected over the USB, click the Program verify button:

    10. In the next dialog click the start button to start the upload. Note: Any previous programs on the board will be overwritten!

    11. Your blue (PB6) and green (PB7) LEDs should now start blinking. First the green one turns on, then the blue and green, then both off.
    12. If you want to upload the demo program that came with the original board (to verify that your board works properly), select the STM32L-Discovery.hex file from C:\Program Files (x86)\STMicroelectronics\STM32L1_Discovery_Firmware_Pack_V1.0.3\Projects\AN3413-Current_consumption_touch_sensing\Binary when uploading in ST Link. This will verify if you are using the ST Link Software correctly and can help you with troubleshooting.

    Conclusions

    This tutorial showed you how to install and setup an Eclipse environment with your STM32F1xx board and how to setup and create a blink LED program. If you find any mistakes or have feedback, please let me know at edisondev[dot]net[at]gmail[dot]com.

    Potential Problems

    • Internal Builder: Cannot run program “arm-none-eabi-gcc” (in directory “C:\stm32-discovery-projects\workspace\Template\Debug”): Create Process error=2, The system cannot find the file specified

    Solution: Under Properties->Environmnet check your path variables that it points to the proper CodeSourcery folder






    Advertisement


Leave a Reply

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