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
296301int HardwareSerial_available (void )
@@ -301,6 +306,7 @@ int HardwareSerial_available(void)
301306int 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}
0 commit comments