0%

codeblocks与STM32的开发环境配置

软件硬件配置

OpenOCD

  • 这里省略下载编译 opencd 环节.

  • openocd 安装在/usr/local 下

  • /usr/local/bin/openocd

  • 配置文件目录

  • /usr/local/share/openocd/

  • 在当前用户目录下创建一个.openocd目录

  • 添加下面三行到~/.openocd/openocd.cfg

1
2
3
source [find /usr/local/share/openocd/scripts/interface/stlink-v2.cfg]
source [find /usr/local/share/openocd/scripts/target/stm32f4x.cfg]
reset_config srst_only srst_nogate
  • 连接 STM32F4 开发板,运行/usr/local/bin/openocd
  • 正常会出现下面信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

michael@debian:~$ /usr/local/bin/openocd
Open On-Chip Debugger 0.10.0-dev-00146-g332023f (2015-12-05-18:09)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 2000 kHz
adapter_nsrst_delay: 100
none separate
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : clock speed 1800 kHz
Info : STLINK v2 JTAG v24 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 2.881129
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints

1
2
3
4
5
6
~$ git clone https://github.com/texane/stlink.git
~$ cd STLink
~$ ./authorgen.sh
~$ ./configure --prefix=/opt/STLink
~$ make && sudo make install
~$ sudo cp 49-stlinkv2.rules /etc/udev/rules.d/49-stlinkv2.rules
  • 通过 lsusb 命令找出开板板的 ID
1
2
3
$ lsusb
Bus 001 Device 002: ID 0483:3748 STMicroelectronics ST-LINK/V2
Bus 003 Device 010: ID 174c:5106 ASMedia Technology Inc. Transcend StoreJet 25M3
  • 修改/etc/udev/rules.d/49-stlinkv2.rules,注意 ATTRS{idProduct}==“3748”
1
2
3
4
5
6
7
8
9
10
11
12
13

$ cat /etc/udev/rules.d/49-stlinkv2.rules
# stm32 discovery boards, with onboard st/linkv2
# ie, STM32L, STM32F4.
# STM32VL has st/linkv1, which is quite different
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", \
MODE:="0666", \
SYMLINK+="stlinkv2_%n",OWNER="michael"
# If you share your linux system with other users, or just don't like the
# idea of write permission for everybody, you can replace MODE:="0666" with
# OWNER:="yourusername" to create the device owned by you, or with
# GROUP:="somegroupname" and mange access using standard unix groups.

刷新系统规则

1
2
3
4

~$ sudo udevadm control --reload-rules
~$ sudo udevadm trigger

GCC ARM Embedded

  • 下载 GCC 工具链解压到/home/user/Embedded-System/gcc-arm-none-eabi-4_9-2015q3,并且把/home/user/Embedded-System/gcc-arm-none-eabi-4_9-2015q3/bin 添加系统环境变量里面

Codeblocks 配置

  • 安装相应平台的 codeblocks 二进制包.
  • 打开Settings->Editor->Code completion . 设置如下图.
    codecompletion.png

编译器的全局设置

  • 设置 ARM 工具链的路径,根据自己的环境做相应改变,如下图所示.
    compiler_settings.png

  • 工具链头文件设置,如下图所示.
    compiler_settings_inc.png

  • 工具链库文件设置,如下图所示.
    compiler_settings_lib.png

STM32 工程设置

  • 这里是针对 STM32F4 Discovery 开发板,工程设置的一些参数

  • 编译选项设置,如下图所示.

  • 设置基于特定 CPU 的 CFLAGS 参数.-mthumb -mthumb-interwork -mlittle-endian -O2 -Wall -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -DUSE_STDPERIPH_DRIVER -DSTM32F4XX -std=c99

project_options.png

  • 编译宏定义设置,如下图所示.
    project_define.png

  • 链接选项设置,如下图所示.
    project_link.png

  • 工程头文件查找目录,库文件查找目录.

project_inc.png
project_search_link.png

  • 结合 openocd 自动烧写 bin 文件的设置,如下图所示.
    project_shell.png

  • 工程中的自动烧写脚本文件.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#!/usr/bin/expect
spawn telnet localhost 4444
set prompt ">"
set hexf [lindex $argv 0];
interact -o -nobuffer -re $prompt return
send "reset halt\r"
interact -o -nobuffer -re $prompt return
send "flash write_image erase unlock $hexf\r"
interact -o -nobuffer -re $prompt return
send "reset run\r"
interact -o -nobuffer -re $prompt return
send "exit\r"
interact

project_shfile.png

  • 因为 GCC 工具链与 Keil 的链接文件是一样,必须在下面这个 LD 文件上修改,不能使用 Keil 版本的链接文件.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
*****************************************************************************
**
** File : stm32_flash.ld
**
** Abstract : Linker script for STM32F407VG Device with
** 1024KByte FLASH, 192KByte RAM
**
** Set heap size, stack size and stack location according
** to application requirements.
**
** Set memory bank area and size if external memory is used.
**
** Target : STMicroelectronics STM32
**
** Environment : Atollic TrueSTUDIO(R)
**
** Distribution: The file is distributed “as is,” without any warranty
** of any kind.
**
** (c)Copyright Atollic AB.
** You may use this file as-is or modify it according to the needs of your
** project. Distribution of this file (unmodified or modified) is not
** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the
** rights to distribute the assembled, compiled & linked contents of this
** file as part of an application binary file, provided that it is built
** using the Atollic TrueSTUDIO(R) toolchain.
**
*****************************************************************************
*/
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = 0x20020000; /* end of 128K RAM on AHB bus*/
/* Generate a link error if heap and stack don\'t fit into RAM */
_Min_Heap_Size = 0; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K
}
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
_exit = .;
} >FLASH
.ARM.extab :
{ *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM :
{
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH

.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(.fini_array*))
KEEP (*(SORT(.fini_array.*)))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH

/* used by the startup to initialize data */
_sidata = .;

/* Initialized data sections goes into RAM, load LMA copy after code */
.data :
AT ( _sidata )
{
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */

. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >RAM

/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)

. = ALIGN(4);
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = _ebss;
} >RAM

/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(4);
PROVIDE ( end = . );
PROVIDE ( _end = . );
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(4);
} >RAM

/* MEMORY_bank1 section, code must be located here explicitly */
/* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
.memory_b1_text :
{
*(.mb1text) /* .mb1text sections (code) */
*(.mb1text*) /* .mb1text* sections (code) */
*(.mb1rodata) /* read-only data (constants) */
*(.mb1rodata*)
} >MEMORY_B1

/* Remove information from the standard libraries */
/DISCARD/ :
{

libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}

.ARM.attributes 0 :
{ *(.ARM.attributes) }
}

编译下载

  • 完成上述设置,每次编译完成后都会自动 telenet 连接到OpenOCD烧写最新的文件
  • 最终运行的效果如下图
    project_build.png

谢谢支持