8000 Add support for W55RP20-EVB-Pico and WIZNET_PIO_SPI communication. by wizhannah · Pull Request #10440 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

Add support for W55RP20-EVB-Pico and WIZNET_PIO_SPI communication. #10440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
29 changes: 29 additions & 0 deletions ports/raspberrypi/Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ SRC_CYW43 :=
SRC_LWIP :=
endif

ifeq ($(CIRCUITPY_WIZNET_PIO_SPI), 1)
INC_WIZNET_PIO_SPI := \
-isystem wiznet_pio_spi \

SRC_WIZNET_PIO_SPI := \
wiznet_pio_spi/wizchip_pio_spi.c \

WIZNET_PIOASM = $(BUILD)/pioasm/pioasm/pioasm
.PHONY: pioasmBuild
pioasmBuild: $(WIZNET_PIOASM)

$(WIZNET_PIOASM):
$(Q)cmake -S pioasm -B $(BUILD)/pioasm
$(MAKE) -C $(BUILD)/pioasm pioasmBuild

$(BUILD)/wizchip_pio_spi.pio.h: wiznet_pio_spi/wizchip_pio_spi.pio $(WIZNET_PIOASM)
$(Q)$(WIZNET_PIOASM) -o c-sdk $< $@
$(BUILD)/wiznet_pio_spi/wizchip_pio_spi.o: $(BUILD)/wizchip_pio_spi.pio.h

$(BUILD)/genhdr/qstr.i.last: $(BUILD)/wizchip_pio_spi.pio.h

else
INC_WIZNET_PIO_SPI :=
SRC_WIZNET_PIO_SPI :=

endif

CHIP_VARIANT_LOWER = $(shell echo $(CHIP_VARIANT) | tr '[:upper:]' '[:lower:]')

INC += \
Expand Down Expand Up @@ -155,6 +182,7 @@ INC += \
-isystem sdk/src/rp2_common/pico_time_adapter/include/ \
-isystem sdk/src/rp2_common/pico_unique_id/include/ \
$(INC_CYW43) \
$(INC_WIZNET_PIO_SPI) \
-Isdk_config \
-I../../lib/tinyusb/src \
-I../../supervisor/shared/usb \
Expand Down Expand Up @@ -541,6 +569,7 @@ SRC_C += \
mphalport.c \
$(SRC_CYW43) \
$(SRC_LWIP) \
$(SRC_WIZNET_PIO_SPI) \


ifeq ($(CIRCUITPY_USB_HOST), 1)
Expand Down
9 changes: 9 additions & 0 deletions ports/raspberrypi/boards/wiznet_w55rp20_evb_pico/board.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include "supervisor/board.h"

// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here.
30 changes: 30 additions & 0 deletions ports/raspberrypi/boards/wiznet_w55rp20_evb_pico/mpconfigboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#pragma once

#define MICROPY_HW_BOARD_NAME "W55RP20-EVB-Pico"
#define MICROPY_HW_MCU_NAME "rp2040"

#define MICROPY_HW_LED_STATUS (&pin_GPIO19)

#define DEFAULT_SPI_BUS_SCK (&pin_GPIO21)
#define DEFAULT_SPI_BUS_MOSI (&pin_GPIO23)
#define DEFAULT_SPI_BUS_MISO (&pin_GPIO22)

#define DEFAULT_UART_BUS_RX (&pin_GPIO1)
#define DEFAULT_UART_BUS_TX (&pin_GPIO0)

// Wiznet HW config.
#define MICROPY_HW_WIZNET_PIO_SPI_ID (0)
#define MICROPY_HW_WIZNET_PIO_SPI_BAUDRATE (20 * 1000 * 1000)
#define MICROPY_HW_WIZNET_PIO_SPI_SCK (21)
#define MICROPY_HW_WIZNET_PIO_SPI_MOSI (23)
#define MICROPY_HW_WIZNET_PIO_SPI_MISO (22)
#define MICROPY_HW_WIZNET_PIO_PIN_CS (20)
#define MICROPY_HW_WIZNET_PIO_PIN_RST (25)
// Connecting the INTN pin enables RECV interrupt handling of incoming data.
#define MICROPY_HW_WIZNET_PIN_INTN (24)
14 changes: 14 additions & 0 deletions ports/raspberrypi/boards/wiznet_w55rp20_evb_pico/mpconfigboard.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
USB_VID = 0x2E8A
USB_PID = 0x0005
USB_PRODUCT = "W55RP20-EVB-Pico"
USB_MANUFACTURER = "WIZnet"

CHIP_VARIANT = RP2040
CHIP_FAMILY = rp2

EXTERNAL_FLASH_DEVICES = "W25Q16JVxQ"

CIRCUITPY_WIZNET_PIO_SPI = 1
CIRCUITPY__EVE = 1
CIRCUITPY_SSL = 1
CIRCUITPY_USB_HOST = 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#pragma once

