timer ticks for DHCP state machine for wiznet

This commit is contained in:
Nick Moore 2018-10-16 23:10:18 +11:00
parent 45974978ef
commit 06894be294
3 changed files with 28 additions and 15 deletions

View File

@ -146,6 +146,7 @@ const mod_network_nic_type_t mod_network_nic_type_wiznet5k = {
.setsockopt = wiznet5k_socket_setsockopt,
.settimeout = wiznet5k_socket_settimeout,
.ioctl = wiznet5k_socket_ioctl,
.timer_tick = wiznet5k_socket_timer_tick,
};
#endif // MICROPY_PY_WIZNET5K

View File

@ -317,24 +317,34 @@ int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, m
}
}
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++) {
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
if (wiznet5k_obj.dhcp_active) {
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_run();
}
DHCP_stop();
WIZCHIP_EXPORT(close)(0);
}
static void wiznet5k_start_dhcp(void) {
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
if (!wiznet5k_obj.dhcp_active) {
// 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);
wiznet5k_obj.dhcp_active = 1;
}
}
#if 0
static void wiznet5k_stop_dhcp(void) {
if (wiznet5k_obj.dhcp_active) {
wiznet5k_obj.dhcp_active = 0;
DHCP_stop();
WIZCHIP_EXPORT(close)(0);
}
}
#endif
/// Create and return a WIZNET5K object.
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) {
// seems we need a small delay after init
mp_hal_delay_ms(250);
wiznet5k_try_dhcp();
wiznet5k_start_dhcp();
// register with network module
network_module_register_nic(&wiznet5k_obj);

View File

@ -39,6 +39,7 @@ typedef struct _wiznet5k_obj_t {
digitalio_digitalinout_obj_t cs;
digitalio_digitalinout_obj_t rst;
uint8_t socket_used;
bool dhcp_active;
} wiznet5k_obj_t;
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,
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);
int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket);
mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in);
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in);