Skip to content

Commit f76435e

Browse files
use a serialEventCallback pointer only call serialEventDispatch when acually at least one serialEvent*() handler has been defined
1 parent 86dbdcc commit f76435e

File tree

7 files changed

+56
-35
lines changed

7 files changed

+56
-35
lines changed

cores/stm32l4/CDC.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
stm32l4_usbd_cdc_t stm32l4_usbd_cdc;
3636

37-
CDC::CDC(struct _stm32l4_usbd_cdc_t *usbd_cdc)
37+
CDC::CDC(struct _stm32l4_usbd_cdc_t *usbd_cdc, bool serialEvent)
3838
{
3939
_usbd_cdc = usbd_cdc;
4040

@@ -47,6 +47,9 @@ CDC::CDC(struct _stm32l4_usbd_cdc_t *usbd_cdc)
4747
_completionCallback = NULL;
4848

4949
stm32l4_usbd_cdc_create(usbd_cdc);
50+
51+
if (serialEvent)
52+
serialEventCallback = serialEventDispatch;
5053
}
5154

5255
void CDC::begin(unsigned long baudrate)
@@ -512,6 +515,8 @@ bool CDC::rts() {
512515
return stm32l4_usbd_cdc_info.lineState & 2;
513516
}
514517

515-
CDC SerialUSB(&stm32l4_usbd_cdc);
518+
extern void serialEvent() __attribute__((weak));
516519

517520
bool SerialUSB_empty() { return SerialUSB.empty(); }
521+
522+
CDC SerialUSB(&stm32l4_usbd_cdc, (serialEvent != NULL));

cores/stm32l4/CDC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class CDC : public HardwareSerial
2828
{
2929
public:
30-
CDC(struct _stm32l4_usbd_cdc_t *usbd_cdc);
30+
CDC(struct _stm32l4_usbd_cdc_t *usbd_cdc, bool serialEvent);
3131
void begin(unsigned long baudRate);
3232
void begin(unsigned long baudrate, uint16_t config);
3333
void end(void);

cores/stm32l4/HardwareSerial.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,55 +34,57 @@
3434
void serialEvent() __attribute__((weak));
3535
bool SerialUSB_empty() __attribute__((weak));
3636

37-
void serial1Event() __attribute__((weak));
37+
void serialEvent1() __attribute__((weak));
3838
bool Serial1_empty() __attribute__((weak));
3939

4040
#if SERIAL_INTERFACES_COUNT > 1
4141

42-
void serial2Event() __attribute__((weak));
42+
void serialEvent2() __attribute__((weak));
4343
bool Serial2_empty() __attribute__((weak));
4444

4545
#endif
4646

4747
#if SERIAL_INTERFACES_COUNT > 2
4848

49-
void serial3Event() __attribute__((weak));
49+
void serialEvent3() __attribute__((weak));
5050
bool Serial3_empty() __attribute__((weak));
5151

5252
#endif
5353

5454
#if SERIAL_INTERFACES_COUNT > 3
5555

56-
void serial4Event() __attribute__((weak));
56+
void serialEvent4() __attribute__((weak));
5757
bool Serial4_empty() __attribute__((weak));
5858

5959
#endif
6060

6161
#if SERIAL_INTERFACES_COUNT > 4
6262

63-
void serial5Event() __attribute__((weak));
63+
void serialEvent5() __attribute__((weak));
6464
bool Serial5_empty() __attribute__((weak));
6565

6666
#endif
6767

68-
void serialEventRun(void)
68+
void (*serialEventCallback)(void) = NULL;
69+
70+
void serialEventDispatch(void)
6971
{
7072
if (serialEvent && SerialUSB_empty && !SerialUSB_empty()) { serialEvent(); }
7173

72-
if (serial1Event && Serial1_empty && !Serial1_empty()) { serial1Event(); }
74+
if (serialEvent1 && Serial1_empty && !Serial1_empty()) { serialEvent1(); }
7375
#if SERIAL_INTERFACES_COUNT > 1
74-
if (serial2Event && Serial2_empty && !Serial2_empty()) { serial2Event(); }
76+
if (serialEvent2 && Serial2_empty && !Serial2_empty()) { serialEvent2(); }
7577
#endif
7678
#if SERIAL_INTERFACES_COUNT > 2
77-
if (serial3Event && Serial3_empty && !Serial3_empty()) { serial3Event(); }
79+
if (serialEvent3 && Serial3_empty && !Serial3_empty()) { serialEvent3(); }
7880
#endif
7981
#if SERIAL_INTERFACES_COUNT > 3
80-
if (serial4Event && Serial4_empty && !Serial4_empty()) { serial4Event(); }
82+
if (serialEvent4 && Serial4_empty && !Serial4_empty()) { serialEvent4(); }
8183
#endif
8284
#if SERIAL_INTERFACES_COUNT > 4
83-
if (serial5Event && Serial5_empty && !Serial5_empty()) { serial5Event(); }
85+
if (serialEvent5 && Serial5_empty && !Serial5_empty()) { serialEvent5(); }
8486
#endif
8587
#if SERIAL_INTERFACES_COUNT > 5
86-
if (serial6Event && Serial6_empty && !Serial6_empty()) { serial6Event(); }
88+
if (serialEvent6 && Serial6_empty && !Serial6_empty()) { serialEvent6(); }
8789
#endif
8890
}

cores/stm32l4/HardwareSerial.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class HardwareSerial : public Stream
6868
virtual operator bool() = 0;
6969
};
7070

