Skip to content

Commit 3d13d61

Browse files
committed
split into separate source files
1 parent 85e9b60 commit 3d13d61

5 files changed

Lines changed: 320 additions & 339 deletions

File tree

test/pinmode/Makefile

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,24 @@ SDBASE=../../sduino/hardware/sduino/stm8
1111
LIBBASE=$(SDBASE)/STM8S_StdPeriph_Driver
1212
SDUINO=$(SDBASE)/cores/sduino
1313

14-
CFLAGS= --debug -mstm8 -DF_CPU=16000000L -DSTM8S103 \
14+
CPPFLAGS=-DF_CPU=16000000L -DSTM8S103 -Ddouble=float \
1515
-I. -I$(SDUINO) -I$(SDBASE)/variants/standard \
1616
-I$(LIBBASE)/inc -I$(LIBBASE)/src \
1717
-I$(SDCCBASE)/share/sdcc/include/
18-
# -DSUPPORT_ALTERNATE_MAPPINGS
18+
CFLAGS= -mstm8 --debug
19+
LDFLAGS=-L$(LIBBASE)/lib -L$(SDCCBASE)/share/sdcc/lib/stm8 -lSTM8S103
1920

20-
LDFLAGS=-L$(LIBBASE)/lib -L$(SDCCBASE)/share/sdcc/lib/stm8 -lSTM8S103
21-
22-
OBJECTS=$(BASENAME).rel
23-
SDOBJECTS=main.rel wiring.rel wiring_digital.rel SPI.rel \
24-
HardwareSerial.rel Print.rel
21+
OBJECTS=pinmode-c.rel pinmode-asm.rel main.rel
2522

2623
.PHONY: all clean flash statistics
2724

2825
#all: $(OBJECTS)
2926

30-
$(EXECUTABLE): $(OBJECTS) #$(SDOBJECTS)
27+
$(EXECUTABLE): $(OBJECTS)
3128
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
3229

3330
$(OBJECTS) : %.rel : %.c
34-
$(CC) -c $(CFLAGS) $^ -o $@
35-
36-
$(SDOBJECTS) : %.rel : $(SDUINO)/%.c
37-
$(CC) -c $(CFLAGS) $^ -o $@
31+
$(CC) -c $(CPPFLAGS) $(CFLAGS) $^ -o $@
3832

3933
flash: $(EXECUTABLE)
4034
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
@@ -45,9 +39,12 @@ readopt:
4539

4640
# lists the length of functions and constants defined in the primary source
4741
# file
48-
statistics: $(EXECUTABLE)
49-
grep "[0-9] _" $(BASENAME).rst |\
50-
awk 'BEGIN {print "length\tfunction\n------\t----------";}{ a=strtonum("0x"$$1);if (name) print a-s "\t" name; s=a; name=$$3}'
42+
statistics:
43+
awk '/ CODE/ {print FILENAME "\t" strtonum("0x"$$4)}' *.sym
44+
45+
46+
# grep "[0-9] _" $(BASENAME).rst |\
47+
# awk 'BEGIN {print "length\tfunction\n------\t----------";}{ a=strtonum("0x"$$1);if (name) print a-s "\t" name; s=a; name=$$3}'
5148

5249

5350
clean:

test/pinmode/main.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* test spi functions
3+
*/
4+
5+
#define ARDUINO_MAIN
6+
#include "Arduino.h"
7+
#include "wiring_private.h"
8+
#include "stm8s_it.h"
9+
10+
void pinMode_c(uint8_t pin, uint8_t mode);
11+
void pinMode_asm(uint8_t pin, uint8_t mode);
12+
13+
14+
uint8_t checkresult(uint8_t *adr, uint8_t *data)
15+
{
16+
uint8_t i,ok;
17+
18+
ok = 1;
19+
for (i=0; i<3; ++i) {
20+
if (adr[2+i] != data[i]) ok=0;
21+
}
22+
return ok;
23+
}
24+
25+
26+
void main(void)
27+
{
28+
// expected result: xx xx 00 00 00
29+
pinMode_asm(PA1,INPUT);
30+
checkresult(GPIOA_BaseAddress, "\x00\x00\x00");
31+
32+
// expected result: xx xx 20 20 00
33+
pinMode_asm(PB5,OUTPUT);
34+
checkresult(GPIOB_BaseAddress, "\x20\x20\x00");
35+
36+
// expected result: xx xx 00 10 00
37+
pinMode_asm(PC4,INPUT_PULLUP);
38+
checkresult(GPIOC_BaseAddress, "\x00\x10\x00");
39+
40+
// expected result: xx xx 20 10 00
41+
pinMode_asm(PC5,OUTPUT_OD);
42+
checkresult(GPIOC_BaseAddress, "\x20\x10\x00");
43+
44+
// expected result: xx xx 02 02 02
45+
pinMode_asm(PD1,OUTPUT_FAST);
46+
checkresult(GPIOD_BaseAddress, "\x02\x02\x02");
47+
48+
// expected result: xx xx 42 02 42
49+
pinMode_asm(PD6,OUTPUT_OD_FAST);
50+
checkresult(GPIOD_BaseAddress, "\x42\x02\x42");
51+
}
52+
53+
/*
54+
* empty default IRQ functions to make the linker happy if the
55+
* respective module is not to linked.
56+
*/
57+
58+
void UART1_TX_IRQHandler(void) __interrupt(ITC_IRQ_UART1_TX){}
59+
void UART1_RX_IRQHandler(void) __interrupt(ITC_IRQ_UART1_RX){}
60+
61+
// define as __naked to avoid the "clr a/dix x,a" prolog
62+
#define IMPLEMENT_ISR(vect, interrupt) \
63+
void vect(void) __interrupt((interrupt)>>8) __naked { \
64+
__asm__("iret"); \
65+
}
66+
67+
IMPLEMENT_ISR(EXTI_PORTA_IRQHandler, INT_PORTA) /* EXTI PORTA */
68+
IMPLEMENT_ISR(EXTI_PORTB_IRQHandler, INT_PORTB) /* EXTI PORTB */
69+
IMPLEMENT_ISR(EXTI_PORTC_IRQHandler, INT_PORTC) /* EXTI PORTC */
70+
IMPLEMENT_ISR(EXTI_PORTD_IRQHandler, INT_PORTD) /* EXTI PORTD */
71+
IMPLEMENT_ISR(EXTI_PORTE_IRQHandler, INT_PORTE) /* EXTI PORTE */
72+
IMPLEMENT_ISR(TIM1_CAP_COM_IRQHandler, INT_TIM1_CAPCOM)
73+
//IMPLEMENT_ISR(TIM1_UPD_OVF_TRG_BRK_IRQHandler, INT_TIM1_OVF)
74+
//IMPLEMENT_ISR(TIM2_CAP_COM_IRQHandler, INT_TIM2_CAPCOM)
75+
//IMPLEMENT_ISR(TIM2_UPD_OVF_TRG_BRK_IRQHandler, INT_TIM2_OVF)
76+
77+
void TIM4_UPD_OVF_IRQHandler(void) __interrupt(ITC_IRQ_TIM4_OVF){}

