Skip to content

Commit 8371fbc

Browse files
committed
add some Arduino examples
1 parent 9f5d9b6 commit 8371fbc

13 files changed

Lines changed: 387 additions & 6 deletions

File tree

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ besorgen. Das braucht aber eine neue Version der libstdc++6. Deshalb:
3030
apt-get update
3131
apt-get install libstdc++6
3232

33-
3433
git clone https://github.com/vdudouyt/stm8flash.git
3534
cd stm8flash
3635
make
@@ -76,12 +75,13 @@ HardwareSerial
7675
Print (without float)
7776
digitalWrite()
7877
analogRead
79-
analogWrite
8078
delay
8179

8280
implemented and partly working:
81+
analogWrite
8382

8483
tested, but not working:
84+
alternateFunctions()
8585

8686
not tested
8787
ShiftIn()
@@ -91,6 +91,7 @@ ShiftOut()
9191
not implemented:
9292
yield()
9393
SPI
94+
Wire/I2C
9495

9596

9697
## Differences to the original Arduino environment
@@ -476,6 +477,25 @@ functions. This allows for three more PWM pins, but maybe it adds to much
476477
complexity for the Arduino API. Not sure if it should stay.
477478

478479

480+
### Performance compared with the original Arduino environment
481+
482+
Benchmarking the original Arduino examples from Arduino 1.0.5. The simple
483+
Blinky cmopiles to 57 bytes of code, the total binary including the sduino
484+
libraries is 1868 Bytes (0x74c).
485+
486+
So far, wiring_analog depends on wiring_digital, even when analogWrite is not
487+
used. This could be solved by compiling the sduino functions separately into
488+
a library.
489+
490+
name code total linked files other than main and wiring
491+
BareMinimum 2 1238 -
492+
Blink 57 1870 wiring_digital
493+
AnalogReadSerial 205 3452 digital, analog, serial, print
494+
DigitalReadSerial 57 3160 digital, serial, print
495+
Fade 226 2189 digital, analog
496+
ReadAnalogVoltage float not yet implemented
497+
498+
479499
### Besondere Features, die von Arduino nicht unterstützt werden
480500

481501
Input-Capture-Mode: min. für Timer1 auf allen vier Kanälen möglich.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
AnalogReadSerial
3+
Reads an analog input on pin 0, prints the result to the serial monitor.
4+
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
5+
6+
This example code is in the public domain.
7+
*/
8+
9+
#include <Serial.h>
10+
11+
// the setup routine runs once when you press reset:
12+
void setup() {
13+
// initialize serial communication at 9600 bits per second:
14+
Serial_begin(9600);
15+
}
16+
17+
// the loop routine runs over and over again forever:
18+
void loop() {
19+
// read the input on analog pin 0:
20+
int sensorValue = analogRead(A0);
21+
// print out the value you read:
22+
Serial_println_u(sensorValue);
23+
delay(1); // delay in between reads for stability
24+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
BASENAME=$(shell basename $$(pwd))
2+
EXECUTABLE=$(BASENAME).ihx
3+
4+
SDCCBASE=/opt/sdcc
5+
BINDIR=$(SDCCBASE)/bin
6+
LIBDIR=$(SDCCBASE)/share/sdcc/lib
7+
INCDIR=$(SDCCBASE)/share/include
8+
CC=$(BINDIR)/sdcc
9+
LD=$(BINDIR)/sdld
10+
11+
SPLBASE=../../../STM8S_StdPeriph_Driver
12+
SDUINO=../../../sduino
13+
14+
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 \
15+
-I. -I$(SDUINO) -I$(SPLBASE)/inc -I$(INCDIR)
16+
17+
LDFLAGS=-L$(SPLBASE)/src -L$(LIBDIR)/stm8 -lstm8s103
18+
19+
OBJECTS=$(BASENAME).rel
20+
SDOBJECTS=main.rel wiring.rel wiring_digital.rel wiring_analog.rel \
21+
HardwareSerial.rel Print.rel
22+
23+
.PHONY: all clean flash
24+
25+
#all: $(OBJECTS)
26+
27+
$(EXECUTABLE): $(OBJECTS) $(SDOBJECTS)
28+
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
29+
30+
$(OBJECTS) : %.rel : %.c
31+
$(CC) -c $(CFLAGS) $^ -o $@
32+
33+
$(SDOBJECTS) : %.rel : $(SDUINO)/%.c
34+
$(CC) -c $(CFLAGS) $^ -o $@
35+
36+
flash: $(EXECUTABLE)
37+
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
38+
39+
readopt:
40+
stm8flash -c stlinkv2 -p stm8s103?3 -s opt -r opt.bin
41+
42+
43+
clean:
44+
rm -f *.lib *.rst *.rel *.lst *.ihx *.sym *.asm *.lk *.map \
45+
*.cdb *.adb *~ *.bak
46+
rm -f $(EXECUTABLE)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void setup() {
2+
// put your setup code here, to run once:
3+
4+
}
5+
6+
void loop() {
7+
// put your main code here, to run repeatedly:
8+
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
BASENAME=$(shell basename $$(pwd))
2+
EXECUTABLE=$(BASENAME).ihx
3+
4+
SDCCBASE=/opt/sdcc
5+
BINDIR=$(SDCCBASE)/bin
6+
LIBDIR=$(SDCCBASE)/share/sdcc/lib
7+
INCDIR=$(SDCCBASE)/share/include
8+
CC=$(BINDIR)/sdcc
9+
LD=$(BINDIR)/sdld
10+
11+
SPLBASE=../../../STM8S_StdPeriph_Driver
12+
SDUINO=../../../sduino
13+
14+
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 \
15+
-I. -I$(SDUINO) -I$(SPLBASE)/inc -I$(INCDIR)
16+
17+
LDFLAGS=-L$(SPLBASE)/src -L$(LIBDIR)/stm8 -lstm8s103
18+
19+
OBJECTS=$(BASENAME).rel
20+
SDOBJECTS=main.rel wiring.rel
21+
# wiring_digital.rel wiring_analog.rel
22+
# HardwareSerial.rel Print.rel
23+
24+
.PHONY: all clean flash
25+
26+
#all: $(OBJECTS)
27+
28+
$(EXECUTABLE): $(OBJECTS) $(SDOBJECTS)
29+
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
30+
31+
$(OBJECTS) : %.rel : %.c
32+
$(CC) -c $(CFLAGS) $^ -o $@
33+
34+
$(SDOBJECTS) : %.rel : $(SDUINO)/%.c
35+
$(CC) -c $(CFLAGS) $^ -o $@
36+
37+
flash: $(EXECUTABLE)
38+
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
39+
40+
readopt:
41+
stm8flash -c stlinkv2 -p stm8s103?3 -s opt -r opt.bin
42+
43+
44+
clean:
45+
rm -f *.lib *.rst *.rel *.lst *.ihx *.sym *.asm *.lk *.map \
46+
*.cdb *.adb *~ *.bak
47+
rm -f $(EXECUTABLE)

examples/01.Basics/Blink/Blink.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Blink
3+
Turns on an LED on for one second, then off for one second, repeatedly.
4+
5+
This example code is in the public domain.
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
// Pin 13 has an LED connected on most Arduino boards.
11+
// Pin 3 for the STM8S103 break out board
12+
// give it a name:
13+
int led = 3;
14+
15+
// the setup routine runs once when you press reset:
16+
void setup() {
17+
// initialize the digital pin as an output.
18+
pinMode(led, OUTPUT);
19+
}
20+
21+
// the loop routine runs over and over again forever:
22+
void loop() {
23+
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
24+
delay(1000); // wait for a second
25+
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
26+
delay(1000); // wait for a second
27+
}

examples/01.Basics/Blink/Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
BASENAME=$(shell basename $$(pwd))
2+
EXECUTABLE=$(BASENAME).ihx
3+
4+
SDCCBASE=/opt/sdcc
5+
BINDIR=$(SDCCBASE)/bin
6+
LIBDIR=$(SDCCBASE)/share/sdcc/lib
7+
INCDIR=$(SDCCBASE)/share/include
8+
CC=$(BINDIR)/sdcc
9+
LD=$(BINDIR)/sdld
10+
11+
SPLBASE=../../../STM8S_StdPeriph_Driver
12+
SDUINO=../../../sduino
13+
14+
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 \
15+
-I. -I$(SDUINO) -I$(SPLBASE)/inc -I$(INCDIR)
16+
17+
LDFLAGS=-L$(SPLBASE)/src -L$(LIBDIR)/stm8 -lstm8s103
18+
19+
OBJECTS=$(BASENAME).rel
20+
SDOBJECTS=main.rel wiring.rel wiring_digital.rel
21+
# wiring_analog.rel
22+
# HardwareSerial.rel Print.rel
23+
24+
.PHONY: all clean flash
25+
26+
#all: $(OBJECTS)
27+
28+
$(EXECUTABLE): $(OBJECTS) $(SDOBJECTS)
29+
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
30+
31+
$(OBJECTS) : %.rel : %.c
32+
$(CC) -c $(CFLAGS) $^ -o $@
33+
34+
$(SDOBJECTS) : %.rel : $(SDUINO)/%.c
35+
$(CC) -c $(CFLAGS) $^ -o $@
36+
37+
flash: $(EXECUTABLE)
38+
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
39+
40+
readopt:
41+
stm8flash -c stlinkv2 -p stm8s103?3 -s opt -r opt.bin
42+
43+
44+
clean:
45+
rm -f *.lib *.rst *.rel *.lst *.ihx *.sym *.asm *.lk *.map \
46+
*.cdb *.adb *~ *.bak
47+
rm -f $(EXECUTABLE)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
DigitalReadSerial
3+
Reads a digital input on pin 2, prints the result to the serial monitor
4+
5+
This example code is in the public domain.
6+
*/
7+
8+
#include <Serial.h>
9+
10+
// digital pin 2 has a pushbutton attached to it. Give it a name:
11+
int pushButton = 2;
12+
13+
// the setup routine runs once when you press reset:
14+
void setup() {
15+
// initialize serial communication at 9600 bits per second:
16+
Serial_begin(9600);
17+
// make the pushbutton's pin an input:
18+
pinMode(pushButton, INPUT);
19+
}
20+
21+
// the loop routine runs over and over again forever:
22+
void loop() {
23+
// read the input pin:
24+
int buttonState = digitalRead(pushButton);
25+
// print out the state of the button:
26+
Serial_println_u(buttonState);
27+
delay(1); // delay in between reads for stability
28+
}
29+
30+
31+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
BASENAME=$(shell basename $$(pwd))
2+
EXECUTABLE=$(BASENAME).ihx
3+
4+
SDCCBASE=/opt/sdcc
5+
BINDIR=$(SDCCBASE)/bin
6+
LIBDIR=$(SDCCBASE)/share/sdcc/lib
7+
INCDIR=$(SDCCBASE)/share/include
8+
CC=$(BINDIR)/sdcc
9+
LD=$(BINDIR)/sdld
10+
11+
SPLBASE=../../../STM8S_StdPeriph_Driver
12+
SDUINO=../../../sduino
13+
14+
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 \
15+
-I. -I$(SDUINO) -I$(SPLBASE)/inc -I$(INCDIR)
16+
17+
LDFLAGS=-L$(SPLBASE)/src -L$(LIBDIR)/stm8 -lstm8s103
18+
19+
OBJECTS=$(BASENAME).rel
20+
SDOBJECTS=main.rel wiring.rel wiring_digital.rel \
21+
HardwareSerial.rel Print.rel
22+
# wiring_analog.rel
23+
24+
.PHONY: all clean flash
25+
26+
#all: $(OBJECTS)
27+
28+
$(EXECUTABLE): $(OBJECTS) $(SDOBJECTS)
29+
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
30+
31+
$(OBJECTS) : %.rel : %.c
32+
$(CC) -c $(CFLAGS) $^ -o $@
33+
34+
$(SDOBJECTS) : %.rel : $(SDUINO)/%.c
35+
$(CC) -c $(CFLAGS) $^ -o $@
36+
37+
flash: $(EXECUTABLE)
38+
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
39+
40+
readopt:
41+
stm8flash -c stlinkv2 -p stm8s103?3 -s opt -r opt.bin
42+
43+
44+
clean:
45+
rm -f *.lib *.rst *.rel *.lst *.ihx *.sym *.asm *.lk *.map \
46+
*.cdb *.adb *~ *.bak
47+
rm -f $(EXECUTABLE)

examples/01.Basics/Fade/Fade.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Fade
3+
4+
This example shows how to fade an LED on pin 9
5+
using the analogWrite() function.
6+
7+
This example code is in the public domain.
8+
*/
9+
10+
#include <Arduino.h>
11+
12+
int led = 9; // the pin that the LED is attached to
13+
int brightness = 0; // how bright the LED is
14+
int fadeAmount = 5; // how many points to fade the LED by
15+
16+
// the setup routine runs once when you press reset:
17+
void setup() {
18+
// declare pin 9 to be an output:
19+
pinMode(led, OUTPUT);
20+
}
21+
22+
// the loop routine runs over and over again forever:
23+
void loop() {
24+
// set the brightness of pin 9:
25+
analogWrite(led, brightness);
26+
27+
// change the brightness for next time through the loop:
28+
brightness = brightness + fadeAmount;
29+
30+
// reverse the direction of the fading at the ends of the fade:
31+
if (brightness == 0 || brightness == 255) {
32+
fadeAmount = -fadeAmount ;
33+
}
34+
// wait for 30 milliseconds to see the dimming effect
35+
delay(30);
36+
}
37+

0 commit comments

Comments
 (0)