71-
extern void serialEventRun(void) __attribute__((weak));
71+
extern void (*serialEventCallback)(void);
72+
extern void serialEventDispatch(void);
7273

7374
#endif

cores/stm32l4/Uart.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define UART_TX_BUFFER_MASK ((UART_TX_BUFFER_SIZE<<1)-1)
2929
#define UART_TX_INDEX_MASK (UART_TX_BUFFER_SIZE-1)
3030

31-
Uart::Uart(struct _stm32l4_uart_t *uart, unsigned int instance, const struct _stm32l4_uart_pins_t *pins, unsigned int priority, unsigned int mode)
31+
Uart::Uart(struct _stm32l4_uart_t *uart, unsigned int instance, const struct _stm32l4_uart_pins_t *pins, unsigned int priority, unsigned int mode, bool serialEvent)
3232
{
3333
_uart = uart;
3434

@@ -41,11 +41,14 @@ Uart::Uart(struct _stm32l4_uart_t *uart, unsigned int instance, const struct _st
4141
_completionCallback = NULL;
4242

4343
stm32l4_uart_create(uart, instance, pins, priority, mode);
44+
45+
if (serialEvent)
46+
serialEventCallback = serialEventDispatch;
4447
}
4548

4649
void Uart::begin(unsigned long baudrate)
4750
{
48-
begin(baudrate, (uint8_t)SERIAL_8N1);
51+
begin(baudrate, SERIAL_8N1);
4952
}
5053

5154
void Uart::begin(unsigned long baudrate, uint16_t config)
@@ -476,59 +479,69 @@ void Uart::_event_callback(void *context, uint32_t events)
476479
reinterpret_cast<class Uart*>(context)->EventCallback(events);
477480
}
478481

482+
bool Serial1_empty() { return Serial1.empty(); }
483+
484+
extern void serialEvent1() __attribute__((weak));
485+
479486
static stm32l4_uart_t stm32l4_usart3;
480487

481488
static const stm32l4_uart_pins_t stm32l4_usart3_pins = { GPIO_PIN_PC5_USART3_RX, GPIO_PIN_PC4_USART3_TX, GPIO_PIN_NONE, GPIO_PIN_NONE };
482489

483-
Uart __attribute__((weak)) Serial1(&stm32l4_usart3, UART_INSTANCE_USART3, &stm32l4_usart3_pins, STM32L4_UART_IRQ_PRIORITY, UART_MODE_RX_DMA | UART_MODE_TX_DMA);
484-
485-
bool Serial1_empty() { return Serial1.empty(); }
490+
Uart __attribute__((weak)) Serial1(&stm32l4_usart3, UART_INSTANCE_USART3, &stm32l4_usart3_pins, STM32L4_UART_IRQ_PRIORITY, UART_MODE_RX_DMA | UART_MODE_TX_DMA, (serialEvent1 != NULL));
486491

487492
#if SERIAL_INTERFACES_COUNT > 1
488493

494+
bool Serial2_empty() { return Serial2.empty(); }
495+
496+
extern void serialEvent2() __attribute__((weak));
497+
489498
static stm32l4_uart_t stm32l4_uart4;
490499

491500
static const stm32l4_uart_pins_t stm32l4_uart4_pins = { GPIO_PIN_PA1_UART4_RX, GPIO_PIN_PA0_UART4_TX, GPIO_PIN_NONE, GPIO_PIN_NONE };
492501

