Skip to content

Commit 2dfc489

Browse files
committed
update docs, add info about cross-compilation and SPL splitting
1 parent 115adbc commit 2dfc489

6 files changed

Lines changed: 248 additions & 30 deletions

File tree

docs/developer/coding-style.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Coding style
2+
3+
## Development workflow
4+
5+
I recently read this great article about [a successful branching
6+
model](https://nvie.com/posts/a-successful-git-branching-model/) for git and
7+
will implement it. Highly recommended!
8+
9+
10+
## Changelog
11+
12+
Every feature branch (or pull request) should include an update for
13+
Changelog.md
14+
15+
16+
## Code format
17+
18+
When porting an existing Arduino library I try to preserve the original
19+
formatting as much as possible to allow for easy comparison to the original
20+
state.
21+
22+
There is a development of a C++ capable compiler environment going on. There
23+
is no public website for that project yet, but I am in contact with the
24+
developer. He is even targeting Sduino for his project and at one point in
25+
the (still distant) future we might be able to switch back to the orginal
26+
library code again. Preserved code structure would be a big plus then. (But
27+
don't hold your breath yet)
28+
29+
Newly written code is mostly formatted using indent according to the linux
30+
kernel coding style, using Tabs, Tab width 8, line length 80:
31+
32+
```bash
33+
indent -linux
34+
```
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Cross-Compile for OSX
2+
3+
Compiling the tools is really easy. Only setting up the cross compilation
4+
environment requires a little more effort. But luckily, the
5+
[osxcross](https://github.com/tpoechtrager/osxcross) project already took
6+
care of this.
7+
8+
9+
## Setting up the cross compilation environment on Linux Mint 19
10+
11+
### Install the needed dependencies
12+
13+
I needed only these (the others were already present):
14+
15+
sudo apt install clang llvm-dev uuid-dev libssl-dev libbz2-dev
16+
17+
Full list of all dependencies:
18+
19+
sudo apt install clang llvm-dev libxml2-dev uuid-dev libssl-dev bash patch make tar xz-utils bzip2 gzip sed cpio libbz2-dev
20+
21+
22+
### Get the MacOSX SDK
23+
24+
#### The official way
25+
26+
Extract the needed files from the official SDKs Xcode 7.3 from the Apple
27+
Developer website using the scripts that come with osxcross:
28+
29+
sudo apt install cmake libxml2-dev fuse libfuse-dev
30+
./tools/gen_sdk_package_darling_dmg.sh path/to/SDK-file
31+
32+
On Ubuntu-based distributions with a name not containing the string "ubuntu"
33+
(like Linux Mint) you will get an error message "Required kernel module
34+
'fuse' not loaded". To fix this, "echo out" the line `modinfo fuse
35+
&>/dev/null` in tools/gen_sdk_package_darling_dmg.sh by changing it to `echo
36+
modinfo fuse &>/dev/null` before running it (don't delete it or comment it
37+
out).
38+
39+
40+
```diff
41+
diff --git a/tools/gen_sdk_package_darling_dmg.sh b/tools/gen_sdk_package_darling_dmg.sh
42+
index 8cd23e5..b271450 100755
43+
--- a/tools/gen_sdk_package_darling_dmg.sh
44+
+++ b/tools/gen_sdk_package_darling_dmg.sh
45+
@@ -40,7 +40,7 @@ command -v lsb_release 2>&1 > /dev/null
46+
if [[ $? -eq 0 ]] && [[ -n $(lsb_release -a 2>&1 | grep -i ubuntu) ]]; then
47+
echo "Using ubuntu, skipping fuse module check"
48+
else
49+
- modinfo fuse &>/dev/null
50+
+ echo modinfo fuse &>/dev/null
51+
fi
52+
53+
if [ $? -ne 0 ]; then
54+
```
55+
56+
Now move the resulting file `MacOSX10.11.sdk.tar.xz` into the tarball directory.
57+
58+
59+
60+
#### The easy way
61+
62+
Somebody was kind enough to set up a repository with all the [MacOSX
63+
SDKs](https://github.com/phracker/MacOSX-SDKs). This saves you the trouble
64+
of downloading multiple gigabytes of data and running the extraction script.
65+
66+
Download the file
67+
[MacOSX10.11.sdk.tar.xz](https://github.com/phracker/MacOSX-SDKs/releases/download/10.13/MacOSX10.11.sdk.tar.xz)
68+
into the tarball directory.
69+
70+
71+
72+
73+
74+
### Build OSXcross:
75+
76+
./build.sh
77+
sudo mv target /opt/osxcross
78+
79+
Add `/opt/osxcross/bin` to your `$PATH` and prepare macports:
80+
81+
export MACOSX_DEPLOYMENT_TARGET=10.7
82+
export PATH=$PATH:/opt/osxcross/bin
83+
osxcross-macports update-cache
84+
85+
86+
87+
## Build stm8flash
88+
89+
osxcross-macports install libusb-devel
90+
make CC=o64-clang CXX=o64-clang++ PKGCONFIG=x86_64-apple-darwin15-pkg-config RELEASE=yes
91+
92+
Or with full path if you didn't add the osxcross binary directory do your
93+
PATH:
94+
95+
make CC=/opt/osxcross/bin/o64-clang CXX=/opt/osxcross/bin/o64-clang++ PKGCONFIG=/opt/osxcross/bin/x86_64-apple-darwin15-pkg-config RELEASE=yes
96+
97+
98+
## Build stm8gal
99+
100+
make CC=o64-clang
101+
102+
That's all!

docs/developer/links.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Further reading and application notes
44

5+
A very good and compact primer on
6+
[STM8S-Programming](https://github.com/TG9541/stm8ef/wiki/STM8S-Programming).
7+
Does not rely on any external libraries.
8+
59
[A series of
610
articles](https://lujji.github.io/blog/bare-metal-programming-stm8/) on bare
711
metal programming of the STM8S CPUs. By far the best introduction I

docs/developer/optimizations.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Optimizations
2+
3+
The goal is to reduce the size of the generated binaries. This is done by
4+
aiding the linker to eleminate dead code by splitting large source files and
5+
by hand-optimizing individual functions.
6+
7+
## Results
8+
9+
The sum of all these optimizations reduces the total size of Blink.ino by
10+
more than 30%, saving more than 800 bytes of precious flash space:
11+
12+
code | data| RAM| flash total |split stage
13+
---- | ----| ----| ---- |------------
14+
2507 | 138| 72| 2645 |before split
15+
2450 | 138| 72| 2588 |after splitting wiring.c
16+
2381 | 138| 72| 2519 |after splitting wiring_digital.c
17+
1799 | 138| 72| 1937 |after splitting HardwareSerial.c
18+
1686 | 138| 72| 1824 |after optimizing pinMode()
19+
20+
Data is mostly the tables to map the Arduino pin numbers to the actual port
21+
registers. RAM is mostly the transmit and receive buffers for serial
22+
communication. These can't easily be optimized out.
23+
24+
25+
## Splitting files
26+
27+
The SDCC linker does not detect unused functions and constants in an object
28+
file. It always links the whole file even if only a single symbol contained
29+
in the file is referenced. This results in pretty bloated binaries if the
30+
SPL is used.
31+
32+
Splitting larger source files into smaller units and compile them
33+
individually before building the libraries helps the SDCC linker to
34+
eleminate dead code and to produce smaller binaries.
35+
36+
Splitting files is worthwhile for all SPL files and at least some of the
37+
bigger Arduino core files.
38+
39+
40+
### The stategy
41+
42+
The SPL consists of many source files with a very regular structure. This
43+
allows to automate the splitting process with very little preparation work.
44+
45+
All SPL functions are documented with a doxygen comment block. The beginning
46+
of this comment block is a line with only "/**", that can easily be used as
47+
a marker for splitting the files.
48+
49+
Only the very first block is special. It contains definitions and prototypes
50+
that are needed all over the module. This block is saved as a `.h` file and
51+
`#include`'d by all the following blocks.
52+
53+
In most cases this is already sufficient. Only in very rare cases the
54+
position of a split needs the be edited. This is automatically done by the
55+
patches in the `patches/` directory.
56+
57+
**To prevent a split**: Change the `/**` line into something different, `/***`
58+
is used in the scripts.
59+
60+
**To force a split**: Add an empty Doxygen comment block:
61+
```c
62+
/**
63+
*/
64+
```
65+
66+
67+
### Splitting the SPL files
68+
69+
Splitting and compiling the SPL libraries is moved into the separate project
70+
[spl-splitter](https://github.com/tenbaht/spl-splitter) now.
71+
72+
73+
74+
### Split Arduino core files
75+
76+
`wiring.c`, `wiring_digital.c` and `HardwareSerial.c` all compile into quite
77+
large binaries. All of them are linked with almost
78+
every project. This is true even if no serial communication is used since
79+
main.c references `serialEvent()` and this way pulls in all of
80+
HardwareSerial.
81+
82+
Splitting these three files reduces the size of simple sketches
83+
significantly. It does not help so much for complex sketches that use almost
84+
all functions of these modules.
85+
86+
87+
88+
## Optimizing individual functions
89+
90+
`pinMode()` sticks out when looking at the size of individual functions. 270
91+
bytes for just setting the IO-mode of a pin. A re-write in assember reduces
92+
this to just 147 bytes.

docs/developer/spl.md

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,20 @@ zip file. This and [Lujji's
1010
blog](https://lujji.github.io/blog/bare-metal-programming-stm8/) is the most
1111
useful reference on using this library and programming the STM8 in general.
1212

13-
For use with SDCC the library needs to be patched:
13+
For use with SDCC the library needs to be patched with Georg's [SPL-SDCC
14+
patches](https://github.com/gicking/STM8-SPL_SDCC_patch):
1415

1516
```bash
16-
git clone https://github.com/g-gabber/STM8S_StdPeriph_Driver.git
17-
git clone https://github.com/gicking/SPL_2.2.0_SDCC_patch.git
18-
cp ../STM8S_SPL_2.2.0/Libraries/STM8S_StdPeriph_Driver/inc/stm8s.h .
19-
patch -p1 < ../SPL_2.2.0_SDCC_patch/STM8_SPL_v2.2.0_SDCC.patch
20-
cp -av ../STM8S_StdPeriph_Lib/Project/STM8S_StdPeriph_Template/stm8s_conf.h .
21-
cp -av ../STM8S_StdPeriph_Lib/Project/STM8S_StdPeriph_Template/stm8s_it.h .
17+
unzip en.stsw-stm8069.zip
18+
wget https://raw.githubusercontent.com/gicking/STM8-SPL_SDCC_patch/master/STM8S_StdPeriph_Lib_V2.3.1_sdcc.patch
19+
patch -p0 < STM8S_StdPeriph_Lib_V2.3.1_sdcc.patch
2220
```
2321

24-
SDCC uses .rel as the file extension for its object files.
22+
Check out the test project and the project template in
23+
`STM8S_StdPeriph_Lib/Project/STM8S_StdPeriph_test` and
24+
`STM8S_StdPeriph_Lib/Project/STM8S_StdPeriph_Template`
2525

26-
Additional patch required for stm8s_itc.c:
27-
28-
```diff
29-
--- stm8s_itc.c~ 2014-10-21 17:32:20.000000000 +0200
30-
+++ stm8s_itc.c 2016-12-11 21:56:41.786048494 +0100
31-
@@ -55,9 +55,12 @@
32-
return; /* Ignore compiler warning, the returned value is in A register */
33-
#elif defined _RAISONANCE_ /* _RAISONANCE_ */
34-
return _getCC_();
35-
-#else /* _IAR_ */
36-
+#elif defined _IAR_ /* _IAR_ */
37-
asm("push cc");
38-
asm("pop a"); /* Ignore compiler warning, the returned value is in A register */
39-
+#else /* _SDCC_ */
40-
+ __asm__("push cc");
41-
+ __asm__("pop a"); /* Ignore compiler warning, the returned value is in A register */
42-
#endif /* _COSMIC_*/
43-
}
44-
```
26+
SDCC uses .rel as the file extension for its object files.
4527

4628

4729

@@ -79,14 +61,15 @@ clean:
7961
This library can now be used for linking with blink_spl or uart_spl. The
8062
files stm8s_conf.h and stm8s_it.h are still needed for compilation.
8163

64+
65+
### Optimizing the code
66+
8267
The linker does not remove individual unused functions from an object file,
8368
only complete object files can be skipped.
8469
**=> for building a library it is better to separate all functions into
8570
individual source files **
8671

87-
The SPL folder in this archive contains a script `doit` to separate the
88-
functions before compilation.
89-
FIXME: description needed
72+
See [here](../optimizations/#splitting-files) for details.
9073

9174
A suggestion how to move at least the IRQ vectors away from the libray into
9275
the own source files:

mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ nav:
4141
- Developer Guide:
4242
- IDE integration: 'developer/ide-integration.md'
4343
- Compiling the tools: 'developer/compiling-the-tools.md'
44+
- Cross-compiling the tools for OSX: 'developer/cross-compile-for-osx.md'
4445
- Update an automatic installation to a manual installation: 'developer/update-to-manual.md'
4546
- C preprocessor macro magic: 'developer/macro.md'
4647
- Ways to define a pin mapping: 'developer/pin_mapping.md'
48+
- Coding style: 'developer/coding-style.md'
4749
- Using the SDCC compiler: 'developer/sdcc.md'
4850
- Using the SPL with SDCC and sduino: 'developer/spl.md'
4951
- Bare metal programming on the STM8: 'developer/bare-metal-programming.md'
52+
- Optimizations: 'developer/optimizations.md'
5053
- Performance comparison and benchmarking: 'developer/performance.md'
5154
- Links and further reading: 'developer/links.md'
5255
- Contact: contact.md

0 commit comments

Comments
 (0)