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

Add TWAI support for ESP32 and ESP32-S2 #341

Closed
wants to merge 4 commits into from

Conversation

jessebraham
Copy link
Member

While this builds, unfortunately I have not yet gotten it to actually work. Just opening a draft in case somebody else feels like taking a look at it in the meantime. Hoping to dig into this next week, though.

@jessebraham jessebraham linked an issue Jan 9, 2023 that may be closed by this pull request
7 tasks
Copy link

@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.

@jessebraham For ESP32 ECO2 or later revisions, in fn set_baud_rate() we may want to set the interrupt_enable_reg.brp_div field to 0 explicitly. Else, we risk the operating baud rate of the TWAI peripheral to be half of what is specified.

In early versions of the ESP32 (i.e., ECO0 and ECO1), the bus_timing_0_reg.brp field was only 6-bits wide. Assuming an input APB clock of 80MHz and the bit timing configuration below, the lowest achievable bit rate of TWAI on early ESP32 revisions was limited to 25K.

#define TWAI_TIMING_CONFIG_25KBITS()    {.quanta_resolution_hz = 625000, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false}

Thus, to achieve lower bit rates on the ESP32 (without needing to change the bit timing register), in later versions of the ESP32 (i.e. ECO2 and ECO3) we added the interrupt_enable_reg.brp_div. If set, this bit will pre-divide the input APB clock (resulting in an 40MHz input clock) thus allowing lower bit rates to be achieved:

#if (SOC_TWAI_BRP_MAX > 128) || (CONFIG_ESP32_REV_MIN_FULL >= 200)
#define TWAI_TIMING_CONFIG_12_5KBITS()  {.quanta_resolution_hz = 312500, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false}
#define TWAI_TIMING_CONFIG_16KBITS()    {.quanta_resolution_hz = 400000, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false}
#define TWAI_TIMING_CONFIG_20KBITS()    {.quanta_resolution_hz = 400000, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false}
#endif // (SOC_TWAI_BRP_MAX > 128) || (CONFIG_ESP32_REV_MIN_FULL >= 200)

However, I'm not sure if the interrupt_enable_reg.brp_div is reset by default, so it a good idea to clear this bit explicitly. For reference, please take a look at the twai_ll_set_bus_timing() function and the TWAI struct in ESP-IDF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TWAI: support all devices and improve testing of the peripheral driver
2 participants