You are on page 1of 51

INDEX

Sl.No. Content Page No.


1. Eclipse and Plug in 2
2. Tool Chain and Basic Setup 4
3. Sample project using Eclipse IDE (without STM32CubeMx) 14
4. STMCubeF2 & STM32CubeMx 24
5. Sample project using Eclipse +STM32CubeMx 30
6. USB CDC Sample project using Eclipse +STM32CubeMx 36

Enclosed file for your Reference :

Reference File name File Path


No.
1. eclipse-cpp-luna-SR2-linux- /home/kmani/ARM_Toolchain/eclipse-cpp-luna-SR2-linux-
gtk-x86_64.tar.gz gtk-x86_64.tar.gz
2. eclipseARM /home/kmani/ARM_Toolchain/eclipseARM
3. workspaceARM /home/kmani/ARM_Toolchain/workspaceARM
4. gcc-arm-none-eabi-5_4- /home/kmani/ARM_Toolchain/gcc-arm-none-eabi-5_4-
2016q3-20160926- 2016q3-20160926-linux.tar.bz2
linux.tar.bz2
5. gcc-arm-none-eabi /home/kmani/ARM_Toolchain/gcc-arm-none-eabi
6. stlink-master /home/kmani/ARM_Toolchain/stlink-master
7. eclipse /home/kmani/ARM_Toolchain/eclipseARM/eclipse
8. bin /home/kmani/ARM_Toolchain/gcc-arm-none-eabi/bin
9. en.stm32cubemx.zip /home/kmani/ARM_Toolchain/en.stm32cubemx.zip
10. en.stm32cubef2.zip /home/kmani/ARM_Toolchain/en.stm32cubef2.zip
11. STM32CubeF2 /home/kmani/ARM_Toolchain/STM32CubeF2
12. STM32CubeMx /home/kmani/ARM_Toolchain/STM32CubeMx
13. CubeMXImporter /home/kmani/ARM_Toolchain/CubeMXImporter
14. SetupSTM32CubeMX- /home/kmani/ARM_Toolchain/STM32CubeMx/
4.19.0.linux SetupSTM32CubeMX-4.19.0.linux
15. STM32Cube /home/kmani/ARM_Toolchain/STM32Cube
16. Repository /home/kmani/ARM_Toolchain/STM32Cube/Repository
17. Cube_projects /home/kmani/ARM_Toolchain/Cube_projects
18. blink_cube /home/kmani/ARM_Toolchain/Cube_projects/blink_cube
19. STM32Cube_FW_F2_V1.4. /home/kmani/ARM_Toolchain/STM32Cube/Repository
0 /STM32Cube_FW_F2_V1.4.0
20. blink_cube_test /home/kmani/ARM_Toolchain/workspaceARM
/blink_cube_test
21. USB_CDC- /home/kmani/ARM_Toolchain/Cube_projects/USB_CDC-
Virtual_COM_Port Virtual_COM_Port
22. USB_CDC_VCP /
home/kmani/ARM_Toolchain/workspaceARM/USB_CDC_V
CP
23. blink_test_open207FG /
home/kmani/ARM_Toolchain/workspaceARM/blink_test_ope
n207FG
24. stlink-master.tar.gz /home/kmani/ARM_Toolchain/stlink-master.tar.gz
1. Eclipse and Plug in:

1.1 Eclipse download

Tested with Eclipse Luna for this tutorial. Should work with newer versions.

Step 1:
Download the Eclipse Package
Link to Download Luna Eclipse: http://www.eclipse.org/downloads/packages/release/Luna/SR2

From above link download : Eclipse IDE for C/C++ Developers (Reference 1)

Step 2:
Create ARM_Toolchain folder
Copy and extract the Eclipse in ARM_Toolchain

kmani@kmani:~$ mkdir ARM_Toolchain


kmani@kmani:~$ cd ARM_Toolchain
kmani@kmani:~/ARM_Toolchain$ cp -f -r /home/kmani/Downloads/eclipse-cpp-luna-SR2-
linux-gtk-x86_64.tar.gz /home/kmani/ARM_Toolchain
kmani@kmani:~/ARM_Toolchain$ ls
eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz
kmani@kmani:~/ARM_Toolchain$ tar -xzf eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz
kmani@kmani:~/ARM_Toolchain$ ls
eclipse eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz

Change the eclipse into eclipseARM (Reference 2)

kmani@kmani:~/ARM_Toolchain$ ls
eclipseARM eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz

Create Workspace Directory (Reference 3)

kmani@kmani:~/ARM_Toolchain$ mkdir workspaceARM


kmani@kmani:~/ARM_Toolchain$ ls
eclipseARM eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz workspaceARM

Open Eclipse /home/kmani/ARM_Toolchain/eclipseARM/eclipse with workspace workspaceARM

1.2 GNU ARM plugin

Help Install New Software

Work with : http://gnuarmeclipse.sourceforge.net/updates


then click Add button
Name : GNU ARM plugin
Location : http://gnuarmeclipse.sourceforge.net/updates

