Skip to content

Commit 06894be

Browse files
committed
timer ticks for DHCP state machine for wiznet
1 parent 4597497 commit 06894be

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

shared-bindings/wiznet/wiznet5k.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ const mod_network_nic_type_t mod_network_nic_type_wiznet5k = {
146146
.setsockopt = wiznet5k_socket_setsockopt,
147147
.settimeout = wiznet5k_socket_settimeout,
148148
.ioctl = wiznet5k_socket_ioctl,
149+
.timer_tick = wiznet5k_socket_timer_tick,
149150
};
150151

151152
#endif // MICROPY_PY_WIZNET5K

shared-module/wiznet/wiznet5k.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -317,23 +317,33 @@ int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, m
317317
}
318318
}
319319

320-
static void wiznet5k_try_dhcp(void) {
321-
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+
}
322326

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

327-
// try a few times for DHCP ... XXX this should be asynchronous.
328-
for (int i=0; i<10; i++) {
329-
DHCP_time_handler();
330-
int dhcp_state = DHCP_run();
331-
if (dhcp_state == DHCP_IP_LEASED || dhcp_state == DHCP_IP_CHANGED) break;
332-
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;
335+
}
336+
}
337+
338+
#if 0
339+
static void wiznet5k_stop_dhcp(void) {
340+
if (wiznet5k_obj.dhcp_active) {
341+
wiznet5k_obj.dhcp_active = 0;
342+
DHCP_stop();
343+
WIZCHIP_EXPORT(close)(0);
333344
}
334-
DHCP_stop();
335-
WIZCHIP_EXPORT(close)(0);
336345
}
346+
#endif
337347

338348
/// Create and return a WIZNET5K object.
339349
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
@@ -382,7 +392,7 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
382392
// seems we need a small delay after init
383393
mp_hal_delay_ms(250);
384394

385-
wiznet5k_try_dhcp();
395+
wiznet5k_start_dhcp();
386396

387397
// register with network module
388398
network_module_register_nic(&wiznet5k_obj);

shared-module/wiznet/wiznet5k.h

Lines changed: 2 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,6 +56,7 @@ 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

0 commit comments

Comments
 (0)