Skip to content

Add SPI bitwidths to ST targets where supported #14020

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 1 commit into from
Jan 15, 2021

Conversation

pea-pod
Copy link
Contributor

@pea-pod pea-pod commented Dec 10, 2020

Summary of changes

Adds #if defined guards with a switch statement to allow all the number of data width values per family, as per each target's stm32YYxx_hal_spi.h file.

Families not added yet to the list (for example, the WL series) will error out during compilation so that the appropriate values can be set.

Impact of changes

Targets that support data widths besides 8 and 16 are now supported to their maximum number of valid values. 9 bit SPI (used on many display drivers) can now be supported through mbed. Targets that only support 8 and 16 bit SPI will not be changed. One target family even allows for up to 32 bit data widths (the H7 series) and is supported as well.

Migration actions required

None.

Documentation

Updates SPI.h doc comment to reflect capability and target dependent nature.


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)
[X] Covered by existing mbed-os tests (Greentea or Unittest)
[] Tests / results supplied as part of this PR

Reviewers

@jeromecoutant


@ciarmcom
Copy link
Member

@pea-pod, thank you for your changes.
@jeromecoutant @ARMmbed/team-st-mcd @ARMmbed/mbed-os-maintainers please review.

0xc0170
0xc0170 previously approved these changes Dec 10, 2020
@mergify mergify bot added needs: CI and removed needs: review labels Dec 10, 2020
@mergify mergify bot dismissed 0xc0170’s stale review December 10, 2020 14:13

Pull request has been modified.

@pea-pod
Copy link
Contributor Author

pea-pod commented Dec 10, 2020

I updated this a moment ago to correct the capabilities structure that gets returned for the spi_get_capabilities call.

@jeromecoutant: Does the ST implementation of the SPI peripheral do "transfers" or bytes? I could not tell with the little I looked, but I will keep looking in the mean time. I might need to do some more updating to do tests for current data widths.

@jeromecoutant
Copy link
Collaborator

@LMESTM @ABOSTM

Copy link
Collaborator

@jeromecoutant jeromecoutant left a comment

Choose a reason for hiding this comment

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

Hi
I notice that few targets can't compile.
Let's see

@jeromecoutant
Copy link
Collaborator

Checked also SPI FPGA test with STM32L4S target:

| B_L4S5I_IOT01A-ARMC6 | B_L4S5I_IOT01A | hal-tests-tests-mbed_hal_fpga_ci_test_shield-spi | SPI - symbol size testing (12) | 0 | 1 | FAIL | 0.3 |

Note it is not a regression as test was skipped before.

