Skip to content

Commit 053ea4f

Browse files
committed
pinMode und digitalWrite testet
1 parent a5016c9 commit 053ea4f

9 files changed

Lines changed: 150 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
tags
99
# ignore files generated by sdcc:
1010
*.cdb
11+
*.adb
1112
*.ihx
1213
*.asm
1314
*.lst

README.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,59 @@ muss entsprechend angepasst werden. Am Ende hochladen:
4747
stm8flash -c stlinkv2 -p stm8s103?3 -w blinky.ihx
4848

4949

50-
## Infos und Application Notes
50+
## current status and todo list
51+
52+
tested and working:
53+
pinMode()
54+
55+
implemented and partly working:
56+
digitalWrite(): simple write ok, PWM handling untested
57+
58+
tested, but not working:
59+
delay (probl. the timer4 initialisation)
60+
61+
not tested
62+
all time functions
63+
ShiftIn()
64+
ShiftOut()
65+
66+
67+
not implemented:
68+
yield()
69+
analogRead
70+
analogWrite
71+
HardwareSerial
72+
SPI
73+
74+
75+
## Differences to the original Arduino environment
76+
77+
Additinal output pin modes:
78+
OUTPUT outpur, push-pull, slow mode (default)
79+
OUTPUT_OD output, open drain, fast mode
80+
OUTPUT_FAST output, push-pull, fast mode
81+
OUTPUT_OD_FAST output, open drain, fast mode
82+
83+
Timer: millis() uses timer4.
84+
85+
86+
87+
## Further reading and application notes
5188

5289
STM8AF Flash programming manual (PM0051)
5390
STM8 SWIM protocol and debug manual (UM0470)
5491

55-
RS-232-Beispiel:
92+
example for RS-232 handling with SPL:
5693
https://sourceforge.net/p/oggstreamer/oggs-stm8-firmware-001/ci/master/tree/rx_ringbuffer.c
5794

5895

96+
5997
### Anmerkungen zu SDCC
6098

6199
Befehl '_ _ critical{..}' sollte eigentlich den vorherigen Interrupt-Zustand
62100
wiederherstellen, es wird aber einfach ein festes Paar sim/rim produziert.
101+
Mit "push cc; sim" und "pop cc" klappt es im Simulator, aber nicht in der
102+
Realität.
63103

64104
Für jeden benutzten Interrupt __muss__ ein Prototyp in der Datei stehen, in
65105
der auch main() definiert ist. Aber für jeden Prototypen, für den es keine
@@ -178,7 +218,7 @@ generell freigegeben werden, also hier:
178218
UART1_ITConfig(UART1_IT_TXE, ENABLE);
179219
enableInterrupts();
180220

181-
Unklar ist, was die ITC-Priritäten bewirken. Es geht jedenfalls auch ohne:
221+
Unklar ist, was die ITC-Prioritäten bewirken. Es geht jedenfalls auch ohne:
182222

183223
ITC_DeInit();
184224
ITC_SetSoftwarePriority(ITC_IRQ_UART1_TX, ITC_PRIORITYLEVEL_2);

sdunio/Arduino.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ void yield(void);
4444
#define INPUT 0x0
4545
#define OUTPUT 0x1
4646
#define INPUT_PULLUP 0x2
47+
#define OUTPUT_OD 0x03
48+
#define OUTPUT_FAST 0x05
49+
#define OUTPUT_OD_FAST 0x07
4750

4851
// undefine mathlib's pi if encountered
4952
#ifdef PI

sdunio/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ LIBBASE=../STM8S_StdPeriph_Driver
1313
LDFLAGS=-L$(LIBBASE)/src -L/opt/sdcc/share/sdcc/lib/stm8 -lstm8s
1414

1515
OBJECTS=main.rel wiring.rel wiring_digital.rel wiring_shift.rel \
16-
wiring_pulse.rel
16+
wiring_pulse.rel sketch.rel
1717

18-
.PHONY: all clean
18+
.PHONY: all clean flash
1919

2020
#all: $(OBJECTS)
2121

@@ -32,6 +32,10 @@ $(OBJECTS) : %.rel : %.c
3232
.c.rel:
3333
$(CC) -c $(CFLAGS) $^
3434

35+
flash: $(EXECUTABLE)
36+
stm8flash -cstlinkv2 -pstm8s103?3 -w $^
37+
38+
3539
clean:
3640
rm -f *.lib *.rst *.rel *.lst *.ihx *.sym *.asm *.lk *.map \
3741
*.cdb *.adb *~ *.bak

sdunio/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ int main(void)
3636

3737
initVariant();
3838

39-
// setup();
39+
setup();
4040

4141
for (;;) {
42-
// loop();
42+
loop();
4343
//FIXME if (serialEventRun) serialEventRun();
4444
}
4545

sdunio/sketch.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "Arduino.h"
2+
3+
void setup(void)
4+
{
5+
pinMode(LED_BUILTIN, OUTPUT);
6+
}
7+
8+
9+
void loop()
10+
{
11+
u16 i;
12+
13+
digitalWrite(LED_BUILTIN, HIGH);
14+
15+
for(i = 0; i < 30000; i++);
16+
// delay(100);
17+
18+
digitalWrite(LED_BUILTIN, LOW);
19+
20+
for(i = 0; i < 30000; i++);
21+
}