// Put board-specific pico-sdk definitions here. This file must exist.
73 changes: 73 additions & 0 deletions ports/raspberrypi/boards/wiznet_w55rp20_evb_pico/pins.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include "shared-bindings/board/__init__.h"

static const mp_rom_map_elem_t board_module_globals_table[] = {
CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS

{ MP_ROM_QSTR(MP_QSTR_GP0), MP_ROM_PTR(&pin_GPIO0) },
{ MP_ROM_QSTR(MP_QSTR_GP1), MP_ROM_PTR(&pin_GPIO1) },
{ MP_ROM_QSTR(MP_QSTR_GP2), MP_ROM_PTR(&pin_GPIO2) },
{ MP_ROM_QSTR(MP_QSTR_GP3), MP_ROM_PTR(&pin_GPIO3) },
{ MP_ROM_QSTR(MP_QSTR_GP4), MP_ROM_PTR(&pin_GPIO4) },
{ MP_ROM_QSTR(MP_QSTR_GP5), MP_ROM_PTR(&pin_GPIO5) },
{ MP_ROM_QSTR(MP_QSTR_GP6), MP_ROM_PTR(&pin_GPIO6) },
{ MP_ROM_QSTR(MP_QSTR_GP7), MP_ROM_PTR(&pin_GPIO7) },
{ MP_ROM_QSTR(MP_QSTR_GP8), MP_ROM_PTR(&pin_GPIO8) },
{ MP_ROM_QSTR(MP_QSTR_GP9), MP_ROM_PTR(&pin_GPIO9) },
{ MP_ROM_QSTR(MP_QSTR_GP10), MP_ROM_PTR(&pin_GPIO10) },
{ MP_ROM_QSTR(MP_QSTR_GP11), MP_ROM_PTR(&pin_GPIO11) },
{ MP_ROM_QSTR(MP_QSTR_GP12), MP_ROM_PTR(&pin_GPIO12) },
{ MP_ROM_QSTR(MP_QSTR_GP13), MP_ROM_PTR(&pin_GPIO13) },
{ MP_ROM_QSTR(MP_QSTR_GP14), MP_ROM_PTR(&pin_GPIO14) },
{ MP_ROM_QSTR(MP_QSTR_GP15), MP_ROM_PTR(&pin_GPIO15) },

{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO19) },
{ MP_ROM_QSTR(MP_QSTR_GP19), MP_ROM_PTR(&pin_GPIO19) },

{ MP_ROM_QSTR(MP_QSTR_W5K_CS), MP_ROM_PTR(&pin_GPIO20) },
{ MP_ROM_QSTR(MP_QSTR_GP20), MP_ROM_PTR(&pin_GPIO20) },

{ MP_ROM_QSTR(MP_QSTR_W5K_SCK), MP_ROM_PTR(&pin_GPIO21) },
{ MP_ROM_QSTR(MP_QSTR_GP21), MP_ROM_PTR(&pin_GPIO21) },

{ MP_ROM_QSTR(MP_QSTR_W5K_MISO), MP_ROM_PTR(&pin_GPIO22) },
{ MP_ROM_QSTR(MP_QSTR_GP22), MP_ROM_PTR(&pin_GPIO22) },

{ MP_ROM_QSTR(MP_QSTR_W5K_MOSI), MP_ROM_PTR(&pin_GPIO23) },
{ MP_ROM_QSTR(MP_QSTR_GP23), MP_ROM_PTR(&pin_GPIO23) },

{ MP_ROM_QSTR(MP_QSTR_W5K_INT), MP_ROM_PTR(&pin_GPIO24) },
{ MP_ROM_QSTR(MP_QSTR_GP24), MP_ROM_PTR(&pin_GPIO24) },

{ MP_ROM_QSTR(MP_QSTR_W5K_RST), MP_ROM_PTR(&pin_GPIO25) },
{ MP_ROM_QSTR(MP_QSTR_GP25), MP_ROM_PTR(&pin_GPIO25) },

{ MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_GPIO26) },
{ MP_ROM_QSTR(MP_QSTR_GP26_A0), MP_ROM_PTR(&pin_GPIO26) },

{ MP_ROM_QSTR(MP_QSTR_GP26), MP_ROM_PTR(&pin_GPIO26) },
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_GPIO26) },

{ MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_GPIO27) },
{ MP_ROM_QSTR(MP_QSTR_GP27_A1), MP_ROM_PTR(&pin_GPIO27) },

{ MP_ROM_QSTR(MP_QSTR_GP27), MP_ROM_PTR(&pin_GPIO27) },
{ MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_GPIO27) },

{ MP_ROM_QSTR(MP_QSTR_GP28_A2), MP_ROM_PTR(&pin_GPIO28) },
{ MP_ROM_QSTR(MP_QSTR_GP28), MP_ROM_PTR(&pin_GPIO28) },
{ MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_GPIO28) },

{ MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_GPIO29) },
{ MP_ROM_QSTR(MP_QSTR_VOLTAGE_MONITOR), MP_ROM_PTR(&pin_GPIO29) },

{ MP_ROM_QSTR(MP_QSTR_W5K_SPI), MP_ROM_PTR(&board_spi_obj) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
{ MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_i2c_obj) },
};
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);
123 changes: 123 additions & 0 deletions ports/raspberrypi/common-hal/busio/WIZNET_PIO_SPI.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#include "shared-bindings/busio/WIZNET_PIO_SPI.h"

#include "shared/runtime/interrupt_char.h"
#include "py/mperrno.h"
#include "py/runtime.h"

#include "supervisor/board.h"
#include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/Pin.h"

#include "hardware/dma.h"
#include "hardware/gpio.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"

#include "wiznet_pio_spi/wizchip_pio_spi.h"

#define NO_INSTANCE 0xff

#ifndef PIO_SPI_PREFERRED_PIO
#define PIO_SPI_PREFERRED_PIO 1
#endif

// All wiznet spi operations must start with writing a 3 byte header

wiznet_pio_spi_config_t wiznet_pio_spi_config;
wiznet_pio_spi_handle_t wiznet_pio_spi_handle = NULL;

void common_hal_wiznet_pio_spi_construct(wiznet_pio_spi_obj_t *self,
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
const mcu_pin_obj_t *miso, bool half_duplex) {

if (half_duplex) {
mp_raise_NotImplementedError_varg(MP_ERROR_TEXT("%q"), MP_QSTR_half_duplex);
}

wiznet_pio_spi_config.data_in_pin = miso->number;
wiznet_pio_spi_config.data_out_pin = mosi->number;
wiznet_pio_spi_config.clock_pin = clock->number;

if (wiznet_pio_spi_handle != NULL) {
wiznet_pio_spi_close(wiznet_pio_spi_handle);
}

wiznet_pio_spi_handle = wiznet_pio_spi_open(&wiznet_pio_spi_config);
(*wiznet_pio_spi_handle)->set_active(wiznet_pio_spi_handle);

}

bool common_hal_wiznet_pio_spi_deinited(wiznet_pio_spi_obj_t *self) {
return wiznet_pio_spi_config.clock_pin == 0;
}

void common_hal_wiznet_pio_spi_deinit(wiznet_pio_spi_obj_t *self) {
if (common_hal_wiznet_pio_spi_deinited(self)) {
return;
}

common_hal_reset_pin(self->clock);
common_hal_reset_pin(self->MOSI);
common_hal_reset_pin(self->MISO);

wiznet_pio_spi_config.clock_pin = 0;
}

bool common_hal_wiznet_pio_spi_configure(wiznet_pio_spi_obj_t *self,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {

uint32_t clock = clock_get_hz(clk_sys);
uint32_t clock_div = clock / baudrate;

if (clock_div > clock / 4) {
clock_div = clock / 4;
}

wiznet_pio_spi_config.clock_div_major = clock_div;
wiznet_pio_spi_config.clock_div_minor = 0;

return true;
}

bool common_hal_wiznet_pio_spi_try_lock(wiznet_pio_spi_obj_t *self) {
if (common_hal_wiznet_pio_spi_deinited(self)) {
return false;
}

bool grabbed_lock = false;
if (!self->has_lock) {
grabbed_lock = true;
self->has_lock = true;
}
return grabbed_lock;
}

bool common_hal_wiznet_pio_spi_has_lock(wiznet_pio_spi_obj_t *self) {
return self->has_lock;
}

void common_hal_wiznet_pio_spi_unlock(wiznet_pio_spi_obj_t *self) {
self->has_lock = false;
}

bool common_hal_wiznet_pio_spi_write(wiznet_pio_spi_obj_t *self,
const uint8_t *data, size_t len) {
wiznet_pio_spi_write_buffer(data, len);
return true;
}

bool common_hal_wiznet_pio_spi_read(wiznet_pio_spi_obj_t *self,
uint8_t *data, size_t len, uint8_t write_value) {
wiznet_pio_spi_read_buffer(data, len);
return true;
}

bool common_hal_wiznet_pio_spi_transfer(wiznet_pio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len) {
return wiznet_pio_spi_transfer(data_out, len, data_in, len);
}
25 changes: 25 additions & 0 deletions ports/raspberrypi/common-hal/busio/WIZNET_PIO_SPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT

#pragma once

#include "common-hal/microcontroller/Pin.h"

#include "py/obj.h"

#include "hardware/spi.h"

#define SPI_HEADER_LEN 3

typedef struct {
mp_obj_base_t base;
bool has_lock;
const mcu_pin_obj_t *clock;
const mcu_pin_obj_t *MOSI;
const mcu_pin_obj_t *MISO;
} wiznet_pio_spi_obj_t;

void reset_spi(void);
Loading
Loading
0