Skip to content

Commit 70c22cb

Browse files
committed
timer4 is working, tested print functions, deactivate float functions
1 parent 5d0eafa commit 70c22cb

9 files changed

Lines changed: 127 additions & 13 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ muss entsprechend angepasst werden. Am Ende hochladen:
5151

5252
tested and working:
5353
pinMode()
54+
HardwareSerial
55+
Print (without float)
5456

5557
implemented and partly working:
5658
digitalWrite(): simple write ok, PWM handling untested
@@ -68,7 +70,6 @@ not implemented:
6870
yield()
6971
analogRead
7072
analogWrite
71-
HardwareSerial
7273
SPI
7374

7475

@@ -89,6 +90,11 @@ Timer: millis() uses timer4.
8990
STM8AF Flash programming manual (PM0051)
9091
STM8 SWIM protocol and debug manual (UM0470)
9192

93+
Many examples and presentations about the STM8S:
94+
https://github.com/VincentYChen/STM8teach
95+
https://github.com/VincentYChen/STM8teach/tree/master/code/Project/STM8S_StdPeriph_Examples
96+
97+
9298
example for RS-232 handling with SPL:
9399
https://sourceforge.net/p/oggstreamer/oggs-stm8-firmware-001/ci/master/tree/rx_ringbuffer.c
94100

sduino/Print.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ size_t Print_print_i(long n)
7979
return printInt(n,10);
8080
}
8181

82+
size_t Print_println_s(char *str)
83+
{
84+
size_t r;
85+
86+
r = printStr(str);
87+
return r + println();
88+
}
89+
90+
91+
8292
size_t Print_println_u(unsigned long n)
8393
{
8494
size_t r;
@@ -257,7 +267,7 @@ size_t printInt(long n, uint8_t base)
257267
}
258268
}
259269

260-
270+
/*
261271
size_t Print_printFloat(double number, uint8_t digits)
262272
{
263273
size_t n = 0;
@@ -306,3 +316,4 @@ size_t Print_printFloat(double number, uint8_t digits)
306316
307317
return n;
308318
}
319+
*/

sduino/Print.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ size_t Print_print_i(long n);
6161
// print signed integer values (char, short, int, long) to base B
6262
#define Print_print_ib(I,B) printInt(I,B)
6363

64-
64+
size_t Print_println_s(char *str);
6565
size_t Print_println_u(unsigned long n);
6666
size_t Print_println_i(long n);
6767

sduino/wiring.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include "wiring_private.h"
2424

2525
// the prescaler is set so that timer4 ticks every 64 clock cycles, and the
26-
// the overflow handler is called every 256 ticks.
27-
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
26+
// the overflow handler is called every 250 ticks.
27+
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 250))
2828

2929
// the whole number of milliseconds per timer4 overflow
3030
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
@@ -46,18 +46,25 @@ INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, ITC_IRQ_TIM4_OVF) /* TIM4 UPD/OVF */
4646
// copy these to local variables so they can be stored in registers
4747
// (volatile variables must be read from memory on every access)
4848
unsigned long m = timer4_millis;
49+
#if (FRACT_INC != 0)
4950
unsigned char f = timer4_fract;
51+
#endif
5052

5153
m += MILLIS_INC;
54+
#if (FRACT_INC != 0)
5255
f += FRACT_INC;
5356
if (f >= FRACT_MAX) {
5457
f -= FRACT_MAX;
5558
m += 1;
5659
}
5760

5861
timer4_fract = f;
62+
#endif
5963
timer4_millis = m;
6064
timer4_overflow_count++;
65+
66+
/* Clear Interrupt Pending bit */
67+
TIM4_ClearITPendingBit(TIM4_IT_UPDATE); // TIM4->SR1 = (uint8_t)(~0x01);
6168
}
6269

6370
unsigned long millis()
@@ -249,6 +256,9 @@ void delayMicroseconds(unsigned int us)
249256

250257
void init()
251258
{
259+
// set the clock to 16 MHz
260+
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
261+
252262
GPIO_DeInit(GPIOA);
253263
GPIO_DeInit(GPIOB);
254264
GPIO_DeInit(GPIOC);
@@ -259,10 +269,14 @@ void init()
259269

260270
// set timer 0 prescale factor to 64, period 0 (=256)
261271
TIM4_DeInit();
262-
TIM4_TimeBaseInit(TIM4_PRESCALER_64, 0);
263-
264-
// enable timer 0 overflow interrupt
265-
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
272+
TIM4_TimeBaseInit(TIM4_PRESCALER_64, 249);
273+
/* Clear TIM4 update flag */
274+
TIM4_ClearFlag(TIM4_FLAG_UPDATE); // TIM4->SR1 = (uint8_t)(~0x01);
275+
/* Enable update interrupt */
276+
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); // TIM4->IER |= (uint8_t)TIM4_IT;
277+
/* Enable TIM4 */
278+
TIM4_Cmd(ENABLE); // TIM4->CR1 |= TIM4_CR1_CEN;
279+
266280

