Skip to content

Commit abde8b0

Browse files
committed
fix tenbaht#6: implement serialEvent() as a 'weak' function
1 parent 75a36ec commit abde8b0

10 files changed

Lines changed: 100 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88
### Added
9+
- support for serialEvent()
10+
911
### Changed
1012
- using mkdocs for building the project website
13+
1114
### Fixed
1215
- changed method names for printing floats to xxx_print_f and xxx_print_fd
1316

17+
18+
1419
## [0.2.0 - 2017-03-01]
1520
### Added
1621
- example code for SR-HC04 ultrasonic range finder module
@@ -32,6 +37,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3237
### Fixed
3338
- corrected duplicate mapping of PWM-T21 for STM8S003/STM8S103
3439

40+
41+
3542
## [0.1.0 - 2016-02-20]
3643
### Added
3744
- adopted more examples from arduino-1.8

sduino/Arduino.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,9 @@ $(TARGET_ELF): $(LOCAL_OBJS) $(CORE_LIB) $(OTHER_OBJS)
15001500
$(CC) $(LDFLAGS) -o $@ $(LOCAL_OBJS) $(CORE_LIB) $(OTHER_OBJS) $(OTHER_LIBS) -lc -lm $(LINKER_SCRIPTS)
15011501

15021502
$(CORE_LIB): $(CORE_OBJS) $(LIB_OBJS) $(PLATFORM_LIB_OBJS) $(USER_LIB_OBJS)
1503-
$(AR) rcs $@ $(CORE_OBJS) $(LIB_OBJS) $(PLATFORM_LIB_OBJS) $(USER_LIB_OBJS)
1503+
$(AR) rcs $@ \
1504+
$(filter-out %/core/main.c.$(OBJSUFFIX),$(CORE_OBJS)) \
1505+
$(LIB_OBJS) $(PLATFORM_LIB_OBJS) $(USER_LIB_OBJS)
15041506

15051507
error_on_caterina:
15061508
$(ERROR_ON_CATERINA)

sduino/hardware/sduino/stm8/cores/sduino/Arduino.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ typedef unsigned char byte;
171171
//typedef uint8_t byte;
172172

173173
void init(void);
174-
void initVariant(void);
174+
void initVariant(void); // weak
175175

176-
//int atexit(void (*func)()) __attribute__((weak));
176+
int atexit(void (*func)()); // __attribute__((weak));
177+
void serialEvent(void); // weak
178+
extern unsigned char runSerialEvent;
177179

178180
void pinMode(uint8_t pin, uint8_t mode);
179181
void digitalWrite(uint8_t pin, uint8_t val);

sduino/hardware/sduino/stm8/cores/sduino/HardwareSerial.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include"stm8s.h"
3030

31+
#include "Arduino.h"
3132
#include "HardwareSerial.h"
3233

3334

@@ -147,6 +148,7 @@ static void store_char(unsigned char c, ring_buffer *buffer)
147148
if (i != buffer->tail) {
148149
buffer->buffer[buffer->head] = c;
149150
buffer->head = i;
151+
runSerialEvent = 1;
150152
}
151153
}
152154

@@ -237,6 +239,7 @@ void HardwareSerial_begin(unsigned long baud)
237239
UARTx->CR2 = UARTx_CR2_RIEN | UARTx_CR2_TEN | UARTx_CR2_REN;
238240
#endif
239241
initialized = 1;
242+
runSerialEvent = 0;
240243
}
241244

242245

@@ -277,6 +280,7 @@ void HardwareSerial_begin_config(unsigned long baud, uint8_t config)
277280
UARTx->CR2 = UARTx_CR2_RIEN | UARTx_CR2_TEN | UARTx_CR2_REN;
278281
#endif
279282
initialized = 1;
283+
runSerialEvent = 0;
280284
}
281285

282286

@@ -291,6 +295,7 @@ void HardwareSerial_end(void)
291295
// clear any received data
292296
rx_buffer.head = rx_buffer.tail;
293297
initialized = 0;
298+
runSerialEvent = 0;
294299
}
295300