Unselect the GNU ARM C/C++ Freescale Project Templates and then install
1.3 EmbSys Regs View plugin
In Eclipse Neon.2 EmbSys Regs plugin not available so only tested with Eclipse Luna
Help Install New Software

Work with : http://embsysregview.sourceforge.net/update


then click Add button
Name : EmbSys Regs
Location : http://embsysregview.sourceforge.net/update

(Eclipse Versions:
Mars (4.5), Luna (4.4), Kepler (4.3), Juno (4.2, 3.8), Previous to Juno (<=4.1)
Refer Link : https://marketplace.eclipse.org/content/embsysregview )
2.Tool Chain and Basic Setup

2.1.GNU ARM Tool chain:

https://launchpad.net/gcc-arm-embedded

From this link download gcc tool chain


File name : gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2

kmani@kmani:~/ARM_Toolchain$ cp -f -r /home/kmani/Downloads/gcc-arm-none-eabi-5_4-
2016q3-20160926-linux.tar.bz2 /home/kmani/ARM_Toolchain
(Reference 4)
kmani@kmani:~/ARM_Toolchain$ tar xjf gcc-arm-none-eabi-5_4-2016q3-20160926-
linux.tar.bz2
kmani@kmani:~/ARM_Toolchain$ ls
eclipseARM gcc-arm-none-eabi-5_4-2016q3
eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz gcc-arm-none-eabi-5_4-2016q3-20160926-
linux.tar.bz2 workspaceARM
Now go to the diretory ARM_Toolchain and rename the extract file(gcc-arm-none-eabi-
5_4-2016q3-20160926-linux.tar.bz2) as gcc-arm-none-eabi
(Reference 5)

kmani@kmani:~/ARM_Toolchain$ ls
eclipseARM gcc-arm-none-eabi
eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz gcc-arm-none-eabi-5_4-2016q3-20160926-
linux.tar.bz2 workspaceARM
2.1.1. Install downloaded gcc-arm-none-eabi

kmani@kmani:~/ARM_Toolchain/gcc-arm-none-eabi/bin$ sudo apt install gcc-arm-none-eabi


kmani@kmani:~$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2.1.3. Add the toolchain in $PATH variable

kmani@kmani:~/ARM_Toolchain$ cd gcc-arm-none-eabi/bin
kmani@kmani:~/ARM_Toolchain/gcc-arm-none-eabi/bin$ pwd
/home/kmani/ARM_Toolchain/gcc-arm-none-eabi/bin

copy the path location and paste in bashrc

kmani@kmani:~/ARM_Toolchain/gcc-arm-none-eabi/bin$ nano ~/.bashrc

Add following two lines at the end of the file

## ADD YOUR PATH


export PATH=$PATH:/home/kmani/ARM_Toolchain/gcc-arm-none-eabi/bin

(Note : ctr + O to save and then, crtl + X to exit)

2.2 stlink setup for first STM32 flashing:


Download the stlink-master.tar.gz from drop box (Reference 24)
https://www.dropbox.com/s/obnb510wvrtcxrh/stlink-master.tar.gz?dl=0 and paste into the
ARM_Toolchain folder and extract it.
(The Latest one does not seem to build properly so Download from drop box)

kmani@kmani:~/ARM_Toolchain$ sudo apt-get install autoconf


kmani@kmani:~/ARM_Toolchain$ cp -f -r /home/kmani/Downloads/stlink-master.tar.gz
/home/kmani/ARM_Toolchain
kmani@kmani:~/ARM_Toolchain$ tar xzvf stlink-master.tar.gz

kmani@kmani:~/ARM_Toolchain$ ls
eclipseARM gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2
eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz stlink-master
workspaceARM
gcc-arm-none-eabi stlink-master.tar.gz

kmani@kmani:~/ARM_Toolchain$ cd stlink-master/
(Reference 6)
kmani@kmani:~/ARM_Toolchain/stlink-master$ ./autogen.sh
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force
autoreconf: configure.ac: tracing
autoreconf: configure.ac: not using Libtool
autoreconf: running: /usr/bin/autoconf --force
autoreconf: configure.ac: not using Autoheader
autoreconf: running: automake --add-missing --copy --force-missing
autoreconf: Leaving directory `.'
kmani@kmani:~/ARM_Toolchain/stlink-master$ ./configure
kmani@kmani:~/ARM_Toolchain/stlink-master$ make
....
....
gcc -g -O2 -I/usr/include/libusb-1.0 -o st-info src/st_info-st-info.o libstlink.a -lusb-1.0
make[1]: Leaving directory '/home/kmani/ARM_Toolchain/stlink-master'

kmani@kmani:~/ARM_Toolchain/stlink-master$ sudo make install


Making install in .
make[1]: Entering directory '/home/kmani/ARM_Toolchain/stlink-master'
make[2]: Entering directory '/home/kmani/ARM_Toolchain/stlink-master'
/bin/mkdir -p '/usr/local/bin'
/usr/bin/install -c st-flash st-util st-term st-info '/usr/local/bin'
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/home/kmani/ARM_Toolchain/stlink-master'
make[1]: Leaving directory '/home/kmani/ARM_Toolchain/stlink-master'

After Connecting the STM32 Device


kmani@kmani:~/ARM_Toolchain/stlink-master$ st-flash erase
2017-03-16T14:02:02 INFO src/stlink-common.c: Loading device parameters....
2017-03-16T14:02:02 INFO src/stlink-common.c: Device connected is: F2 device, id 0x201f6411
2017-03-16T14:02:02 INFO src/stlink-common.c: SRAM size: 0x20000 bytes (128 KiB), Flash:
0x100000 bytes (1024 KiB) in pages of 131072 bytes
Mass erasing...............

2.3 Flash setup for Eclipse IDE


To Know flash location use which st-flash

kmani@kmani:~/ARM_Toolchain$ which st-flash


/usr/local/bin/st-flash

2.3.1.St-flash Debug

Go to Eclipse -> Run -> External Tools -> External Tools Configuration
Select program and add New launch configuration or double click the program (in left side )

Name : st-flash Debug

In Main Tab:
Location : /usr/local/bin/st-flash
Working Directory:
select varibles as project_loc
then type as ${project_loc}/Debug/
Arguments :
write ${project_name}.bin 0x8000000
then click Apply

(Note: Now the name New configuration change into st-flash Debug )
In Common Tab :

Save as option as Local file


And in Display in favorites menu select the External Tools then click Apply

Dont Run now after Apply just click Close


2.3.2.St-flash Release

Go to Eclipse -> Run -> External Tools -> External Tools Configuration

Select program and add New launch configuration or double click the program (in left side )

Name : st-flash Release

In Main Tab:
Location : /usr/local/bin/st-flash
Working Directory:
select varibles as project_loc
then type as ${project_loc}/Release/
Arguments :
write ${project_name}.bin 0x8000000

then click Apply

(Note: Now the name New configuration change into st-flash Release )
In Common Tab :

Save as option as Local file


And in Display in favorites menu select the External Tools then click Apply

Dont Run now just Close it

Cross check the availability of st-flash Debug and st-flash Release in External tools
Run External Tools
1 St-flash Debug
2 St-flash Release

2.4 Debug Setup for Eclipse IDE:

2.4.1. Install Open OCD and its dependency

kmani@kmani:~/ARM_Toolchain$ sudo apt-get install libusb-dev


kmani@kmani:~/ARM_Toolchain$ sudo apt-get install libusb-1.0-0-dev
kmani@kmani:~/ARM_Toolchain$ sudo apt-get install libftdi-dev
kmani@kmani:~/ARM_Toolchain$ sudo apt-get install libftdi1
kmani@kmani:~/ARM_Toolchain$ sudo apt-get install openocd

To find OpenOCD path:


kmani@kmani:~$ which openocd
/usr/bin/openocd

2.4.2 Setup
Go to Eclipse -> Run -> Debug Configuration

Double click the GDB openOCD Debugging -> select the New_configuration
Name : Debug OpenOCD

In Main Tab

Project : ${project_name}
C/C++ Application: ${project_loc}/Debug/${project_name}.elf
Build Configuration : Use Active

Select : Use workspace settings


then click Apply
(Note: Now the name New configuration change into Debug OpenOCD)
In Debugger Tab
OpenOCD Stup
Select -> Start OpenOCD locally
Executable -> /usr/bin/openocd
GDB port -> 3333
Telnet port ->4444
config options -> -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -f
/usr/share/openocd/scripts/target/stm32f2x_stlink.cfg

Select -> Allocate console for openOCD

GDB Client Setup


Excecutable : /home/kmani/ARM_Toolchain/gcc-arm-none-eabi/bin/arm-none-
eabi-gdb
(Note: for Excecutable -> Browse and select /home/kmani/ARM_Toolchain/gcc-
arm-none-eabi/bin/arm-none-eabi-gdb)

Other options :
(Note : for Other options no need to fill anything )
Commands : set mem inaccessible-by-default off

Dont select the Force thread list update on suspend

Then Click Apply

In Common Tab :

Save as
Select Local file

Display in favorites menu


Select Debug
don't select the Run option

Encoding:
Select Default -inherited (UTF-8)

Standard input and output


Select Allocate console (necessary for input )
Don't select ->File

Select Launch in background


Then Click Apply

(Note: Dontt change startup and source Tab option keep it as default )

Dont Debug now just Close it


Cross check the availability of Debug OpenOCD in Debug
3. Sample project using Eclipse IDE (without STM32CubeMx)

3.1 New Project

Location: /home/kmani/ARM_Toolchain/eclipseARM/eclipse
(Reference 7)
Go to Eclipse - > File ->New -> Project
C/C++ -> C project -> Next -> STM32F2xx C/C++ Project

(Note: in Wizard s: no need to write anything)


(Reference 23)
Project name -> blink_test_open207FG

Next window as default only but unselect the Exclude unused )


Folder option -> default as it is

Select configuration -> Debug and Release


Cross GNU ARM Toolchain

Toolchain Name : GNU Tools for ARM Embedded Processors (arm-none-eabi-gcc)

Tool Chain path : /home/kmani/ARM_Toolchain/gcc-arm-none-eabi/bin


(Reference 8)
(Note: Browse the Tool Chain path and find it /home/kmani/ARM_Toolchain/gcc-arm-
none-eabi/bin)

Then click Finish

3.2. Clean and Run the project

(Note: Dont think about error at starting point processed to Build.


Build option in Debug only not in Release for completed process)

Before Build the Project do the above two settings


Project -> Properties -> C/C++ Build -> Settings -> Cross ARM GNU Create Flash Image
-> General

Set configuration as Debug and change the Output file format as Raw binary from Intel
Hex
Then click Apply
and aslo
Set configuration as Release and change the Output file format as Raw binary from
Intel Hex

Then click Apply and OK


3.3. Flashing :

Connect the Device


Run -> External Tools -> st-flash Debug

Example Console output :

2017-03-17T14:26:17 INFO src/stlink-common.c: Loading device parameters....


2017-03-17T14:26:17 INFO src/stlink-common.c: Device connected is: F2 device, id
0x201f6411
2017-03-17T14:26:17 INFO src/stlink-common.c: SRAM size: 0x20000 bytes (128
KiB), Flash: 0x100000 bytes (1024 KiB) in pages of 131072 bytes
2017-03-17T14:26:17 INFO src/stlink-common.c: Attempting to write 5964 (0x174c)
bytes to stm32 address: 134217728 (0x8000000)
EraseFlash - Sector:0x0 Size:0x4000
2017-03-17T14:26:17
Flash page at addr: 0x08000000 erasedINFO src/stlink-common.c: Finished erasing
1 pages of 16384 (0x4000) bytes
2017-03-17T14:26:17 INFO src/stlink-common.c: Starting Flash write for F2/F4
2017-03-17T14:26:17 INFO src/stlink-common.c: Successfully loaded flash loader
in sram
2017-03-17T14:26:17 INFO src/stlink-common.c: Starting verification of write
complete
2017-03-17T14:26:17 INFO src/stlink-common.c: Flash written and verified! jolly
good!

size: 5964
3.4. Debuging OpenOCD

Connect the Device


And just go stlink-v2.cfg file
kmani@kmani:/usr/share/openocd/scripts/interface$ sudo nano stlink-v2.cfg
Change the line hla_vid_pid 0x0483 0x3748 into hla_vid_pid 0x0483 0x374B

For testing purpose select the toggle Breakpoint by right click in the serial number ,
selecting sl.no 66 and 77 for testing

Now click Debug(like insect symbol) -> Debug OpenOCD


and switch to Debug window

3.4.1 EmbSys Regs in Debug window

From Debug window -> window -> Show view -> Other -> Debug -> EmbSys Registers then
click Ok
In EmbSys Registers Window:

Click Error

Then Architecture Cortex-m3


Vendor STMicro
chip stm32f20x
Now click -> Ok
Set the Windows for your convenient
To Save Perspective
Windows -> Save Perspective as .... ->Debug -> Ok
Do you want to Overwrite -> Yes

To Debug :

Click Resume(F8) button (near to skip all breakpoint button) and/or

To see the output click Step Over (F6) button

After finishing Debug click stop button


Then Switch to Programing window
4. STMCubeF2 & STM32CubeMx:

4.1. STM32CubeF2 Firmware ,STM32CubeMx and CubeMXImporter Download :

To download the software you want to register in the following link


http://www.st.com/content/st_com/en/user-registration.html

Go to:
For STM32 Cube F2 Firmware http://www.st.com/en/embedded-software/stm32cubef2.html
STM32 Cube mx http://www.st.com/en/development-tools/stm32cubemx.html

After download

STM32CubeMx software (File Name: en.stm32cubemx.zip) (Reference 9)


STM32CubeF2 Firmware(File Name : en.stm32cubef2.zip) (Reference 10)

Copy Both file and paste in /home/kmani/ARM_Toolchain/ and extract it

kmani@kmani:~/ARM_Toolchain$ cp -r -f /home/kmani/Downloads/en.stm32cubef2.zip
/home/kmani/ARM_Toolchain
kmani@kmani:~/ARM_Toolchain$ cp -r -f /home/kmani/Downloads/en.stm32cubemx.zip
/home/kmani/ARM_Toolchain

kmani@kmani:~/ARM_Toolchain$ unzip en.stm32cubef2.zip -d


/home/kmani/ARM_Toolchain/STM32CubeF2
(Reference 11)
kmani@kmani:~/ARM_Toolchain$ unzip en.stm32cubemx.zip -d
/home/kmani/ARM_Toolchain/STM32CubeMx
(Reference 12)

CubeMXImporter and its dependency


This tool can be used to import projects generated by STM32CubeMX tool into an Eclipse
project created with the GNU ARM Eclipse plugin

kmani@kmani:~/ARM_Toolchain$ sudo apt-get install python-pip


kmani@kmani:~/ARM_Toolchain$ sudo pip install lxml

kmani@kmani:~/ARM_Toolchain$ git clone https://github.com/cnoviello/CubeMXImporter.git


kmani@kmani:~/ARM_Toolchain$ cd CubeMXImporter/
(Reference 13)
kmani@kmani:~/ARM_Toolchain/CubeMXImporter$ ls
CONTRIBUTORS cubemximporter.py cubemximporter.sublime-build README.md

kmani@kmani:~/ARM_Toolchain/CubeMXImporter$ cd ..

kmani@kmani:~/ARM_Toolchain$ ls
CubeMXImporter en.stm32cubef2.zip gcc-arm-none-eabi-5_4-2016q3-
20160926-linux.tar.bz2 STM32CubeF2
eclipseARM en.stm32cubemx.zip stlink-master
STM32CubeMx
eclipse-cpp-luna-SR2-linux-gtk-x86_64.tar.gz gcc-arm-none-eabi stlink-master.tar.gz
workspaceARM

4.2.1 Installation of STM32 CubeMx

kmani@kmani:~/ARM_Toolchain$ cd STM32CubeMx/
kmani@kmani:~/ARM_Toolchain/STM32CubeMx$ ls
Readme.html SetupSTM32CubeMX-4.19.0.app SetupSTM32CubeMX-4.19.0.exe
SetupSTM32CubeMX-4.19.0.linux
kmani@kmani:~/ARM_Toolchain/STM32CubeMx$ sudo ./SetupSTM32CubeMX-4.19.0.linux
(Reference 14)
STM32 CubeMx Dialog box will appear and follow that
4.2.2. To Open STM32CubeMx:

kmani@kmani:~/ARM_Toolchain/STM32CubeMx$ cd
/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX

kmani@kmani:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$ ls
db help olddb plugins STM32CubeMX Uninstaller

kmani@kmani:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$sudo
./STM32CubeMX

[If needed : To make permanent in Launcher, right click the STM32CubeMx application and lock it]

4.3. Initialization Code generator

4.3.1. Create Repository:

Before Code generator, Create Repository with STM32CubeF2 Firmware

kmani@kmani:~$ cd ARM_Toolchain

kmani@kmani:~/ARM_Toolchain$ mkdir STM32Cube


kmani@kmani:~/ARM_Toolchain$ cd STM32Cube
(Reference 15)
kmani@kmani:~/ARM_Toolchain/STM32Cube$ mkdir Repository
kmani@kmani:~/ARM_Toolchain/STM32Cube$ cd Repository

kmani@kmani:~/ARM_Toolchain/STM32Cube/Repository$ cp -r -f
/home/kmani/ARM_Toolchain/STM32CubeF2/STM32Cube_FW_F2_V1.4.0
/home/kmani/ARM_Toolchain/STM32Cube/Repository/
(Reference 16)
kmani@kmani:~/ARM_Toolchain/STM32Cube/Repository$ cd
STM32Cube_FW_F2_V1.4.0/

kmani@kmani:~/ARM_Toolchain/STM32Cube/Repository/STM32Cube_FW_F2_V1.4.0$ pwd
/home/kmani/ARM_Toolchain/STM32Cube/Repository/STM32Cube_FW_F2_V1.4.0

4.3.2. STM32Cube initialization code generator

kmani@kmani:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$sudo
./STM32CubeMX

File New Project


Now we are going to generate code for Nucleo -F207ZG

so Select Board Selector from top menu

Vendor -> ST Microelectronics , Type of Board -> Nucleo 144 , MCU Series ->
STM32F2

From Board List select -> Type -> Nucleo144


click Ok

Project Settings

Project Settings

Project Name -> blink_cube

Project Location -> /home/kmani/ARM_Toolchain/Cube_projects


(Note: Browse the location and select)
(Reference 17)
Tool chain Folder Location ->
/home/kmani/ARM_Toolchain/Cube_projects/blink_cube
(Note:It will automatically generate )
(Reference 18)
Toolchain/IDE -> SW4STM32
(Note : Select the Generate Under Root)

Linker Setting -> Minimum Heap Size : 0x2000 and Minimum stack size: 0x4000)

Un select Use Default Firmware Location :


/home/kmani/ARM_Toolchain/STM32Cube/Repository/STM32Cube_FW_F2_V1.4.0
(Reference 19)
Click ok

Generate Code:
Project -> Generate Code
Close the Code Generation window

Now open the folder /home/kmani/ARM_Toolchain/Cube_projects/blink_cube and check the


generated coding
5. Sample project using Eclipse +STM32CubeMx

5.1 Create Project

Location: /home/kmani/ARM_Toolchain/eclipseARM/eclipse

Go to Eclipse File -> New -> Project -> C Project


Workspace Location :/home/kmani/ARM_Toolchain/workspaceARM

C Project Window:

Project Name : blink_cube_test

(Note: Select the default location


/home/kmani/ARM_Toolchain/workspaceARM/blink_cube_test)
(Reference 20)
Project Type : Hello World ARM Cortex-M C/C++ Project

Click Next

Target processor settings:


Processor core Cortex-M3
Clock(Hz) 8000000
Flash size (kb) 256
RAM Size (kb) 48
Use system calls as Freestanding
Trace Output as Semihosting DEBUG channel
Select (Default) Check some warnings, Use -Og on Debug, Use newlib nano
Dont select(Default)Check most warnings,Enable-Werror,Use link optimization
Folders Window:

(Most Important) Vendor CMSIS name : stm32f2xx


Remaining are as it is (default)

Select configuration
Select Debug and Release
Cross GNU ARM Toolchain

Toolchain Name : GNU Tools for ARM Embedded Processors (arm-none-eabi-gcc)


Tool Chain path : /home/kmani/ARM_Toolchain/gcc-arm-none-eabi/bin
(Reference 8)
(Note: Browse the Tool Chain path and find it /home/kmani/ARM_Toolchain/gcc-
arm-none-eabi/bin)

click Finish and Project Build All

Open the blink_cube_test and check the main program its look like same as Hello ARM World
exampe program (blink led)
Now close the project by right click and close it

5.2. Import Cube_project into Eclipse


Step 1:
kmani@kmani:~$ cd ARM_Toolchain/CubeMXImporter/
kmani@kmani:~/ARM_Toolchain/CubeMXImporter$ sudo python cubemximporter.py
~/ARM_Toolchain/workspaceARM/blink_cube_test/
~/ARM_Toolchain/Cube_projects/blink_cube/

Step 2:
Give Full Permission to blink_cube_test

kmani@kmani:~/ARM_Toolchain/CubeMXImporter$ sudo chmod -R 777


/home/kmani/ARM_Toolchain/workspaceARM/blink_cube_test

Step 3:
Open the blink_cube_test project
Location: /home/kmani/ARM_Toolchain/eclipseARM/eclipse

Right click the project -> Index -> Rebuild


(If error occur try Project Build All)
Now program get modified
Step 4: Example Code

For testing purpose Change

while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

as

while (1)
{
/* USER CODE END WHILE */
trace_puts("Hello ARM World!");
HAL_GPIO_WritePin(GPIOB, LD2_Pin,GPIO_PIN_SET );
HAL_GPIO_WritePin(GPIOB, LD3_Pin,GPIO_PIN_RESET );
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOB, LD2_Pin,GPIO_PIN_RESET );
HAL_GPIO_WritePin(GPIOB, LD3_Pin,GPIO_PIN_SET );
HAL_Delay(500);
/* USER CODE BEGIN 3 */

and also include the header files


#include "diag/Trace.h"

and include trace line


int main(void)
{
trace_puts("Hello ARM World!");
::::
::::
::::

Step 5: Output file format

Project -> Properties -> C/C++ Build -> Settings -> Cross ARM GNU Create Flash Image ->
General

Set configuration as Debug and change the Output file format as Raw binary from Intel Hex
Then click Apply
and aslo
Set configuration as Release and change the Output file format as Raw binary from Intel Hex
Then click Apply and OK
Step 6: Build the project

Project Buid All

Step 7: Flashing

Run -> External Tools -> st-flash Debug

Step 8: Debug

For Debug ,Refer 3.4 Debuging OpenOCD


6. USB CDC Sample project using Eclipse +STM32CubeMx

6.1.STM32Cube initialization code generator

Step 1:

Open STM32CubeMx:

kmani@kmani:/usr/local/STMicroelectronics/STM32Cube/STM32CubeMX$sudo
./STM32CubeMX

File New Project


Now we are going to generate code for Nucleo -F207ZG

so Select Board Selector from top menu

Vendor -> ST Microelectronics , Type of Board -> Nucleo 144 , MCU Series ->
STM32F2

From Board List select -> Type -> Nucleo144

Then click Ok

Pinout :

Step 1:
Configuration Peripherals RCC Select High Speed Clock (HSE) as Crystal/ceramic
Resonator and disable the Low Speed Clock (LSE)
Step 2:
Configuration Peripherals USB_OTG_FS Mode as Device only and no need to select
Activate_SOF and Activate_VBUS

Step 3:
Configuration MiddleWares USB_Device Class For FS IP Communication Device
Class(Virtual Port Com)
Clock Configuration :

Do you want to run automatic clock issues solver ? Click No

Input Frequency : 8 MHz PLL Source Mux : HSE


PLL_M :8 PLL_N : 240
PLL_P :2 PLL_Q :5
System Clock Mux : PLLCLK AHB_Prescaler :1
APB1 Prescaler :4 APB2 Prescaler :2
Configuration :
Check the configuration or change the Device Description

Project Settings

Project Settings
Project Name -> USB_CDC-Virtual_COM_Port
Project Location -> /home/kmani/ARM_Toolchain/Cube_projects
(Note: Browse the location and select)
(Reference 21)
Tool chain Folder Location ->
/home/kmani/ARM_Toolchain/Cube_projects/USB_CDC-Virtual_COM_Port
(Note:It will automatically generate )
Toolchain/IDE -> SW4STM32
(Note : Select the Generate Under Root)

Linker Setting -> Minimum Heap Size : 0x2000 and Minimum stack size: 0x4000)

Un select Use Default Firmware Location :


/home/kmani/ARM_Toolchain/STM32Cube/Repository/STM32Cube_FW_F2_V1.4.0

Click ok

Generate Code:
Project -> Generate Code
Close the Code Generation window

6.2. Create Project

Location: /home/kmani/ARM_Toolchain/eclipseARM/eclipse
Go to Eclipse File -> New -> Project -> C Project
Workspace Location :/home/kmani/ARM_Toolchain/workspaceARM

C Project Window:
Project Name : USB_CDC_VCP
(Note: Select the default location
/home/kmani/ARM_Toolchain/workspaceARM/USB_CDC_VCP)
(Reference 22)
Project Type : Hello World ARM Cortex-M C/C++ Project
Click Next
Target processor settings:
Processor core Cortex-M3
Clock(Hz) 8000000
Flash size (kb) 256
RAM Size (kb) 48
Use system calls as Freestanding
Trace Output as Semihosting DEBUG channel
Select (Default) Check some warnings, Use -Og on Debug, Use newlib nano
Dont select(Default)Check most warnings,Enable-Werror,Use link optimization

Folders Window:

(Most Important) Vendor CMSIS name : stm32f2xx


Remaining are as it is (default)
Select configuration
Select Debug and Release

Cross GNU ARM Toolchain

Toolchain Name : GNU Tools for ARM Embedded Processors (arm-none-eabi-gcc)


Tool Chain path : /home/kmani/ARM_Toolchain/gcc-arm-none-eabi/bin
(Reference 8)
(Note: Browse the Tool Chain path and find it /home/kmani/ARM_Toolchain/gcc-arm-none-
eabi/bin)
click Finish and Project Build All

Open the USB_CDC_VCP and check the main program its look like same as Hello ARM World
exampe program (blink led)

6.3. Import Cube_project into Eclipse

Step 1:
kmani@kmani:~$ cd ARM_Toolchain/CubeMXImporter/
kmani@kmani:~/ARM_Toolchain/CubeMXImporter$ sudo python
cubemximporter.py ~/ARM_Toolchain/workspaceARM/USB_CDC_VCP/
~/ARM_Toolchain/Cube_projects/USB_CDC-Virtual_COM_Port/

Step 2:
Give Full Permission to blink_cube_test

kmani@kmani:~/ARM_Toolchain/CubeMXImporter$ sudo chmod -R 777


/home/kmani/ARM_Toolchain/workspaceARM/USB_CDC_VCP

6.4 Changes and Build project in Eclipse

Step 1:
Open the USB_CDC_VCP project
Location: /home/kmani/ARM_Toolchain/eclipseARM/eclipse

Right click the project -> Index -> Rebuild


(If error occur try Project Build All)
Now program get modified

Step 2:

Include Header Files

#include "usbd_cdc_if.h"
#include "diag/Trace.h"

#include <stdio.h>
#include <stdlib.h>
Change main.c program and include some variables:
uint8_t receive_data[64]={0};
uint16_t _index=0;
uint8_t cIndex[1]={0};
/*=====================*/

uint16_t HexToAsci(uint8_t cBuff);

typedef union _un_i2cval

{
uint16_t ival;
uint8_t cval[1];

} ichar;

uint16_t cRetVal;
uint16_t cCRCHTOA;
ichar ic1;

uint8_t mystring[]="Hello world 123\n" ;


uint8_t mystring1[]="\n" ;
//uint8_t mystring[]={0};

/* USER CODE END 0 */

int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration----------------------------------------------------------
*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick.
*/
HAL_Init();

/* Configure the system clock */


SystemClock_Config();

/* Initialize all configured peripherals */


MX_GPIO_Init();
MX_USB_DEVICE_Init();

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */


trace_puts("Hello ARM World!");

HAL_GPIO_WritePin(GPIOB, LD2_Pin,GPIO_PIN_SET );
HAL_Delay(10000); // Delay for making index as zero if
this delay not available means the index show 0x0B value
HAL_Delay(10000);
_index=0;
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/*
HAL_GPIO_WritePin(GPIOB, LD2_Pin,GPIO_PIN_SET );
HAL_GPIO_WritePin(GPIOB, LD3_Pin,GPIO_PIN_RESET );
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, LD2_Pin,GPIO_PIN_RESET );
HAL_GPIO_WritePin(GPIOB, LD3_Pin,GPIO_PIN_SET );
HAL_Delay(100);
*/
CDC_Transmit_FS(mystring, strlen((const char*)mystring));

/* For New Line*/


CDC_Transmit_FS(mystring1, strlen((const char*)mystring1));

HAL_GPIO_WritePin(GPIOB, LD3_Pin,GPIO_PIN_RESET );
HAL_Delay(500);

if(receive_data[10]==0x41)
HAL_GPIO_WritePin(GPIOB, LD3_Pin,GPIO_PIN_SET );

CDC_Transmit_FS(receive_data, strlen((const char*)receive_data));


HAL_Delay(500);

CDC_Transmit_FS(mystring1, strlen((const char*)mystring1));


HAL_Delay(500);

cCRCHTOA= HexToAsci(_index);

cIndex[0] = cCRCHTOA;
cIndex[1] = cCRCHTOA>>8;

CDC_Transmit_FS(cIndex, strlen((const char*)cIndex));


HAL_Delay(500);

/* For New Line*/


CDC_Transmit_FS(mystring1, strlen((const char*)mystring1));
HAL_Delay(500);

/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */

At last in the program include the function:

uint16_t HexToAsci(uint8_t cBuff)


{
uint8_t cTemp_Buff1[2];
uint8_t cbuff[2];
if (cBuff <= 9)
{
cTemp_Buff1[0] = 0x30;
cTemp_Buff1[1] = ((cBuff & 0x0F) + 0x30);
}
else
{
// For 0th Buffer
cbuff[0] = ((cBuff>> 4) & 0x0f);

if (((cbuff[0] >= 0x0A) && (cbuff[0] <= 0x0F)))


{
cTemp_Buff1[0] = cbuff[0] + 55;
}
else
{
cTemp_Buff1[0] = cbuff[0] + 0x30;
}

// For 1st Buffer


cbuff[1] = (cBuff & 0x0f);

if (((cbuff[1] >= 0x0A) && (cbuff[1] <= 0x0F)))


{
cTemp_Buff1[1] = cbuff[1] + 55;
}
else
{
cTemp_Buff1[1] = cbuff[1] + 0x30;
}
}
ic1.cval[0] = cTemp_Buff1[0];
ic1.cval[1] = cTemp_Buff1[1];
cRetVal = ic1.ival ;
return(cRetVal);
}

In usbd_cdc_if.c file

Change APP_RX_DATA_SIZE 4 into 64 and also APP_TX_DATA_SIZE 4 into 64 and include two
extra variables

extern uint8_t receive_data[64];


extern uint8_t _index;

#define APP_RX_DATA_SIZE 64
#define APP_TX_DATA_SIZE 64

And Change the CDC_Receive_FS and CDC_Transmit_FS function as

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)


{

receive_data[_index]=Buf[0];
// _index++;
if(++ _index==64)
{
_index=0;
}

USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
return (USBD_OK);
/* USER CODE END 6 */
}

uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)


{
uint8_t result = USBD_OK;
/* USER CODE BEGIN 7 */
memcpy(UserTxBufferFS, Buf, sizeof(uint8_t)*Len);

USBD_CDC_HandleTypeDef *hcdc =
(USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
if (hcdc->TxState != 0){
return USBD_BUSY;
}
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
/* USER CODE END 7 */
return result;
}

Step 3: Output file format

Project -> Properties -> C/C++ Build -> Settings -> Cross ARM GNU Create Flash Image ->
General

Set configuration as Debug and change the Output file format as Raw binary from Intel Hex
Then click Apply
and aslo
Set configuration as Release and change the Output file format as Raw binary from Intel Hex
Then click Apply and OK
Step 4:
Project Build All

Step 5:
Flashing :
Connect the Device
Run -> External Tools -> st-flash Debug

6.5 USB Communication


Step 1:
Install gtkterm
Method 1: kmani@kmani:~$ sudo apt-get install gtkterm
Method 2: By Ubuntu software center search gtkterm and install

Step 2:
kmani@kmani:~$ lsusb
Bus 001 Device 002: ID 8087:8001 Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 004: ID 8087:07dc Intel Corp.
Bus 002 Device 003: ID 5986:0671 Acer, Inc
Bus 002 Device 002: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 002 Device 006: ID 0483:374b STMicroelectronics ST-LINK/V2.1 (Nucleo-F103RB)
Bus 002 Device 005: ID 04ca:0061 Lite-On Technology Corp.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Step 3:
Connect USB Cable to the connector CN13 (Nucleo F207ZG)

Step 4:
kmani@kmani:~$ lsusb
Bus 001 Device 002: ID 8087:8001 Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 004: ID 8087:07dc Intel Corp.
Bus 002 Device 003: ID 5986:0671 Acer, Inc
Bus 002 Device 002: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 002 Device 010: ID 0483:5740 STMicroelectronics STM32F407
Bus 002 Device 008: ID 0483:374b STMicroelectronics ST-LINK/V2.1 (Nucleo-F103RB)
Bus 002 Device 005: ID 04ca:0061 Lite-On Technology Corp.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Step 5:
kmani@kmani:~$ dmesg | grep tty
[ 0.000000] console [tty0] enabled
[ 2148.472809] cdc_acm 2-2:1.2: ttyACM0: USB ACM device
[ 2252.291029] cdc_acm 2-3:1.0: ttyACM1: USB ACM device
[ 3156.788736] cdc_acm 2-3:1.0: ttyACM2: USB ACM device
[ 3349.233343] cdc_acm 2-3:1.0: ttyACM2: USB ACM device
[ 3739.701215] cdc_acm 2-3:1.0: ttyACM2: USB ACM device
[ 3857.222540] cdc_acm 2-2:1.2: ttyACM0: USB ACM device
[ 3876.334099] cdc_acm 2-3:1.0: ttyACM2: USB ACM device

Step 6:
Open gtkterm
Step 7:
Select Port as /dev/ttyACM2
select the proper serial port here the serail port is ttyACM2
Configuration Port

Step 8:
To send data
View Send Hexadecimal data

[Note In bottom side


Hexadecimal data to send : 41 42 43 44 45 ]
It display ABCDE
Because for (HEXA to ASCII ) 41 A, 42 B, 43 C, 44 D, 45 E

Step 9:
To view hexa decimal character
View Hexa Decimal