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

i2c: stm32: dma enhancement #81814

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

sgilbert182
Copy link

@sgilbert182 sgilbert182 commented Nov 23, 2024

Add DMA enhancement for stm32 i2c_v2 driver

Fix #34354

Copy link

Hello @sgilbert182, and thank you very much for your first pull request to the Zephyr project!
Our Continuous Integration pipeline will execute a series of checks on your Pull Request commit messages and code, and you are expected to address any failures by updating the PR. Please take a look at our commit message guidelines to find out how to format your commit messages, and at our contribution workflow to understand how to update your Pull Request. If you haven't already, please make sure to review the project's Contributor Expectations and update (by amending and force-pushing the commits) your pull request if necessary.
If you are stuck or need help please join us on Discord and ask your question there. Additionally, you can escalate the review when applicable. 😊

@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from 1adf73d to 601d56f Compare November 23, 2024 18:55
@erwango
Copy link
Member

erwango commented Nov 25, 2024

@sgilbert182 Thanks for this contribution. We'll try to review in best delays. Meanwhile,please have a look at feedback reported by compliance scripts
Also, would you mind providing some details on testing (which platforms, which tests, ..) ?

@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch 2 times, most recently from 357f005 to d465675 Compare November 25, 2024 16:29
@sgilbert182
Copy link
Author

Hi, no worries. I tested this on a g4

@erwango
Copy link
Member

erwango commented Nov 25, 2024

