Skip to content

Commit 5fcafe9

Browse files
complete serialEventRun logic
1 parent 093cdf2 commit 5fcafe9

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed

cores/stm32l4/CDC.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#define CDC_TX_BUFFER_MASK ((CDC_TX_BUFFER_SIZE<<1)-1)
3333
#define CDC_TX_INDEX_MASK (CDC_TX_BUFFER_SIZE-1)
3434

35-
static stm32l4_usbd_cdc_t stm32l4_usbd_cdc;
35+
stm32l4_usbd_cdc_t stm32l4_usbd_cdc;
3636

3737
CDC::CDC(struct _stm32l4_usbd_cdc_t *usbd_cdc)
3838
{
@@ -56,7 +56,13 @@ void CDC::begin(unsigned long baudrate)
5656

5757
void CDC::begin(unsigned long baudrate, uint16_t config)
5858
{
59-
stm32l4_usbd_cdc_enable(_usbd_cdc, 0, CDC::_event_callback, (void*)this, (USBD_CDC_EVENT_RECEIVE | USBD_CDC_EVENT_TRANSMIT));
59+
/* If USBD_CDC has already been enabled/initialized by STDIO, just add the notify.
60+
*/
61+
if (_usbd_cdc->state == USBD_CDC_STATE_INIT) {
62+
stm32l4_usbd_cdc_enable(_usbd_cdc, 0, CDC::_event_callback, (void*)this, (USBD_CDC_EVENT_RECEIVE | USBD_CDC_EVENT_TRANSMIT));
63+
} else {
64+
stm32l4_usbd_cdc_notify(_usbd_cdc, CDC::_event_callback, (void*)this, (USBD_CDC_EVENT_RECEIVE | USBD_CDC_EVENT_TRANSMIT));
65+
}
6066
}
6167

6268
void CDC::end()
@@ -507,3 +513,5 @@ bool CDC::rts() {
507513
}
508514

509515
CDC SerialUSB(&stm32l4_usbd_cdc);
516+
517+
bool SerialUSB_empty() { return SerialUSB.empty(); }

cores/stm32l4/CDC.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class CDC : public HardwareSerial
5151
// STM32L4 EXTENSTION: asynchronous receive callback
5252
void onReceive(void(*callback)(int));
5353

54+
// STM32L4 EXTENSTION: quick check for empty
55+
inline int empty() { return (_rx_read == _rx_write); };
56+
5457
operator bool();
5558

5659
// These return the settings specified by the USB host for the

cores/stm32l4/HardwareSerial.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
HardwareSerial.cpp - Hardware serial library for Wiring
3+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
Modified 23 November 2006 by David A. Mellis
20+
Modified 28 September 2010 by Mark Sproul
21+
Modified 14 August 2012 by Alarus
22+
Modified 3 December 2013 by Matthijs Kooijman
23+
*/
24+
25+
#include "Arduino.h"
26+
#include "HardwareSerial.h"
27+
28+
// SerialEvent functions are weak, so when the user doesn't define them,
29+
// the linker just sets their address to 0 (which is checked below).
30+
// The Serialx_available is just a wrapper around Serialx.available(),
31+
// but we can refer to it weakly so we don't pull in the entire
32+
// HardwareSerial instance if the user doesn't also refer to it.
33+
34+
void serialEvent() __attribute__((weak));
35+
bool SerialUSB_empty() __attribute__((weak));
36+
37+
void serial1Event() __attribute__((weak));
38+
bool Serial1_empty() __attribute__((weak));
39+
40+
#if SERIAL_INTERFACES_COUNT > 1
41+
42+
void serial2Event() __attribute__((weak));
43+
bool Serial2_empty() __attribute__((weak));
44+
45+
#endif
46+
47+
#if SERIAL_INTERFACES_COUNT > 2
48+
49+
void serial3Event() __attribute__((weak));
50+
bool Serial3_empty() __attribute__((weak));
51+
52+
#endif
53+
54+
#if SERIAL_INTERFACES_COUNT > 3
55+
56+
void serial4Event() __attribute__((weak));
57+
bool Serial4_empty() __attribute__((weak));
58+
59+
#endif
60+
61+
#if SERIAL_INTERFACES_COUNT > 4
62+
63+
void serial5Event() __attribute__((weak));
64+
bool Serial5_empty() __attribute__((weak));
65+
66+
#endif
67+
68+
void serialEventRun(void)
69+
{
70+
if (serialEvent && SerialUSB_empty && !SerialUSB_empty()) { serialEvent(); }
71+
72+
if (serial1Event && Serial1_empty && !Serial1_empty()) { serial1Event(); }
73+
#if SERIAL_INTERFACES_COUNT > 1
74+
if (serial2Event && Serial2_empty && !Serial2_empty()) { serial2Event(); }
75+
#endif
76+
#if SERIAL_INTERFACES_COUNT > 2
77+
if (serial3Event && Serial3_empty && !Serial3_empty()) { serial3Event(); }
78+
#endif
79+
#if SERIAL_INTERFACES_COUNT > 3
80+
if (serial4Event && Serial4_empty && !Serial4_empty()) { serial4Event(); }
81+
#endif
82+
#if SERIAL_INTERFACES_COUNT > 4
83+
if (serial5Event && Serial5_empty && !Serial5_empty()) { serial5Event(); }
84+
#endif
85+
#if SERIAL_INTERFACES_COUNT > 5
86+
if (serial6Event && Serial6_empty && !Serial6_empty()) { serial6Event(); }
87+
#endif
88+
}

cores/stm32l4/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SRCS = \
2121
../../variants/dragonfly/variant.cpp \
2222
CDC.cpp \
2323
FS.cpp \
24+
HardwareSerial.cpp \
2425
IPAddress.cpp \
2526
Print.cpp \
2627
Reset.cpp \
@@ -50,6 +51,7 @@ OBJS = \
5051
../../variants/dragonfly/variant.o \
5152
CDC.o \
5253
FS.o \
54+
HardwareSerial.o \
5355
IPAddress.o \
5456
Print.o \
5557
Reset.o \

cores/stm32l4/Uart.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ static const stm32l4_uart_pins_t stm32l4_usart3_pins = { GPIO_PIN_PC5_USART3_RX,
482482

483483
Uart __attribute__((weak)) Serial1(&stm32l4_usart3, UART_INSTANCE_USART3, &stm32l4_usart3_pins, STM32L4_UART_IRQ_PRIORITY, UART_MODE_RX_DMA | UART_MODE_TX_DMA);
484484

485+
bool Serial1_empty() { return Serial1.empty(); }
486+
485487
#if SERIAL_INTERFACES_COUNT > 1
486488

487489
static stm32l4_uart_t stm32l4_uart4;
@@ -490,6 +492,8 @@ static const stm32l4_uart_pins_t stm32l4_uart4_pins = { GPIO_PIN_PA1_UART4_RX, G
490492

491493
Uart __attribute__((weak)) Serial2(&stm32l4_uart4, UART_INSTANCE_UART4, &stm32l4_uart4_pins, STM32L4_UART_IRQ_PRIORITY, UART_MODE_RX_DMA | UART_MODE_RX_DMA_SECONDARY);
492494

495+
bool Serial2_empty() { return Serial2.empty(); }
496+
493497
#endif
494498

495499
#if SERIAL_INTERFACES_COUNT > 2
@@ -500,6 +504,8 @@ static const stm32l4_uart_pins_t stm32l4_usart2_pins = { GPIO_PIN_PA3_USART2_RX,
500504

501505
Uart __attribute__((weak)) Serial3(&stm32l4_usart2, UART_INSTANCE_USART2, &stm32l4_usart2_pins, STM32L4_UART_IRQ_PRIORITY, 0);
502506

507+
bool Serial3_empty() { return Serial3.empty(); }
508+
503509
#endif
504510

505511
#if SERIAL_INTERFACES_COUNT > 3
@@ -510,6 +516,8 @@ static const stm32l4_uart_pins_t stm32l4_uart5_pins = { GPIO_PIN_PD2_UART5_RX, G
510516

511517
Uart __attribute__((weak)) Serial4(&stm32l4_uart5, UART_INSTANCE_UART5, &stm32l4_uart5_pins, STM32L4_UART_IRQ_PRIORITY, 0);
512518

519+
bool Serial4_empty() { return Serial4.empty(); }
520+
513521
#endif
514522

515523
#if SERIAL_INTERFACES_COUNT > 4
@@ -520,5 +528,7 @@ static const stm32l4_uart_pins_t stm32l4_usart1_pins = { GPIO_PIN_PB7_USART1_RX,
520528

521529
Uart __attribute__((weak)) Serial5(&stm32l4_usart1, UART_INSTANCE_USART1, &stm32l4_usart1_pins, STM32L4_UART_IRQ_PRIORITY, 0);
522530

531+
bool Serial5_empty() { return Serial5.empty(); }
532+
523533
#endif
524534

cores/stm32l4/Uart.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class Uart : public HardwareSerial
4949
// STM32L4 EXTENSTION: asynchronous receive callback
5050
void onReceive(void(*callback)(int));
5151

52+
// STM32L4 EXTENSTION: quick check for empty
53+
inline int empty() { return (_rx_read == _rx_write); };
54+
5255
operator bool() { return true; }
5356

5457
private:

0 commit comments

Comments
 (0)