Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

twai: Don't abort on interrupt allocation failure (IDFGH-10231) #11494

Closed
wants to merge 1 commit into from

Conversation

nebkat
Copy link
Contributor

@nebkat nebkat commented May 24, 2023

twai_driver_install() currently aborts if there is a failure allocation the interrupt handler, unlike for example UART driver which simply returns the error code:

ret = esp_intr_alloc(uart_periph_signal[uart_num].irq, intr_alloc_flags,
uart_rx_intr_handler_default, p_uart_obj[uart_num],
&p_uart_obj[uart_num]->intr_handle);
ESP_GOTO_ON_ERROR(ret, err, UART_TAG, "Could not allocate an interrupt for UART");

With this change execution can continue even if the allocation fails.

@espressif-bot espressif-bot added the Status: Opened Issue is new label May 24, 2023
@github-actions github-actions bot changed the title twai: Don't abort on interrupt allocation failure twai: Don't abort on interrupt allocation failure (IDFGH-10231) May 24, 2023
@Dazza0 Dazza0 self-requested a review May 26, 2023 08:17
Copy link
Contributor

@Dazza0 Dazza0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nebkat Thanks for your contribution, the requested changes looks reasonable to me.

I would probably want to rearrange the order of operations in the twai_driver_install() so that all resource allocations are done at the start. Furthermore, would you be able to target this PR for the master branch (it would work better with our backporting workflow)?

@@ -446,7 +449,8 @@ esp_err_t twai_driver_install(const twai_general_config_t *g_config, const twai_

//Allocate GPIO and Interrupts
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));
ret = esp_intr_alloc(ETS_TWAI_INTR_SOURCE, g_config->intr_flags, twai_intr_handler_main, NULL, &p_twai_obj->isr_handle);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer if we

  • Allocated interrupts immediately after allocating the driver object. This way, if any resource allocation fails, we can return early
  • Added the ESP_INTR_FLAG_INTRDISABLED flag so that an allocated interrupt remains disabled until we explicitly enable it using esp_intr_enable()

So something like...

Suggested change
ret = esp_intr_alloc(ETS_TWAI_INTR_SOURCE, g_config->intr_flags, twai_intr_handler_main, NULL, &p_twai_obj->isr_handle);
//Create a TWAI object (including queues and semaphores)
p_twai_obj_dummy = twai_alloc_driver_obj(g_config->tx_queue_len, g_config->rx_queue_len);
TWAI_CHECK(p_twai_obj_dummy != NULL, ESP_ERR_NO_MEM);
//Allocate interrupt for TWAI driver
intr_handle_t isr_hdl;
ret = esp_intr_alloc(ETS_TWAI_INTR_SOURCE, g_config->intr_flags | ESP_INTR_FLAG_INTRDISABLED, twai_intr_handler_main, NULL, &isr_hdl);
if (ret != ESP_OK) {
ESP_LOGE(TWAI_TAG, "Interrupt allocation failed");
goto intr_alloc_err;
}

@Dazza0 Dazza0 self-assigned this Jun 21, 2023
@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: Opened Issue is new labels Jun 26, 2023
@nebkat
Copy link
Contributor Author

nebkat commented Jun 29, 2023

Sorry never got around to finishing this, thanks for the change!

@Dazza0
Copy link
Contributor

Dazza0 commented Jun 29, 2023

@nebkat No worries. I've made some modifications to your PR commit, but commit author is still attributed to you. Thanks for the contribution

espressif-bot pushed a commit that referenced this pull request Aug 22, 2023
…lure

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

Closes #11494

[darian@espressif.com: Refactored object allocation and free procedures]
[darian@espressif.com: Updated commit message]
Signed-off-by: Darian Leung <darian@espressif.com>
espressif-bot pushed a commit that referenced this pull request Sep 1, 2023
…lure

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

Closes #11494

[darian@espressif.com: Refactored object allocation and free procedures]
[darian@espressif.com: Updated commit message]
Signed-off-by: Darian Leung <darian@espressif.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants