reset wiznet at network deinitialize adafruit/circuitpython#1800

This commit is contained in:
Nick Moore 2019-05-10 13:55:45 +10:00
parent e00a702ce9
commit af0bba0622
5 changed files with 27 additions and 8 deletions

View File

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

View File

@ -43,6 +43,11 @@ void network_module_init(void) {
}
void network_module_deinit(void) {
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
if (nic_type->deinit != NULL) nic_type->deinit(nic);
}
mp_obj_list_set_len(&MP_STATE_PORT(mod_network_nic_list), 0);
}

View File

@ -62,6 +62,7 @@ typedef struct _mod_network_nic_type_t {
int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno);
int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno);
void (*timer_tick)(struct _mod_network_socket_obj_t *socket);
void (*deinit)(struct _mod_network_socket_obj_t *socket);
} mod_network_nic_type_t;
typedef struct _mod_network_socket_obj_t {

View File

@ -357,6 +357,23 @@ bool wiznet5k_check_dhcp(void) {
return wiznet5k_obj.dhcp_socket >= 0;
}
void wiznet5k_reset(void) {
if (wiznet5k_obj.rst.pin) {
// hardware reset if using RST pin
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 0);
mp_hal_delay_us(10); // datasheet says 2us
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 1);
mp_hal_delay_ms(150); // datasheet says 150ms
} else {
// otherwise, software reset
wizchip_sw_reset();
}
}
void wiznet5k_socket_deinit(mod_network_socket_obj_t *socket) {
wiznet5k_reset();
}
/// 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) {
@ -381,14 +398,8 @@ mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.cs, cs_in);
common_hal_digitalio_digitalinout_switch_to_output(&wiznet5k_obj.cs, 1, DRIVE_MODE_PUSH_PULL);
if (rst_in) {
common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
common_hal_digitalio_digitalinout_switch_to_output(&wiznet5k_obj.rst, 1, DRIVE_MODE_PUSH_PULL);
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 0);
mp_hal_delay_us(10); // datasheet says 2us
common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.rst, 1);
mp_hal_delay_ms(160); // datasheet says 150ms
}
if (rst_in) common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
wiznet5k_reset();
reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit);
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);

View File

@ -57,6 +57,7 @@ int wiznet5k_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level
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);
void wiznet5k_socket_deinit(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);