Skip to content

Commit baa9c02

Browse files
committed
Add a kw-only argument "dhcp" to wiznet5k object
1 parent 09d0e99 commit baa9c02

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

shared-bindings/wiznet/wiznet5k.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "shared-bindings/digitalio/DigitalInOut.h"
4242
#include "shared-bindings/digitalio/DriveMode.h"
4343
#include "shared-bindings/busio/SPI.h"
44+
#include "shared-bindings/microcontroller/Pin.h"
4445

4546
#include "shared-module/network/__init__.h"
4647
#include "shared-module/wiznet/wiznet5k.h"
@@ -56,14 +57,27 @@
5657
//|
5758
//| :param spi: spi bus to use
5859
//| :param cs: pin to use for Chip Select
59-
//| :param rst: pin to sue for Reset
60+
//| :param rst: pin to use for Reset
6061
//|
6162

62-
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) {
63-
// check arguments
64-
mp_arg_check_num(n_args, kw_args, 3, 3, false);
65-
66-
return wiznet5k_create(args[0], args[1], args[2]);
63+
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) {
64+
enum { ARG_spi, ARG_cs, ARG_rst, ARG_dhcp };
65+
static const mp_arg_t allowed_args[] = {
66+
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
67+
{ MP_QSTR_cs, MP_ARG_REQUIRED | MP_ARG_OBJ },
68+
{ MP_QSTR_rst, MP_ARG_REQUIRED | MP_ARG_OBJ },
69+
{ MP_QSTR_dhcp, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = true } },
70+
};
71+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
72+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
73+
// XXX check type of ARG_spi?
74+
// XXX should ARG_rst be optional?
75+
assert_pin(args[ARG_cs].u_obj, false);
76+
assert_pin(args[ARG_rst].u_obj, false);
77+
78+
mp_obj_t ret = wiznet5k_create(args[ARG_spi].u_obj, args[ARG_cs].u_obj, args[ARG_rst].u_obj);
79+
if (args[ARG_dhcp].u_bool) wiznet5k_start_dhcp();
80+
return ret;
6781
}
6882

6983
//| .. attribute:: connected
@@ -99,9 +113,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_dhcp_get_value_obj, wiznet5k_dhcp_get_
99113
STATIC mp_obj_t wiznet5k_dhcp_set_value(mp_obj_t self_in, mp_obj_t value) {
100114
(void)self_in;
101115
if (mp_obj_is_true(value)) {
102-
wiznet5k_start_dhcp();
116+
int ret = wiznet5k_start_dhcp();
117+
if (ret) mp_raise_OSError(ret);
103118
} else {
104-
wiznet5k_stop_dhcp();
119+
int ret = wiznet5k_stop_dhcp();
120+
if (ret) mp_raise_OSError(ret);
105121
}
106122
return mp_const_none;
107123
}

shared-module/wiznet/wiznet5k.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,27 +328,29 @@ void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
328328
}
329329
}
330330

331-
void wiznet5k_start_dhcp(void) {
331+
int wiznet5k_start_dhcp(void) {
332332
// XXX this should throw an error if DHCP fails
333333
static DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
334334

335335
if (wiznet5k_obj.dhcp_socket < 0) {
336336
// Set up the socket to listen on UDP 68 before calling DHCP_init
337337
wiznet5k_obj.dhcp_socket = get_available_socket(&wiznet5k_obj);
338-
if (wiznet5k_obj.dhcp_socket < 0) return;
338+
if (wiznet5k_obj.dhcp_socket < 0) return MP_EMFILE;
339339

340340
WIZCHIP_EXPORT(socket)(wiznet5k_obj.dhcp_socket, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
341341
DHCP_init(wiznet5k_obj.dhcp_socket, dhcp_buf);
342342
}
343+
return 0;
343344
}
344345

345-
void wiznet5k_stop_dhcp(void) {
346+
int wiznet5k_stop_dhcp(void) {
346347
if (wiznet5k_obj.dhcp_socket >= 0) {
347348
DHCP_stop();
348349
WIZCHIP_EXPORT(close)(wiznet5k_obj.dhcp_socket);
349350
wiznet5k_obj.socket_used &= ~(1 << wiznet5k_obj.dhcp_socket);
350351
wiznet5k_obj.dhcp_socket = -1;
351352
}
353+
return 0;
352354
}
353355

354356
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) {
403405
// seems we need a small delay after init
404406
mp_hal_delay_ms(250);
405407

406-
// dhcp is started by default
407-
wiznet5k_start_dhcp();
408-
409408
// register with network module
410409
network_module_register_nic(&wiznet5k_obj);
411410

shared-module/wiznet/wiznet5k.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct _wiznet5k_obj_t {
3939
digitalio_digitalinout_obj_t cs;
4040
digitalio_digitalinout_obj_t rst;
4141
uint8_t socket_used;
42-
int8_t dhcp_socket;
42+
int8_t dhcp_socket; // -1 for DHCP not in use
4343
} wiznet5k_obj_t;
4444

4545
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);
6060
mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in);
6161
mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in);
6262

63-
void wiznet5k_start_dhcp(void);
64-
void wiznet5k_stop_dhcp(void);
63+
int wiznet5k_start_dhcp(void);
64+
int wiznet5k_stop_dhcp(void);
6565
bool wiznet5k_check_dhcp(void);
6666

6767
extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;

0 commit comments

Comments
 (0)