8000 RP2: machine.spi LSB first is unsupported (also SoftSPI) · Issue #11404 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

RP2: machine.spi LSB first is unsupported (also SoftSPI) #11404

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

Closed
peterhinch opened this issue May 3, 2023 · 11 comments
Closed

RP2: machine.spi LSB first is unsupported (also SoftSPI) #11404

peterhinch opened this issue May 3, 2023 · 11 comments
Labels

Comments

@peterhinch
Copy link
Contributor

This arose here. The driver sets up a passed SPI instance as follows:

    def __init__(self, spi, pincs, height=240, width=400, vcom=False):
        spi.init(baudrate=2_000_000, firstbit=machine.SPI.LSB)
        self._spi = spi

which fails with NotImplementedError: LSB (hard SPI) or ValueError:firstbit must be MSB (soft SPI).

The constant LSB does exist.

>>> dir(SPI)
['__class__', '__name__', 'read', 'readinto', 'write', '__bases__', '__dict__', 'LSB', 'MSB', 'deinit', 'init', 'write_readinto']
@peterhinch peterhinch added the bug label May 3, 2023
@slabua
Copy link
slabua commented Jul 9, 2024

Any update?

@robert-hh
Copy link
Contributor

I made a PR once for LSB first with SoftSPI, but closed it since there were too many PRs open. I can re-open that PR, such that people can build their own firmware based on it.

@robert-hh
Copy link
Contributor

See PR #15436.

@slabua
Copy link
slabua commented Jul 10, 2024

@robert-hh thank you!

@dpgeorge
Copy link
Member

The RP2040 hardware SPI doesn't support LSB mode. And it's quite difficult to synthesize LSB mode: eg if the driver transforms all the bytes before sending them out that won't be possible if the data to send is a read-only bytes object (and in ROM).

So I think the rp2 port just has to remain without SPI.LSB for hardware SPI. Instead one would need to use software SPI if LSB mode is needed (and software SPI.LSB will be implemented in #15436).

@slabua
Copy link
slabua commented Jul 12, 2024

@dpgeorge i see thanks. Currently I am using hardware SPI by preprocessing the data thru a function that converts MSB to LSB. Not optimal but the only way I found.

@dpgeorge
Copy link
Member

Currently I am using hardware SPI by preprocessing the data thru a function that converts MSB to LSB. Not optimal but the only way I found.

Once #15436 is merged I suggest you use machine.SoftSPI in LSB mode.

@slabua
Copy link
slabua commented Jul 12, 2024

@dpgeorge thanks, will do!

dpgeorge pushed a commit to robert-hh/micropython that referenced this issue Jul 12, 2024
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
Copy link
Member

LSB for SoftSPI was implemented in ee10360

@peterhinch I think we can now close this issue. I know rp2 hardware SPI still doesn't support LSB, but that's a hardware limitation that we can't really resolve. Unless it automatically switches to software SPI when configured in LSB mode... the argument for that would be to make it transparently support LSB mode, instead of the user having to explicitly use SoftSPI when they want LSB.

@mattytrentini
Copy link
Contributor

@peterhinch I think we can now close this issue. I know rp2 hardware SPI still doesn't support LSB, but that's a hardware limitation that we can't really resolve. Unless it automatically switches to software SPI when configured in LSB mode...

What's the behaviour when you try to use HW SPI and LSB on a port that doesn't have it? Ideally I think it should not be a syntax error (ie firstbit should still be present) but an exception should be raised (ValueError?) that indicates that the option is not available for that port. I don't think it should automatically switch to SoftSPI...

@dpgeorge
Copy link
Member

What's the behaviour when you try to use HW SPI and LSB on a port that doesn't have it?

On rp2, if you try to construct a hardware SPI object in LSB mode, it raises a NotImplementedError().

I don't think it should automatically switch to SoftSPI...

As a counterpoint, the rp2 hardware I2C implementation switches automatically to software I2C for zero-length writes, eg i2c.write(42, b""). That's because the RP2040 hardware doesn't support zero-length I2C write transactions. In this case it would be very annoying if it raised an exception for zero-length writes, because it would mean that you'd need to manually switch to software I2C if you wanted a zero-length write but wanted to take advantage of hardware I2C for all non-zero-length writes.

But in the case of SPI LSB mode, arguably you don't switch between LSB and MSB dynamically. Rather, you pick one of those modes at the start and stick with it. So if you need LSB then just use machine.SoftSPI.

graeme-winter pushed a commit to winter-special-projects/micropython that referenced this issue Sep 21, 2024
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants
0