Comment on lines 401 to 404
#if defined(TARGET_STM32F0) || defined(TARGET_STM32F3) || defined(TARGET_STM32F7) || \
defined(TARGET_STM32G0) || defined(TARGET_STM32G4) || defined(TARGET_STM32L4) || \
defined(TARGET_STM32L5) || defined(TARGET_STM32WB) || defined(TARGET_STM32H7)
case 4:
DataSize = SPI_DATASIZE_4BIT;
break;
case 5:
DataSize = SPI_DATASIZE_5BIT;
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about code like:

    switch (bits) {
#if defined(SPI_DATASIZE_4BIT)
        case 4:
            DataSize = SPI_DATASIZE_4BIT;
            break;
#endif
#if defined(SPI_DATASIZE_4BIT)
        case 5:
            DataSize = SPI_DATASIZE_5BIT;
            break;
#endif
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a little more verbose on the #if flags, but I think it follows your general style better. Plus, it has the benefit of not having to port in new families when they hit, assuming that the semantics remain the same.

While looking at #13941 regarding the default output for async transfers, I noticed that there was at least one place where the count passed to the structure was transfers and not bytes. I will look for those locations and update them similarly.

Comment on lines 93 to 100
#if defined(TARGET_STM32F0) || defined(TARGET_STM32F3) || defined(TARGET_STM32F7) || \
defined(TARGET_STM32G0) || defined(TARGET_STM32G4) || defined(TARGET_STM32L4) || \
defined(TARGET_STM32L5) || defined(TARGET_STM32WB)
cap->word_length = 0x0000FFF8; // 4 through 16 bit symbols, inclusive
#elif defined(TARGET_STM32H7)
cap->word_length = 0xFFFFFFF8; // 4 through 32 bit symbols, inclusive
#elif defined(TARGET_STM32F1) || defined(TARGET_STM32F2) || defined(TARGET_STM32F4) || \
defined(TARGET_STM32L0) || defined(TARGET_STM32L1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe value could be defined in each subfamily in each TARGET_STM32xx/spi_device.h for ex ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would not mind necessarily. However, there is some precedence for putting specific feature items in here. For example on line 332, you find this: #if defined SPI1_BASE, which is then followed by function calls. I understand that this is different from a value, on the other hand, the value is only used here.

I am neutral on this though.

@jeromecoutant
Copy link
Collaborator

2 other comments:

@jeromecoutant
Copy link
Collaborator

* in mbed API:
  https://os.mbed.com/docs/mbed-os/v6.5/mbed-os-api-doxy/classmbed_1_1_s_p_i.html
  Seems that number of bits per SPI frame should be between 4 and 16 ?

Doc should be wrong as FPGA test is verifying 24 and 32 bits...
https://github.com/ARMmbed/mbed-os/blob/master/hal/tests/TESTS/mbed_hal_fpga_ci_test_shield/spi/main.cpp#L405-L406

@0xc0170

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 17, 2020

@jeromecoutant IF that is the cas,e can you send a pull request fixing the comment?

@pea-pod
Copy link
Contributor Author

pea-pod commented Dec 17, 2020

@0xc0170 Would you like for me to include it in this PR? Either way is fine. My proposed change I made locally only was to change it from "4 to 16" (or close enough) to "4 to 32, target dependent".

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 17, 2020

@pea-pod yes, please do

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 18, 2020

Once updated, let us know, we can start CI

@pea-pod
Copy link
Contributor Author

pea-pod commented Dec 19, 2020

@0xc0170 I have pushed changes based on @jeromecoutant's comments.

@jeromecoutant: On the spi_slave_write function, I reduced the code calls to the direct register write and replaced them with the LL inline functions. This matches the same calls elsewhere. I checked both the stm32h7xx_ll_spi.h file and the stm32l4xx_ll_spi.h file and found their calls to be the same as what the function was doing directly. So I changed that. Please let me know if this causes problems I am unaware of and I can change it back. This way just seems easier due to the HAL (or rather, the LL code) calling them directly. This reduced the number of #if defined blocks in the stm_spi_api.c file.

@0xc0170 and @jeromecoutant: One more urgent issue with the mbed HAL API:

The spi_master_block_write has a char write_fill parameter for feeding the SPI peripheral the correct dummy data. The issue here is that the way this is written, if for some reason a 9-bit value was required, the char cannot contain it. The current implementation only provides for an 8 bit dummy value which, in my example, could cause problems if the ninth bit and the first bit are not the same. I do not have any ideas on how this should be addressed. You can see the issue here:

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
                           char *rx_buffer, int rx_length, char write_fill)
{
    struct spi_s *spiobj = SPI_S(obj);
    SPI_HandleTypeDef *handle = &(spiobj->handle);
    int total = (tx_length > rx_length) ? tx_length : rx_length;
    if (handle->Init.Direction == SPI_DIRECTION_2LINES) {
        for (int i = 0; i < total; i++) {
            char out = (i < tx_length) ? tx_buffer[i] : write_fill;
            char in = spi_master_write(obj, out);
...

I could be wrong on this, but I do not think this particular problem is specific to ST's mbed SPI HAL implementation only.

@mergify mergify bot added needs: work and removed needs: CI labels Dec 19, 2020
@pea-pod pea-pod force-pushed the stm-spi-more-bits branch 2 times, most recently from 28a0cad to bbe7094 Compare December 19, 2020 16:01
@pea-pod
Copy link
Contributor Author

pea-pod commented Jan 11, 2021

@jeromecoutant I'm sorry, but I managed to not commit a typo fix somehow. I don't know how, but it happened. Anyway, it'll need another restart once the checks clear. Sorry.

@mbed-ci
Copy link

mbed-ci commented Jan 11, 2021

Jenkins CI Test : ✔️ SUCCESS

Build Number: 1 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_cmake-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-ARM ✔️
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_build-greentea-ARM ✔️
jenkins-ci/mbed-os-ci_build-greentea-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-cloud-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-cloud-example-ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-test ✔️
jenkins-ci/mbed-os-ci_greentea-test ✔️

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 11, 2021

CI restarted

@0xc0170 0xc0170 added release-type: patch Indentifies a PR as containing just a patch and removed release-type: feature labels Jan 11, 2021
@0xc0170
Copy link
Contributor

0xc0170 commented Jan 11, 2021

I set this to patch release (target update).

@mbed-ci
Copy link

mbed-ci commented Jan 11, 2021

Jenkins CI Test : ✔️ SUCCESS

Build Number: 2 | 🔒 Jenkins CI Job | 🌐 Logs & Artifacts

CLICK for Detailed Summary

jobs Status
jenkins-ci/mbed-os-ci_unittests ✔️
jenkins-ci/mbed-os-ci_cmake-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-greentea-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-greentea-ARM ✔️
jenkins-ci/mbed-os-ci_build-cloud-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_build-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-cloud-example-ARM ✔️
jenkins-ci/mbed-os-ci_build-example-GCC_ARM ✔️
jenkins-ci/mbed-os-ci_cmake-example-test ✔️
jenkins-ci/mbed-os-ci_greentea-test ✔️

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 11, 2021

Updates SPI.h doc comment to reflect capability and target dependent nature.

@MarceloSalazar Please review this change

@MarceloSalazar MarceloSalazar requested a review from a team January 11, 2021 17:43
@ithinuel
Copy link
Member

This is something that has been addressed in the overhauled SPI API on https://github.com/ARMmbed/mbed-os/blob/feature-hal-spec-spi/hal/spi_api.h.

@donatieng What's the status of this feature branch/overhauled API. And if it's still relevant, how do we proceed with this PR ?

@pea-pod
Copy link
Contributor Author

pea-pod commented Jan 11, 2021

@ithinuel If your comment is regarding the PR itself (and not the issue I found that others addressed on the feature branch), I believe my PR should go through. My changes only open up a number of ST targets to additional bitwidths. But it does not change existing behavior. The 16 bit transfer size existed before and already has these issues. This just lets me do things like talk to certain displays without additional pins today through mbed without waiting on a feature branch to hit master.

If this is in regards to updating the feature branch somehow, well, I would still prefer to have mine go through so at least more of my Nucleo features are out-of-the-box with mbed. If it is slightly incongruous with the feature branch, I do not think it too far outside where the API is going.

The way ST handled it being something other than 8 or 16 was that it picked it. My PR doesn't change that or attempt to fix or work around the API.

For what it's worth, I would be willing to help get the SPI spec branch moving forward, but I would ask that this go forward in the meantime, regardless.

@pea-pod
Copy link
Contributor Author

pea-pod commented Jan 11, 2021

Ugh. Sometimes I don't write words well.

"The way ST handled it being something other than 8 or 16 was that it picked it."

In real English, I mean that ST's HAL implementation of the format function is to force any entry into one of the two values: 8 or 16. My PR doesn't alter this, except you can now pick 4 or 15 on the devices that support it. If the word length is set outside of the bounds, the behavior is the same. In the current system, if it is not 16 bits, it is set to 8. Now you can make it 5 if the family peripheral supports it. If it does not, it will be set to 8.

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 12, 2021

Thanks @pea-pod , appreciate any help towards improvements like spi feature branch. As the last comment received +1 from @ithinuel , we agreed with merging this PR as it is.

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 14, 2021

@ithinuel Please let us know what we shall do with this one.

@ithinuel
Copy link
Member

@0xc0170 I don't know, hence the question to @donatieng 😉

@donatieng
Copy link
Contributor

This PR looks good to me, if @LMESTM and @jeromecoutant are happy, I'm happy! Thanks @pea-pod.
@ithinuel there's more work needed to bring in the new SPI branch, we'll need to discuss with all stakeholders.

@0xc0170 0xc0170 merged commit aef93ca into ARMmbed:master Jan 15, 2021
@mergify mergify bot removed the ready for merge label Jan 15, 2021
@mbedmain mbedmain added release-version: 6.7.0 Release-pending and removed release-type: patch Indentifies a PR as containing just a patch Release-pending labels Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants