8000 extmod/machine_spi.c: Support firstbit=machine.LSB for machine.SoftSPI. by robert-hh · Pull Request #15436 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

extmod/machine_spi.c: Support firstbit=machine.LSB for machine.SoftSPI. #15436

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
Jul 12, 2024

Conversation

robert-hh
Copy link
Contributor

Easy to add, sometimes needed and sought for.

For the change the definition of MICROPY_PY_MACHINE_SPI_MSB/ -LSB was moved to py/mpconfig.h, making them available to all ports. The identical defines in esp32/mpconfigport.h were deleted.

Tested with the RP2 and ESP32 port.

Copy link
codecov bot commented Jul 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.42%. Comparing base (20b00ca) to head (ee10360).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15436   +/-   ##
=======================================
  Coverage   98.42%   98.42%           
=======================================
  Files         161      161           
  Lines       21253    21253           
=======================================
  Hits        20919    20919           
  Misses        334      334           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
github-actions bot commented Jul 9, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:  +108 +0.028% PYBV10
     mimxrt:   +80 +0.022% TEENSY40
        rp2:   +80 +0.009% RPI_PICO_W
       samd:   +80 +0.030% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Jul 11, 2024
mp_raise_ValueError(MP_ERROR_TEXT("firstbit must be MSB"));
}
self->spi.firstbit = args[ARG_firstbit].u_int == MICROPY_PY_MACHINE_SPI_MSB ?
MICROPY_PY_MACHINE_SPI_MSB : MICROPY_PY_MACHINE_SPI_LSB;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's any need to check for valid inputs like this. Instead doing self->spi.firstbit = args[ARG_firstbit].u_int; is fine (that will anyway give equivalent behaviour because mp_soft_spi_transfer is validating the value as well).

Alternatively, could replace the firstbit variable here with bool msb and do:

self->spi.msb = args[ARG_firstbit].u_int == MICROPY_PY_MACHINE_SPI_MSB;

Then in mp_soft_spi_transfer it's simply self->spi.msb ? src[i] : swap_bits(src[i]). That may save a few bytes of code size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. The first variant is kept, such that the name of the symbol matches the name of the option. Since the value of MICROPY_PY_MACHINE_SPI_MSB is 0 and self->spi.msb ? ....., is actually self->spi.msb != 0 ? .....` most likely the compiler will generate the same code for both variants in mp_soft_spi_transfer.

Copy link
Member

Choose a reason for hiding this comment

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

The first variant is kept, such that the name of the symbol matches the name of the option.

Yes, good idea.

And I checked code size and indeed it doesn't change when using bool msb. So the compiler is optimising it as we would expect it to.

@dpgeorge
Copy link
Member

Thanks for this. I always wondered how LSB could be implemented in SoftSPI without too much overhead and this is a neat way of doing it. Good to support LSB for the cases that need it (and if the hardware doesn't support it / not implemented).

Links to relevant issues: #5340, #11404.

@robert-hh
Copy link
Contributor Author

The review comments are integrated and the value of firstbit is shown by the object print method.

Being able to send data out in LSB format can be useful, and having support
in the low-level driver is much better than requiring Python code to
reorder the bits before sending them / after receiving them.  In particular
if the hardware does not support the LSB format (eg RP2040) then one needs
to use the SoftSPI in LSB mode.

For this change a default definition of `MICROPY_PY_MACHINE_SPI_MSB/_LSB`
was added to `py/mpconfig.h`, making them available to all ports.  The
identical defines in `esp32/mpconfigport.h` were deleted.

Resolves issues micropython#5340, micropython#11404.

Signed-off-by: robert-hh <robert@hammelrath.com>
@dpgeorge dpgeorge merged commit ee10360 into micropython:master Jul 12, 2024
62 of 63 checks passed
@robert-hh robert-hh deleted the softspi_lsb branch July 14, 2024 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0