Merge branch 'feature/amazon_freertos_compat_v2' into 'master'

Changes for Amazon Freertos compatibility

See merge request idf/esp-idf!2123
This commit is contained in:
Ivan Grokhotkov 2018-04-20 14:55:47 +08:00
commit 22fbcd276c
14 changed files with 71 additions and 28 deletions

View File

@ -438,7 +438,7 @@ bool IRAM_ATTR spicommon_dmaworkaround_req_reset(int dmachan, dmaworkaround_cb_t
{ {
int otherchan = (dmachan == 1) ? 2 : 1; int otherchan = (dmachan == 1) ? 2 : 1;
bool ret; bool ret;
portENTER_CRITICAL(&dmaworkaround_mux); portENTER_CRITICAL_ISR(&dmaworkaround_mux);
if (dmaworkaround_channels_busy[otherchan-1]) { if (dmaworkaround_channels_busy[otherchan-1]) {
//Other channel is busy. Call back when it's done. //Other channel is busy. Call back when it's done.
dmaworkaround_cb = cb; dmaworkaround_cb = cb;
@ -450,7 +450,7 @@ bool IRAM_ATTR spicommon_dmaworkaround_req_reset(int dmachan, dmaworkaround_cb_t
periph_module_reset( PERIPH_SPI_DMA_MODULE ); periph_module_reset( PERIPH_SPI_DMA_MODULE );
ret = true; ret = true;
} }
portEXIT_CRITICAL(&dmaworkaround_mux); portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
return ret; return ret;
} }
@ -461,7 +461,7 @@ bool IRAM_ATTR spicommon_dmaworkaround_reset_in_progress()
void IRAM_ATTR spicommon_dmaworkaround_idle(int dmachan) void IRAM_ATTR spicommon_dmaworkaround_idle(int dmachan)
{ {
portENTER_CRITICAL(&dmaworkaround_mux); portENTER_CRITICAL_ISR(&dmaworkaround_mux);
dmaworkaround_channels_busy[dmachan-1] = 0; dmaworkaround_channels_busy[dmachan-1] = 0;
if (dmaworkaround_waiting_for_chan == dmachan) { if (dmaworkaround_waiting_for_chan == dmachan) {
//Reset DMA //Reset DMA
@ -471,14 +471,14 @@ void IRAM_ATTR spicommon_dmaworkaround_idle(int dmachan)
dmaworkaround_cb(dmaworkaround_cb_arg); dmaworkaround_cb(dmaworkaround_cb_arg);
} }
portEXIT_CRITICAL(&dmaworkaround_mux); portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
} }
void IRAM_ATTR spicommon_dmaworkaround_transfer_active(int dmachan) void IRAM_ATTR spicommon_dmaworkaround_transfer_active(int dmachan)
{ {
portENTER_CRITICAL(&dmaworkaround_mux); portENTER_CRITICAL_ISR(&dmaworkaround_mux);
dmaworkaround_channels_busy[dmachan-1] = 1; dmaworkaround_channels_busy[dmachan-1] = 1;
portEXIT_CRITICAL(&dmaworkaround_mux); portEXIT_CRITICAL_ISR(&dmaworkaround_mux);
} }

View File

@ -38,8 +38,6 @@
#include "freertos/queue.h" #include "freertos/queue.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.h"
#include "tcpip_adapter.h"
#include "esp_heap_caps_init.h" #include "esp_heap_caps_init.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "esp_system.h" #include "esp_system.h"
@ -55,6 +53,7 @@
#include "esp_newlib.h" #include "esp_newlib.h"
#include "esp_brownout.h" #include "esp_brownout.h"
#include "esp_int_wdt.h" #include "esp_int_wdt.h"
#include "esp_task.h"
#include "esp_task_wdt.h" #include "esp_task_wdt.h"
#include "esp_phy_init.h" #include "esp_phy_init.h"
#include "esp_cache_err_int.h" #include "esp_cache_err_int.h"

View File

@ -61,10 +61,10 @@ static void IRAM_ATTR esp_crosscore_isr(void *arg) {
DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0); DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0);
} }
//Grab the reason and clear it. //Grab the reason and clear it.
portENTER_CRITICAL(&reason_spinlock); portENTER_CRITICAL_ISR(&reason_spinlock);
my_reason_val=*my_reason; my_reason_val=*my_reason;
*my_reason=0; *my_reason=0;
portEXIT_CRITICAL(&reason_spinlock); portEXIT_CRITICAL_ISR(&reason_spinlock);
//Check what we need to do. //Check what we need to do.
if (my_reason_val & REASON_YIELD) { if (my_reason_val & REASON_YIELD) {

View File

@ -243,7 +243,7 @@ void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
static void IRAM_ATTR timer_alarm_isr(void *arg) static void IRAM_ATTR timer_alarm_isr(void *arg)
{ {
portENTER_CRITICAL(&s_time_update_lock); portENTER_CRITICAL_ISR(&s_time_update_lock);
// Timekeeping: adjust s_time_base_us if counter has passed ALARM_OVERFLOW_VAL // Timekeeping: adjust s_time_base_us if counter has passed ALARM_OVERFLOW_VAL
if (timer_overflow_happened()) { if (timer_overflow_happened()) {
timer_count_reload(); timer_count_reload();
@ -256,17 +256,17 @@ static void IRAM_ATTR timer_alarm_isr(void *arg)
// Set alarm to the next overflow moment. Later, upper layer function may // Set alarm to the next overflow moment. Later, upper layer function may
// call esp_timer_impl_set_alarm to change this to an earlier value. // call esp_timer_impl_set_alarm to change this to an earlier value.
REG_WRITE(FRC_TIMER_ALARM_REG(1), ALARM_OVERFLOW_VAL); REG_WRITE(FRC_TIMER_ALARM_REG(1), ALARM_OVERFLOW_VAL);
portEXIT_CRITICAL(&s_time_update_lock); portEXIT_CRITICAL_ISR(&s_time_update_lock);
// Call the upper layer handler // Call the upper layer handler
(*s_alarm_handler)(arg); (*s_alarm_handler)(arg);
} }
void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us) void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
{ {
portENTER_CRITICAL(&s_time_update_lock); portENTER_CRITICAL_ISR(&s_time_update_lock);
/* Bail out if the timer is not initialized yet */ /* Bail out if the timer is not initialized yet */
if (s_timer_interrupt_handle == NULL) { if (s_timer_interrupt_handle == NULL) {
portEXIT_CRITICAL(&s_time_update_lock); portEXIT_CRITICAL_ISR(&s_time_update_lock);
return; return;
} }
@ -308,7 +308,7 @@ void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
s_timer_ticks_per_us = new_ticks_per_us; s_timer_ticks_per_us = new_ticks_per_us;
s_timer_us_per_overflow = ALARM_OVERFLOW_VAL / new_ticks_per_us; s_timer_us_per_overflow = ALARM_OVERFLOW_VAL / new_ticks_per_us;
portEXIT_CRITICAL(&s_time_update_lock); portEXIT_CRITICAL_ISR(&s_time_update_lock);
} }
esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler) esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)

View File

@ -3878,6 +3878,10 @@ BaseType_t xTaskGetAffinity( TaskHandle_t xTask )
static void prvDeleteTCB( TCB_t *pxTCB ) static void prvDeleteTCB( TCB_t *pxTCB )
{ {
/* This call is required for any port specific cleanup related to task.
It must be above the vPortFree() calls. */
portCLEAN_UP_TCB( pxTCB );
/* Free up the memory allocated by the scheduler for the task. It is up /* Free up the memory allocated by the scheduler for the task. It is up
to the task to free any memory allocated at the application level. */ to the task to free any memory allocated at the application level. */
#if ( configUSE_NEWLIB_REENTRANT == 1 ) #if ( configUSE_NEWLIB_REENTRANT == 1 )
@ -3920,7 +3924,6 @@ BaseType_t xTaskGetAffinity( TaskHandle_t xTask )
/* Neither the stack nor the TCB were allocated dynamically, so /* Neither the stack nor the TCB were allocated dynamically, so
nothing needs to be freed. */ nothing needs to be freed. */
configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB ) configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
portCLEAN_UP_TCB( pxTCB );
mtCOVERAGE_TEST_MARKER(); mtCOVERAGE_TEST_MARKER();
} }
} }

View File

@ -77,7 +77,7 @@ esp_err_t heap_trace_start(heap_trace_mode_t mode_param)
if (buffer == NULL || total_records == 0) { if (buffer == NULL || total_records == 0) {
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
taskENTER_CRITICAL(&trace_mux); portENTER_CRITICAL(&trace_mux);
tracing = false; tracing = false;
mode = mode_param; mode = mode_param;
@ -87,7 +87,7 @@ esp_err_t heap_trace_start(heap_trace_mode_t mode_param)
has_overflowed = false; has_overflowed = false;
heap_trace_resume(); heap_trace_resume();
taskEXIT_CRITICAL(&trace_mux); portEXIT_CRITICAL(&trace_mux);
return ESP_OK; return ESP_OK;
} }
@ -128,13 +128,13 @@ esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record)
} }
esp_err_t result = ESP_OK; esp_err_t result = ESP_OK;
taskENTER_CRITICAL(&trace_mux); portENTER_CRITICAL(&trace_mux);
if (index >= count) { if (index >= count) {
result = ESP_ERR_INVALID_ARG; /* out of range for 'count' */ result = ESP_ERR_INVALID_ARG; /* out of range for 'count' */
} else { } else {
memcpy(record, &buffer[index], sizeof(heap_trace_record_t)); memcpy(record, &buffer[index], sizeof(heap_trace_record_t));
} }
taskEXIT_CRITICAL(&trace_mux); portEXIT_CRITICAL(&trace_mux);
return result; return result;
} }
@ -192,7 +192,7 @@ void heap_trace_dump(void)
/* Add a new allocation to the heap trace records */ /* Add a new allocation to the heap trace records */
static IRAM_ATTR void record_allocation(const heap_trace_record_t *record) static IRAM_ATTR void record_allocation(const heap_trace_record_t *record)
{ {
taskENTER_CRITICAL(&trace_mux); portENTER_CRITICAL(&trace_mux);
if (tracing) { if (tracing) {
if (count == total_records) { if (count == total_records) {
has_overflowed = true; has_overflowed = true;
@ -211,7 +211,7 @@ static IRAM_ATTR void record_allocation(const heap_trace_record_t *record)
count++; count++;
total_allocations++; total_allocations++;
} }
taskEXIT_CRITICAL(&trace_mux); portEXIT_CRITICAL(&trace_mux);
} }
// remove a record, used when freeing // remove a record, used when freeing
@ -224,7 +224,7 @@ static void remove_record(int index);
*/ */
static IRAM_ATTR void record_free(void *p, void **callers) static IRAM_ATTR void record_free(void *p, void **callers)
{ {
taskENTER_CRITICAL(&trace_mux); portENTER_CRITICAL(&trace_mux);
if (tracing && count > 0) { if (tracing && count > 0) {
total_frees++; total_frees++;
/* search backwards for the allocation record matching this free */ /* search backwards for the allocation record matching this free */
@ -244,7 +244,7 @@ static IRAM_ATTR void record_free(void *p, void **callers)
} }
} }
} }
taskEXIT_CRITICAL(&trace_mux); portEXIT_CRITICAL(&trace_mux);
} }
/* remove the entry at 'index' from the ringbuffer of saved records */ /* remove the entry at 'index' from the ringbuffer of saved records */