296301
int HardwareSerial_available(void)
@@ -301,6 +306,7 @@ int HardwareSerial_available(void)
301306
int HardwareSerial_peek(void)
302307
{
303308
if (rx_buffer.head == rx_buffer.tail) {
309+
// if (!runSerialEvent) {
304310
return -1;
305311
} else {
306312
return rx_buffer.buffer[rx_buffer.tail];
@@ -311,10 +317,12 @@ int HardwareSerial_read(void)
311317
{
312318
// if the head isn't ahead of the tail, we don't have any characters
313319
if (rx_buffer.head == rx_buffer.tail) {
320+
// if (!runSerialEvent) {
314321
return -1;
315322
} else {
316323
unsigned char c = rx_buffer.buffer[rx_buffer.tail];
317324
rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
325+
runSerialEvent = (rx_buffer.head != rx_buffer.tail);
318326
return c;
319327
}
320328
}

sduino/hardware/sduino/stm8/cores/sduino/main.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
// make sure to define prototypes for all used interrupts
2323
//#include "stm8s_it.h"
2424

25-
// Declared weak in Arduino.h to allow user redefinitions.
26-
int atexit(void (*func)()) { return 0; }
27-
28-
// Weak empty variant initialization function.
29-
// May be redefined by variant files.
30-
//void initVariant() __attribute__((weak));
31-
void initVariant() { }
25+
unsigned char runSerialEvent;
3226

3327
int main(void)
3428
{
@@ -40,7 +34,7 @@ int main(void)
4034

4135
for (;;) {
4236
loop();
43-
//FIXME if (serialEventRun) serialEventRun();
37+
if (runSerialEvent) serialEvent();
4438
}
4539

4640
// return 0;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* "weak" functions to be used if no user-defined function is defined.
3+
*
4+
* This function stub is compiled into the core library. Functions from
5+
* libraries are only linked if a referenced function isnt't defined any other
6+
* object file. This way all library function are a kind of "weak" functions.
7+
*
8+
* This function is defined in a separate source file to avoid pulling in
9+
* other unrelated definitions.
10+
*/
11+
12+
// Declared weak in Arduino.h to allow user redefinitions.
13+
int atexit(void (*func)()) { return 0; }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* "weak" functions to be used if no user-defined function is defined.
3+
*
4+
* This function stub is compiled into the core library. Functions from
5+
* libraries are only linked if a referenced function isnt't defined any other
6+
* object file. This way all library function are a kind of "weak" functions.
7+
*
8+
* This function is defined in a separate source file to avoid pulling in
9+
* other unrelated definitions.
10+
*/
11+
12+
// Weak empty variant initialization function.
13+
// May be redefined by variant files.
14+
void initVariant() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//#include "HardwareSerialEvent.h"
2+
3+
/**
4+
* "weak" functions to be used if no user-defined function is defined.
5+
*
6+
* This function stub is compiled into the core library. Functions from
7+
* libraries are only linked if a referenced function isnt't defined any other
8+
* object file. This way all library function are a kind of "weak" functions.
9+
*
10+
* This function is defined in a separate source file to avoid pulling in all
11+
* the rest of HardwareSerial, even if no serial functions are used by the
12+
* sketch.
13+
*/
14+
15+
//unsigned char runSerialEvent;
16+
void serialEvent(void) {}

test/serialEvent/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../sduino/sduino.mk

test/serialEvent/serialEvent.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* test serialEvent()
3+
*
4+
* Read data from the serial interface an echo it back using the serialEvent()
5+
* function.
6+
*
7+
* written Sep. 2017 by Michael Mayer for the SDuino project
8+
* Code is in the public domain.
9+
*/
10+
11+
#include "Arduino.h"
12+
13+
14+
void setup(void)
15+
{
16+
Serial_begin(9600);
17+
Serial_println_s("Hello World!");
18+
}
19+
20+
21+
void loop (void)
22+
{
23+
}
24+
25+
void serialEvent(void)
26+
{
27+
while(HardwareSerial_available()) {
28+
Serial_write(Serial_read());
29+
};
30+
}

0 commit comments

Comments
 (0)