Electronics - Keil - Creating a project by copying a default example

You are likely playing with your MDK-ARM uVision4 IDE with default examples provided by the ST firmware.

But each time you try to change something you see a message saying that the project is read only.

Therefor in this tutorial we will create our own project by copying an example provided in order to customize it.

All the tutorial will use the STM32F3-DISCOVERY board but it should of course work with other boards.

Settting up the installation

Our workspace will be:

  • H:\dev\mcu\keil

Let's assume that we installed the ST firmware in this directory:

  • H:\dev\mcu\st\st-firmware

Then let's copy the Libraries and Utilities directories in our own workspace.

We will create a __firmware directory to put both Libraries and Utilities.

So copy all files from:

  • H:\dev\mcu\st\st-firmware\Libraries to H:\dev\mcu\keil\__firmware\Libraries
  • H:\dev\mcu\st\st-firmware\Utilities to H:\dev\mcu\keil\__firmware\Utilities

Our project will be created in this folder:

  • H:\dev\mcu\keil\badprog_adc

Then add a directory to badprog_adc called user and copy all files from:

  • H:\dev\mcu\st\st-firmware\Project\Peripheral_Examples\ADC_Example

into this user folder.

We have so:

  • H:\dev\mcu\keil\badprog_adc\user\main.c
  • H:\dev\mcu\keil\badprog_adc\user\main.h
  • H:\dev\mcu\keil\badprog_adc\user\readme.txt
  • H:\dev\mcu\keil\badprog_adc\user\stm32f30x_conf.h
  • H:\dev\mcu\keil\badprog_ad2c\user\stm32f30x_it.c
  • H:\dev\mcu\keil\badprog_adc\user\stm32f30x_it.h
  • H:\dev\mcu\keil\badprog_adc\user\system_stm32f30x.c

As all those files are read only, right click the H:\dev\mcu\keil\badprog_adc\user directory and Properties > in Attributes, disable the Read only checkbox and click Apply > Apply all modification to sub folders.

Now check that each file in the user directory is not read only.
Right click one file and Properties.
Attributes should have Read only disabled.

If all files are like this one, let's continue!

Creating a project

For this first project we are going to copy the ADC example.

Open the IDE then > Project > New uVision Project...

Select the badprog_adc directory and name the file badprog_adc.uvproj and click Save.

A window appears to choose your device. For this tutorial we are using the STM32F3-DISCOVERY board.
And the microcontroller  is a STM32F303VCT6.

So in the list, we select > STMicroelectronics > STM32F303VC > OK.

A new window popups asking if you would like to Copy 'startup_stm32f30x.s' to Project Folder and Add File to Project ?

Click the Yes button.
This file is added automatically and is the same as the one you can find in the following directory:

  • H:\dev\mcu\st\st-firmware\Libraries\CMSIS\Device\ST\STM32F30x\Source\Templates\arm

Now in the Project window, we can see:

Target
            Source Group 1
                           startup_stm32f30x.s

Let's rename Target 1 to myADC and Source Group 1 to MDK-ARM-startup.

So the new arborescence is:

myAdc
            MDK-ARM-startup
                           startup_stm32f30x.s

Adding groups and files

We are now going to create groups and files.

Still in the Projec window, right click on myADC > Add Group...

A group is added with the name New Group.

To change this name, left click it and click on the F2 key on your keyboard.

Let's name it src.

Right click src > Add Files to Group 'src'...

Then select all .c files in:

  • H:\dev\mcu\keil\badprog_adc\user

We have now this arborescence:

myAdc
            MDK-ARM-startup
                           startup_stm32f30x.s
            src
                           main.c
                           stm32f30x_it.c
                           system_stm32f30x.c

Let's add a new group called STM32F3-DISCOVERY with files taken from:

  • H:\dev\mcu\st\st-firmware\Utilities\STM32F3_Discovery

Our tree is:

myAdc
            MDK-ARM-startup
                           startup_stm32f30x.s
            src
                           main.c
                           stm32f30x_it.c
                           system_stm32f30x.c
            STM32F3-Discovery
                           stm32f3_discovery.c

We will add a last directory called STM32F30x_StdPeriph_Driver and files taken from:

  • H:\dev\mcu\st\st-firmware\Libraries\STM32F30x_StdPeriph_Driver\src

We don't need all files, so let's take only ones we will use for our ADC example.

So our arborescence is finally:

myAdc
            MDK-ARM-startup
                           startup_stm32f30x.s
            src
                           main.c
                           stm32f30x_it.c
                           system_stm32f30x.c
            STM32F3-Discovery
                           stm32f3_discovery.c
            STM32F30x_StdPeriph_Driver
                           stm32f30x_adc.c
                           stm32f30x_exti.c
                           stm32f30x_gpio.c
                           stm32f30x_misc.c
                           stm32f30x_rcc.c
                           stm32f30x_syscfg.c

Setting up the options

Right click myADC > Options for Target 'myADC'... > C/C++ > Preprocessor Symbols > Define > write USE_STDPERIPH_DRIVER,STM32F30X,

Still in the options > C/C++:

  • > Language / Code Generation > Optimization > choose Level 3 (-O3)
  • > Language / Code Generation > Warnings > choose All Warnings
  • > Language / Code Generation > enable the One ELF Section per Function checkbox

Still in the options > C/C++, in the Include Paths part, click the button at right with "..." and insert the following paths for includes:

  • ..\__firmware\Utilities\STM32F3_Discovery
  • ..\__firmware\Libraries\CMSIS\Device\ST\STM32F30x\Include
  • ..\__firmware\Libraries\STM32F30x_StdPeriph_Driver\inc
  • .\user

Now still in the options > Linker > enable Use Memory Layout from Target Dialog.

Still in the options > Debug > On the right, select ST-Link Debugger.
Still in the options > Debug > On the right, enabled Run to main().
Still in the options > Debug > On the right, click the Settings button:

  • > Debug > Debug Adapter > Port > select SW
  • > Flash Download > Download Function > enable the Reset and Run checkbox
  • > Flash Download > Programming Algorithm > Click Add > select STM32Fxx Flash 256k

Still in the options > Utilities > Configure Flash Menu Command > Use Target Driver for Flash Programming > select ST-Link Debugger.

Finally click OK.

Compiling, linking, loading, erasing, programming, veryfing, running

We can now build our project > Project > Rebuild all target files.

In the Build Output we should see the following output:

Rebuild target 'myADC'
assembling startup_stm32f30x.s...
compiling main.c...
compiling stm32f30x_it.c...
compiling system_stm32f30x.c...
compiling stm32f3_discovery.c...
compiling stm32f30x_adc.c...
compiling stm32f30x_exti.c...
compiling stm32f30x_misc.c...
compiling stm32f30x_rcc.c...
compiling stm32f30x_syscfg.c...
compiling stm32f30x_gpio.c...
linking...
Program Size: Code=1612 RO-data=424 RW-data=40 ZI-data=1688  
"badprog_adc.axf" - 0 Error(s), 0 Warning(s).

Select now > Flash > Download.

The program is sent to the board!

If all is OK, the following ouput should appear:

Load "H:\\dev\\mcu\\keil\\badprog_adc\\badprog_adc.axf"
Erase Done.
Programming Done.
Verify OK.
Application running ...

Conclusion

You are now able to change what you want on your files, created, and of course save them.
It was not so easy, but well done, you've made it! cool
 

Comments

Comment: 

Very useful..
Thanks a lot!!

Comment: 

I followed all the steps but 10 errors appeared of core cm4.h not included

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.