Skip to content

drivers: spi: spi_max32: Fix word size support #92027

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

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

Conversation

ttmut
Copy link
Contributor

@ttmut ttmut commented Jun 23, 2025

Recent updates to SPI loopback tests (https://github.com/zephyrproject-rtos/zephyr/commits/main/tests/drivers/spi/spi_loopback/src?since=2025-03-21&until=2025-06-23) uncovered a few bugs in the MAX32 SPI driver, mostly related to word size handling.

Fix the driver so that SPI word lengths other than 8-bits are properly handled. Return -ENOTSUP if the requested word length is not supported.

A couple of other fixes:

  • Update frequency values in several overlay files. The actual rate set ended up being higher than the requested rate, causing SPI transfers finishing earlier than expected, and failed test_spi_complete_multiple_timed test.
  • Handle NULL buffers of 0 lengths passed to driver. Fixes failures in test_nop_nil_bufs and test_spi_null_tx_rx_buf_set.

Before:

------ TESTSUITE SUMMARY START ------

SUITE PASS - 100.00% [spi_extra_api_features]: pass = 2, fail = 0, skip = 0, total = 2 duration = 0.007 seconds
 - PASS - [spi_extra_api_features.test_spi_hold_on_cs] duration = 0.005 seconds
 - PASS - [spi_extra_api_features.test_spi_lock_release] duration = 0.002 seconds

SUITE FAIL -  73.08% [spi_loopback]: pass = 19, fail = 7, skip = 0, total = 26 duration = 1.939 seconds
 - FAIL - [spi_loopback.test_nop_nil_bufs] duration = 0.216 seconds
 - PASS - [spi_loopback.test_spi_async_call] duration = 0.487 seconds
 - PASS - [spi_loopback.test_spi_complete_large_transfers] duration = 0.483 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_0] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_1] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_2] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_3] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_multiple] duration = 0.004 seconds
 - FLAKY - [spi_loopback.test_spi_complete_multiple_timed] - (Failed 1 of 2 attempts) - duration = 0.026 seconds
 - PASS - [spi_loopback.test_spi_concurrent_transfer_different_spec] duration = 0.007 seconds
 - PASS - [spi_loopback.test_spi_concurrent_transfer_same_spec] duration = 0.007 seconds
 - PASS - [spi_loopback.test_spi_null_rx_buf_set] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_null_tx_buf] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_null_tx_buf_set] duration = 0.002 seconds
 - FAIL - [spi_loopback.test_spi_null_tx_rx_buf_set] duration = 0.216 seconds
 - PASS - [spi_loopback.test_spi_rx_bigger_than_tx] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_rx_every_4] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_rx_half_end] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_rx_half_start] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_same_buf_cmd] duration = 0.003 seconds
 - FAIL - [spi_loopback.test_spi_word_size_16] duration = 0.217 seconds
 - FAIL - [spi_loopback.test_spi_word_size_24] duration = 0.015 seconds
 - FAIL - [spi_loopback.test_spi_word_size_32] duration = 0.015 seconds
 - PASS - [spi_loopback.test_spi_word_size_7] duration = 0.002 seconds
 - FAIL - [spi_loopback.test_spi_word_size_9] duration = 0.217 seconds
 - PASS - [spi_loopback.test_spi_write_back] duration = 0.002 seconds

 ------ TESTSUITE SUMMARY END ------

After:

------ TESTSUITE SUMMARY START ------

SUITE PASS - 100.00% [spi_extra_api_features]: pass = 2, fail = 0, skip = 0, total = 2 duration = 0.007 seconds
 - PASS - [spi_extra_api_features.test_spi_hold_on_cs] duration = 0.005 seconds
 - PASS - [spi_extra_api_features.test_spi_lock_release] duration = 0.002 seconds