test/pinmode/pinmode-asm.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* test spi functions
3+
*/
4+
5+
#include "Arduino.h"
6+
#include "wiring_private.h"
7+
8+
9+
void pinMode_asm(uint8_t pin, uint8_t mode)
10+
{
11+
(void) pin; // empty code to avoid warning
12+
(void) mode;
13+
__asm
14+
sub sp, #16
15+
#if 1
16+
; pinmode.c: 10: uint8_t bit = digitalPinToBitMask(pin);
17+
clrw x
18+
19+
ld a,(0x03, sp)
20+
ld xl,a
21+
addw x, #_digital_pin_to_bit_mask_PGM+0
22+
ld a, (x) ; bit
23+
ld yl,a ; yl = bit
24+
cpl a
25+
ld yh,a ; yh = ~bit
26+
27+
; pinmode.c: 11: uint8_t port = digitalPinToPort(pin);
28+
;;hier addw x, #_digital_pin_to_bit_mask_PGM - _digital_pin_to_bit_mask_PGM
29+
ld a, (x) ; port
30+
jreq 00018$
31+
32+
sll a
33+
clrw x
34+
ld xl,a
35+
addw x, #_port_to_output_PGM+0 ; x = gpio
36+
37+
ld a,(0x04,sp) ; a = mode, flags are set
38+
#else
39+
; pinmode.c: 10: uint8_t bit = digitalPinToBitMask(pin);
40+
ldw x, #_digital_pin_to_bit_mask_PGM+0
41+
ld a, xl
42+
add a, (0x13, sp)
43+
rlwa x
44+
adc a, #0x00
45+
ld xh, a
46+
ld a, (x)
47+
ld (0x04, sp), a ; bit := 4
48+
; pinmode.c: 11: uint8_t port = digitalPinToPort(pin);
49+
ldw x, #_digital_pin_to_port_PGM+0
50+
ld a, xl
51+
add a, (0x13, sp)
52+
rlwa x
53+
adc a, #0x00
54+
ld xh, a
55+
ld a, (x)
56+
ld (0x03, sp), a
57+
ld a, (0x03, sp)
58+
ld (0x07, sp), a ; port := 7
59+
; pinmode.c: 14: if (port == NOT_A_PIN) return;
60+
tnz (0x03, sp)
61+
jrne 00002$
62+
jp 00018$
63+
00002$:
64+
; pinmode.c: 16: gpio = (GPIO_TypeDef *) portOutputRegister(port);
65+
ldw x, #_port_to_output_PGM+0
66+
ldw (0x0e, sp), x
67+
ld a, (0x07, sp)
68+
ld xl, a
69+
ld a, #0x02
70+
mul x, a
71+
addw x, (0x0e, sp)
72+
ldw x, (x) ; jetzt ist gpio in x
73+
74+
ld a, (0x04, sp) ; bit
75+
ld yl,a ; yl = bit
76+
cpl a
77+
ld yh,a ; yh = ~bit
78+
ld a, (0x14, sp) ; a=mode, flags are set
79+
#endif
80+
81+
; gpio->DDR: (2,x) (war an c,SP)
82+
; gpio->CR1: (3,x) (war an 8,SP)
83+
; gpio->CR2: (4,x) (war an 5,SP)
84+
85+
sim
86+
; pinmode.c: 18: if (mode == INPUT) {
87+
jrne 00016$
88+
; pinmode.c: 20: gpio->CR2 &= ~bit; // first: deactivate interrupt
89+
ld a,yh
90+
and a,(4,x)
91+
ld (4,x),a
92+
; pinmode.c: 21: gpio->CR1 &= ~bit; // release top side
93+
ld a,yh
94+
and a,(3,x)
95+
ld (3,x),a
96+
; pinmode.c: 22: gpio->DDR &= ~bit; // now set direction
97+
ld a,yh
98+
and a,(2,x)
99+
jp 00022$
100+
101+
00016$:
102+
; pinmode.c: 24: } else if (mode == INPUT_PULLUP) {
103+
cp a, #0x02
104+
jrne 00013$
105+
; pinmode.c: 26: gpio->CR2 &= ~bit; // first: deactivate interrupt
106+
ld a,yh
107+
and a,(4,x)
108+
ld (4,x),a
109+
; pinmode.c: 27: gpio->DDR &= ~bit; // set direction before
110+
ld a,yh
111+
and a,(2,x)
112+
ld (2,x),a
113+
; pinmode.c: 28: gpio->CR1 |= bit; // activating the pull up
114+
ld a,yl
115+
or a,(3,x)
116+
ld (3,x),a
117+
jp 00018$
118+
119+
00013$:
120+
; pinmode.c: 30: } else if (mode == OUTPUT_FAST) {// output push-pull, fast
121+
cp a, #0x05
122+
jrne 00010$
123+
; pinmode.c: 32: gpio->CR1 |= bit;
124+
ld a,yl
125+
or a,(3,x)
126+
jra 00020$
127+
; pinmode.c: 39: gpio->DDR |= bit; // direction before setting CR2 to
128+
; pinmode.c: 40: gpio->CR2 |= bit; // avoid accidental interrupt
129+
00010$:
130+
; pinmode.c: 36: } else if (mode == OUTPUT_OD_FAST) { // output open drain, fast
131+
cp a, #0x07
132+
jrne 00007$
133+
; pinmode.c: 38: gpio->CR1 &= ~bit;
134+
ld a,yh
135+
and a,(3,x)
136+
00020$: ld (3,x),a
137+
; pinmode.c: 39: gpio->DDR |= bit; // direction before setting CR2 to
138+
ld a,yl
139+
or a,(2,x)
140+
ld (2,x),a
141+
; pinmode.c: 40: gpio->CR2 |= bit; // avoid accidental interrupt
142+
ld a,yl
143+
or a,(4,x)
144+
ld (4,x),a
145+
jra 00018$
146+
147+
00007$:
148+
; pinmode.c: 42: } else if (mode == OUTPUT_OD) { // output open drain, slow
149+
cp a, #0x03
150+
jrne 00004$
151+
; pinmode.c: 44: gpio->CR1 &= ~bit;
152+
ld a,yh
153+
and a,(3,x)
154+
jra 00021$
155+
; pinmode.c: 45: gpio->CR2 &= ~bit;
156+
; pinmode.c: 46: gpio->DDR |= bit;
157+
00004$:
158+
; pinmode.c: 50: gpio->CR1 |= bit;
159+
ld a,yl
160+
or a,(3,x)
161+
00021$: ld (3,x),a
162+
; pinmode.c: 51: gpio->CR2 &= ~bit;
163+
ld a,yh
164+
and a,(4,x)
165+
ld (4,x),a
166+
; pinmode.c: 52: gpio->DDR |= bit;
167+
ld a,yl
168+
or a,(2,x)
169+
00022$: ld (2,x),a
170+
00018$:
171+
rim
172+
addw sp, #16
173+
__endasm;
174+
}
175+
176+

test/pinmode/pinmode-c.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* test spi functions
3+
*/
4+
5+
#include "Arduino.h"
6+
#include "wiring_private.h"
7+
8+
void pinMode_c(uint8_t pin, uint8_t mode)
9+
{
10+
uint8_t bit = digitalPinToBitMask(pin);
11+
uint8_t port = digitalPinToPort(pin);
12+
volatile GPIO_TypeDef *gpio;
13+
14+
if (port == NOT_A_PIN) return;
15+
16+
gpio = (GPIO_TypeDef *) portOutputRegister(port);
17+
18+
if (mode == INPUT) {
19+
BEGIN_CRITICAL
20+
gpio->CR2 &= ~bit; // first: deactivate interrupt
21+
gpio->CR1 &= ~bit; // release top side
22+
gpio->DDR &= ~bit; // now set direction
23+
END_CRITICAL
24+
} else if (mode == INPUT_PULLUP) {
25+
BEGIN_CRITICAL
26+
gpio->CR2 &= ~bit; // first: deactivate interrupt
27+
gpio->DDR &= ~bit; // set direction before
28+
gpio->CR1 |= bit; // activating the pull up
29+
END_CRITICAL
30+
} else if (mode == OUTPUT_FAST) {// output push-pull, fast
31+
BEGIN_CRITICAL
32+
gpio->CR1 |= bit;
33+
gpio->DDR |= bit; // direction before setting CR2 to
34+
gpio->CR2 |= bit; // avoid accidental interrupt
35+
END_CRITICAL
36+
} else if (mode == OUTPUT_OD_FAST) { // output open drain, fast
37+
BEGIN_CRITICAL
38+
gpio->CR1 &= ~bit;
39+
gpio->DDR |= bit; // direction before setting CR2 to
40+
gpio->CR2 |= bit; // avoid accidental interrupt
41+
END_CRITICAL
42+
} else if (mode == OUTPUT_OD) { // output open drain, slow
43+
BEGIN_CRITICAL
44+
gpio->CR1 &= ~bit;
45+
gpio->CR2 &= ~bit;
46+
gpio->DDR |= bit;
47+
END_CRITICAL
48+
} else { // output push-pull, slow
49+
BEGIN_CRITICAL
50+
gpio->CR1 |= bit;
51+
gpio->CR2 &= ~bit;
52+
gpio->DDR |= bit;
53+
END_CRITICAL
54+
}
55+
}

0 commit comments

Comments
 (0)