Skip to content

Commit 9c90435

Browse files
committed
Merge branch 'circuitpython/nickzoic/703-wiznet-5500-native-dhcp' into circuitpython/nickzoic/703-wiznet-5500-native
2 parents 5bb1279 + b714f5d commit 9c90435

File tree

6 files changed

+93
-29
lines changed

6 files changed

+93
-29
lines changed

ports/atmel-samd/background.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "usb_mass_storage.h"
3232

3333
#include "shared-module/displayio/__init__.h"
34+
#include "shared-module/network/__init__.h"
3435

3536
volatile uint64_t last_finished_tick = 0;
3637

@@ -41,6 +42,9 @@ void run_background_tasks(void) {
4142
#ifdef CIRCUITPY_DISPLAYIO
4243
displayio_refresh_display();
4344
#endif
45+
#ifdef MICROPY_PY_NETWORK
46+
network_module_background();
47+
#endif
4448
usb_msc_background();
4549
usb_cdc_background();
4650
last_finished_tick = ticks_ms;

shared-bindings/wiznet/wiznet5k.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,54 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
6666
return wiznet5k_create(args[0], args[1], args[2]);
6767
}
6868

69+
//| .. attribute:: connected
70+
//|
71+
//| is this device physically connected?
72+
//|
73+
6974
STATIC mp_obj_t wiznet5k_connected_get_value(mp_obj_t self_in) {
7075
(void)self_in;
7176
return mp_obj_new_bool(wizphy_getphylink() == PHY_LINK_ON);
7277
}
7378
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_connected_get_value_obj, wiznet5k_connected_get_value);
7479

75-
//| .. attribute:: connected
76-
//|
77-
//| is this device physically connected?
78-
//|
79-
8080
const mp_obj_property_t wiznet5k_connected_obj = {
8181
.base.type = &mp_type_property,
8282
.proxy = {(mp_obj_t)&wiznet5k_connected_get_value_obj,
8383
(mp_obj_t)&mp_const_none_obj,
8484
(mp_obj_t)&mp_const_none_obj},
8585
};
8686

87+
//| .. attribute:: dhcp
88+
//|
89+
//| is DHCP active on this device? (set to true to activate DHCP, false to turn it off)
90+
//|
91+
92+
STATIC mp_obj_t wiznet5k_dhcp_get_value(mp_obj_t self_in) {
93+
(void)self_in;
94+
return mp_obj_new_bool(wiznet5k_check_dhcp());
95+
}
96+
97+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_dhcp_get_value_obj, wiznet5k_dhcp_get_value);
98+
99+
STATIC mp_obj_t wiznet5k_dhcp_set_value(mp_obj_t self_in, mp_obj_t value) {
100+
(void)self_in;
101+
if (mp_obj_is_true(value)) {
102+
wiznet5k_start_dhcp();
103+
} else {
104+
wiznet5k_stop_dhcp();
105+
}
106+
return mp_const_none;
107+
}
108+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(wiznet5k_dhcp_set_value_obj, wiznet5k_dhcp_set_value);
109+
110+
const mp_obj_property_t wiznet5k_dhcp_obj = {
111+
.base.type = &mp_type_property,
112+
.proxy = {(mp_obj_t)&wiznet5k_dhcp_get_value_obj,
113+
(mp_obj_t)&wiznet5k_dhcp_set_value_obj,
114+
(mp_obj_t)&mp_const_none_obj},
115+
};
116+
87117
//| .. method:: ifconfig(...)
88118
//|
89119
//| Called without parameters, returns a tuple of
@@ -121,6 +151,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_ifconfig_obj, 1, 2, wiznet5k
121151
STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = {
122152
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) },
123153
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&wiznet5k_connected_obj) },
154+
{ MP_ROM_QSTR(MP_QSTR_dhcp), MP_ROM_PTR(&wiznet5k_dhcp_obj) },
124155
};
125156

126157
STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table);
@@ -146,6 +177,7 @@ const mod_network_nic_type_t mod_network_nic_type_wiznet5k = {
146177
.setsockopt = wiznet5k_socket_setsockopt,
147178
.settimeout = wiznet5k_socket_settimeout,
148179
.ioctl = wiznet5k_socket_ioctl,
180+
.timer_tick = wiznet5k_socket_timer_tick,
149181
};
150182

151183
#endif // MICROPY_PY_WIZNET5K

shared-module/network/__init__.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <stdio.h>
28+
2729
#include "py/objlist.h"
2830
#include "py/runtime.h"
2931
#include "py/mphal.h"
3032
#include "py/mperrno.h"
3133

3234
#include "shared-bindings/random/__init__.h"
3335

36+
#include "shared-module/network/__init__.h"
37+
3438
// mod_network_nic_list needs to be declared in mpconfigport.h
3539

