Skip to content

Commit 56ffd4e

Browse files
committed
Add functions and define for wiznet_pio_spi support.
1 parent 987cade commit 56ffd4e

File tree

12 files changed

+1175
-2
lines changed

12 files changed

+1175
-2
lines changed

ports/raspberrypi/Makefile

100644100755
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ SRC_CYW43 :=
8383
SRC_LWIP :=
8484
endif
8585

86+
ifeq ($(CIRCUITPY_WIZNET_PIO_SPI), 1)
87+
INC_WIZNET_PIO_SPI := \
88+
-isystem wiznet_pio_spi \
89+
90+
SRC_WIZNET_PIO_SPI := \
91+
wiznet_pio_spi/wizchip_pio_spi.c \
92+
93+
WIZNET_PIOASM = $(BUILD)/pioasm/pioasm/pioasm
94+
.PHONY: pioasmBuild
95+
pioasmBuild: $(WIZNET_PIOASM)
96+
97+
$(WIZNET_PIOASM):
98+
$(Q)cmake -S pioasm -B $(BUILD)/pioasm
99+
$(MAKE) -C $(BUILD)/pioasm pioasmBuild
100+
101+
$(BUILD)/wizchip_pio_spi.pio.h: wiznet_pio_spi/wizchip_pio_spi.pio $(WIZNET_PIOASM)
102+
$(Q)$(WIZNET_PIOASM) -o c-sdk $< $@
103+
$(BUILD)/wiznet_pio_spi/wizchip_pio_spi.o: $(BUILD)/wizchip_pio_spi.pio.h
104+
105+
$(BUILD)/genhdr/qstr.i.last: $(BUILD)/wizchip_pio_spi.pio.h
106+
107+
else
108+
INC_WIZNET_PIO_SPI :=
109+
SRC_WIZNET_PIO_SPI :=
110+
111+
endif
112+
86113
CHIP_VARIANT_LOWER = $(shell echo $(CHIP_VARIANT) | tr '[:upper:]' '[:lower:]')
87114

88115
INC += \
@@ -155,6 +182,7 @@ INC += \
155182
-isystem sdk/src/rp2_common/pico_time_adapter/include/ \
156183
-isystem sdk/src/rp2_common/pico_unique_id/include/ \
157184
$(INC_CYW43) \
185+
$(INC_WIZNET_PIO_SPI) \
158186
-Isdk_config \
159187
-I../../lib/tinyusb/src \
160188
-I../../supervisor/shared/usb \
@@ -541,6 +569,7 @@ SRC_C += \
541569
mphalport.c \
542570
$(SRC_CYW43) \
543571
$(SRC_LWIP) \
572+
$(SRC_WIZNET_PIO_SPI) \
544573

545574

546575
ifeq ($(CIRCUITPY_USB_HOST), 1)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
USB_VID = 0x2E8A
22
USB_PID = 0x0005
3-
USB_PRODUCT = "WIZNET W55RP20 EVB PICO"
4-
USB_MANUFACTURER = "WIZNET"
3+
USB_PRODUCT = "W55RP20-EVB-Pico"
4+
USB_MANUFACTURER = "WIZnet"
55

66
CHIP_VARIANT = RP2040
77
CHIP_FAMILY = rp2
88

99
EXTERNAL_FLASH_DEVICES = "W25Q16JVxQ"
1010

