This repository has been archived by the owner on Nov 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectGenerator_Rust.sh
302 lines (270 loc) · 8.31 KB
/
ProjectGenerator_Rust.sh
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
PRJ_N="stm32f10x"
echo "Project name (example: stm32f10x):"
read PRJ_N
Series_N="f1"
echo "Series MCU name (example: f1):"
read Series_N
NameMCU_N="07"
echo "Model MCU (example: 03):"
read NameMCU_N
Prog_CFG="interface/stlink-v2.cfg"
echo "Config File Prog (example: interface/stlink-v2.cfg):"
read Prog_CFG
MCU_CFG="target/stm32f1x.cfg"
echo "Config File MCU (example: target/stm32f1x.cfg):"
read MCU_CFG
MCU_SVD="STM32F103xx.svd"
echo "This file must be taken on the MCU manufacturers website and placed in the same directory as the script"
echo "Name SVD File (example: STM32F103xx.svd):"
read MCU_SVD
#создать новый проект и иерархию папок
cargo new $PRJ_N
mkdir ./$PRJ_N/.vscode/
mkdir ./$PRJ_N/.cargo/
cp ./$MCU_SVD ./$PRJ_N/.vscode/
cd ./$PRJ_N/
#установим целевую платформу для компилятора
rustup target add thumbv7m-none-eabi
#создаем файл конфигураций для обозначили цель компиляции по умолчанию
cat > ./.cargo/config << EOF
[target.thumbv7m-none-eabi]
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
rustflags = ["-C", "link-arg=-Tlink.x"]
[build]
target = "thumbv7m-none-eabi" # Cortex-M3
EOF
#файл с указанием разметки памяти нашего контроллера
cat > ./memory.x << EOF
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
RAM : ORIGIN = 0x20000000, LENGTH = 20K
}
EOF
echo "Do not forget to indicate the memory layout of your MCU in memory.x"
#подключим необходимые библиотеки в файле Cargo.toml
cat >> Cargo.toml << EOF
# Зависимости для разработки под процессор Cortex-M3
cortex-m = "*"
cortex-m-rt = "*"
cortex-m-semihosting = "*"
panic-halt = "*"
nb = "*"
embedded-hal = "*"
# Пакет для разработки под отладочные платы stm32${Series_N}
[dependencies.stm32${Series_N}xx-hal]
version = "*"
features = ["stm32${Series_N}${NameMCU_N}", "rt"]
# Позволяет использовать "cargo fix"!
[[bin]]
name = "$PRJ_N"
test = false
bench = false
# Включение оптимизации кода
[profile.release]
codegen-units = 1 # Лучшая оптимизация
debug = true # Нормальные символы, не увеличивающие размер на Flash памяти
#lto = true # Лучшая оптимизация
EOF
#создаем файлы для системы сборки
cat > ./.vscode/launch.json << EOF
{
"version": "0.2.0",
"configurations": [
{
"name": "Rust Cortex Debug",
"type": "cortex-debug",
"request": "launch",
"servertype": "openocd",
"cwd": "\${workspaceFolder}",
"executable": "./target/thumbv7m-none-eabi/release/$PRJ_N",
"svdFile": ".vscode/$MCU_SVD",
"configFiles": [
"$Prog_CFG",
"$MCU_CFG"
],
"preLaunchTask": "RUST: cargo build (release)"
}
]
}
EOF
cat > ./.vscode/tasks.json << EOF
{
"version": "2.0.0",
"tasks": [
{
"label": "CPU: Build, Download and run",
"type": "shell",
"command": "/usr/bin/openocd",
"args": [
"-f",
"$Prog_CFG",
"-f",
"$MCU_CFG",
"-c",
"program ./target/thumbv7m-none-eabi/release/$PRJ_N verify reset exit"
],
"problemMatcher": [],
"dependsOn": "RUST: cargo build (release)"
},
/*{
"label": "CPU: Download and run",
"type": "shell",
"command": "/usr/bin/openocd",
"args": [
"-f",
"$Prog_CFG",
"-f",
"$MCU_CFG",
"-c",
"program ./target/thumbv7m-none-eabi/release/$PRJ_N verify reset exit"
],
"problemMatcher": []
},*/
{
"label": "CPU: Reset and run",
"type": "shell",
"command": "/usr/bin/openocd",
"args": [
"-f",
"$Prog_CFG",
"-f",
"$MCU_CFG",
"-c init",
"-c reset",
"-c exit"
],
"problemMatcher": []
},
/*{
"label": "CPU: Stop",
"type": "shell",
"command": "/usr/bin/openocd",
"args": [
"-f",
"$Prog_CFG",
"-f",
"$MCU_CFG",
"-c init",
"-c halt",
"-c exit"
],
"problemMatcher": []
},
{
"label": "CPU: Run",
"type": "shell",
"command": "/usr/bin/openocd",
"args": [
"-f",
"$Prog_CFG",
"-f",
"$MCU_CFG",
"-c init",
"-c resume",
"-c exit"
],
"problemMatcher": []
},*/
{
"label": "CPU: Build to BIN",
"type": "shell",
"command": "cargo",
"args": [
"objcopy",
"--bin",
"$PRJ_N",
"--target",
"thumbv7m-none-eabi",
"--release",
"$PRJ_N.bin"
],
"presentation": {
"focus": true
},
"problemMatcher": [
"\$rustc"
],
},
{
"type": "shell",
"command": "cargo",
"args": [
"build",
"--release"
],
"problemMatcher": [
"\$rustc"
],
"group": "build",
"label": "RUST: cargo build (release)"
},
{
"type": "cargo",
"subcommand": "clean",
"problemMatcher": [
"\$rustc"
],
"label": "RUST: cargo clean"
},
{
"type": "cargo",
"subcommand": "check",
"problemMatcher": [
"\$rustc"
],
"group": "build",
"label": "RUST: cargo check"
},
{
"type": "shell",
"command": "cargo",
"args": [
"fmt"
],
"problemMatcher": [
"\$rustc"
],
"label": "RUST: Format code"
}
]
}
EOF
#базовый код для проверки проекта, моргание светодиодом
cat > ./src/main.rs << EOF
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use panic_halt as _;
use nb::block;
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32${Series_N}xx_hal::{pac, prelude::*, timer::Timer};
// Определяем входную функцию.
#[entry]
fn main() -> ! {
// Получаем управление над аппаратными средствами
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
// Конфигурируем пин c13 как двухтактный выход.
// Регистр "crh" передаётся в функцию для настройки порта.
// Для пинов 0-7, необходимо передавать регистр "crl".
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
// Конфигурируем системный таймер на запуск обновления каждую секунду.
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());
// Ждём пока таймер запустит обновление
// и изменит состояние светодиода.
loop {
block!(timer.wait()).unwrap();
led.set_high().unwrap();
block!(timer.wait()).unwrap();
led.set_low().unwrap();
}
}
EOF
echo "Good luck ^_^"