I am using MicroPython for the master and PIO for the slave in an SPI communication setup between two RP2040 boards. The master sends data using MicroPython’s SPI module, and the slave successfully receives it using a PIO-based RX implementation. However, I am facing an issue where the master is not receiving data sent from the slave. The slave transmits data using a PIO-based TX implementation, but the master consistently receives 0x00. I suspect the problem might be related to how MISO is handled on the master side. Do I need additional configuration for MISO on the master, or is there an issue with my PIO TX implementation? Any guidance would be appreciated!
This is my Master code :This is my Slave code:
Any suggestion or help would be appriciated.
This is my Master code :
Code:
from machine import SoftSPI, Pinimport timeMISO_PIN = 0CS_PIN = 1CLK_PIN = 2MOSI_PIN = 3spi = SoftSPI(baudrate=115200, polarity=0, phase=0, sck=Pin(CLK_PIN), mosi=Pin(MOSI_PIN), miso=Pin(MISO_PIN, Pin.IN, Pin.PULL_UP))spi.init() # set the baudratecs = machine.Pin(CS_PIN, machine.Pin.OUT)# This works to send from master to slavespi.write(b"\x90")# Try to read from the slave with spi.read()print(spi.read(1))Code:
from machine import Pinfrom rp2 import PIO, StateMachine, asm_pio# Pin mappings, and frequencyPIO_BAUD = 115200*16PIO_MISO_PIN = Pin(0, Pin.OUT, Pin.PULL_UP)PIO_CS_PIN = Pin(1, Pin.IN, Pin.PULL_UP)PIO_CLK_PIN = Pin(2, Pin.IN, Pin.PULL_UP)PIO_MOSI_PIN = Pin(3, Pin.IN, Pin.PULL_UP)'''To convert the int data to hex format'''def convert_padded_hex(data): return "{:02x}".format(data)'''Clocked input code for PIO'''@asm_pio(autopush=True,push_thresh=8,in_shiftdir=PIO.SHIFT_LEFT, fifo_join=PIO.JOIN_RX)def spi_slave_pico_recv(): label("start") jmp("check_cs") # if chip select is low, continue, otherwise jump to check_cs wrap_target() wait(0, pins, 2) wait(1, pins, 2) in_(pins, 1) label("check_cs") jmp(pin, "start") # if chip select is high, go to 'start' state# Receive bytes from master when using spi.write() on masterreceive_sm = StateMachine(0, spi_slave_pico_recv, freq=PIO_BAUD, in_base=(PIO_MOSI_PIN), jmp_pin=(PIO_CS_PIN))receive_sm.active(1)# This works and able to receive, if we uncommentwhile True: if receive_sm.rx_fifo(): print(convert_padded_hex(receive_sm.get()))'''Clocked output code for PIO'''@asm_pio(autopull=True,pull_thresh=8,out_shiftdir=PIO.SHIFT_LEFT,out_init=PIO.OUT_LOW)def spi_slave_pico_send(): label("start") jmp("check_cs") # if chip select is low, continue, otherwise jump to check_cs wrap_target() wait(0, pins, 2) out(pins, 1) wait(1, pins, 2) label("check_cs") jmp(pin, "start") # if chip select is high, go to 'start' statesend_sm = StateMachine(1, spi_slave_pico_send, freq=PIO_BAUD, out_base=(PIO_MISO_PIN), in_base=(PIO_MOSI_PIN), jmp_pin=(PIO_CS_PIN))send_sm.active(1)while True: send_sm.put(0x10) Any suggestion or help would be appriciated.
Statistics: Posted by jayashree03 — Wed Apr 02, 2025 1:19 pm — Replies 0 — Views 13