summaryrefslogtreecommitdiff
path: root/shared-bindings/wiznet
diff options
context:
space:
mode:
authorNick Moore <nick@zoic.org>2018-10-09 16:28:30 +1100
committerNick Moore <nick@zoic.org>2018-10-09 16:28:30 +1100
commita60700b1c5a97047b26290745dae26025e3514cc (patch)
treef6d315010f634ad6307ac1f2fd8af8b7499daf09 /shared-bindings/wiznet
parent9b36d33df142d4a3dd2c238799e227c05b537d95 (diff)
Get DHCP working ...
Diffstat (limited to 'shared-bindings/wiznet')
-rw-r--r--shared-bindings/wiznet/wiznet5k.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/shared-bindings/wiznet/wiznet5k.c b/shared-bindings/wiznet/wiznet5k.c
index 558867264..b561806ae 100644
--- a/shared-bindings/wiznet/wiznet5k.c
+++ b/shared-bindings/wiznet/wiznet5k.c
@@ -48,6 +48,7 @@
#include "ethernet/wizchip_conf.h"
#include "ethernet/socket.h"
#include "internet/dns/dns.h"
+#include "internet/dhcp/dhcp.h"
/// \moduleref network
@@ -337,6 +338,24 @@ STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
}
#endif
+static void wiznet5k_try_dhcp(void) {
+ DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
+
+ // Set up the socket to listen on UDP 68 before calling DHCP_init
+ WIZCHIP_EXPORT(socket)(0, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
+ DHCP_init(0, dhcp_buf);
+
+ // try a few times for DHCP ... XXX this should be asynchronous.
+ for (int i=0; i<10; i++) {
+ DHCP_time_handler();
+ int dhcp_state = DHCP_run();
+ if (dhcp_state == DHCP_IP_LEASED || dhcp_state == DHCP_IP_CHANGED) break;
+ mp_hal_delay_ms(1000);
+ }
+ DHCP_stop();
+ WIZCHIP_EXPORT(close)(0);
+}
+
/******************************************************************************/
// MicroPython bindings
@@ -377,16 +396,12 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
reg_wizchip_spi_cbfunc(wiz_spi_read, wiz_spi_write);
- uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2k buffer for each socket
+ // 2k buffer for each socket
+ uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
ctlwizchip(CW_INIT_WIZCHIP, sn_size);
- // set some sensible default values; they are configurable using ifconfig method
wiz_NetInfo netinfo = {
- .ip = {192, 168, 0, 18},
- .sn = {255, 255, 255, 0},
- .gw = {192, 168, 0, 1},
- .dns = {8, 8, 8, 8}, // Google public DNS
- .dhcp = NETINFO_STATIC,
+ .dhcp = NETINFO_DHCP,
};
network_module_create_random_mac_address(netinfo.mac);
ctlnetwork(CN_SET_NETINFO, (void*)&netinfo);
@@ -394,6 +409,8 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
// seems we need a small delay after init
mp_hal_delay_ms(250);
+ wiznet5k_try_dhcp();
+
// register with network module
network_module_register_nic(&wiznet5k_obj);