Skip to content

Commit bca816f

Browse files
andrewleechdpgeorge
authored andcommitted
rp2: Add support for using Wiznet hardware as an Ethernet NIC.
Uses the extmod/network_wiznet5k driver to provide network connectivity. Signed-off-by: Andrew Leech <[email protected]>
1 parent 15fea3a commit bca816f

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,53 @@ if (MICROPY_PY_NETWORK_NINAW10)
244244
)
245245
endif()
246246

247+
if (MICROPY_PY_WIZNET5K)
248+
target_compile_definitions(${MICROPY_TARGET} PRIVATE
249+
MICROPY_PY_WIZNET5K=1
250+
WIZCHIP_PREFIXED_EXPORTS=1
251+
_WIZCHIP_=${MICROPY_PY_WIZNET5K}
252+
WIZCHIP_YIELD=mpy_wiznet_yield
253+
)
254+
255+
if (MICROPY_PY_LWIP)
256+
target_compile_definitions(${MICROPY_TARGET} PRIVATE
257+
# When using MACRAW mode (with lwIP), maximum buffer space must be used for the raw socket
258+
WIZCHIP_USE_MAX_BUFFER=1
259+
)
260+
endif()
261+
262+
target_include_directories(${MICROPY_TARGET} PRIVATE
263+
${MICROPY_DIR}/lib/wiznet5k/
264+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/
265+
)
266+
267+
list(APPEND MICROPY_SOURCE_LIB
268+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/W5100/w5100.c
269+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/W5100S/w5100s.c
270+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/W5200/w5200.c
271+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/W5300/w5300.c
272+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/W5500/w5500.c
273+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/socket.c
274+
${MICROPY_DIR}/lib/wiznet5k/Ethernet/wizchip_conf.c
275+
${MICROPY_DIR}/lib/wiznet5k/Internet/DNS/dns.c
276+
${MICROPY_DIR}/lib/wiznet5k/Internet/DHCP/dhcp.c
277+
)
278+
279+
list(APPEND MICROPY_SOURCE_EXTMOD
280+
${MICROPY_DIR}/extmod/modnetwork.c
281+
${MICROPY_DIR}/extmod/modusocket.c
282+
${MICROPY_DIR}/extmod/network_wiznet5k.c
283+
)
284+
285+
list(APPEND MICROPY_SOURCE_QSTR
286+
${MICROPY_DIR}/extmod/modnetwork.c
287+
${MICROPY_DIR}/extmod/modusocket.c
288+
${MICROPY_DIR}/extmod/network_wiznet5k.c
289+
)
290+
291+
string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/wiznet5k)
292+
endif()
293+
247294
# Define mpy-cross flags
248295
set(MICROPY_CROSS_FLAGS -march=armv7m)
249296

ports/rp2/machine_spi.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,15 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
269269
}
270270
}
271271

