Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pio/spi/spi.pio
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static inline void pio_spi_init(PIO pio, uint sm, uint prog_offs, uint n_bits,
// and this is a cheesy way to get CPOL=1
gpio_set_outover(pin_sck, cpol ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL);
// SPI is synchronous, so bypass input synchroniser to reduce input delay.
hw_set_bits(&pio->input_sync_bypass, (pin_miso >= 32) ? (1u << (pin_miso - 16)) : (1u << pin_miso));
pio_set_input_sync_bypass_with_mask64(pio, 1ull << pin_miso, 1ull << pin_miso);

pio_sm_init(pio, sm, prog_offs, &c);
pio_sm_set_enabled(pio, sm, true);
Expand Down Expand Up @@ -157,7 +157,7 @@ static inline void pio_spi_cs_init(PIO pio, uint sm, uint prog_offs, uint n_bits
pio_gpio_init(pio, pin_sck);
pio_gpio_init(pio, pin_sck + 1);
gpio_set_outover(pin_sck, cpol ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL);
hw_set_bits(&pio->input_sync_bypass, (pin_miso >= 32) ? (1u << (pin_miso - 16)) : (1u << pin_miso));
pio_set_input_sync_bypass_with_mask64(pio, 1ull << pin_miso, 1ull << pin_miso);

uint entry_point = prog_offs + (cpha ? spi_cpha1_cs_offset_entry_point : spi_cpha0_cs_offset_entry_point);
pio_sm_init(pio, sm, entry_point, &c);
Expand Down
18 changes: 15 additions & 3 deletions pio/spi/spi_loopback.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#define BUF_SIZE 20

static int spi_example_passes;
static int spi_example_runs;

void test(const pio_spi_inst_t *spi) {
static uint8_t txbuf[BUF_SIZE];
static uint8_t rxbuf[BUF_SIZE];
Expand All @@ -40,17 +43,23 @@ void test(const pio_spi_inst_t *spi) {
printf(" %02x", (int) rxbuf[i]);
mismatch = mismatch || rxbuf[i] != txbuf[i];
}
if (mismatch)
if (mismatch) {
printf("\nNope\n");
else
} else {
printf("\nOK\n");
spi_example_passes++;
}
}

int main() {
stdio_init_all();

printf("spi_loopback start\n");
pio_spi_inst_t spi[2];
#if 1
float clkdiv = 1; // as fast as possible!
#else
float clkdiv = 31.25f; // 1 MHz @ 125 clk_sys
#endif

// pio_claim_free_sm_and_add_program_for_gpio_range finds a free pio and state machine for a program and sets the gpio base correctly
const uint pin_base = MIN(PIN_SCK, MIN(PIN_MOSI, PIN_MISO));
Expand All @@ -60,6 +69,7 @@ int main() {

for (int cpha = 0; cpha <= 1; ++cpha) {
for (int cpol = 0; cpol <= 1; ++cpol) {
spi_example_runs++;
printf("CPHA = %d, CPOL = %d\n", cpha, cpol);
pio_spi_init(spi[cpha].pio, spi[cpha].sm,
spi[cpha].offset,
Expand All @@ -75,4 +85,6 @@ int main() {
sleep_ms(10);
}
}
printf("Example %s\n", spi_example_passes == spi_example_runs ? "PASSED" : "FAILED");
assert(spi_example_passes == spi_example_runs);
}
Loading