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

Draft
wants to merge 11 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.

Comment on lines 58 to 64
config I2C_DMA
bool "DMA support"
depends on I2C_STM32_V2
help
Enable DMA support for the STM32 I2C driver.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
config I2C_DMA
bool "DMA support"
depends on I2C_STM32_V2
help
Enable DMA support for the STM32 I2C driver.
config I2C_STM32_DMA
bool "STM32 I2C DMA support"
depends on I2C_STM32_V2
select DMA
help
Enable DMA support for the STM32 I2C driver.
Limited to `st,stm32-i2c-v2` compatible SoCs for now.

Optionally we should add select CACHE_MANAGEMENT if CPU_HAS_DCACHE if tested on H7/F7.
Otherwise, let's specify it is not supported for now on those (depends on !DCACHE and provide details in help section)

Copy link
Author

Choose a reason for hiding this comment

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

Added

Comment on lines 547 to 548
data->current.is_err = 0;
dma_stop(dma_dev, channel);
Copy link
Member

Choose a reason for hiding this comment

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

Indentation

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

switch (status) {
case DMA_STATUS_COMPLETE:
data->current.is_err = 0;
dma_stop(dma_dev, channel);
Copy link
Member

Choose a reason for hiding this comment

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

Indentation

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines 206 to 207
uint8_t *next_msg_flags = NULL;

if (num_msgs > 1) {
next = current + 1;
next_msg_flags = &(next->flags);
}
ret = stm32_i2c_transaction(dev, *current, next_msg_flags, slave);
ret = stm32_i2c_transaction(dev, *current, next ? &(next->flags) : NULL, slave);
Copy link
Member

Choose a reason for hiding this comment

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

Let's not mix non related DMA changes within this PR.

Copy link
Author

Choose a reason for hiding this comment

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

reverted

Comment on lines +392 to +394
k_sem_init(&data->dma_rx_sem, 1, K_SEM_MAX_LIMIT);
k_sem_init(&data->dma_tx_sem, 1, K_SEM_MAX_LIMIT);
Copy link
Member

Choose a reason for hiding this comment

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

See definition for K_SEM_MAX_LIMIT

This is intended for use when a semaphore does not have an explicit maximum limit, and instead is just used for counting purposes.

Are you sure this is the intended behavior in this case ?

Note that we probably have other bad usages elsewhere.

Copy link
Author

Choose a reason for hiding this comment

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

I used this to catch an unknown number of threads accessing the same I2C bus, do you have any known good usages I can follow instead?

Copy link
Member

Choose a reason for hiding this comment

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

k_sem_init(&data->dma_rx_sem, 1, 1);
k_sem_init(&data->dma_tx_sem, 1, 1);

Comment on lines 550 to 551
case DMA_STATUS_BLOCK:
break;
Copy link
Member

Choose a reason for hiding this comment

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

Support of DMA_STATUS_BLOCK is related to enabling circular mode. If not supported for now (support could be enabled later) let's generate an assert in this case

Copy link
Author

Choose a reason for hiding this comment

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

Would you prefer an assert or a log_err?

Comment on lines 121 to 127
static int configure_dma(const struct device *dma_dev, uint32_t dma_channel,
struct dma_config *dma_cfg, struct dma_block_config *blk_cfg,
uint32_t source_address, uint32_t dest_address, struct i2c_msg *msg,
struct k_sem *dma_sem, const char *direction)
Copy link
Member

Choose a reason for hiding this comment

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

It is suboptimal to use more than 4 parameters in a function on ARM platforms. 4 first params are accessed from registers (r0-r3), other params will be accessed from stack.
Any way to do this differently ?

Copy link
Author

Choose a reason for hiding this comment

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

Glad you mentioned it, I've been playing with re-jigging the structs which may help here... so yes!

Copy link
Author

Choose a reason for hiding this comment

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

structs re-jigged

@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
Add option to enable DMA support

Signed-off-by: Simon Gilbert <[email protected]>
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 header files

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
Added stop DMA fot transmit complete or other master end conditions
Clang format

Signed-off-by: Simon Gilbert <[email protected]>
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from 94c6e1d to c14037a Compare November 26, 2024 17:37
Add assert catches in DMA callbacks

Signed-off-by: Simon Gilbert <[email protected]>
use structs to pass dma device and config settings more easily

Signed-off-by: Simon Gilbert <[email protected]>
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from f9bdf86 to cfd0b0a Compare November 27, 2024 09:01
Second pass at applying Clang Format

Signed-off-by: Simon Gilbert <[email protected]>
@sgilbert182 sgilbert182 force-pushed the feature/stm32-i2c-dma-enhancement branch from cfd0b0a to 8c030ea Compare November 27, 2024 09:27
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!
3 participants