@@ -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.
339349mp_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 );
0 commit comments