Repository: raspberrypi/pico-sdk
File: src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.c (C)

Code before:
        )
        panic_unsupported();
    }
    pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_mov(pio_pins, pio_null)); // for next time we turn output on

    stop_spi_comms();
    DUMP_SPI_TRANSACTIONS(
            printf("RXed:");
            dump_bytes(rx, rx_length);
            printf("\n");
    )

    return 0;
}

// Initialise our gpios
void cyw43_spi_gpio_setup(void) {
    // Setup CYW43_WL_REG_ON_PIN (23)
    gpio_init(CYW43_WL_REG_ON_PIN);
    gpio_set_dir(CYW43_WL_REG_ON_PIN, GPIO_OUT);
    gpio_pull_up(CYW43_WL_REG_ON_PIN);

    // Setup DO, DI and IRQ (24)
    gpio_init(CYW43_DATA_OUT_PIN);
    gpio_set_dir(CYW43_DATA_OUT_PIN, GPIO_OUT);
    gpio_put(CYW43_DATA_OUT_PIN, false);

    // Setup CS (25)
    gpio_init(CYW43_CS_PIN);
    gpio_set_dir(CYW43_CS_PIN, GPIO_OUT);
    gpio_put(CYW43_CS_PIN, true);
}

// Reset wifi chip
void cyw43_spi_reset(void) {
    gpio_put(CYW43_WL_REG_ON_PIN, false); // off
    sleep_ms(20);
    gpio_put(CYW43_WL_REG_ON_PIN, true); // on
    sleep_ms(250);

    // Setup IRQ (24) - also used for DO, DI
    gpio_init(CYW43_IRQ_PIN);
    gpio_set_dir(CYW43_IRQ_PIN, GPIO_IN);
}

static inline uint32_t make_cmd(bool write, bool inc, uint32_t fn, uint32_t addr, uint32_t sz) {
    return write << 31 | inc << 30 | fn << 28 | (addr & 0x1ffff) << 11 | sz;
}

#if CYW43_VERBOSE_DEBUG
static const char *func_name(int fn) {

Review comment (refactor):
I guess this implies that the code would need quite a bit of refactoring if `CYW43_DATA_OUT_PIN`, `CYW43_DATA_IN_PIN` and `CYW43_IRQ_PIN` **weren't** all set to the same value? I wonder if it might be worth enforcing that with a compile-time assertion? :thinking: 

Code after:
    gpio_pull_up(CYW43_PIN_WL_REG_ON);

    // Setup DO, DI and IRQ (24)
    gpio_init(CYW43_PIN_WL_DATA_OUT);
    gpio_set_dir(CYW43_PIN_WL_DATA_OUT, GPIO_OUT);
    gpio_put(CYW43_PIN_WL_DATA_OUT, false);

    // Setup CS (25)
    gpio_init(CYW43_PIN_WL_CS);
    gpio_set_dir(CYW43_PIN_WL_CS, GPIO_OUT);
    gpio_put(CYW43_PIN_WL_CS, true);
}

// Reset wifi chip
void cyw43_spi_reset(void) {
    gpio_put(CYW43_PIN_WL_REG_ON, false); // off
    sleep_ms(20);
    gpio_put(CYW43_PIN_WL_REG_ON, true); // on
    sleep_ms(250);

    // Setup IRQ (24) - also used for DO, DI
    gpio_init(CYW43_PIN_WL_HOST_WAKE);
    gpio_set_dir(CYW43_PIN_WL_HOST_WAKE, GPIO_IN);
}

static inline uint32_t make_cmd(bool write, bool inc, uint32_t fn, uint32_t addr, uint32_t sz) {
    return write << 31 | inc << 30 | fn << 28 | (addr & 0x1ffff) << 11 | sz;
}

#if CYW43_VERBOSE_DEBUG
static const char *func_name(int fn) {
    switch (fn)
    {
        case BUS_FUNCTION:
            return "BUS_FUNCTION";
        case BACKPLANE_FUNCTION:
            return "BACKPLANE_FUNCTION";
        case WLAN_FUNCTION:
            return "WLAN_FUNCTION";
        default:
            return "UNKNOWN";
    }
}
#endif

uint32_t read_reg_u32_swap(cyw43_int_t *self, uint32_t fn, uint32_t reg) {
    uint32_t buf[2] = {0};
    assert(fn != BACKPLANE_FUNCTION);
    buf[0] = SWAP32(make_cmd(false, true, fn, reg, 4));
    int ret = cyw43_spi_transfer(self, NULL, 4, (uint8_t *)buf, 8);
    if (ret != 0) {