Skip to content

Commit 6658585

Browse files
feat: new stm32 using cpp
1 parent bd52e93 commit 6658585

17 files changed

Lines changed: 548 additions & 0 deletions

File tree

codes_and_assets/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# =========================
2+
# Clangd
3+
# =========================
4+
.clangd/
5+
compile_commands.json
6+
.cache/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Qt-style clang-format
2+
# Based on Qt coding style with slight modern C++ friendliness
3+
4+
BasedOnStyle: LLVM
5+
6+
Language: Cpp
7+
Standard: c++20
8+
9+
# =========================
10+
# 缩进 & 基础排版
11+
# =========================
12+
IndentWidth: 4
13+
TabWidth: 4
14+
UseTab: Never
15+
16+
ContinuationIndentWidth: 8
17+
IndentCaseLabels: true
18+
IndentCaseBlocks: false
19+
IndentGotoLabels: false
20+
21+
AccessModifierOffset: -4
22+
23+
# =========================
24+
# 大括号风格(Qt 风格)
25+
# =========================
26+
BreakBeforeBraces: Custom
27+
BraceWrapping:
28+
AfterClass: true
29+
AfterStruct: true
30+
AfterUnion: true
31+
AfterEnum: true
32+
AfterFunction: true
33+
AfterNamespace: true
34+
AfterControlStatement: false
35+
BeforeElse: true
36+
BeforeCatch: true
37+
IndentBraces: false
38+
39+
# =========================
40+
# 行宽 & 自动换行
41+
# =========================
42+
ColumnLimit: 100
43+
AllowAllArgumentsOnNextLine: false
44+
AllowAllParametersOfDeclarationOnNextLine: false
45+
BinPackArguments: false
46+
BinPackParameters: false
47+
48+
PenaltyBreakBeforeFirstCallParameter: 1
49+
50+
# =========================
51+
# 指针 & 引用(Qt 习惯)
52+
# =========================
53+
PointerAlignment: Right
54+
ReferenceAlignment: Right
55+
DerivePointerAlignment: false
56+
57+
# =========================
58+
# 空格规则
59+
# =========================
60+
SpaceBeforeParens: ControlStatements
61+
SpaceBeforeAssignmentOperators: true
62+
SpaceAfterCStyleCast: true
63+
SpaceBeforeRangeBasedForLoopColon: true
64+
65+
SpacesInAngles: false
66+
SpacesInParentheses: false
67+
SpacesInSquareBrackets: false
68+
SpacesInCStyleCastParentheses: false
69+
70+
# =========================
71+
# if / for / while 格式
72+
# =========================
73+
AllowShortIfStatementsOnASingleLine: Never
74+
AllowShortLoopsOnASingleLine: false
75+
AllowShortFunctionsOnASingleLine: Inline
76+
77+
78+
# =========================
79+
# include 排序(Qt 项目常用)
80+
# =========================
81+
SortIncludes: true
82+
IncludeBlocks: Regroup
83+
IncludeCategories:
84+
- Regex: '^<Qt.*>'
85+
Priority: 1
86+
- Regex: '^<Q.*>'
87+
Priority: 2
88+
- Regex: '^<.*>'
89+
Priority: 3
90+
- Regex: '^".*"'
91+
Priority: 4
92+
93+
# =========================
94+
# 命名空间
95+
# =========================
96+
NamespaceIndentation: All
97+
CompactNamespaces: false
98+
99+
# =========================
100+
# 注释
101+
# =========================
102+
AlignTrailingComments: true
103+
ReflowComments: true
104+
KeepEmptyLinesAtTheStartOfBlocks: false
105+
106+
# =========================
107+
# 其他 Qt / 工程实践
108+
# =========================
109+
InsertTrailingCommas: Wrapped
110+
Cpp11BracedListStyle: true
111+
FixNamespaceComments: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
6+
compile_commands.json
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"emeraldwalk.runonsave": {
3+
"commands": [
4+
{
5+
"match": "\\.(c|cpp|h|hpp)$",
6+
"cmd": "workbench.action.tasks.runTask Generate compile_commands"
7+
}
8+
]
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Generate compile_commands",
6+
"type": "shell",
7+
"command": "${workspaceFolder}/tools/auto_gen_db.ps1",
8+
"problemMatcher": [],
9+
"group": {
10+
"kind": "none",
11+
"isDefault": false
12+
}
13+
}
14+
]
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the convention is to give header files names that end with `.h'.
29+
30+
Read more about using header files in official GCC documentation:
31+
32+
* Include Syntax
33+
* Include Operation
34+
* Once-Only Headers
35+
* Computed Includes
36+
37+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#pragma once
2+
#include <stm32f1xx_hal.h>
3+
4+
// 枚举端口
5+
enum class PortName
6+
{
7+
A,
8+
B,
9+
C
10+
};
11+
12+
// 编译期函数,返回 GPIO_TypeDef*
13+
14+
enum class Mode
15+
{
16+
Input,
17+
Output
18+
};
19+
enum class Pull
20+
{
21+
None,
22+
Up,
23+
Down
24+
};
25+
26+
template <PortName Port, uint16_t Pin> class GPIO
27+
{
28+
public:
29+
static GPIO_TypeDef *getPort(PortName port)
30+
{
31+
switch (port) {
32+
case PortName::A:
33+
return GPIOA;
34+
case PortName::B:
35+
return GPIOB;
36+
case PortName::C:
37+
return GPIOC;
38+
}
39+
return nullptr; // 不会发生
40+
}
41+
static void init(Mode mode, Pull pull = Pull::None)
42+
{
43+
GPIO_TypeDef *port = getPort(Port);
44+
45+
// 使能端口时钟
46+
if (Port == PortName::A)
47+
__HAL_RCC_GPIOA_CLK_ENABLE();
48+
else if (Port == PortName::B)
49+
__HAL_RCC_GPIOB_CLK_ENABLE();
50+
else if (Port == PortName::C)
51+
__HAL_RCC_GPIOC_CLK_ENABLE();
52+
53+
GPIO_InitTypeDef gpioInit{};
54+
gpioInit.Pin = Pin;
55+
56+
if (mode == Mode::Output) {
57+
gpioInit.Mode = GPIO_MODE_OUTPUT_PP;
58+
gpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
59+
}
60+
else {
61+
gpioInit.Mode = GPIO_MODE_INPUT;
62+
if (pull == Pull::Up)
63+
gpioInit.Pull = GPIO_PULLUP;
64+
else if (pull == Pull::Down)
65+
gpioInit.Pull = GPIO_PULLDOWN;
66+
else
67+
gpioInit.Pull = GPIO_NOPULL;
68+
}
69+
70+
HAL_GPIO_Init(port, &gpioInit);
71+
}
72+
73+
static void set() { getPort(Port)->BSRR = Pin; }
74+
static void reset() { getPort(Port)->BRR = Pin; }
75+
static void toggle()
76+
{
77+
if (read())
78+
reset();
79+
else
80+
set();
81+
}
82+
static bool read() { return (getPort(Port)->IDR & Pin) != 0; }
83+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include <stdint.h>
3+
4+
namespace system_clock {
5+
6+
7+
void system_clock_init();
8+
/**
9+
* @brief delay the global system for given miliseconds
10+
*
11+
* @param delay_ms
12+
*/
13+
void system_delay_ms(uint32_t delay_ms);
14+
15+
/**
16+
* @brief delay the global system for given museconds
17+
*
18+
* @param us
19+
*/
20+
void system_delay_us(uint32_t us);
21+
22+
/**
23+
* @brief provide the current frequency of system clock
24+
*
25+
* @return uint32_t , the raw Systicks Frequency
26+
*/
27+
uint32_t provide_application_sysfreq();
28+
}
29+
30+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into the executable file.
4+
5+
The source code of each library should be placed in a separate directory
6+
("lib/your_library_name/[Code]").
7+
8+
For example, see the structure of the following example libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
Example contents of `src/main.c` using Foo and Bar:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
The PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries by scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

0 commit comments

Comments
 (0)