@sgilbert182 Quick hint: Clang format is not mandatory (and I agree this isn't clear at all, these warning are recent and should be deactivated IMO), if you want to make your life easier, you can drop format related changes.

@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from d465675 to 57b1147 Compare November 25, 2024 19:18
@erwango
Copy link
Member

erwango commented Nov 26, 2024

To be clear on formatting changes (and hopefully we can focus on the content afterwards):

You should fix those (not fixing will block CI)
Screenshot from 2024-11-26 11-05-31

You are not requested to fix those (and even sometimes, it is better not to apply):
Screenshot from 2024-11-26 11-05-44

@sgilbert182
Copy link
Author

To be clear on formatting changes (and hopefully we can focus on the content afterwards):

You should fix those (not fixing will block CI) Screenshot from 2024-11-26 11-05-31

You are not requested to fix those (and even sometimes, it is better not to apply): Screenshot from 2024-11-26 11-05-44

I seem to have some issues with Clang Format, I run it locally and it produces results that fail CI. Looks like I need to make the changes manually

@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from 57b1147 to 057bc2b Compare November 26, 2024 10:37
Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

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

Initial review attempt:

Mains request before going further:
Did you consider implementing I2C_CALLBACK which is supposed to be the async version of I2C API while you're at?
If adding DMA support it would be more logical to make use of this API which allows truly async transfers rather than adding DMA support on a blocking API.
Reason I ask is to avoid current state of STM32 SPI driver which is in a similar state today with a sync API allowing use of DMA and ASYNC API effectively blocking.

(Note also that driver is supposed to get further changes to support RTIO API, so let's limit the changes to where it makes real sense)

Then let's club all DMA related additions to the driver in a single commit to make it easier to review.

Finally, if possible, let's rebase on top of #82015 (once merged) in order to get rid of clang-format suggestions.

drivers/i2c/Kconfig.stm32 Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32_v2.c Outdated Show resolved Hide resolved
@sgilbert182
Copy link
Author

Mains request before going further: Did you consider implementing I2C_CALLBACK which is supposed to be the async version of I2C API while you're at? If adding DMA support it would be more logical to make use of this API which allows truly async transfers rather than adding DMA support on a blocking API. Reason I ask is to avoid current state of STM32 SPI driver which is in a similar state today with a sync API allowing use of DMA and ASYNC API effectively blocking.

I did not, I was porting over changes I was using for a project but I can take a look at this when I have time.

(Note also that driver is supposed to get further changes to support RTIO API, so let's limit the changes to where it makes real sense)

I will revert this

Then let's club all DMA related additions to the driver in a single commit to make it easier to review.

Finally, if possible, let's rebase on top of #82015 (once merged) in order to get rid of clang-format suggestions.

should I close this MR until this can be rebased and I've had a chance to look at the callback mechanism?

@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from 057bc2b to 94c6e1d Compare November 26, 2024 12:29
@erwango
Copy link
Member

erwango commented Nov 26, 2024

I did not, I was porting over changes I was using for a project but I can take a look at this when I have time.

Would be great. Thanks

should I close this MR until this can be rebased and I've had a chance to look at the callback mechanism?

Not required. But for clarity, you can move it to draft.

@sgilbert182 sgilbert182 marked this pull request as draft November 26, 2024 13:43
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch 3 times, most recently from cfd0b0a to 8c030ea Compare November 27, 2024 09:27
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from 1405310 to 246d8b9 Compare December 30, 2024 18:06
@@ -605,23 +687,20 @@ static int stm32_i2c_msg_write(const struct device *dev, struct i2c_msg *msg,
stm32_i2c_enable_transfer_interrupts(dev);
LL_I2C_EnableIT_TX(i2c);

if (k_sem_take(&data->device_sync_sem,
K_MSEC(STM32_I2C_TRANSFER_TIMEOUT_MSEC)) != 0) {
if (k_sem_take(&data->device_sync_sem, K_MSEC(STM32_I2C_TRANSFER_TIMEOUT_MSEC)) != 0) {
stm32_i2c_master_mode_end(dev);
k_sem_take(&data->device_sync_sem, K_FOREVER);
is_timeout = true;
}

Copy link
Contributor

@erian747 erian747 Dec 31, 2024

Choose a reason for hiding this comment

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

For devices with DCACHE enabled and rx buffer is in cached mem, it think a cache invalidation should be done here
Another option would be to issue a log warning when transfer is started that memory should be non-cacheable like it is done in uart-stm32.c

Copy link
Author

Choose a reason for hiding this comment

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

I've added cache support, slightly modified from the UART implementation see if it makes sense where I put it. I don't have a platform at hand to test it though. I'll have to dig through my box of bits and see if I have something

Copy link
Contributor

@erian747 erian747 Jan 1, 2025

Choose a reason for hiding this comment

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

Use clean cache operation before starting DMA write and use invalidate cache after read DMA finishes
Also note that clean/invalidate addresses shall align with cache line size, 32 bytes

Copy link
Author

Choose a reason for hiding this comment

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

Good catch, corrected

@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch 2 times, most recently from ba7b10e to d72fc63 Compare January 2, 2025 11:17
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from d72fc63 to af7b858 Compare January 21, 2025 10:33
Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

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

Thanks @sgilbert182 for the update.

Some comments on the code. Regarding the PR it self, there will be some more work required:

  • Blocking: We don't merge fixup commits. So please directly fix initial commits.
  • Not blocking: Regarding formatting, the clang formatting suggestions have now been disabled (they'll disappear if you rebase on top of latest main). So ideally, I'd suggest to drop the formatting changes.

drivers/i2c/i2c_ll_stm32_v2.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32_v2.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32_v2.c Outdated Show resolved Hide resolved
drivers/i2c/i2c_ll_stm32_v2.c Show resolved Hide resolved
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch 9 times, most recently from ef3e9f3 to 1d0b5b1 Compare January 22, 2025 21:44
@sgilbert182
Copy link
Author

sgilbert182 commented Jan 22, 2025

Hi @erwango, thanks for your comments

I've attempted to fix the initial commits (sorry for the flurry of forced pushes) but there's still a couple of commits I see as step changes rather than just fixes but you might feel differently. Let me know if you're happy or what still needs adjusting.

  • Blocking: We don't merge fixup commits. So please directly fix initial commits.

I've rebased but should the formatting changes be kept so the file is more in line with current style?

  • Not blocking: Regarding formatting, the clang formatting suggestions have now been disabled (they'll disappear if you rebase on top of latest main). So ideally, I'd suggest to drop the formatting changes.

@erwango
Copy link
Member

erwango commented Jan 23, 2025

Thanks for the update.

As mentioned on the code style, my personal taste would be to drop them, but they are done on a dedicated commit and maintain readability, so I don't have objective reason to block.

Regarding implementation steps, to me they clearly falls under points 3 and 5 in https://docs.zephyrproject.org/latest/contribute/contributor_expectations.html#pr-requirements, so they should be squashed.

Last, I didn't came back on this in last review, but in my initial review I was mentioning that use of I2C_CALLBACK was preferred for DMA support (following the same experience that we had on SPI). I won't block on this point as I guess this new API variant can still be added later.

drivers/i2c/Kconfig.stm32 Outdated Show resolved Hide resolved
Add option to enable DMA support

Signed-off-by: Simon Gilbert <[email protected]>
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from 1d0b5b1 to 6a98a6c Compare January 24, 2025 10:55
Add initial DMA settings structs to stm32 i2c config and data structs

Signed-off-by: Simon Gilbert <[email protected]>
Add macrobatics to pull DMA settings from device tree

Signed-off-by: Simon Gilbert <[email protected]>
Add DMA options (phandle-array and names) to yaml file

Signed-off-by: Simon Gilbert <[email protected]>
Tidy up of i2c_stm32_transfer and added TX and RX semaphore inits

Signed-off-by: Simon Gilbert <[email protected]>
Added stop DMA fot transmit complete or other master end conditions

Signed-off-by: Simon Gilbert <[email protected]>
Clang format

Signed-off-by: Simon Gilbert <[email protected]>
Add basic cache memory support

Signed-off-by: Simon Gilbert <[email protected]>
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from 6a98a6c to 0267207 Compare January 24, 2025 14:39
Add asynchronous callback function and supporting functions

Signed-off-by: Simon Gilbert [email protected]
@sgilbert182
Copy link
Author

sgilbert182 commented Jan 24, 2025

I've condensed the formats to a single commit, I could drop it here and create a follow up MR with those formatting changes but it feels heavy handed.

As mentioned on the code style, my personal taste would be to drop them, but they are done on a dedicated commit and maintain readability, so I don't have objective reason to block.

All fixits should now be merged into the initial commits.

Regarding implementation steps, to me they clearly falls under points 3 and 5 in https://docs.zephyrproject.org/latest/contribute/contributor_expectations.html#pr-requirements, so they should be squashed.

I have had a little time over the last few days and have attempted to add the async support but I haven't enough time to fully exercise it. It's a single commit so can be dropped and I can create a new MR when I have more time to work with it.

Last, I didn't came back on this in last review, but in my initial review I was mentioning that use of I2C_CALLBACK was preferred for DMA support (following the same experience that we had on SPI). I won't block on this point as I guess this new API variant can still be added later.

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

Successfully merging this pull request may close these issues.

Please investigate adding DMA support to STM32 I2C!
6 participants