Skip to content

Commit f6c34a2

Browse files
committed
SerialBase and CAN: fix Callbacks comparision
As Callback currently does not have fully functional comparision (see #5017), we workaround by doing null check.
1 parent 39f31c3 commit f6c34a2

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

drivers/CAN.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222

2323
namespace mbed {
2424

25-
static void donothing() {}
26-
2725
CAN::CAN(PinName rd, PinName td) : _can(), _irq() {
2826
// No lock needed in constructor
2927

3028
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
31-
_irq[i] = callback(donothing);
29+
_irq[i] = NULL;
3230
}
3331

3432
can_init(&_can, rd, td);
@@ -39,7 +37,7 @@ CAN::CAN(PinName rd, PinName td, int hz) : _can(), _irq() {
3937
// No lock needed in constructor
4038

4139
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
42-
_irq[i] = callback(donothing);
40+
_irq[i] = NULL;
4341
}
4442

4543
can_init_freq(&_can, rd, td, hz);
@@ -117,25 +115,27 @@ void CAN::attach(Callback<void()> func, IrqType type) {
117115
lock();
118116
if (func) {
119117
// lock deep sleep only the first time
120-
if (_irq[(CanIrqType)type] == callback(donothing)) {
118+
if (!_irq[(CanIrqType)type]) {
121119
sleep_manager_lock_deep_sleep();
122120
}
123121
_irq[(CanIrqType)type] = func;
124122
can_irq_set(&_can, (CanIrqType)type, 1);
125123
} else {
126124
// unlock deep sleep only the first time
127-
if (_irq[(CanIrqType)type] != callback(donothing)) {
125+
if (_irq[(CanIrqType)type]) {
128126
sleep_manager_unlock_deep_sleep();
129127
}
130-
_irq[(CanIrqType)type] = callback(donothing);
128+
_irq[(CanIrqType)type] = NULL;
131129
can_irq_set(&_can, (CanIrqType)type, 0);
132130
}
133131
unlock();
134132
}
135133

136134
void CAN::_irq_handler(uint32_t id, CanIrqType type) {
137135
CAN *handler = (CAN*)id;
138-
handler->_irq[type].call();
136+
if (handler->_irq[type]) {
137+
handler->_irq[type].call();
138+
}
139139
}
140140

141141
void CAN::lock() {

drivers/SerialBase.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,17 @@
2222

2323
namespace mbed {
2424

25-
static void donothing() {};
26-
static void donothing2(int arg) {};
27-
28-
2925
SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
3026
#if DEVICE_SERIAL_ASYNCH
3127
_thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
32-
_rx_usage(DMA_USAGE_NEVER), _tx_callback(donothing2),
33-
_rx_callback(donothing2),
28+
_rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL),
29+
_rx_callback(NULL),
3430
#endif
3531
_serial(), _baud(baud) {
3632
// No lock needed in the constructor
3733

3834
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
39-
_irq[i] = donothing;
35+
_irq[i] = NULL;
4036
}
4137

4238
serial_init(&_serial, tx, rx);
@@ -78,17 +74,17 @@ void SerialBase::attach(Callback<void()> func, IrqType type) {
7874
core_util_critical_section_enter();
7975
if (func) {
8076
// lock deep sleep only the first time
81-
if (_irq[type] == donothing) {
77+
if (!_irq[type]) {
8278
sleep_manager_lock_deep_sleep();
8379
}
8480
_irq[type] = func;
8581
serial_irq_set(&_serial, (SerialIrq)type, 1);
8682
} else {
8783
// unlock deep sleep only the first time
88-
if (_irq[type] != donothing) {
84+
if (_irq[type]) {
8985
sleep_manager_unlock_deep_sleep();
9086
}
91-
_irq[type] = donothing;
87+
_irq[type] = NULL;
9288
serial_irq_set(&_serial, (SerialIrq)type, 0);
9389
}
9490
core_util_critical_section_exit();
@@ -97,7 +93,9 @@ void SerialBase::attach(Callback<void()> func, IrqType type) {
9793

9894
void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) {
9995
SerialBase *handler = (SerialBase*)id;
100-
handler->_irq[irq_type]();
96+
if (handler->_irq[irq_type]) {
97+
handler->_irq[irq_type]();
98+
}
10199
}
102100

103101
int SerialBase::_base_getc() {
@@ -192,20 +190,20 @@ void SerialBase::start_write(const void *buffer, int buffer_size, char buffer_wi
192190
void SerialBase::abort_write(void)
193191
{
194192
// rx might still be active
195-
if (_rx_callback == &donothing2) {
193+
if (_rx_callback) {
196194
sleep_manager_unlock_deep_sleep();
197195
}
198-
_tx_callback = donothing2;
196+
_tx_callback = NULL;
199197
serial_tx_abort_asynch(&_serial);
200198
}
201199

202200
void SerialBase::abort_read(void)
203201
{
204202
// tx might still be active
205-
if (_tx_callback == &donothing2) {
203+
if (_tx_callback) {
206204
sleep_manager_unlock_deep_sleep();
207205
}
208-
_rx_callback = donothing2;
206+
_rx_callback = NULL;
209207
serial_rx_abort_asynch(&_serial);
210208
}
211209

0 commit comments

Comments
 (0)