Thanks to visit codestin.com
Credit goes to github.com

Skip to content

rp2pio: Enable read and read/write transactions #4173

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
wants to merge 2 commits into from

Conversation

jepler
Copy link

@jepler jepler commented Feb 10, 2021

Here's my test program, with a workaround for adafruit/Adafruit_CircuitPython_PIOASM#7 applied:

import adafruit_pioasm
import rp2pio
import board
import time

spi_cpha0_source = """
.program spi_cpha0
.side_set 1

.wrap_target
    out pins, 1 side 0 [1] ; Stall here on empty (sideset proceeds even if
    in pins,  1 side 1 [1] ; instruction stalls, so we stall with SCK low)
.wrap

"""

spi_cpha0_assembled = adafruit_pioasm.assemble(spi_cpha0_source)

pio_spi_sm = rp2pio.StateMachine(
    spi_cpha0_assembled,
    frequency=8000000,  # 2 MHz, / 4 (?) clocks per bit
    init=adafruit_pioasm.assemble("set pindirs 3"),
    first_sideset_pin=board.GP1,
    first_out_pin=board.GP0,
    first_in_pin=board.GP2,
    first_set_pin=board.GP0,
    set_pin_count=3,
    auto_pull=True,
    auto_push=True,
    out_shift_right=True,
    in_shift_right=True,
    pull_threshold=8,
    push_threshold=8,
)

print(dir(pio_spi_sm))
b = bytearray(4)
while True:
    b[0] = b[2] = 0xaa
    b[1] = b[3] = 0x55
    print(b, end=" -> ")
    pio_spi_sm.write_readinto(b, b)
    print(b)

I get pulses out on GP0/1 and if I connect GP0 to GP2 I read back the same value I write.

This doesn't actually work for me (adapting the duplex spi example),
though I think the problem lies either with my pio code or the construction
of the StateMachine.
@tannewt
Copy link
Member

tannewt commented Feb 10, 2021

We don't automatically set the pin direction for the output pins so you need init code to do it. Your current init uses the set instruction to do it but doesn't provide the first set pin (should be the same as output.)

We probably should automatically set the sideset direction automatically, otherwise we need to use set for it as well.

@jepler jepler changed the title WIP: rp2pio: Enable read and read/write transactions rp2pio: Enable read and read/write transactions Feb 12, 2021
@jepler
Copy link
Author

jepler commented Feb 12, 2021

@tannewt I know you have similar code. If you want to take yours in favor of mine (if so feel free to close this up!), make sure you have the equivalent of this change for getting the right shifted-in bits:

     if (rx) {
-        rx_source = (const volatile uint8_t*) &self->pio->rxf[self->state_machine];
-        if (!self->in_shift_right) {
+        rx_source = (volatile uint8_t*) &self->pio->rxf[self->state_machine];
+        if (self->in_shift_right) {
             rx_source += 3;
         }
     }

@tannewt
Copy link
Member

tannewt commented Feb 12, 2021

Ok, I deleted the ! which I think is the only change.

@jepler jepler closed this Feb 12, 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

Successfully merging this pull request may close these issues.

2 participants