10000 subscribe and unsubscribe consistent · esp8266/Arduino@5bc2ea5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bc2ea5

Browse files
committed
subscribe and unsubscribe consistent
Since subscribe can not be automatically disable and enable interrupts due to other external uart setup must be done at the same time within the disabled interrupts, then unsubscribe should be made consistent
1 parent 2e9a1c0 commit 5bc2ea5

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

cores/esp8266/uart.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ struct uart_
7272
struct uart_rx_buffer_ * rx_buffer;
7373
};
7474

75-
struct uartIntContext_t
75+
struct uartIsrContext_t
7676
{
7777
uartInterruptHandler callback;
7878
void* arg;
7979
};
8080

81-
static volatile struct uartIntContext_t s_uartInterruptContext[2];
81+
// NOTE: GCC will generated an invalid warning for the following line
82+
// see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
83+
static volatile struct uartIsrContext_t s_uartInterruptContext[2] = { 0 };
8284

8385
/*
8486
In the context of the naming conventions in this file, "_unsafe" means two things:
@@ -91,16 +93,13 @@ static volatile struct uartIntContext_t s_uartInterruptContext[2];
9193
wrap the unsafe ones with disabling/enabling of the uart interrupt for safe public use.
9294
*/
9395

94-
95-
9696
// called by ISR
9797
inline size_t ICACHE_RAM_ATTR
9898
uart_rx_fifo_available(const int uart_nr)
9999
{
100100
return (USS(uart_nr) >> USRXC) & 0xFF;
101101
}
102102

103-
104103
/**********************************************************/
105104
/************ UNSAFE FUNCTIONS ****************************/
106105
/**********************************************************/
@@ -312,7 +311,8 @@ uart_isr(void* arg)
312311
}
313312
else
314313
{
315-
// Clear all interrupts flags (just in case)
314+
// No registered handler, so
315+
// clear all interrupts flags (just in case)
316316
USIE(uartNum) = 0;
317317
USIC(uartNum) = 0xffff;
318318
}
@@ -355,11 +355,9 @@ uart_subscribeInterrupt(int uart_nr, uartInterruptHandler callback, void* param)
355355
}
356356
}
357357

358-
void
358+
bool
359359
uart_unsubscribeInterrupt(int uart_nr, uartInterruptHandler callback)
360360
{
361-
ETS_UART_INTR_DISABLE();
362-
363361
if (s_uartInterruptContext[uart_nr].callback == callback)
364362
{
365363
// turn off uart
@@ -376,12 +374,13 @@ uart_unsubscribeInterrupt(int uart_nr, uartInterruptHandler callback)
376374
// detach our ISR
377375
ETS_UART_INTR_ATTACH(NULL, NULL);
378376

379-
// return so we don't enable interrupts since there is no ISR anymore
380-
return;
377+
// return false so we don't enable interrupts
378+
// since there is no need for the ISR anymore
379+
return false;
381380
}
382381
}
383382

384-
ETS_UART_INTR_ENABLE();
383+
return true;
385384
}
386385

387386

@@ -485,7 +484,6 @@ uart_wait_tx_empty(uart_t* uart)
485484

486485
while (uart_tx_fifo_available(uart->uart_nr) > 0)
487486
delay(0);
488-
489487
}
490488

491489
void
@@ -652,7 +650,11 @@ uart_uninit(uart_t* uart)
652650

653651
if (uart->rx_enabled)
654652
{
655-
uart_unsubscribeInterrupt(uart->uart_nr, uart_isrDefault);
653+
ETS_UART_INTR_DISABLE();
654+
if (uart_unsubscribeInterrupt(uart->uart_nr, uart_isrDefault))
655+
{
656+
ETS_UART_INTR_ENABLE();
657+
}
656658
free(uart->rx_buffer->buffer);
657659
free(uart->rx_buffer);
658660
}

cores/esp8266/uart.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ extern "C" {
149149
void uart_start_detect_baudrate(int uart_nr);
150150
int uart_detect_baudrate(int uart_nr);
151151

152-
// attach MUST be called within a
153-
// ETS_UART_INTR_DISABLE()/ETS_UART_INTR_ENABLE() sandwich
152+
// uart_subscribeInterrupt & uart_unsubscribeInterrupt are not safe and must
153+
// be called within ETS_UART_INTR_DISABLE()/ETS_UART_INTR_ENABLE() protection
154+
//
154155
void uart_subscribeInterrupt(int uart_nr, uartInterruptHandler callback, void* param);
155-
void uart_unsubscribeInterrupt(int uart_nr, uartInterruptHandler callback);
156+
// if uart_unsubscribeInterrupt returns false, then ETS_UART_INTR_ENABLE() doesn't
157+
// need to be called after it
158+
bool uart_unsubscribeInterrupt(int uart_nr, uartInterruptHandler callback);
156159

157160
#if defined (__cplusplus)
158161
} // extern "C"

0 commit comments

Comments
 (0)
0