Add a kw-only argument "dhcp" to wiznet5k object

This commit is contained in:
Nick Moore 2019-05-02 15:42:10 +10:00
parent 09d0e99d5b
commit baa9c02c8b
3 changed files with 31 additions and 16 deletions

View File

@ -41,6 +41,7 @@
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/digitalio/DriveMode.h"
#include "shared-bindings/busio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-module/network/__init__.h"
#include "shared-module/wiznet/wiznet5k.h"
@ -56,14 +57,27 @@
//|
//| :param spi: spi bus to use
//| :param cs: pin to use for Chip Select
//| :param rst: pin to sue for Reset
//| :param rst: pin to use for Reset
//|
STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
// check arguments
mp_arg_check_num(n_args, kw_args, 3, 3, false);
STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_spi, ARG_cs, ARG_rst, ARG_dhcp };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_cs, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_rst, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_dhcp, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = true } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// XXX check type of ARG_spi?
// XXX should ARG_rst be optional?
assert_pin(args[ARG_cs].u_obj, false);
assert_pin(args[ARG_rst].u_obj, false);
return wiznet5k_create(args[0], args[1], args[2]);
mp_obj_t ret = wiznet5k_create(args[ARG_spi].u_obj, args[ARG_cs].u_obj, args[ARG_rst].u_obj);
if (args[ARG_dhcp].u_bool) wiznet5k_start_dhcp();
return ret;
}
//| .. attribute:: connected
@ -99,9 +113,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_dhcp_get_value_obj, wiznet5k_dhcp_get_
STATIC mp_obj_t wiznet5k_dhcp_set_value(mp_obj_t self_in, mp_obj_t value) {
(void)self_in;
if (mp_obj_is_true(value)) {
wiznet5k_start_dhcp();
int ret = wiznet5k_start_dhcp();
if (ret) mp_raise_OSError(ret);
} else {
wiznet5k_stop_dhcp();
int ret = wiznet5k_stop_dhcp();
if (ret) mp_raise_OSError(ret);
}
return mp_const_none;
}

View File

@ -328,27 +328,29 @@ void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
}
}
void wiznet5k_start_dhcp(void) {
int wiznet5k_start_dhcp(void) {
// XXX this should throw an error if DHCP fails
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
if (wiznet5k_obj.dhcp_socket < 0) {
// Set up the socket to listen on UDP 68 before calling DHCP_init
wiznet5k_obj.dhcp_socket = get_available_socket(&wiznet5k_obj);
if (wiznet5k_obj.dhcp_socket < 0) return;
if (wiznet5k_obj.dhcp_socket < 0) return MP_EMFILE;
WIZCHIP_EXPORT(socket)(wiznet5k_obj.dhcp_socket, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
DHCP_init(wiznet5k_obj.dhcp_socket, dhcp_buf);
}
return 0;
}
void wiznet5k_stop_dhcp(void) {
int wiznet5k_stop_dhcp(void) {
if (wiznet5k_obj.dhcp_socket >= 0) {
DHCP_stop();
WIZCHIP_EXPORT(close)(wiznet5k_obj.dhcp_socket);
wiznet5k_obj.socket_used &= ~(1 << wiznet5k_obj.dhcp_socket);
wiznet5k_obj.dhcp_socket = -1;
}
return 0;
}
bool wiznet5k_check_dhcp(void) {
@ -403,9 +405,6 @@ 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);
// dhcp is started by default
wiznet5k_start_dhcp();
// register with network module
network_module_register_nic(&wiznet5k_obj);

View File

@ -39,7 +39,7 @@ typedef struct _wiznet5k_obj_t {
digitalio_digitalinout_obj_t cs;
digitalio_digitalinout_obj_t rst;
uint8_t socket_used;
int8_t dhcp_socket;
int8_t dhcp_socket; // -1 for DHCP not in use
} wiznet5k_obj_t;
int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip);
@ -60,8 +60,8 @@ 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);
void wiznet5k_start_dhcp(void);
void wiznet5k_stop_dhcp(void);
int wiznet5k_start_dhcp(void);
int wiznet5k_stop_dhcp(void);
bool wiznet5k_check_dhcp(void);
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;