forked from M-Labs/artiq
1
0
Fork 0

test/spi: port to NAC3

This commit is contained in:
Sébastien Bourdeauducq 2024-08-19 22:15:17 +08:00
parent 5ffa6e0522
commit 8183ed1eb0
1 changed files with 20 additions and 15 deletions

View File

@ -3,16 +3,21 @@ from artiq.experiment import *
from artiq.test.hardware_testbench import ExperimentCase from artiq.test.hardware_testbench import ExperimentCase
from artiq.language.core import kernel from artiq.language.core import kernel
from artiq.language.units import us from artiq.language.units import us
from artiq.coredevice import spi2 as spi from artiq.coredevice.core import Core
from artiq.coredevice.spi2 import *
_SDCARD_SPI_CONFIG = (0*spi.SPI_OFFLINE | 0*spi.SPI_END | _SDCARD_SPI_CONFIG = (0*SPI_OFFLINE | 0*SPI_END |
0*spi.SPI_INPUT | 0*spi.SPI_CS_POLARITY | 0*SPI_INPUT | 0*SPI_CS_POLARITY |
0*spi.SPI_CLK_POLARITY | 0*spi.SPI_CLK_PHASE | 0*SPI_CLK_POLARITY | 0*SPI_CLK_PHASE |
0*spi.SPI_LSB_FIRST | 0*spi.SPI_HALF_DUPLEX) 0*SPI_LSB_FIRST | 0*SPI_HALF_DUPLEX)
@nac3
class CardTest(EnvExperiment): class CardTest(EnvExperiment):
core: KernelInvariant[Core]
spi_mmc: KernelInvariant[SPIMaster]
def build(self): def build(self):
self.setattr_device("core") self.setattr_device("core")
self.setattr_device("spi_mmc") self.setattr_device("spi_mmc")
@ -20,18 +25,18 @@ class CardTest(EnvExperiment):
@kernel @kernel
def run(self): def run(self):
self.core.reset() self.core.reset()
delay(1*ms) self.core.delay(1.*ms)
freq = 1*MHz freq = 1.*MHz
cs = 1 cs = 1
# run a couple of clock cycles with miso high to wake up the card # run a couple of clock cycles with miso high to wake up the card
self.spi_mmc.set_config(_SDCARD_SPI_CONFIG, 32, freq, 0) self.spi_mmc.set_config(_SDCARD_SPI_CONFIG, 32, freq, 0)
for i in range(10): for i in range(10):
self.spi_mmc.write(0xffffffff) self.spi_mmc.write(-1)
self.spi_mmc.set_config(_SDCARD_SPI_CONFIG | spi.SPI_END, 32, freq, 0) self.spi_mmc.set_config(_SDCARD_SPI_CONFIG | SPI_END, 32, freq, 0)
self.spi_mmc.write(0xffffffff) self.spi_mmc.write(-1)
delay(200*us) self.core.delay(200.*us)
self.spi_mmc.set_config(_SDCARD_SPI_CONFIG, 8, freq, cs) self.spi_mmc.set_config(_SDCARD_SPI_CONFIG, 8, freq, cs)
self.spi_mmc.write(0x40 << 24) # CMD self.spi_mmc.write(0x40 << 24) # CMD
@ -39,20 +44,20 @@ class CardTest(EnvExperiment):
self.spi_mmc.write(0x00000000) # ARG self.spi_mmc.write(0x00000000) # ARG
self.spi_mmc.set_config(_SDCARD_SPI_CONFIG, 8, freq, cs) self.spi_mmc.set_config(_SDCARD_SPI_CONFIG, 8, freq, cs)
self.spi_mmc.write(0x95 << 24) # CRC self.spi_mmc.write(0x95 << 24) # CRC
self.spi_mmc.set_config(_SDCARD_SPI_CONFIG | spi.SPI_INPUT, 8, freq, cs) self.spi_mmc.set_config(_SDCARD_SPI_CONFIG | SPI_INPUT, 8, freq, cs)
idle = False idle = False
response = 0 response = 0
for i in range(8): for i in range(8):
self.spi_mmc.write(0xff << 24) # NCR self.spi_mmc.write(0xff << 24) # NCR
response = self.spi_mmc.read() response = self.spi_mmc.read()
delay(100*us) self.core.delay(100.*us)
if response == 0x01: if response == 0x01:
idle = True idle = True
break break
self.spi_mmc.set_config(_SDCARD_SPI_CONFIG | spi.SPI_END, 8, freq, cs) self.spi_mmc.set_config(_SDCARD_SPI_CONFIG | SPI_END, 8, freq, cs)
self.spi_mmc.write(0xff << 24) self.spi_mmc.write(0xff << 24)
if not idle: if not idle:
print(response) print_rpc(response)
raise ValueError("SD Card did not reply with IDLE") raise ValueError("SD Card did not reply with IDLE")