493-
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);
494-
495-
bool Serial2_empty() { return Serial2.empty(); }
502+
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, (serialEvent2 != NULL));
496503

497504
#endif
498505

499506
#if SERIAL_INTERFACES_COUNT > 2
500507

508+
bool Serial3_empty() { return Serial3.empty(); }
509+
510+
extern void serialEvent3() __attribute__((weak));
511+
501512
static stm32l4_uart_t stm32l4_usart2;
502513

503514
static const stm32l4_uart_pins_t stm32l4_usart2_pins = { GPIO_PIN_PA3_USART2_RX, GPIO_PIN_PA2_USART2_TX, GPIO_PIN_NONE, GPIO_PIN_NONE };
504515

505-
Uart __attribute__((weak)) Serial3(&stm32l4_usart2, UART_INSTANCE_USART2, &stm32l4_usart2_pins, STM32L4_UART_IRQ_PRIORITY, 0);
506-
507-
bool Serial3_empty() { return Serial3.empty(); }
516+
Uart __attribute__((weak)) Serial3(&stm32l4_usart2, UART_INSTANCE_USART2, &stm32l4_usart2_pins, STM32L4_UART_IRQ_PRIORITY, 0, (serialEvent3 != NULL));
508517

509518
#endif
510519

511520
#if SERIAL_INTERFACES_COUNT > 3
512521

522+
bool Serial4_empty() { return Serial4.empty(); }
523+
524+
extern void serialEvent4() __attribute__((weak));
525+
513526
static stm32l4_uart_t stm32l4_uart5;
514527

515528
static const stm32l4_uart_pins_t stm32l4_uart5_pins = { GPIO_PIN_PD2_UART5_RX, GPIO_PIN_PC12_UART5_TX, GPIO_PIN_NONE, GPIO_PIN_NONE };
516529

517-
Uart __attribute__((weak)) Serial4(&stm32l4_uart5, UART_INSTANCE_UART5, &stm32l4_uart5_pins, STM32L4_UART_IRQ_PRIORITY, 0);
518-
519-
bool Serial4_empty() { return Serial4.empty(); }
530+
Uart __attribute__((weak)) Serial4(&stm32l4_uart5, UART_INSTANCE_UART5, &stm32l4_uart5_pins, STM32L4_UART_IRQ_PRIORITY, 0, (serialEvent4 != NULL));
520531

521532
#endif
522533

523534
#if SERIAL_INTERFACES_COUNT > 4
524535

536+
bool Serial5_empty() { return Serial5.empty(); }
537+
538+
extern void serialEvent5() __attribute__((weak));
539+
525540
static stm32l4_uart_t stm32l4_usart1;
526541

527542
static const stm32l4_uart_pins_t stm32l4_usart1_pins = { GPIO_PIN_PB7_USART1_RX, GPIO_PIN_PB6_USART1_TX, GPIO_PIN_NONE, GPIO_PIN_NONE };
528543

529-
Uart __attribute__((weak)) Serial5(&stm32l4_usart1, UART_INSTANCE_USART1, &stm32l4_usart1_pins, STM32L4_UART_IRQ_PRIORITY, 0);
530-
531-
bool Serial5_empty() { return Serial5.empty(); }
544+
Uart __attribute__((weak)) Serial5(&stm32l4_usart1, UART_INSTANCE_USART1, &stm32l4_usart1_pins, STM32L4_UART_IRQ_PRIORITY, 0, (serialEvent5 != NULL));
532545

533546
#endif
534547

cores/stm32l4/Uart.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class Uart : public HardwareSerial
2727
{
2828
public:
29-
Uart(struct _stm32l4_uart_t *uart, unsigned int instance, const struct _stm32l4_uart_pins_t *pins, unsigned int priority, unsigned int mode);
29+
Uart(struct _stm32l4_uart_t *uart, unsigned int instance, const struct _stm32l4_uart_pins_t *pins, unsigned int priority, unsigned int mode, bool serialEvent);
3030
void begin(unsigned long baudRate);
3131
void begin(unsigned long baudrate, uint16_t config);
3232
void end();

cores/stm32l4/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int main( void )
4040
for (;;)
4141
{
4242
loop();
43-
if (serialEventRun) serialEventRun();
43+
if (serialEventCallback) (*serialEventCallback)();
4444
}
4545

4646
return 0;

0 commit comments

Comments
 (0)