sdunio/wiring.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void delay(unsigned long ms)
9898
uint32_t start = micros();
9999

100100
while (ms > 0) {
101-
// yield();
101+
yield();
102102
while ( ms > 0 && (micros() - start) >= 1000) {
103103
ms--;
104104
start += 1000;
@@ -249,6 +249,11 @@ void delayMicroseconds(unsigned int us)
249249

250250
void init()
251251
{
252+
GPIO_DeInit(GPIOA);
253+
GPIO_DeInit(GPIOB);
254+
GPIO_DeInit(GPIOC);
255+
GPIO_DeInit(GPIOD);
256+
GPIO_DeInit(GPIOE);
252257
// set timer 0 prescale factor to 64, period 0 (=256)
253258
TIM4_DeInit();
254259
TIM4_TimeBaseInit(TIM4_PRESCALER_64, 0);

sdunio/wiring_digital.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const uc_p ccmrx[7]={
4747

4848

4949

50-
50+
/* arduino-style pinMode
5151
void pinMode(uint8_t pin, uint8_t mode)
5252
{
5353
uint8_t bit = digitalPinToBitMask(pin);
@@ -75,6 +75,56 @@ void pinMode(uint8_t pin, uint8_t mode)
7575
END_CRITICAL
7676
}
7777
}
78+
*/
79+
80+
void pinMode(uint8_t pin, uint8_t mode)
81+
{
82+
uint8_t bit = digitalPinToBitMask(pin);
83+
uint8_t port = digitalPinToPort(pin);
84+
volatile GPIO_TypeDef *gpio;
85+
86+
if (port == NOT_A_PIN) return;
87+
88+
gpio = (GPIO_TypeDef *) portOutputRegister(port);
89+
90+
if (mode == INPUT) {
91+
BEGIN_CRITICAL
92+
gpio->CR2 &= ~bit; // first: deactivate interrupt
93+
gpio->CR1 &= ~bit; // release top side
94+
gpio->DDR &= ~bit; // now set direction
95+
END_CRITICAL
96+
} else if (mode == INPUT_PULLUP) {
97+
BEGIN_CRITICAL
98+
gpio->CR2 &= ~bit; // first: deactivate interrupt
99+
gpio->DDR &= ~bit; // set direction before
100+
gpio->CR1 |= bit; // activating the pull up
101+
END_CRITICAL
102+
} else if (mode == OUTPUT_FAST) {// output push-pull, fast
103+
BEGIN_CRITICAL
104+
gpio->CR1 |= bit;
105+
gpio->DDR |= bit; // direction before setting CR2 to
106+
gpio->CR2 |= bit; // avoid accidental interrupt
107+
END_CRITICAL
108+
} else if (mode == OUTPUT_OD_FAST) { // output open drain, fast
109+
BEGIN_CRITICAL
110+
gpio->CR1 &= ~bit;
111+
gpio->DDR |= bit; // direction before setting CR2 to
112+
gpio->CR2 |= bit; // avoid accidental interrupt
113+
END_CRITICAL
114+
} else if (mode == OUTPUT_OD) { // output open drain, slow
115+
BEGIN_CRITICAL
116+
gpio->CR1 &= ~bit;
117+
gpio->CR2 &= ~bit;
118+
gpio->DDR |= bit;
119+
END_CRITICAL
120+
} else { // output push-pull, slow
121+
BEGIN_CRITICAL
122+
gpio->CR1 |= bit;
123+
gpio->CR2 &= ~bit;
124+
gpio->DDR |= bit;
125+
END_CRITICAL
126+
}
127+
}
78128

79129
/* using an array of pointers compiles way more efficient than doing simple
80130
* pointer arithmetics like
@@ -114,7 +164,7 @@ void pinMode(uint8_t pin, uint8_t mode)
114164
*/
115165
static void turnOffPWM(uint8_t timer)
116166
{
117-
*((unsigned char *) ccmrx[timer-TIMER11]) &= ~TIM1_CCMR_OCM; // 0x8f
167+
*((unsigned char *) ccmrx[timer-TIMER11]) &= ~TIM1_CCMR_OCM;
118168
}
119169

120170

sdunio/wiring_private.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131

3232
#include "Arduino.h"
3333

34+
/* --- workarounds and dummy functions ---------------------------------- */
35+
36+
// not implemented yet, ignore:
37+
#define yield(X)
38+
39+
40+
/*
3441
#ifdef __cplusplus
3542
extern "C"{
3643
#endif
@@ -41,12 +48,20 @@ extern "C"{
4148
#ifndef sbi
4249
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
4350
#endif
51+
*/
4452

4553
/* for SDCC this is supposed to be "__critical{" and "}", but up to
4654
* sdcc version 3.6.4 it is wrongly implemented. */
55+
/* so geht es nicht:
4756
#define BEGIN_CRITICAL __asm__("push\tcc");__asm__("sim");
4857
#define END_CRITICAL __asm__("pop\tcc");
49-
58+
*/
59+
#define BEGIN_CRITICAL __critical {
60+
#define END_CRITICAL }
61+
/* klappt:
62+
#define BEGIN_CRITICAL
63+
#define END_CRITICAL
64+
*/
5065

5166
uint32_t countPulseASM(volatile uint8_t *port, uint8_t bit, uint8_t stateMask, unsigned long maxloops);
5267

0 commit comments

Comments
 (0)