diff --git a/components/freertos/port/esp8266/include/freertos/portmacro.h b/components/freertos/port/esp8266/include/freertos/portmacro.h index 27ea1f7f..d04b4558 100644 --- a/components/freertos/port/esp8266/include/freertos/portmacro.h +++ b/components/freertos/port/esp8266/include/freertos/portmacro.h @@ -224,6 +224,13 @@ void esp_mem_trace(const void *ptr, const char *trace, int no); */ #define esp_mem_mark_file(ptr) esp_mem_trace((ptr), __FILE__, LINE__) +/* + * @brief check if CPU core interrupt is disable + * + * @return true if interrupt is disable or false + */ +bool interrupt_is_disable(void); + #ifdef __cplusplus } #endif diff --git a/components/freertos/port/esp8266/port.c b/components/freertos/port/esp8266/port.c index aa3c5e1b..5aa58680 100644 --- a/components/freertos/port/esp8266/port.c +++ b/components/freertos/port/esp8266/port.c @@ -300,6 +300,20 @@ void ResetCcountVal(unsigned int cnt_val) asm volatile("wsr a2, ccount"); } +/* + * @brief check if CPU core interrupt is disable + */ +bool interrupt_is_disable(void) +{ + uint32_t tmp; + + __asm__ __volatile__ ( + "rsr %0, PS\n" + : "=a"(tmp) : : "memory"); + + return tmp & 0xFUL ? true : false; +} + _xt_isr_entry isr[16]; char _xt_isr_status = 0; diff --git a/components/newlib/newlib/port/locks.c b/components/newlib/newlib/port/locks.c index 83a55104..1c709fd5 100644 --- a/components/newlib/newlib/port/locks.c +++ b/components/newlib/newlib/port/locks.c @@ -112,6 +112,10 @@ void _lock_close_recursive(_lock_t *lock) { mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX */ static int lock_acquire_generic(_lock_t *lock, uint32_t delay, uint8_t mutex_type) { + /* If application function has disabled interrupt, then it must not acquire the mutex */ + if (interrupt_is_disable() == true && !xPortInIsrContext()) + return 0; + xSemaphoreHandle h = (xSemaphoreHandle)(*lock); if (!h) { if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) { @@ -171,6 +175,10 @@ int _lock_try_acquire_recursive(_lock_t *lock) { mutex_type is queueQUEUE_TYPE_RECURSIVE_MUTEX or queueQUEUE_TYPE_MUTEX */ static void lock_release_generic(_lock_t *lock, uint8_t mutex_type) { + /* If application function has disabled interrupt, then it must not release the mutex */ + if (interrupt_is_disable() == true && !xPortInIsrContext()) + return ; + xSemaphoreHandle h = (xSemaphoreHandle)(*lock); if (h == NULL) { /* This is probably because the scheduler isn't running yet,