View File

@ -16,7 +16,6 @@
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <rom/ets_sys.h> #include <rom/ets_sys.h>
#include <assert.h> #include <assert.h>
@ -24,14 +23,14 @@
we need to use portmux spinlocks here not RTOS mutexes */ we need to use portmux spinlocks here not RTOS mutexes */
#define MULTI_HEAP_LOCK(PLOCK) do { \ #define MULTI_HEAP_LOCK(PLOCK) do { \
if((PLOCK) != NULL) { \ if((PLOCK) != NULL) { \
taskENTER_CRITICAL((portMUX_TYPE *)(PLOCK)); \ portENTER_CRITICAL((portMUX_TYPE *)(PLOCK)); \
} \ } \
} while(0) } while(0)
#define MULTI_HEAP_UNLOCK(PLOCK) do { \ #define MULTI_HEAP_UNLOCK(PLOCK) do { \
if ((PLOCK) != NULL) { \ if ((PLOCK) != NULL) { \
taskEXIT_CRITICAL((portMUX_TYPE *)(PLOCK)); \ portEXIT_CRITICAL((portMUX_TYPE *)(PLOCK)); \
} \ } \
} while(0) } while(0)

View File

@ -446,6 +446,7 @@ extern const struct __sFILE_fake __sf_fake_stderr;
_NULL \ _NULL \
} }
#ifndef ESP_PLATFORM
#define _REENT_INIT_PTR(var) \ #define _REENT_INIT_PTR(var) \
{ memset((var), 0, sizeof(*(var))); \ { memset((var), 0, sizeof(*(var))); \
(var)->_stdin = (__FILE *)&__sf_fake_stdin; \ (var)->_stdin = (__FILE *)&__sf_fake_stdin; \
@ -453,6 +454,10 @@ extern const struct __sFILE_fake __sf_fake_stderr;
(var)->_stderr = (__FILE *)&__sf_fake_stderr; \ (var)->_stderr = (__FILE *)&__sf_fake_stderr; \
(var)->_current_locale = "C"; \ (var)->_current_locale = "C"; \
} }
#else
extern void esp_reent_init(struct _reent* reent);
#define _REENT_INIT_PTR(var) esp_reent_init(var)
#endif
/* Only built the assert() calls if we are built with debugging. */ /* Only built the assert() calls if we are built with debugging. */
#if DEBUG #if DEBUG

