twai: twai_driver_install() returns error on interrupt allocation failure

This commit updates twai_driver_install() so that an error is returned when
esp_intr_alloc() fails, instead of aborting.

Closes https://github.com/espressif/esp-idf/pull/11494

[darian@espressif.com: Refactored object allocation and free procedures]
[darian@espressif.com: Updated commit message]
Signed-off-by: Darian Leung <darian@espressif.com>
This commit is contained in:
Nebojsa Cvetkovic
2023-05-24 23:31:17 +02:00
committed by Darian Leung
parent 92bc2c74fc
commit 222190f08d

View File

@ -1,10 +1,9 @@
/* /*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#include "sdkconfig.h" #include "sdkconfig.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@ -309,12 +308,15 @@ static void twai_configure_gpio(gpio_num_t tx, gpio_num_t rx, gpio_num_t clkout,
static void twai_free_driver_obj(twai_obj_t *p_obj) static void twai_free_driver_obj(twai_obj_t *p_obj)
{ {
//Free driver object and any dependent SW resources it uses (queues, semaphores etc) //Free driver object and any dependent SW resources it uses (queues, semaphores, interrupts, PM locks etc)
#ifdef CONFIG_PM_ENABLE #if CONFIG_PM_ENABLE
if (p_obj->pm_lock != NULL) { if (p_obj->pm_lock != NULL) {
ESP_ERROR_CHECK(esp_pm_lock_delete(p_obj->pm_lock)); ESP_ERROR_CHECK(esp_pm_lock_delete(p_obj->pm_lock));
} }
#endif #endif //CONFIG_PM_ENABLE
if (p_obj->isr_handle) {
ESP_ERROR_CHECK(esp_intr_free(p_obj->isr_handle));
}
//Delete queues and semaphores //Delete queues and semaphores
if (p_obj->tx_queue != NULL) { if (p_obj->tx_queue != NULL) {
vQueueDelete(p_obj->tx_queue); vQueueDelete(p_obj->tx_queue);
@ -336,63 +338,81 @@ static void twai_free_driver_obj(twai_obj_t *p_obj)
free(p_obj); free(p_obj);
} }
static twai_obj_t *twai_alloc_driver_obj(uint32_t tx_queue_len, uint32_t rx_queue_len) static esp_err_t twai_alloc_driver_obj(const twai_general_config_t *g_config, twai_obj_t **p_twai_obj_ret)
{ {
//Allocates driver object and any dependent SW resources it uses (queues, semaphores etc) //Allocate driver object and any dependent SW resources it uses (queues, semaphores, interrupts, PM locks etc)
esp_err_t ret;
//Create a TWAI driver object //Create a TWAI driver object
twai_obj_t *p_obj = heap_caps_calloc(1, sizeof(twai_obj_t), TWAI_MALLOC_CAPS); twai_obj_t *p_obj = heap_caps_calloc(1, sizeof(twai_obj_t), TWAI_MALLOC_CAPS);
if (p_obj == NULL) { if (p_obj == NULL) {
return NULL; return ESP_ERR_NO_MEM;
} }
#ifdef CONFIG_TWAI_ISR_IN_IRAM #ifdef CONFIG_TWAI_ISR_IN_IRAM
//Allocate memory for queues and semaphores in DRAM //Allocate memory for queues and semaphores in DRAM
if (tx_queue_len > 0) { if (g_config->tx_queue_len > 0) {
p_obj->tx_queue_buff = heap_caps_calloc(tx_queue_len, sizeof(twai_hal_frame_t), TWAI_MALLOC_CAPS); p_obj->tx_queue_buff = heap_caps_calloc(g_config->tx_queue_len, sizeof(twai_hal_frame_t), TWAI_MALLOC_CAPS);
p_obj->tx_queue_struct = heap_caps_calloc(1, sizeof(StaticQueue_t), TWAI_MALLOC_CAPS); p_obj->tx_queue_struct = heap_caps_calloc(1, sizeof(StaticQueue_t), TWAI_MALLOC_CAPS);
if (p_obj->tx_queue_buff == NULL || p_obj->tx_queue_struct == NULL) { if (p_obj->tx_queue_buff == NULL || p_obj->tx_queue_struct == NULL) {
goto cleanup; ret = ESP_ERR_NO_MEM;
goto err;
} }
} }
p_obj->rx_queue_buff = heap_caps_calloc(rx_queue_len, sizeof(twai_hal_frame_t), TWAI_MALLOC_CAPS); p_obj->rx_queue_buff = heap_caps_calloc(g_config->rx_queue_len, sizeof(twai_hal_frame_t), TWAI_MALLOC_CAPS);
p_obj->rx_queue_struct = heap_caps_calloc(1, sizeof(StaticQueue_t), TWAI_MALLOC_CAPS); p_obj->rx_queue_struct = heap_caps_calloc(1, sizeof(StaticQueue_t), TWAI_MALLOC_CAPS);
p_obj->semphr_struct = heap_caps_calloc(1, sizeof(StaticSemaphore_t), TWAI_MALLOC_CAPS); p_obj->semphr_struct = heap_caps_calloc(1, sizeof(StaticSemaphore_t), TWAI_MALLOC_CAPS);
if (p_obj->rx_queue_buff == NULL || p_obj->rx_queue_struct == NULL || p_obj->semphr_struct == NULL) { if (p_obj->rx_queue_buff == NULL || p_obj->rx_queue_struct == NULL || p_obj->semphr_struct == NULL) {
goto cleanup; ret = ESP_ERR_NO_MEM;
goto err;
} }
//Create static queues and semaphores //Create static queues and semaphores
if (tx_queue_len > 0) { if (g_config->tx_queue_len > 0) {
p_obj->tx_queue = xQueueCreateStatic(tx_queue_len, sizeof(twai_hal_frame_t), p_obj->tx_queue_buff, p_obj->tx_queue_struct); p_obj->tx_queue = xQueueCreateStatic(g_config->tx_queue_len, sizeof(twai_hal_frame_t), p_obj->tx_queue_buff, p_obj->tx_queue_struct);
if (p_obj->tx_queue == NULL) { if (p_obj->tx_queue == NULL) {
goto cleanup; ret = ESP_ERR_NO_MEM;
goto err;
} }
} }
p_obj->rx_queue = xQueueCreateStatic(rx_queue_len, sizeof(twai_hal_frame_t), p_obj->rx_queue_buff, p_obj->rx_queue_struct); p_obj->rx_queue = xQueueCreateStatic(g_config->rx_queue_len, sizeof(twai_hal_frame_t), p_obj->rx_queue_buff, p_obj->rx_queue_struct);
p_obj->alert_semphr = xSemaphoreCreateBinaryStatic(p_obj->semphr_struct); p_obj->alert_semphr = xSemaphoreCreateBinaryStatic(p_obj->semphr_struct);
if (p_obj->rx_queue == NULL || p_obj->alert_semphr == NULL) { if (p_obj->rx_queue == NULL || p_obj->alert_semphr == NULL) {
goto cleanup; ret = ESP_ERR_NO_MEM;
goto err;
} }
#else //CONFIG_TWAI_ISR_IN_IRAM #else //CONFIG_TWAI_ISR_IN_IRAM
if (tx_queue_len > 0) { if (g_config->tx_queue_len > 0) {
p_obj->tx_queue = xQueueCreate(tx_queue_len, sizeof(twai_hal_frame_t)); p_obj->tx_queue = xQueueCreate(g_config->tx_queue_len, sizeof(twai_hal_frame_t));
} }
p_obj->rx_queue = xQueueCreate(rx_queue_len, sizeof(twai_hal_frame_t)); p_obj->rx_queue = xQueueCreate(g_config->rx_queue_len, sizeof(twai_hal_frame_t));
p_obj->alert_semphr = xSemaphoreCreateBinary(); p_obj->alert_semphr = xSemaphoreCreateBinary();
if ((tx_queue_len > 0 && p_obj->tx_queue == NULL) || p_obj->rx_queue == NULL || p_obj->alert_semphr == NULL) { if ((g_config->tx_queue_len > 0 && p_obj->tx_queue == NULL) || p_obj->rx_queue == NULL || p_obj->alert_semphr == NULL) {
goto cleanup; ret = ESP_ERR_NO_MEM;
goto err;
} }
#endif //CONFIG_TWAI_ISR_IN_IRAM #endif //CONFIG_TWAI_ISR_IN_IRAM
//Allocate interrupt
#ifdef CONFIG_PM_ENABLE ret = esp_intr_alloc(ETS_TWAI_INTR_SOURCE,
esp_err_t pm_err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "twai", &(p_obj->pm_lock)); g_config->intr_flags | ESP_INTR_FLAG_INTRDISABLED,
if (pm_err != ESP_OK ) { twai_intr_handler_main,
goto cleanup; NULL,
&p_obj->isr_handle);
if (ret != ESP_OK) {
goto err;
} }
#endif #if CONFIG_PM_ENABLE
return p_obj; // XTAL freq can be closed in light sleep, so we need to create a lock to prevent light sleep
ret = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "twai", &(p_obj->pm_lock));
if (ret != ESP_OK) {
goto err;
}
#endif //CONFIG_PM_ENABLE
cleanup: *p_twai_obj_ret = p_obj;
return ESP_OK;
err:
twai_free_driver_obj(p_obj); twai_free_driver_obj(p_obj);
return NULL; return ret;
} }
/* ---------------------------- Public Functions ---------------------------- */ /* ---------------------------- Public Functions ---------------------------- */
@ -417,17 +437,19 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_
esp_err_t ret; esp_err_t ret;
twai_obj_t *p_twai_obj_dummy; twai_obj_t *p_twai_obj_dummy;
//Create a TWAI object (including queues and semaphores) //Create a TWAI object (including queues, semaphores, interrupts, and PM locks)
p_twai_obj_dummy = twai_alloc_driver_obj(g_config->tx_queue_len, g_config->rx_queue_len); ret = twai_alloc_driver_obj(g_config, &p_twai_obj_dummy);
TWAI_CHECK(p_twai_obj_dummy != NULL, ESP_ERR_NO_MEM); if (ret != ESP_OK) {
return ret;
}
//Initialize flags and variables. All other members are already set to zero by twai_alloc_driver_obj() //Initialize flags and variables. All other members are already set to zero by twai_alloc_driver_obj()
p_twai_obj_dummy->state = TWAI_STATE_STOPPED; p_twai_obj_dummy->state = TWAI_STATE_STOPPED;
p_twai_obj_dummy->mode = g_config->mode; p_twai_obj_dummy->mode = g_config->mode;
p_twai_obj_dummy->alerts_enabled = g_config->alerts_enabled; p_twai_obj_dummy->alerts_enabled = g_config->alerts_enabled;
//Initialize TWAI peripheral registers, and allocate interrupt
TWAI_ENTER_CRITICAL(); TWAI_ENTER_CRITICAL();
//Assign the TWAI object
if (p_twai_obj == NULL) { if (p_twai_obj == NULL) {
p_twai_obj = p_twai_obj_dummy; p_twai_obj = p_twai_obj_dummy;
} else { } else {
@ -436,21 +458,29 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_
ret = ESP_ERR_INVALID_STATE; ret = ESP_ERR_INVALID_STATE;
goto err; goto err;
} }
//Reset and enable the TWAI peripheral
periph_module_reset(PERIPH_TWAI_MODULE); periph_module_reset(PERIPH_TWAI_MODULE);
periph_module_enable(PERIPH_TWAI_MODULE); //Enable APB CLK to TWAI peripheral periph_module_enable(PERIPH_TWAI_MODULE);
//Initialize TWAI HAL layer
bool init = twai_hal_init(&twai_context); bool init = twai_hal_init(&twai_context);
assert(init); assert(init);
(void)init; (void)init;
twai_hal_configure(&twai_context, t_config, f_config, DRIVER_DEFAULT_INTERRUPTS, g_config->clkout_divider); twai_hal_configure(&twai_context, t_config, f_config, DRIVER_DEFAULT_INTERRUPTS, g_config->clkout_divider);
TWAI_EXIT_CRITICAL(); TWAI_EXIT_CRITICAL();
//Allocate GPIO and Interrupts //Assign GPIO
twai_configure_gpio(g_config->tx_io, g_config->rx_io, g_config->clkout_io, g_config->bus_off_io); twai_configure_gpio(g_config->tx_io, g_config->rx_io, g_config->clkout_io, g_config->bus_off_io);
ESP_ERROR_CHECK(esp_intr_alloc(ETS_TWAI_INTR_SOURCE, g_config->intr_flags, twai_intr_handler_main, NULL, &p_twai_obj->isr_handle));
#ifdef CONFIG_PM_ENABLE #if CONFIG_PM_ENABLE
ESP_ERROR_CHECK(esp_pm_lock_acquire(p_twai_obj->pm_lock)); //Acquire pm_lock to keep APB clock at 80MHz //Acquire PM lock
#endif if (p_twai_obj->pm_lock) {
ESP_ERROR_CHECK(esp_pm_lock_acquire(p_twai_obj->pm_lock)); //Acquire pm_lock during the whole driver lifetime
}
#endif //CONFIG_PM_ENABLE
//Enable interrupt
ESP_ERROR_CHECK(esp_intr_enable(p_twai_obj->isr_handle));
return ESP_OK; //TWAI module is still in reset mode, users need to call twai_start() afterwards return ESP_OK; //TWAI module is still in reset mode, users need to call twai_start() afterwards
err: err:
@ -473,12 +503,12 @@ esp_err_t twai_driver_uninstall(void)
p_twai_obj = NULL; p_twai_obj = NULL;
TWAI_EXIT_CRITICAL(); TWAI_EXIT_CRITICAL();
ESP_ERROR_CHECK(esp_intr_free(p_twai_obj_dummy->isr_handle)); //Free interrupt
#ifdef CONFIG_PM_ENABLE #ifdef CONFIG_PM_ENABLE
//Release and delete power management lock //Release the power management lock
ESP_ERROR_CHECK(esp_pm_lock_release(p_twai_obj_dummy->pm_lock)); ESP_ERROR_CHECK(esp_pm_lock_release(p_twai_obj_dummy->pm_lock));
#endif #endif
//Disable interrupt
ESP_ERROR_CHECK(esp_intr_disable(p_twai_obj_dummy->isr_handle));
//Free can driver object //Free can driver object
twai_free_driver_obj(p_twai_obj_dummy); twai_free_driver_obj(p_twai_obj_dummy);
return ESP_OK; return ESP_OK;
@ -633,7 +663,7 @@ esp_err_t twai_reconfigure_alerts(uint32_t alerts_enabled, uint32_t *current_ale
TWAI_ENTER_CRITICAL(); TWAI_ENTER_CRITICAL();
//Clear any unhandled alerts //Clear any unhandled alerts
if (current_alerts != NULL) { if (current_alerts != NULL) {
*current_alerts = p_twai_obj->alerts_triggered;; *current_alerts = p_twai_obj->alerts_triggered;
} }
p_twai_obj->alerts_triggered = 0; p_twai_obj->alerts_triggered = 0;
p_twai_obj->alerts_enabled = alerts_enabled; //Update enabled alerts p_twai_obj->alerts_enabled = alerts_enabled; //Update enabled alerts
@ -706,6 +736,7 @@ esp_err_t twai_clear_transmit_queue(void)
return ESP_OK; return ESP_OK;
} }
esp_err_t twai_clear_receive_queue(void) esp_err_t twai_clear_receive_queue(void)
{ {
//Check State //Check State