Skip to content

Nuvoton M480: Some SPI fixes using test shield #467

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

Merged
merged 6 commits into from
Jun 2, 2025

Conversation

multiplemonomials
Copy link
Collaborator

@multiplemonomials multiplemonomials commented Jun 1, 2025

Summary of changes

For this PR, I ran the M480 via the test shield SPI test suite, and found and fixed a number of bugs, most of which were related to word sizes.

  • spi_master_block_write() and spi_master_transfer() would write garbage if passed a word size greater than 1 byte, because the implementation did not realize the length was passed in bytes, not words
  • Aborting an interrupt-based async transfer would cause the next transfer to always fail, because the Rx overflow flag would be set during the abort and never cleared.
  • Calling spi_init() again for an SPI instance that already had DMA channels would not free those DMA channels, which would not only cause those DMA channels to get leaked, but would also break DMA SPI because those DMA channels would still have the SPI request source.

I also added a few new features:

  • spi_get_peripheral_name() now implemented. This allows Mbed applications to use multiple SPI objects referencing the same SPI peripheral without glitching the SPI pins.
  • spi_get_capabilities() now implemented. This is used by the test suite.

Unfortunately there are still five issues happening that I wasn't able to fix after a few hours of trying.

  • One of them I wrote up as Nuvoton M480: Aborting a DMA SPI transfer corrupts the next transfer #466 . I am not sure how to fix this because I don't have enough information about how/if it's possible to abort a DMA operation in progress.
  • The second one was very intermittent and was causing a HardFault during the MicroSD card test, but I wasn't able to reproduce it reliably and even when I could reproduce it I couldn't figure out the exact cause. I think it has to do with the DMA controller transferring bytes too early in spi_master_transfer(), before the line NVIC_SetVector(modinit->irq_n, handler);. But I'm not 100% sure,
  • The third one was also very intermittent and I'm not sure what was causing it. I'd see spi_frequency() get stuck in SPI_DISABLE_SYNC(), where the SPI bus would be busy forever, with no apparent reason (no transfer in progress). But I never saw this one outside of debugging the previous issue so not sure if it was something I was doing.
  • The fourth issue is that this MCU doesn't pass the SD card SPI stress test. It is able to connect to the card, but files on the card don't read and write properly. This happens in synchronous and asynchronous mode, so it may be some sort of low level or electrical issue (though this card and board have worked with other Mbed boards). Don't have time to debug this at present but will keep an eye out.
  • The fifth issue is that the SPI slave test doesn't work in SPI modes 1 or 2. This is an issue that @JohnK1987 is investigating for STMicro chips, so going to hold off on this until his investigation gets some results as it might be the same issue.

Impact of changes

  • M480 SPI will behave better with non-8-bit word sizes
  • Aborting async interrupt SPI transactions will no longer break the next async transaction on M480 SPI
  • M480 SPI now supports spi_get_peripheral_name() and spi_get_capabilities()
  • Doing things that cause a re-init of the SPI instance (e.g. using the same peripheral from multiple SPI objects) will no longer break DMA SPI on M480

Migration actions required

Documentation

None


Pull request type

[X] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[X] Tests / results supplied as part of this PR

SPI tests pass except for failures discussed above.

@multiplemonomials multiplemonomials requested a review from ccli8 June 1, 2025 16:51
@multiplemonomials multiplemonomials self-assigned this Jun 1, 2025
Copy link

@ccli8 ccli8 left a comment

Choose a reason for hiding this comment

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

Thanks for this fix on m480 spi These changes are fine, especially on word size, which has been confusing me since before. For other parts resolved, leave them noted at present

@multiplemonomials
Copy link
Collaborator Author

Yes I 100% agree with you that API documentation on word size is/was confusing as hell. I have tried my best to fix it up over the last couple years.

@multiplemonomials multiplemonomials merged commit 40a020a into master Jun 2, 2025
52 checks passed
@multiplemonomials multiplemonomials deleted the dev/m480-testshield-spi-fixes branch June 2, 2025 07:09
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.

2 participants