272+
machine_spi_obj_t *spi_from_mp_obj(mp_obj_t o) {
273+
if (mp_obj_is_type(o, &machine_spi_type)) {
274+
machine_spi_obj_t *self = MP_OBJ_TO_PTR(o);
275+
return self;
276+
} else {
277+
mp_raise_TypeError(MP_ERROR_TEXT("expecting an SPI object"));
278+
}
279+
}
280+
272281
STATIC const mp_machine_spi_p_t machine_spi_p = {
273282
.init = machine_spi_init,
274283
.transfer = machine_spi_transfer,

ports/rp2/modmachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ void machine_pin_init(void);
1717
void machine_pin_deinit(void);
1818
void machine_i2s_init0(void);
1919

20+
struct _machine_spi_obj_t *spi_from_mp_obj(mp_obj_t o);
21+
2022
#endif // MICROPY_INCLUDED_RP2_MODMACHINE_H

ports/rp2/mpconfigport.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#define MICROPY_PY_SYS_PLATFORM "rp2"
7878
#define MICROPY_PY_THREAD (1)
7979
#define MICROPY_PY_THREAD_GIL (0)
80+
#define MICROPY_THREAD_YIELD() mp_handle_pending(true)
8081

8182
// Extended modules
8283
#define MICROPY_EPOCH_IS_1970 (1)
@@ -163,12 +164,24 @@ extern const struct _mod_network_nic_type_t mod_network_nic_type_nina;
163164
#define MICROPY_PORT_ROOT_POINTER_NINAW10
164165
#endif
165166

167+
#if MICROPY_PY_WIZNET5K
168+
#if MICROPY_PY_LWIP
169+
extern const struct _mp_obj_type_t mod_network_nic_type_wiznet5k;
170+
#else
171+
extern const struct _mod_network_nic_type_t mod_network_nic_type_wiznet5k;
172+
#endif
173+
#define MICROPY_HW_NIC_WIZNET5K { MP_ROM_QSTR(MP_QSTR_WIZNET5K), MP_ROM_PTR(&mod_network_nic_type_wiznet5k) },
174+
#else
175+
#define MICROPY_HW_NIC_WIZNET5K
176+
#endif
177+
166178
#ifndef MICROPY_BOARD_NETWORK_INTERFACES
167179
#define MICROPY_BOARD_NETWORK_INTERFACES
168180
#endif
169181

170182
#define MICROPY_PORT_NETWORK_INTERFACES \
171183
MICROPY_HW_NIC_NINAW10 \
184+
MICROPY_HW_NIC_WIZNET5K \
172185
MICROPY_BOARD_NETWORK_INTERFACES \
173186

174187
#ifndef MICROPY_BOARD_ROOT_POINTERS

ports/rp2/mphalport.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "tusb.h"
3434
#include "uart.h"
3535
#include "hardware/rtc.h"
36+
#include "pico/unique_id.h"
3637

3738
#if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_ENABLE_USBDEV
3839

@@ -164,3 +165,20 @@ uint64_t mp_hal_time_ns(void) {
164165
uint64_t s = timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.min, t.sec);
165166
return s * 1000000000ULL;
166167
}
168+
169+
// Generate a random locally administered MAC address (LAA)
170+
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]) {
171+
pico_unique_board_id_t pid;
172+
pico_get_unique_board_id(&pid);
173+
buf[0] = 0x02; // LAA range
174+
buf[1] = (pid.id[7] << 4) | (pid.id[6] & 0xf);
175+
buf[2] = (pid.id[5] << 4) | (pid.id[4] & 0xf);
176+
buf[3] = (pid.id[3] << 4) | (pid.id[2] & 0xf);
177+
buf[4] = pid.id[1];
178+
buf[5] = (pid.id[0] << 2) | idx;
179+
}
180+
181+
// A board can override this if needed
182+
MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
183+
mp_hal_generate_laa_mac(idx, buf);
184+
}

ports/rp2/mphalport.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,13 @@ enum mp_hal_pin_interrupt_trigger {
136136

137137
void mp_hal_pin_interrupt(mp_hal_pin_obj_t pin, mp_obj_t handler, mp_uint_t trigger, bool hard);
138138

139+
enum {
140+
MP_HAL_MAC_WLAN0 = 0,
141+
MP_HAL_MAC_BDADDR,
142+
MP_HAL_MAC_ETH0,
143+
};
144+
145+
void mp_hal_get_mac(int idx, uint8_t buf[6]);
146+
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]);
147+
139148
#endif // MICROPY_INCLUDED_RP2_MPHALPORT_H

ports/rp2/mpnetworkport.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ static alarm_id_t lwip_alarm_id = -1;
3939
static bool lwip_can_poll = true;
4040
static bool lwip_poll_pending = false;
4141

42+
#if MICROPY_PY_WIZNET5K
43+
static bool wiznet_poll_pending = false;
44+
45+
void wiznet5k_poll(void);
46+
void wiznet5k_deinit(void);
47+
#endif
48+
4249
u32_t sys_now(void) {
4350
// Used by LwIP
4451
return mp_hal_ticks_ms();
@@ -57,6 +64,13 @@ void lwip_lock_acquire(void) {
5764

5865
void lwip_lock_release(void) {
5966
lwip_can_poll = false;
67+
#if MICROPY_PY_WIZNET5K
68+
if (wiznet_poll_pending) {
69+
wiznet5k_poll();
70+
wiznet_poll_pending = false;
71+
}
72+
#endif
73+
6074
if (lwip_poll_pending) {
6175
lwip_poll();
6276
lwip_poll_pending = false;
@@ -76,11 +90,29 @@ uint32_t lwip_try_poll(void) {
7690
return ret;
7791
}
7892

93+
#if MICROPY_PY_WIZNET5K
94+
void wiznet5k_try_poll(void) {
95+
if (lwip_can_poll) {
96+
lwip_can_poll = false;
97+
wiznet5k_poll();
98+
lwip_can_poll = true;
99+
} else {
100+
wiznet_poll_pending = true;
101+
}
102+
}
103+
#endif
104+
79105
STATIC int64_t alarm_callback(alarm_id_t id, void *user_data) {
106+
#if MICROPY_PY_WIZNET5K
107+
wiznet5k_try_poll();
108+
#endif
80109
return lwip_try_poll();
81110
}
82111

83112
void mod_network_lwip_init(void) {
113+
#if MICROPY_PY_WIZNET5K
114+
wiznet5k_deinit();
115+
#endif
84116
if (lwip_alarm_id != -1) {
85117
cancel_alarm(lwip_alarm_id);
86118
}

0 commit comments

Comments
 (0)