Skip to content
Open
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,8 @@

#define BUF_SIZE 20

static uint32_t match_count;

void test(const pio_spi_inst_t *spi) {
static uint8_t txbuf[BUF_SIZE];
static uint8_t rxbuf[BUF_SIZE];
Expand All @@ -40,26 +42,34 @@ 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");
match_count++;
}
}

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));
const uint pin_count = MAX(PIN_SCK, MAX(PIN_MOSI, PIN_MISO)) - pin_base + 1;
hard_assert(pio_claim_free_sm_and_add_program_for_gpio_range(&spi_cpha0_program, &spi[0].pio, &spi[0].sm, &spi[0].offset, pin_base, pin_count, true));
hard_assert(pio_claim_free_sm_and_add_program_for_gpio_range(&spi_cpha1_program, &spi[1].pio, &spi[1].sm, &spi[1].offset, pin_base, pin_count, true));

int spi_example_runs = 0;
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", match_count == spi_example_runs ? "PASSED" : "FAILED");
assert(match_count == spi_example_runs);
}
Loading