SUITE PASS - 100.00% [spi_loopback]: pass = 24, fail = 0, skip = 2, total = 26 duration = 0.946 seconds
 - PASS - [spi_loopback.test_nop_nil_bufs] duration = 0.001 seconds
 - PASS - [spi_loopback.test_spi_async_call] duration = 0.443 seconds
 - PASS - [spi_loopback.test_spi_complete_large_transfers] duration = 0.439 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_0] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_1] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_2] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_loop_mode_3] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_complete_multiple] duration = 0.004 seconds
 - PASS - [spi_loopback.test_spi_complete_multiple_timed] duration = 0.008 seconds
 - PASS - [spi_loopback.test_spi_concurrent_transfer_different_spec] duration = 0.006 seconds
 - PASS - [spi_loopback.test_spi_concurrent_transfer_same_spec] duration = 0.006 seconds
 - PASS - [spi_loopback.test_spi_null_rx_buf_set] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_null_tx_buf] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_null_tx_buf_set] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_null_tx_rx_buf_set] duration = 0.001 seconds
 - PASS - [spi_loopback.test_spi_rx_bigger_than_tx] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_rx_every_4] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_rx_half_end] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_rx_half_start] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_same_buf_cmd] duration = 0.002 seconds
 - PASS - [spi_loopback.test_spi_word_size_16] duration = 0.002 seconds
 - SKIP - [spi_loopback.test_spi_word_size_24] duration = 0.004 seconds
 - SKIP - [spi_loopback.test_spi_word_size_32] duration = 0.004 seconds
 - PASS - [spi_loopback.test_spi_word_size_7] duration = 0.001 seconds
 - PASS - [spi_loopback.test_spi_word_size_9] duration = 0.001 seconds
 - PASS - [spi_loopback.test_spi_write_back] duration = 0.002 seconds

------ TESTSUITE SUMMARY END ------

Copy link
Member

@MaureenHelm MaureenHelm left a comment

Choose a reason for hiding this comment

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

@vladislav-pejic @dimitrije-lilic can you test this with RTIO?