267281
#if 0 //FIXME
268282
// timers 1 and 2 are used for phase-correct hardware pwm

test/print/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CFLAGS= --debug -mstm8 -DF_CPU=2000000L -DSTM8S103 \
1616
LDFLAGS=-L$(LIBBASE)/src -L$(SDCCBASE)/share/sdcc/lib/stm8 -lstm8s
1717

1818
OBJECTS=$(BASENAME).rel
19-
SDOBJECTS=main.rel wiring.rel wiring_digital.rel \
19+
SDOBJECTS=main.rel wiring.rel \
2020
HardwareSerial.rel Print.rel
2121

2222
.PHONY: all clean flash

test/print/print.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
#include "Arduino.h"
2+
#include "HardwareSerial.h"
23
#include "Print.h"
34

45
void setup (void)
56
{
7+
HardwareSerial_begin(115200);
8+
Print_println_s("\nPrint test");
9+
610
printNumber(1234,10); printChr(10);
711
printNumber(1234,16); printChr(10);
812
printNumber(1234,2); printChr(10);
913

1014
Print_println_u(2345);
1115
Print_println_u(-3);
1216
Print_println_i(-3);
13-
17+
/*
1418
Print_printFloat(PI,10); printChr(10);
1519
Print_printFloat(PI,11); printChr(10);
1620
Print_printFloat(PI,12); printChr(10);
1721
Print_printFloat(-PI,13); printChr(10);
22+
*/
1823
}
1924

20-
void loop(){}
25+
void loop()
26+
{
27+
char c;
28+
29+
if (HardwareSerial_available()) {
30+
c = HardwareSerial_read();
31+
printStr("character read: 0x");
32+
printNumber(c,HEX);
33+
println();
34+
}
35+
}

test/serial2/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CFLAGS= --debug -mstm8 -DF_CPU=2000000L -DSTM8S103 \
1616
LDFLAGS=-L$(LIBBASE)/src -L/opt/sdcc/share/sdcc/lib/stm8 -lstm8s
1717

1818
OBJECTS=$(BASENAME).rel
19-
SDOBJECTS=main.rel wiring.rel wiring_digital.rel HardwareSerial.rel
19+
SDOBJECTS=main.rel wiring.rel HardwareSerial.rel
2020

2121
.PHONY: all clean flash
2222

test/timer1/Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
BASENAME=$(shell basename $$(pwd))
2+
EXECUTABLE=$(BASENAME).ihx
3+
4+
#SDCCBASE=/usr/local
5+
SDCCBASE=/opt/sdcc
6+
BINDIR=$(SDCCBASE)/bin
7+
CC=$(BINDIR)/sdcc
8+
LD=$(BINDIR)/sdld
9+
10+
LIBBASE=../../STM8S_StdPeriph_Driver
11+
SDUINO=../../sduino
12+
13+
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 \
14+
-I. -I$(SDUINO) -I$(LIBBASE)/inc -I/usr/share/sdcc/include/
15+
16+
LDFLAGS=-L$(LIBBASE)/src -L/opt/sdcc/share/sdcc/lib/stm8 -lstm8s
17+
18+
OBJECTS=$(BASENAME).rel
19+
SDOBJECTS=main.rel wiring.rel HardwareSerial.rel Print.rel
20+
21+
.PHONY: all clean flash
22+
23+
#all: $(OBJECTS)
24+
25+
$(EXECUTABLE): $(OBJECTS) $(SDOBJECTS)
26+
#test.rel wiring_digital.rel
27+
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
28+
29+
$(OBJECTS) : %.rel : %.c
30+
$(CC) -c $(CFLAGS) $^ -o $@
31+
32+
$(SDOBJECTS) : %.rel : $(SDUINO)/%.c
33+
$(CC) -c $(CFLAGS) $^ -o $@
34+
35+
flash: $(EXECUTABLE)
36+
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
37+
38+
39+
clean:
40+
rm -f *.lib *.rst *.rel *.lst *.ihx *.sym *.asm *.lk *.map \
41+
*.cdb *.adb *~ *.bak
42+
rm -f $(EXECUTABLE)

test/timer1/timer1.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* test timer4, show the value for millis() over time
3+
*/
4+
5+
#include "Arduino.h"
6+
#include "HardwareSerial.h"
7+
#include "Print.h"
8+
9+
10+
void setup(void)
11+
{
12+
HardwareSerial_begin(115200);
13+
}
14+
15+
16+
void loop (void)
17+
{
18+
uint32_t i;
19+
20+
Print_print_s("millis()=");
21+
Print_print_u(millis());
22+
Print_print_s("\tTIM4_CNTR=");
23+
Print_println_u(TIM4->CNTR);
24+
25+
for (i=40000; i; i--);
26+
}

0 commit comments

Comments
 (0)