View File

@ -7,3 +7,7 @@ COMPONENT_SRCDIRS := .
COMPONENT_ADD_INCLUDEDIRS := include COMPONENT_ADD_INCLUDEDIRS := include
COMPONENT_ADD_LDFLAGS := -lpthread COMPONENT_ADD_LDFLAGS := -lpthread
ifdef CONFIG_ENABLE_STATIC_TASK_CLEAN_UP_HOOK
COMPONENT_ADD_LDFLAGS += -Wl,--wrap=vPortCleanUpTCB
endif

View File

@ -142,6 +142,29 @@ static void pthread_local_storage_thread_deleted_callback(int index, void *v_tls
free(tls); free(tls);
} }
#if defined(CONFIG_ENABLE_STATIC_TASK_CLEAN_UP_HOOK)
/* Called from FreeRTOS task delete hook */
void pthread_local_storage_cleanup(TaskHandle_t task)
{
void *tls = pvTaskGetThreadLocalStoragePointer(task, PTHREAD_TLS_INDEX);
if (tls != NULL) {
pthread_local_storage_thread_deleted_callback(PTHREAD_TLS_INDEX, tls);
vTaskSetThreadLocalStoragePointer(task, PTHREAD_TLS_INDEX, NULL);
}
}
void __real_vPortCleanUpTCB(void *tcb);
/* If static task cleanup hook is defined then its applications responsibility to define `vPortCleanUpTCB`.
Here we are wrapping it, so that we can do pthread specific TLS cleanup and then invoke application
real specific `vPortCleanUpTCB` */
void __wrap_vPortCleanUpTCB(void *tcb)
{
pthread_local_storage_cleanup(tcb);
__real_vPortCleanUpTCB(tcb);
}
#endif
/* this function called from pthread_task_func for "early" cleanup of TLS in a pthread */ /* this function called from pthread_task_func for "early" cleanup of TLS in a pthread */
void pthread_internal_local_storage_destructor_callback() void pthread_internal_local_storage_destructor_callback()
{ {
@ -151,10 +174,14 @@ void pthread_internal_local_storage_destructor_callback()
/* remove the thread-local-storage pointer to avoid the idle task cleanup /* remove the thread-local-storage pointer to avoid the idle task cleanup
calling it again... calling it again...
*/ */
#if defined(CONFIG_ENABLE_STATIC_TASK_CLEAN_UP_HOOK)
vTaskSetThreadLocalStoragePointer(NULL, PTHREAD_TLS_INDEX, NULL);
#else
vTaskSetThreadLocalStoragePointerAndDelCallback(NULL, vTaskSetThreadLocalStoragePointerAndDelCallback(NULL,
PTHREAD_TLS_INDEX, PTHREAD_TLS_INDEX,
NULL, NULL,
NULL); NULL);
#endif
} }
} }
@ -196,10 +223,14 @@ int pthread_setspecific(pthread_key_t key, const void *value)
if (tls == NULL) { if (tls == NULL) {
return ENOMEM; return ENOMEM;
} }
#if defined(CONFIG_ENABLE_STATIC_TASK_CLEAN_UP_HOOK)
vTaskSetThreadLocalStoragePointer(NULL, PTHREAD_TLS_INDEX, tls);
#else
vTaskSetThreadLocalStoragePointerAndDelCallback(NULL, vTaskSetThreadLocalStoragePointerAndDelCallback(NULL,
PTHREAD_TLS_INDEX, PTHREAD_TLS_INDEX,
tls, tls,
pthread_local_storage_thread_deleted_callback); pthread_local_storage_thread_deleted_callback);
#endif
} }
value_entry_t *entry = find_value(tls, key); value_entry_t *entry = find_value(tls, key);

View File

@ -0,0 +1,2 @@
#
# Component Makefile

View File

@ -19,7 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "rom/ets_sys.h" #include "rom/ets_sys.h"
#include "lwip/mem.h"
typedef long os_time_t; typedef long os_time_t;
/** /**