Skip to content
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

SPI mode 3 #21

Open
dordor3 opened this issue Jan 18, 2021 · 3 comments
Open

SPI mode 3 #21

dordor3 opened this issue Jan 18, 2021 · 3 comments

Comments

@dordor3
Copy link

dordor3 commented Jan 18, 2021

how can I change the spi mode to 3?
it's mentions that it was verify on Micron N25Q, but it required spi mode = 3
I see any option to change the spi mode

@eblot
Copy link
Owner

eblot commented Jan 18, 2021

It's been a very long time since I've used N25Q, but it used to support mode = 0:

N25Q64

@dordor3
Copy link
Author

dordor3 commented Jan 19, 2021

actually, I'm using MT25QL128ABA8ESF, which has the same JEDEC ID ads the N25Q.
in mode=0 I'm getting bit shift. if I change the script to mode=3 it work perfectly fine.
since I don't want to change the original script I wonder if there is a way to set this mode without editing.

@eblot
Copy link
Owner

eblot commented Jan 19, 2021

I can think of two hack-ish fully untested ways (there may be more):

  1. Use your pre-instantiated SpiPort
    from pyftdi.spi import SpiController
    from spiflash.serialflash import SerialFlashManager
    cs = 1
    frequency = 6E6
    spi = SpiController(url)
    # instantiate the port, with the proper mode
    _ = spi.get_port(cs, freq= frequency, mode=3)
    # instantiate the flash device, should use the existing port, w/ changing its mode
    flash = SerialFlashManager.get_from_controller(spi, cs, frequency)
    This one is fragile, as it relies on the fact that PyFtdi does not cross check a pre-existing setting, so it may break in the future...
  2. Change the mode once the flash is discovered
    from spiflash.serialflash import SerialFlashManager
    cs = 1
    frequency = 6E6
    flash = SerialFlashManager.get_flash_device(url, cs, frequency)
    flash._spi.set_mode(3)
    This one would only work if the flash is properly discovered in mode 0, the bit shifting you are observing is only seen during data transfer
  3. Re-implement SerialFlashManager.get_flash_device()
    from spiflash.serialflash import SerialFlashManager
    class MySerialFlashManager(SerialFlashManager):
        def get_flash_device(url: str, cs: int = 0, mode: int = 0, freq: Optional[float] = None) \
                -> '_SpiFlashDevice':
            ctrl = SpiController(cs_count=cs+1)
            ctrl.configure(url)
            spi = ctrl.get_port(cs, freq, mode)
            jedec = SerialFlashManager.read_jedec_id(spi)
            if not jedec:
                # it is likely that the latency setting is too low if this
                # condition is encountered
                raise SerialFlashUnknownJedec("Unable to read JEDEC Id")
            flash = SerialFlashManager._get_flash(spi, jedec)
            flash.set_spi_frequency(freq)
            return flash
    cs = 1
    frequency = 6E6
    flash = MySerialFlashManager.get_flash_device(url, cs, 3, frequency)
    This one may also break with a future update of SpiFlash.

In any case, please do not close this ticket, as SpiFlash definitely needs a way to set the mode.

@eblot eblot changed the title spa mode 3 SPI mode 3 Jan 19, 2021
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

No branches or pull requests

2 participants