3640

@@ -41,6 +45,19 @@ void network_module_init(void) {
4145
void network_module_deinit(void) {
4246
}
4347

48+
void network_module_background(void) {
49+
static uint32_t next_tick = 0;
50+
uint32_t this_tick = ticks_ms;
51+
if (this_tick < next_tick) return;
52+
next_tick = this_tick + 1000;
53+
54+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
55+
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
56+
mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
57+
if (nic_type->timer_tick != NULL) nic_type->timer_tick(nic);
58+
}
59+
}
60+
4461
void network_module_register_nic(mp_obj_t nic) {
4562
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
4663
if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {

shared-module/network/__init__.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct _mod_network_nic_type_t {
6161
int (*setsockopt)(struct _mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno);
6262
int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
6363
int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
64+
void (*timer_tick)(struct _mod_network_socket_obj_t *socket);
6465
} mod_network_nic_type_t;
6566

6667
typedef struct _mod_network_socket_obj_t {
@@ -82,6 +83,7 @@ extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
8283

8384
void network_module_init(void);
8485
void network_module_deinit(void);
86+
void network_module_background(void);
8587
void network_module_register_nic(mp_obj_t nic);
8688
mp_obj_t network_module_find_nic(const uint8_t *ip);
8789

shared-module/wiznet/wiznet5k.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,7 @@
5050
#include "internet/dns/dns.h"
5151
#include "internet/dhcp/dhcp.h"
5252

53-
typedef struct _wiznet5k_obj_t {
54-
mp_obj_base_t base;
55-
mp_uint_t cris_state;
56-
busio_spi_obj_t *spi;
57-
digitalio_digitalinout_obj_t cs;
58-
digitalio_digitalinout_obj_t rst;
59-
uint8_t socket_used;
60-
} wiznet5k_obj_t;
61-
62-
static wiznet5k_obj_t wiznet5k_obj;
53+
#include "shared-module/wiznet/wiznet5k.h"
6354

6455
STATIC wiznet5k_obj_t wiznet5k_obj;
6556

@@ -326,22 +317,34 @@ int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, m
326317
}
327318
}
328319

329-
static void wiznet5k_try_dhcp(void) {
330-
DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
320+
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
321+
if (wiznet5k_obj.dhcp_active) {
322+
DHCP_time_handler();
323+
DHCP_run();
324+
}
325+
}
331326

332-
// Set up the socket to listen on UDP 68 before calling DHCP_init
333-
WIZCHIP_EXPORT(socket)(0, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
334-
DHCP_init(0, dhcp_buf);
327+
void wiznet5k_start_dhcp(void) {
328+
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
335329

336-
// try a few times for DHCP ... XXX this should be asynchronous.
337-
for (int i=0; i<10; i++) {
338-
DHCP_time_handler();
339-
int dhcp_state = DHCP_run();
340-
if (dhcp_state == DHCP_IP_LEASED || dhcp_state == DHCP_IP_CHANGED) break;
341-
mp_hal_delay_ms(1000);
330+
if (!wiznet5k_obj.dhcp_active) {
331+
// 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;
342335
}
343-
DHCP_stop();
344-
WIZCHIP_EXPORT(close)(0);
336+
}
337+
338+
void wiznet5k_stop_dhcp(void) {
339+
if (wiznet5k_obj.dhcp_active) {
340+
wiznet5k_obj.dhcp_active = 0;
341+
DHCP_stop();
342+
WIZCHIP_EXPORT(close)(0);
343+
}
344+
}
345+
346+
bool wiznet5k_check_dhcp(void) {
347+
return wiznet5k_obj.dhcp_active;
345348
}
346349

347350
/// Create and return a WIZNET5K object.
@@ -391,7 +394,7 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
391394
// seems we need a small delay after init
392395
mp_hal_delay_ms(250);
393396

394-
wiznet5k_try_dhcp();
397+
wiznet5k_start_dhcp();
395398

396399
// register with network module
397400
network_module_register_nic(&wiznet5k_obj);

shared-module/wiznet/wiznet5k.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +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;
4243
} wiznet5k_obj_t;
4344

4445
int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip);
@@ -55,9 +56,14 @@ mp_uint_t wiznet5k_socket_recvfrom(mod_network_socket_obj_t *socket, byte *buf,
5556
int wiznet5k_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno);
5657
int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
5758
int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
59+
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket);
5860
mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in);
5961
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in);
6062

63+
void wiznet5k_start_dhcp(void);
64+
void wiznet5k_stop_dhcp(void);
65+
bool wiznet5k_check_dhcp(void);
66+
6167
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
6268

6369
#endif // MICROPY_INCLUDED_SHARED_MODULE_WIZNET_WIZNET5K_H

0 commit comments

Comments
 (0)