Skip to content

Commit 46164c6

Browse files
authored
Merge pull request adafruit#1837 from nickzoic/circuitpython-nickzoic-1800-wiznet-socket
WIP: Circuitpython nickzoic 1800 wiznet socket
2 parents 03e000c + 0d08dde commit 46164c6

File tree

6 files changed

+113
-47
lines changed

6 files changed

+113
-47
lines changed

shared-bindings/socket/__init__.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ STATIC mp_int_t _socket_recv_into(mod_network_socket_obj_t *sock, byte *buf, mp_
248248
if (ret == -1) {
249249
mp_raise_OSError(_errno);
250250
}
251-
return len;
251+
return ret;
252252
}
253253

254254

shared-bindings/wiznet/wiznet5k.c

+40-13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "shared-bindings/digitalio/DigitalInOut.h"
4242
#include "shared-bindings/digitalio/DriveMode.h"
4343
#include "shared-bindings/busio/SPI.h"
44+
#include "shared-bindings/microcontroller/Pin.h"
4445

4546
#include "shared-module/network/__init__.h"
4647
#include "shared-module/wiznet/wiznet5k.h"
@@ -50,25 +51,44 @@
5051
//| :class:`WIZNET5K` -- wrapper for Wiznet 5500 Ethernet interface
5152
//| ===============================================================
5253
//|
53-
//| .. class:: WIZNET5K(spi, cs, rst)
54+
//| .. class:: WIZNET5K(spi, cs, rst, dhcp=True)
5455
//|
5556
//| Create a new WIZNET5500 interface using the specified pins
5657
//|
57-
//| :param spi: spi bus to use
58-
//| :param cs: pin to use for Chip Select
59-
//| :param rst: pin to sue for Reset
58+
//| :param ~busio.SPI spi: spi bus to use
59+
//| :param ~microcontroller.Pin cs: pin to use for Chip Select
60+
//| :param ~microcontroller.Pin rst: pin to use for Reset (optional)
61+
//| :param bool dhcp: boolean flag, whether to start DHCP automatically (optional, keyword only, default True)
62+
//|
63+
//| * The reset pin is optional: if supplied it is used to reset the
64+
//| wiznet board before initialization.
65+
//| * The SPI bus will be initialized appropriately by this library.
66+
//| * At present, the WIZNET5K object is a singleton, so only one WizNet
67+
//| interface is supported at a time.
6068
//|
6169

62-
STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
63-
// check arguments
64-
mp_arg_check_num(n_args, kw_args, 3, 3, false);
65-
66-
return wiznet5k_create(args[0], args[1], args[2]);
70+
STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
71+
enum { ARG_spi, ARG_cs, ARG_rst, ARG_dhcp };
72+
static const mp_arg_t allowed_args[] = {
73+
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
74+
{ MP_QSTR_cs, MP_ARG_REQUIRED | MP_ARG_OBJ },
75+
{ MP_QSTR_rst, MP_ARG_OBJ, { .u_obj = mp_const_none } },
76+
{ MP_QSTR_dhcp, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = true } },
77+
};
78+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
79+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
80+
// TODO check type of ARG_spi?
81+
assert_pin(args[ARG_cs].u_obj, false);
82+
assert_pin(args[ARG_rst].u_obj, true); // may be NULL
83+
84+
mp_obj_t ret = wiznet5k_create(args[ARG_spi].u_obj, args[ARG_cs].u_obj, args[ARG_rst].u_obj);
85+
if (args[ARG_dhcp].u_bool) wiznet5k_start_dhcp();
86+
return ret;
6787
}
6888

6989
//| .. attribute:: connected
7090
//|
71-
//| is this device physically connected?
91+
//| (boolean, readonly) is this device physically connected?
7292
//|
7393

7494
STATIC mp_obj_t wiznet5k_connected_get_value(mp_obj_t self_in) {
@@ -86,7 +106,9 @@ const mp_obj_property_t wiznet5k_connected_obj = {
86106

87107
//| .. attribute:: dhcp
88108
//|
89-
//| is DHCP active on this device? (set to true to activate DHCP, false to turn it off)
109+
//| (boolean, readwrite) is DHCP active on this device?
110+
//|
111+
//| * set to True to activate DHCP, False to turn it off
90112
//|
91113

92114
STATIC mp_obj_t wiznet5k_dhcp_get_value(mp_obj_t self_in) {
@@ -99,9 +121,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_dhcp_get_value_obj, wiznet5k_dhcp_get_
99121
STATIC mp_obj_t wiznet5k_dhcp_set_value(mp_obj_t self_in, mp_obj_t value) {
100122
(void)self_in;
101123
if (mp_obj_is_true(value)) {
102-
wiznet5k_start_dhcp();
124+
int ret = wiznet5k_start_dhcp();
125+
if (ret) mp_raise_OSError(ret);
103126
} else {
104-
wiznet5k_stop_dhcp();
127+
int ret = wiznet5k_stop_dhcp();
128+
if (ret) mp_raise_OSError(ret);
105129
}
106130
return mp_const_none;
107131
}
@@ -120,6 +144,7 @@ const mp_obj_property_t wiznet5k_dhcp_obj = {
120144
//| (ip_address, subnet_mask, gateway_address, dns_server)
121145
//|
122146
//| Or can be called with the same tuple to set those parameters.
147+
//| Setting ifconfig parameters turns DHCP off, if it was on.
123148
//|
124149

125150
STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) {
@@ -136,6 +161,7 @@ STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) {
136161
return mp_obj_new_tuple(4, tuple);
137162
} else {
138163
// set
164+
wiznet5k_stop_dhcp();
139165
mp_obj_t *items;
140166
mp_obj_get_array_fixed_n(args[1], 4, &items);
141167
netutils_parse_ipv4_addr(items[0], netinfo.ip, NETUTILS_BIG);
@@ -178,6 +204,7 @@ const mod_network_nic_type_t mod_network_nic_type_wiznet5k = {
178204
.settimeout = wiznet5k_socket_settimeout,
179205
.ioctl = wiznet5k_socket_ioctl,
180206
.timer_tick = wiznet5k_socket_timer_tick,
207+
.deinit = wiznet5k_socket_deinit,
181208
};
182209

183210
#endif // MICROPY_PY_WIZNET5K

shared-module/network/__init__.c

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ void network_module_init(void) {
4343
}
4444

4545
void network_module_deinit(void) {
46+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
47+
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
48+
mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
49+
if (nic_type->deinit != NULL) nic_type->deinit(nic);
50+
}
51+
mp_obj_list_set_len(&MP_STATE_PORT(mod_network_nic_list), 0);
4652
}
4753

4854
void network_module_background(void) {
@@ -93,3 +99,7 @@ void network_module_create_random_mac_address(uint8_t *mac) {
9399
mac[4] = (uint8_t)(rb2 >> 8);
94100
mac[5] = (uint8_t)(rb2);
95101
}
102+
103+
uint16_t network_module_create_random_source_tcp_port(void) {
104+
return 0xc000 | shared_modules_random_getrandbits(14);
105+
}

shared-module/network/__init__.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
void network_module_create_random_mac_address(uint8_t *mac);
29-
3028
#ifndef MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H
3129
#define MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H
3230

31+
void network_module_create_random_mac_address(uint8_t *mac);
32+
uint16_t network_module_create_random_source_tcp_port(void);
33+
3334
#define MOD_NETWORK_IPADDR_BUF_SIZE (4)
3435

3536
#define MOD_NETWORK_AF_INET (2)
@@ -62,6 +63,7 @@ typedef struct _mod_network_nic_type_t {
6263
int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
6364
int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
6465
void (*timer_tick)(struct _mod_network_socket_obj_t *socket);
66+
void (*deinit)(struct _mod_network_socket_obj_t *socket);
6567
} mod_network_nic_type_t;
6668

6769
typedef struct _mod_network_socket_obj_t {

shared-module/wiznet/wiznet5k.c

+54-28
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_
9393
}
9494
}
9595

96+
int get_available_socket(wiznet5k_obj_t *wiz) {
97+
for (uint8_t sn = 0; sn < _WIZCHIP_SOCK_NUM_; sn++) {
98+
if ((wiz->socket_used & (1 << sn)) == 0) {
99+
wiz->socket_used |= (1 << sn);
100+
return sn;
101+
}
102+
}
103+
return -1;
104+
}
105+
96106
int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
97107
if (socket->u_param.domain != MOD_NETWORK_AF_INET) {
98108
*_errno = MP_EAFNOSUPPORT;
@@ -107,13 +117,7 @@ int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
107117

108118
if (socket->u_param.fileno == -1) {
109119
// get first unused socket number
110-
for (mp_uint_t sn = 0; sn < _WIZCHIP_SOCK_NUM_; sn++) {
111-
if ((wiznet5k_obj.socket_used & (1 << sn)) == 0) {
112-
wiznet5k_obj.socket_used |= (1 << sn);
113-
socket->u_param.fileno = sn;
114-
break;
115-
}
116-
}
120+
socket->u_param.fileno = get_available_socket(&wiznet5k_obj);
117121
if (socket->u_param.fileno == -1) {
118122
// too many open sockets
119123
*_errno = MP_EMFILE;
@@ -199,8 +203,12 @@ int wiznet5k_socket_accept(mod_network_socket_obj_t *socket, mod_network_socket_
199203
}
200204

201205
int wiznet5k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) {
206+
uint16_t src_port = network_module_create_random_source_tcp_port();
207+
// make sure same outgoing port number can't be in use by two different sockets.
208+
src_port = (src_port & ~(_WIZCHIP_SOCK_NUM_ - 1)) | socket->u_param.fileno;
209+
202210
// use "bind" function to open the socket in client mode
203-
if (wiznet5k_socket_bind(socket, ip, 0, _errno) != 0) {
211+
if (wiznet5k_socket_bind(socket, NULL, src_port, _errno) != 0) {
204212
return -1;
205213
}
206214

@@ -318,33 +326,56 @@ int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, m
318326
}
319327

320328
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
321-
if (wiznet5k_obj.dhcp_active) {
329+
if (wiznet5k_obj.dhcp_socket >= 0) {
322330
DHCP_time_handler();
323331
DHCP_run();
324332
}
325333
}
326334

327-
void wiznet5k_start_dhcp(void) {
335+
int wiznet5k_start_dhcp(void) {
336+
// XXX this should throw an error if DHCP fails
328337
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
329338

330-
if (!wiznet5k_obj.dhcp_active) {
339+
if (wiznet5k_obj.dhcp_socket < 0) {
331340
// Set up the socket to listen on UDP 68 before calling DHCP_init
332-
WIZCHIP_EXPORT(socket)(0, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
333-
DHCP_init(0, dhcp_buf);
334-
wiznet5k_obj.dhcp_active = 1;
341+
wiznet5k_obj.dhcp_socket = get_available_socket(&wiznet5k_obj);
342+
if (wiznet5k_obj.dhcp_socket < 0) return MP_EMFILE;
343+
344+
WIZCHIP_EXPORT(socket)(wiznet5k_obj.dhcp_socket, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
345+
DHCP_init(wiznet5k_obj.dhcp_socket, dhcp_buf);
335346
}
347+
return 0;
336348
}
337349

338-
void wiznet5k_stop_dhcp(void) {
339-
if (wiznet5k_obj.dhcp_active) {
340-
wiznet5k_obj.dhcp_active = 0;
350+
int wiznet5k_stop_dhcp(void) {
351+
if (wiznet5k_obj.dhcp_socket >= 0) {
341352
DHCP_stop();
342-
WIZCHIP_EXPORT(close)(0);
353+
WIZCHIP_EXPORT(close)(wiznet5k_obj.dhcp_socket);
354+
wiznet5k_obj.socket_used &= ~(1 << wiznet5k_obj.dhcp_socket);
355+
wiznet5k_obj.dhcp_socket = -1;
343356
}
357+
return 0;
344358
}
345359

346360
bool wiznet5k_check_dhcp(void) {
347-
return wiznet5k_obj.dhcp_active;
361+
return wiznet5k_obj.dhcp_socket >= 0;
362+
}
363+
364+
void wiznet5k_reset(void) {
365+
if (wiznet5k_obj.rst.pin) {
366+
// hardware reset if using RST pin
367+
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 0);
368+
mp_hal_delay_us(10); // datasheet says 2us
369+
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 1);
370+
mp_hal_delay_ms(150); // datasheet says 150ms
371+
} else {
372+
// otherwise, software reset
373+
wizchip_sw_reset();
374+
}
375+
}
376+
377+
void wiznet5k_socket_deinit(mod_network_socket_obj_t *socket) {
378+
wiznet5k_reset();
348379
}
349380

350381
/// Create and return a WIZNET5K object.
@@ -354,9 +385,8 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
354385
wiznet5k_obj.base.type = (mp_obj_type_t*)&mod_network_nic_type_wiznet5k;
355386
wiznet5k_obj.cris_state = 0;
356387
wiznet5k_obj.spi = MP_OBJ_TO_PTR(spi_in);
357-
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.cs, cs_in);
358-
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
359388
wiznet5k_obj.socket_used = 0;
389+
wiznet5k_obj.dhcp_socket = -1;
360390

361391
/*!< SPI configuration */
362392
// XXX probably should check if the provided SPI is already configured, and
@@ -369,13 +399,11 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
369399
8 // 8 BITS
370400
);
371401

402+
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.cs, cs_in);
372403
common_hal_digitalio_digitalinout_switch_to_output(&wiznet5k_obj.cs, 1, DRIVE_MODE_PUSH_PULL);
373-
common_hal_digitalio_digitalinout_switch_to_output(&wiznet5k_obj.rst, 1, DRIVE_MODE_PUSH_PULL);
374404

375-
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 0);
376-
mp_hal_delay_us(10); // datasheet says 2us
377-
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 1);
378-
mp_hal_delay_ms(160); // datasheet says 150ms
405+
if (rst_in) common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
406+
wiznet5k_reset();
379407

380408
reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit);
381409
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
@@ -394,8 +422,6 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
394422
// seems we need a small delay after init
395423
mp_hal_delay_ms(250);
396424

397-
wiznet5k_start_dhcp();
398-
399425
// register with network module
400426
network_module_register_nic(&wiznet5k_obj);
401427

shared-module/wiznet/wiznet5k.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct _wiznet5k_obj_t {
3939
digitalio_digitalinout_obj_t cs;
4040
digitalio_digitalinout_obj_t rst;
4141
uint8_t socket_used;
42-
bool dhcp_active;
42+
int8_t dhcp_socket; // -1 for DHCP not in use
4343
} wiznet5k_obj_t;
4444

4545
int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip);
@@ -57,11 +57,12 @@ int wiznet5k_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level
5757
int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
5858
int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
5959
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket);
60+
void wiznet5k_socket_deinit(mod_network_socket_obj_t *socket);
6061
mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in);
6162
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in);
6263

63-
void wiznet5k_start_dhcp(void);
64-
void wiznet5k_stop_dhcp(void);
64+
int wiznet5k_start_dhcp(void);
65+
int wiznet5k_stop_dhcp(void);
6566
bool wiznet5k_check_dhcp(void);
6667

6768
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;

0 commit comments

Comments
 (0)