11+
CIRCUITPY_WIZNET_PIO_SPI = 1
1112
CIRCUITPY__EVE = 1
1213
CIRCUITPY_SSL = 1
1314
CIRCUITPY_USB_HOST = 0
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#include "shared-bindings/busio/WIZNET_PIO_SPI.h"
8+
9+
#include "shared/runtime/interrupt_char.h"
10+
#include "py/mperrno.h"
11+
#include "py/runtime.h"
12+
13+
#include "supervisor/board.h"
14+
#include "common-hal/microcontroller/Pin.h"
15+
#include "shared-bindings/microcontroller/Pin.h"
16+
17+
#include "hardware/dma.h"
18+
#include "hardware/gpio.h"
19+
#include "hardware/pio.h"
20+
#include "hardware/clocks.h"
21+
22+
#include "wiznet_pio_spi/wizchip_pio_spi.h"
23+
24+
#define NO_INSTANCE 0xff
25+
26+
#ifndef PIO_SPI_PREFERRED_PIO
27+
#define PIO_SPI_PREFERRED_PIO 1
28+
#endif
29+
30+
// All wiznet spi operations must start with writing a 3 byte header
31+
32+
wiznet_pio_spi_config_t wiznet_pio_spi_config;
33+
wiznet_pio_spi_handle_t wiznet_pio_spi_handle = NULL;
34+
35+
void common_hal_wiznet_pio_spi_construct(wiznet_pio_spi_obj_t *self,
36+
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
37+
const mcu_pin_obj_t *miso, bool half_duplex) {
38+
39+
if (half_duplex) {
40+
mp_raise_NotImplementedError_varg(MP_ERROR_TEXT("%q"), MP_QSTR_half_duplex);
41+
}
42+
43+
wiznet_pio_spi_config.data_in_pin = miso->number;
44+
wiznet_pio_spi_config.data_out_pin = mosi->number;
45+
wiznet_pio_spi_config.clock_pin = clock->number;
46+
47+
if (wiznet_pio_spi_handle != NULL) {
48+
wiznet_pio_spi_close(wiznet_pio_spi_handle);
49+
}
50+
51+
wiznet_pio_spi_handle = wiznet_pio_spi_open(&wiznet_pio_spi_config);
52+
(*wiznet_pio_spi_handle)->set_active(wiznet_pio_spi_handle);
53+
54+
}
55+
56+
bool common_hal_wiznet_pio_spi_deinited(wiznet_pio_spi_obj_t *self) {
57+
return wiznet_pio_spi_config.clock_pin == 0;
58+
}
59+
60+
void common_hal_wiznet_pio_spi_deinit(wiznet_pio_spi_obj_t *self) {
61+
if (common_hal_wiznet_pio_spi_deinited(self)) {
62+
return;
63+
}
64+
65+
common_hal_reset_pin(self->clock);
66+
common_hal_reset_pin(self->MOSI);
67+
common_hal_reset_pin(self->MISO);
68+
69+
wiznet_pio_spi_config.clock_pin = 0;
70+
}
71+
72+
bool common_hal_wiznet_pio_spi_configure(wiznet_pio_spi_obj_t *self,
73+
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
74+
75+
uint32_t clock = clock_get_hz(clk_sys);
76+
uint32_t clock_div = clock / baudrate;
77+
78+
if (clock_div > clock / 4) {
79+
clock_div = clock / 4;
80+
}
81+
82+
wiznet_pio_spi_config.clock_div_major = clock_div;
83+
wiznet_pio_spi_config.clock_div_minor = 0;
84+
85+
return true;
86+
}
87+
88+
bool common_hal_wiznet_pio_spi_try_lock(wiznet_pio_spi_obj_t *self) {
89+
if (common_hal_wiznet_pio_spi_deinited(self)) {
90+
return false;
91+
}
92+
93+
bool grabbed_lock = false;
94+
if (!self->has_lock) {
95+
grabbed_lock = true;
96+
self->has_lock = true;
97+
}
98+
return grabbed_lock;
99+
}
100+
101+
bool common_hal_wiznet_pio_spi_has_lock(wiznet_pio_spi_obj_t *self) {
102+
return self->has_lock;
103+
}
104+
105+
void common_hal_wiznet_pio_spi_unlock(wiznet_pio_spi_obj_t *self) {
106+
self->has_lock = false;
107+
}
108+
109+
bool common_hal_wiznet_pio_spi_write(wiznet_pio_spi_obj_t *self,
110+
const uint8_t *data, size_t len) {
111+
wiznet_pio_spi_write_buffer(data, len);
112+
return true;
113+
}
114+
115+
bool common_hal_wiznet_pio_spi_read(wiznet_pio_spi_obj_t *self,
116+
uint8_t *data, size_t len, uint8_t write_value) {
117+
wiznet_pio_spi_read_buffer(data, len);
118+
return true;
119+
}
120+
121+
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) {
122+
return wiznet_pio_spi_transfer(data_out, len, data_in, len);
123+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
#pragma once
8+
9+
#include "common-hal/microcontroller/Pin.h"
10+
11+
#include "py/obj.h"
12+
13+
#include "hardware/spi.h"
14+
15+
#define SPI_HEADER_LEN 3
16+
17+
typedef struct {
18+
mp_obj_base_t base;
19+
bool has_lock;
20+
const mcu_pin_obj_t *clock;
21+
const mcu_pin_obj_t *MOSI;
22+
const mcu_pin_obj_t *MISO;
23+
} wiznet_pio_spi_obj_t;
24+
25+
void reset_spi(void);

0 commit comments

Comments
 (0)