@MaureenHelm MaureenHelm added this to the v4.2.0 milestone Jun 23, 2025
@MaureenHelm MaureenHelm added the bug The issue is a bug, or the PR is fixing a bug label Jun 23, 2025
@@ -103,7 +103,7 @@ static int spi_configure(const struct device *dev, const struct spi_config *conf

ret = Wrap_MXC_SPI_Init(regs, master_mode, quad_mode, num_slaves, ss_polarity, spi_speed);
if (ret) {
return ret;
return -EINVAL;
Copy link
Contributor

Choose a reason for hiding this comment

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

could you make a different commit for fixing the error codes ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will do.

@ttmut ttmut requested a review from tbursztyka June 24, 2025 13:49
decsny
decsny previously approved these changes Jun 24, 2025
MaureenHelm
MaureenHelm previously approved these changes Jun 24, 2025
ozersa
ozersa previously approved these changes Jun 25, 2025
@ttmut ttmut dismissed stale reviews from ozersa, MaureenHelm, and decsny via 85f17f6 June 25, 2025 06:59
@ttmut
Copy link
Contributor Author

ttmut commented Jun 25, 2025

Updated the copyright dates in spi_max32.c.

ozersa
ozersa previously approved these changes Jun 25, 2025
@vladislav-pejic
Copy link
Contributor

@vladislav-pejic @dimitrije-lilic can you test this with RTIO?

@MaureenHelm We have tested this fix with RTIO for AD4052 and ADXL367 and in both cases RTIO SPI is not working. Functions for RTIO SPI were not updated and are still using old shifts. We used sample.sensor.accel_polling.adxl367-stream test for ADXL367 and sample.driver.adc_stream.ad4052-stream for AD4052 (from this PR #90285).

@ttmut
Copy link
Contributor Author

ttmut commented Jun 25, 2025

@vladislav-pejic @dimitrije-lilic can you test this with RTIO?

@MaureenHelm We have tested this fix with RTIO for AD4052 and ADXL367 and in both cases RTIO SPI is not working. Functions for RTIO SPI were not updated and are still using old shifts. We used sample.sensor.accel_polling.adxl367-stream test for ADXL367 and sample.driver.adc_stream.ad4052-stream for AD4052 (from this PR #90285).

I have added the necessary fixes for the RTIO part, and tests succeed on my setup. However, I do not have an ADXL367 with me at the moment so is it possible for you to try again? Thanks.

@vladislav-pejic
Copy link
Contributor

@vladislav-pejic @dimitrije-lilic can you test this with RTIO?

@MaureenHelm We have tested this fix with RTIO for AD4052 and ADXL367 and in both cases RTIO SPI is not working. Functions for RTIO SPI were not updated and are still using old shifts. We used sample.sensor.accel_polling.adxl367-stream test for ADXL367 and sample.driver.adc_stream.ad4052-stream for AD4052 (from this PR #90285).

I have added the necessary fixes for the RTIO part, and tests succeed on my setup. However, I do not have an ADXL367 with me at the moment so is it possible for you to try again? Thanks.

We run the tests again and results are the same. RTIO SPI is still not working with both chips. When chips are initializing, first SPI read for device id and part id returns wrong values (0xc0 and 0xc0 for ADXL367, 0xff and 0xff for AD4052).

@ttmut ttmut added the DNM This PR should not be merged (Do Not Merge) label Jun 26, 2025
ttmut added 3 commits July 9, 2025 18:56
Driver was not handling SPI word sizes other than 8 bits. Apply DFS
shift wherever necessary to support non 8-bit transfers.

DMA mode cannot support word sizes that are less than 8 bits so return
-ENOTSUP if word size less than 8-bits is required.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
spi_configure was returning HAL error codes that are incompatible with
Zephyr error definitions straight back to the caller. Replace these with
error codes that Zephyr can correctly interpret.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
The requested SPI clock rate and the actual rate that is set can be
different depending on the peripheral clock and divisors available to
the SPI peripheral. For some MAX32 SoCs, actual rate ended up being
higher than the devicetree setting. This would then cause latency tests
to fail as transfers finish earlier than minimum expected duration.

Update the test frequency values in several MAX32 board overlays to pass
latency tests.

Signed-off-by: Tahsin Mutlugun <Tahsin.Mutlugun@analog.com>
@ttmut ttmut removed the DNM This PR should not be merged (Do Not Merge) label Jul 9, 2025
@ttmut
Copy link
Contributor Author

ttmut commented Jul 9, 2025

@vladislav-pejic @dimitrije-lilic can you test this with RTIO?

@MaureenHelm We have tested this fix with RTIO for AD4052 and ADXL367 and in both cases RTIO SPI is not working. Functions for RTIO SPI were not updated and are still using old shifts. We used sample.sensor.accel_polling.adxl367-stream test for ADXL367 and sample.driver.adc_stream.ad4052-stream for AD4052 (from this PR #90285).

I have added the necessary fixes for the RTIO part, and tests succeed on my setup. However, I do not have an ADXL367 with me at the moment so is it possible for you to try again? Thanks.

We run the tests again and results are the same. RTIO SPI is still not working with both chips. When chips are initializing, first SPI read for device id and part id returns wrong values (0xc0 and 0xc0 for ADXL367, 0xff and 0xff for AD4052).

I believe I have sorted it out now. Still trying to find an ADXL367 to verify.

Copy link

sonarqubecloud bot commented Jul 9, 2025

@nashif nashif assigned MaureenHelm and unassigned tbursztyka Jul 9, 2025
Copy link
Member

@MaureenHelm MaureenHelm left a comment

Choose a reason for hiding this comment

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

@vladislav-pejic @dimitrije-lilic can you test this with RTIO?

@MaureenHelm We have tested this fix with RTIO for AD4052 and ADXL367 and in both cases RTIO SPI is not working. Functions for RTIO SPI were not updated and are still using old shifts. We used sample.sensor.accel_polling.adxl367-stream test for ADXL367 and sample.driver.adc_stream.ad4052-stream for AD4052 (from this PR #90285).

I have added the necessary fixes for the RTIO part, and tests succeed on my setup. However, I do not have an ADXL367 with me at the moment so is it possible for you to try again? Thanks.

We run the tests again and results are the same. RTIO SPI is still not working with both chips. When chips are initializing, first SPI read for device id and part id returns wrong values (0xc0 and 0xc0 for ADXL367, 0xff and 0xff for AD4052).

I believe I have sorted it out now. Still trying to find an ADXL367 to verify.

I tested sample.sensor.accel_polling.adxl345-stream and it works well.

@ttmut
Copy link
Contributor Author

ttmut commented Jul 14, 2025

Tested the sample.sensor.accel_polling.adxl367-stream, seems ok.

--- 29 messages dropped ---
Accel data for adxl367@1 (0.127529, -9.228730, 0.426733) 37728429575ns
Accel data for adxl367@1 (0.112814, -9.243445, 0.458616) 37808429575ns
Accel data for adxl367@1 (0.090742, -9.233635, 0.343348) 37888429575ns
Accel data for adxl367@1 (0.088289, -9.248350, 0.252606) 37968429575ns
Accel data for adxl367@1 (0.083384, -9.248350, 0.279584) 38048429575ns
Accel data for adxl367@1 (0.115267, -9.226277, 0.240344) 38128429575ns
Accel data for adxl367@1 (0.083384, -9.243445, 0.213366) 38208429575ns
Accel data for adxl367@1 (0.076027, -9.238540, 0.255059) 38288429575ns
Accel data for adxl367@1 (0.110362, -9.240992, 0.262416) 38368429575ns
Accel data for adxl367@1 (0.095647, -9.272874, 0.304109) 38448429575ns
Accel data for adxl367@1 (0.107909, -9.248350, 0.242796) 38528429575ns
Accel data for adxl367@1 (0.103004, -9.238540, 0.161864) 38608429575ns
Accel data for adxl367@1 (0.078479, -9.248350, 0.245249) 38688429575ns
Accel data for adxl367@1 (-0.242796, -3.536494, 9.358712) 41087653033ns
--- 29 messages dropped ---
Accel data for adxl367@1 (0.058859, -2.984683, 9.481336) 41167653033ns
Accel data for adxl367@1 (-0.100552, -3.097498, 9.390594) 41247653033ns
Accel data for adxl367@1 (0.169221, -3.161263, 9.567174) 41327653033ns
Accel data for adxl367@1 (-0.049049, -5.204189, 9.049698) 41407653033ns
Accel data for adxl367@1 (-0.792155, -5.778072, 6.472128) 41487653033ns
Accel data for adxl367@1 (0.546905, -8.125108, 3.355010) 41567653033ns
Accel data for adxl367@1 (0.475783, -9.223825, -0.340896) 41647653033ns
Accel data for adxl367@1 (0.399756, -8.981028, -3.237290) 41727653033ns
Accel data for adxl367@1 (-0.068669, -9.147797, -2.344583) 41807653033ns
Accel data for adxl367@1 (0.110362, -9.030078, -1.584310) 41887653033ns
Accel data for adxl367@1 (0.071122, -9.086485, -0.517475) 41967653033ns
Accel data for adxl367@1 (0.058859, -9.255707, -0.591050) 42047653033ns
Accel data for adxl367@1 (0.093194, -9.204205, -0.456163) 42127653033ns
Accel data for adxl367@1 (0.073574, -9.228730, 0.647458) 44526380241ns

@danieldegrasse danieldegrasse added the backport-v4.2-branch Request backport to the v4.2-branch label Jul 15, 2025
@danieldegrasse danieldegrasse removed this from the v4.2.0 milestone Jul 15, 2025
@nashif nashif removed the bug The issue is a bug, or the PR is fixing a bug label Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: SPI SPI bus backport-v4.2-branch Request backport to the v4.2-branch platform: ADI Analog Devices, Inc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants