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
1 change: 1 addition & 0 deletions src/rp2_common/pico_cyw43_driver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cc_library(
name = "pico_cyw43_driver",
srcs = [
"cyw43_bus_pio_spi.c",
"cyw43_bus_spi_common.c",
"cyw43_driver.c",
],
hdrs = [
Expand Down
1 change: 1 addition & 0 deletions src/rp2_common/pico_cyw43_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ if (EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE})
pico_add_library(cyw43_driver_picow NOFLAG)
target_sources(cyw43_driver_picow INTERFACE
${CMAKE_CURRENT_LIST_DIR}/cyw43_bus_pio_spi.c
${CMAKE_CURRENT_LIST_DIR}/cyw43_bus_spi_common.c
)
pico_generate_pio_header(cyw43_driver_picow ${CMAKE_CURRENT_LIST_DIR}/cyw43_bus_pio_spi.pio)
pico_mirrored_target_link_libraries(cyw43_driver_picow INTERFACE
Expand Down
192 changes: 0 additions & 192 deletions src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,6 @@ static uint32_t counter = 0;
#define DUMP_SPI_TRANSACTIONS(A)
#endif

//#define SWAP32(A) ((((A) & 0xff000000U) >> 8) | (((A) & 0xff0000U) << 8) | (((A) & 0xff00U) >> 8) | (((A) & 0xffU) << 8))
__force_inline static uint32_t __swap16x2(uint32_t a) {
#ifndef __riscv
pico_default_asm ("rev16 %0, %0" : "+l" (a) : : );
#else
uint32_t tmp;
pico_default_asm (
"rev8 %1, %0\n"
"rori %0, %1, 16\n"
: "+l" (a), "=l" (tmp));
#endif
return a;
}
#define SWAP32(a) __swap16x2(a)

typedef struct {
PIO pio;
uint pio_offset;
Expand Down Expand Up @@ -367,183 +352,6 @@ void cyw43_spi_reset(void) {
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) {
return ret;
}
return SWAP32(buf[1]);
}

static inline uint32_t _cyw43_read_reg(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint size) {
// Padding plus max read size of 32 bits + another 4?
static_assert(CYW43_BACKPLANE_READ_PAD_LEN_BYTES % 4 == 0, "");
int index = (CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4) + 1 + 1;
uint32_t buf32[index];
uint8_t *buf = (uint8_t *)buf32;
const uint32_t padding = (fn == BACKPLANE_FUNCTION) ? CYW43_BACKPLANE_READ_PAD_LEN_BYTES : 0; // Add response delay
buf32[0] = make_cmd(false, true, fn, reg, size);

if (fn == BACKPLANE_FUNCTION) {
logic_debug_set(pin_BACKPLANE_READ, 1);
}
int ret = cyw43_spi_transfer(self, NULL, 4, buf, 8 + padding);
if (fn == BACKPLANE_FUNCTION) {
logic_debug_set(pin_BACKPLANE_READ, 0);
}

if (ret != 0) {
return ret;
}
uint32_t result = buf32[padding > 0 ? index - 1 : 1];
CYW43_VDEBUG("cyw43_read_reg_u%d %s 0x%lx=0x%lx\n", size * 8, func_name(fn), reg, result);
return result;
}

uint32_t cyw43_read_reg_u32(cyw43_int_t *self, uint32_t fn, uint32_t reg) {
return _cyw43_read_reg(self, fn, reg, 4);
}

int cyw43_read_reg_u16(cyw43_int_t *self, uint32_t fn, uint32_t reg) {
return _cyw43_read_reg(self, fn, reg, 2);
}

int cyw43_read_reg_u8(cyw43_int_t *self, uint32_t fn, uint32_t reg) {
return _cyw43_read_reg(self, fn, reg, 1);
}

// This is only used to switch the word order on boot
int write_reg_u32_swap(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val) {
uint32_t buf[2];
// Boots up in little endian so command needs swapping too
buf[0] = SWAP32(make_cmd(true, true, fn, reg, 4));
buf[1] = SWAP32(val);
int ret = cyw43_spi_transfer(self, (uint8_t *)buf, 8, NULL, 0);
CYW43_VDEBUG("write_reg_u32_swap %s 0x%lx=0x%lx\n", func_name(fn), reg, val);
return ret;
}

static inline int _cyw43_write_reg(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val, uint size) {
uint32_t buf[2];
buf[0] = make_cmd(true, true, fn, reg, size);
buf[1] = val;

if (fn == BACKPLANE_FUNCTION) {
logic_debug_set(pin_BACKPLANE_WRITE, 1);
}

int ret = cyw43_spi_transfer(self, (uint8_t *)buf, 8, NULL, 0);

if (fn == BACKPLANE_FUNCTION) {
logic_debug_set(pin_BACKPLANE_WRITE, 0);
}

CYW43_VDEBUG("cyw43_write_reg_u%d %s 0x%lx=0x%lx\n", size * 8, func_name(fn), reg, val);
return ret;
}

int cyw43_write_reg_u32(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val) {
return _cyw43_write_reg(self, fn, reg, val, 4);
}

int cyw43_write_reg_u16(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint16_t val) {
return _cyw43_write_reg(self, fn, reg, val, 2);
}

int cyw43_write_reg_u8(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val) {
return _cyw43_write_reg(self, fn, reg, val, 1);
}

#if CYW43_BUS_MAX_BLOCK_SIZE > 0x7f8
Comment thread
peterharperuk marked this conversation as resolved.
Resolved
#error Block size is wrong for SPI
#endif

int cyw43_read_bytes(cyw43_int_t *self, uint32_t fn, uint32_t addr, size_t len, uint8_t *buf) {
assert(fn != BACKPLANE_FUNCTION || (len <= CYW43_BUS_MAX_BLOCK_SIZE));
const uint32_t padding = (fn == BACKPLANE_FUNCTION) ? CYW43_BACKPLANE_READ_PAD_LEN_BYTES : 0; // Add response delay
size_t aligned_len = (len + 3) & ~3;
assert(aligned_len > 0 && aligned_len <= 0x7f8);
assert(buf == self->spid_buf || buf < self->spid_buf || buf >= (self->spid_buf + sizeof(self->spid_buf)));
self->spi_header[padding > 0 ? 0 : (CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)] = make_cmd(false, true, fn, addr, len);
if (fn == WLAN_FUNCTION) {
logic_debug_set(pin_WIFI_RX, 1);
}
int ret = cyw43_spi_transfer(self, NULL, 4, (uint8_t *)&self->spi_header[padding > 0 ? 0 : (CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)], aligned_len + 4 + padding);
if (fn == WLAN_FUNCTION) {
logic_debug_set(pin_WIFI_RX, 0);
}
if (ret != 0) {
CYW43_PRINTF("cyw43_read_bytes error %d", ret);
return ret;
}
if (buf != self->spid_buf) { // avoid a copy in the usual case just to add the header
memcpy(buf, self->spid_buf, len);
}
return 0;
}

// See whd_bus_spi_transfer_bytes
// Note, uses spid_buf if src isn't using it already
// Apart from firmware download this appears to only be used for wlan functions?
int cyw43_write_bytes(cyw43_int_t *self, uint32_t fn, uint32_t addr, size_t len, const uint8_t *src) {
assert(fn != BACKPLANE_FUNCTION || (len <= CYW43_BUS_MAX_BLOCK_SIZE));
const size_t aligned_len = (len + 3) & ~3u;
assert(aligned_len > 0 && aligned_len <= 0x7f8);
if (fn == WLAN_FUNCTION) {
// Wait for FIFO to be ready to accept data
int f2_ready_attempts = 1000;
while (f2_ready_attempts-- > 0) {
uint32_t bus_status = cyw43_read_reg_u32(self, BUS_FUNCTION, SPI_STATUS_REGISTER);
if (bus_status & STATUS_F2_RX_READY) {
logic_debug_set(pin_F2_RX_READY_WAIT, 0);
break;
} else {
logic_debug_set(pin_F2_RX_READY_WAIT, 1);
}
}
if (f2_ready_attempts <= 0) {
CYW43_PRINTF("F2 not ready\n");
return CYW43_FAIL_FAST_CHECK(-CYW43_EIO);
}
}
if (src == self->spid_buf) { // avoid a copy in the usual case just to add the header
self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)] = make_cmd(true, true, fn, addr, len);
logic_debug_set(pin_WIFI_TX, 1);
int res = cyw43_spi_transfer(self, (uint8_t *)&self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)], aligned_len + 4, NULL, 0);
logic_debug_set(pin_WIFI_TX, 0);
return res;
} else {
// todo: would be nice to get rid of this. Only used for firmware download?
assert(src < self->spid_buf || src >= (self->spid_buf + sizeof(self->spid_buf)));
self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)] = make_cmd(true, true, fn, addr, len);
memcpy(self->spid_buf, src, len);
return cyw43_spi_transfer(self, (uint8_t *)&self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)], aligned_len + 4, NULL, 0);
}
}
#endif

#if CYW43_PIN_WL_DYNAMIC
Expand Down
Loading
Loading