summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-11-07 14:12:22 -0500
committerDan Halbert <halbert@halwitz.org>2018-11-07 14:12:22 -0500
commit64d457dad9bb3843ee468136022996ecb91a98df (patch)
treec3bfee81668dbd842039c178222fda9df77a9b76 /shared-module
parent4bc24c4f6084e414f0fb00299aae7d7cfdb76aba (diff)
parent283e07254e9eb50e52d6f07b7ac177feeef02d83 (diff)
bring bleio PR up to date
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/audioio/WaveFile.c8
-rw-r--r--shared-module/network/__init__.c95
-rw-r--r--shared-module/network/__init__.h90
-rw-r--r--shared-module/socket/__init__.c0
-rw-r--r--shared-module/wiznet/__init__.c0
-rw-r--r--shared-module/wiznet/wiznet5k.c406
-rw-r--r--shared-module/wiznet/wiznet5k.h69
7 files changed, 668 insertions, 0 deletions
diff --git a/shared-module/audioio/WaveFile.c b/shared-module/audioio/WaveFile.c
index e5a7b1a3b..d5dd9419c 100644
--- a/shared-module/audioio/WaveFile.c
+++ b/shared-module/audioio/WaveFile.c
@@ -141,6 +141,14 @@ void common_hal_audioio_wavefile_set_sample_rate(audioio_wavefile_obj_t* self,
self->sample_rate = sample_rate;
}
+uint8_t common_hal_audioio_wavefile_get_bits_per_sample(audioio_wavefile_obj_t* self) {
+ return self->bits_per_sample;
+}
+
+uint8_t common_hal_audioio_wavefile_get_channel_count(audioio_wavefile_obj_t* self) {
+ return self->channel_count;
+}
+
bool audioio_wavefile_samples_signed(audioio_wavefile_obj_t* self) {
return self->bits_per_sample > 8;
}
diff --git a/shared-module/network/__init__.c b/shared-module/network/__init__.c
new file mode 100644
index 000000000..a674e8478
--- /dev/null
+++ b/shared-module/network/__init__.c
@@ -0,0 +1,95 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Nick Moore
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+
+#include "py/objlist.h"
+#include "py/runtime.h"
+#include "py/mphal.h"
+#include "py/mperrno.h"
+
+#include "shared-bindings/random/__init__.h"
+
+#include "shared-module/network/__init__.h"
+
+// mod_network_nic_list needs to be declared in mpconfigport.h
+
+
+void network_module_init(void) {
+ mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
+}
+
+void network_module_deinit(void) {
+}
+
+void network_module_background(void) {
+ static uint32_t next_tick = 0;
+ uint32_t this_tick = ticks_ms;
+ if (this_tick < next_tick) return;
+ next_tick = this_tick + 1000;
+
+ 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->timer_tick != NULL) nic_type->timer_tick(nic);
+ }
+}
+
+void network_module_register_nic(mp_obj_t nic) {
+ for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
+ if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) {
+ // nic already registered
+ return;
+ }
+ }
+ // nic not registered so add to list
+ mp_obj_list_append(MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list)), nic);
+}
+
+mp_obj_t network_module_find_nic(const uint8_t *ip) {
+ // find a NIC that is suited to given IP address
+ 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];
+ // TODO check IP suitability here
+ //mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
+ return nic;
+ }
+
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, translate("no available NIC")));
+}
+
+void network_module_create_random_mac_address(uint8_t *mac) {
+ uint32_t rb1 = shared_modules_random_getrandbits(24);
+ uint32_t rb2 = shared_modules_random_getrandbits(24);
+ // first octet has multicast bit (0) cleared and local bit (1) set
+ // everything else is just set randomly
+ mac[0] = ((uint8_t)(rb1 >> 16) & 0xfe) | 0x02;
+ mac[1] = (uint8_t)(rb1 >> 8);
+ mac[2] = (uint8_t)(rb1);
+ mac[3] = (uint8_t)(rb2 >> 16);
+ mac[4] = (uint8_t)(rb2 >> 8);
+ mac[5] = (uint8_t)(rb2);
+}
diff --git a/shared-module/network/__init__.h b/shared-module/network/__init__.h
new file mode 100644
index 000000000..00a3c3957
--- /dev/null
+++ b/shared-module/network/__init__.h
@@ -0,0 +1,90 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 Damien P. George
+ * Copyright (c) 2018 Nick Moore
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+void network_module_create_random_mac_address(uint8_t *mac);
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H
+#define MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H
+
+#define MOD_NETWORK_IPADDR_BUF_SIZE (4)
+
+#define MOD_NETWORK_AF_INET (2)
+#define MOD_NETWORK_AF_INET6 (10)
+
+#define MOD_NETWORK_SOCK_STREAM (1)
+#define MOD_NETWORK_SOCK_DGRAM (2)
+#define MOD_NETWORK_SOCK_RAW (3)
+
+struct _mod_network_socket_obj_t;
+
+typedef struct _mod_network_nic_type_t {
+ mp_obj_type_t base;
+
+ // API for non-socket operations
+ int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out);
+
+ // API for socket operations; return -1 on error
+ int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);
+ void (*close)(struct _mod_network_socket_obj_t *socket);
+ int (*bind)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
+ int (*listen)(struct _mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
+ int (*accept)(struct _mod_network_socket_obj_t *socket, struct _mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
+ int (*connect)(struct _mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
+ mp_uint_t (*send)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
+ mp_uint_t (*recv)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
+ mp_uint_t (*sendto)(struct _mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno);
+ mp_uint_t (*recvfrom)(struct _mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno);
+ int (*setsockopt)(struct _mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno);
+ 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);
+} mod_network_nic_type_t;
+
+typedef struct _mod_network_socket_obj_t {
+ mp_obj_base_t base;
+ mp_obj_t nic;
+ mod_network_nic_type_t *nic_type;
+ union {
+ struct {
+ uint8_t domain;
+ uint8_t type;
+ int8_t fileno;
+ } u_param;
+ mp_uint_t u_state;
+ };
+} mod_network_socket_obj_t;
+
+extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
+extern const mod_network_nic_type_t mod_network_nic_type_cc3k;
+
+void network_module_init(void);
+void network_module_deinit(void);
+void network_module_background(void);
+void network_module_register_nic(mp_obj_t nic);
+mp_obj_t network_module_find_nic(const uint8_t *ip);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H
diff --git a/shared-module/socket/__init__.c b/shared-module/socket/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/socket/__init__.c
diff --git a/shared-module/wiznet/__init__.c b/shared-module/wiznet/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/wiznet/__init__.c
diff --git a/shared-module/wiznet/wiznet5k.c b/shared-module/wiznet/wiznet5k.c
new file mode 100644
index 000000000..ee732859a
--- /dev/null
+++ b/shared-module/wiznet/wiznet5k.c
@@ -0,0 +1,406 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "py/objlist.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+#include "py/stream.h"
+#include "py/mperrno.h"
+#include "py/mphal.h"
+#include "lib/netutils/netutils.h"
+
+#if MICROPY_PY_WIZNET5K
+
+#include "shared-module/network/__init__.h"
+#include "shared-bindings/digitalio/DigitalInOut.h"
+#include "shared-bindings/digitalio/DriveMode.h"
+#include "shared-bindings/busio/SPI.h"
+
+#include "shared-module/network/__init__.h"
+
+#include "ethernet/wizchip_conf.h"
+#include "ethernet/socket.h"
+#include "internet/dns/dns.h"
+#include "internet/dhcp/dhcp.h"
+
+#include "shared-module/wiznet/wiznet5k.h"
+
+STATIC wiznet5k_obj_t wiznet5k_obj;
+
+STATIC void wiz_cris_enter(void) {
+ wiznet5k_obj.cris_state = MICROPY_BEGIN_ATOMIC_SECTION();
+}
+
+STATIC void wiz_cris_exit(void) {
+ MICROPY_END_ATOMIC_SECTION(wiznet5k_obj.cris_state);
+}
+
+STATIC void wiz_cs_select(void) {
+ common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.cs, 0);
+}
+
+STATIC void wiz_cs_deselect(void) {
+ common_hal_digitalio_digitalinout_set_value(&wiznet5k_obj.cs, 1);
+}
+
+STATIC void wiz_spi_read(uint8_t *buf, uint32_t len) {
+ (void)common_hal_busio_spi_read(wiznet5k_obj.spi, buf, len, 0);
+}
+
+STATIC void wiz_spi_write(const uint8_t *buf, uint32_t len) {
+ (void)common_hal_busio_spi_write(wiznet5k_obj.spi, buf, len);
+}
+
+int wiznet5k_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip) {
+ uint8_t dns_ip[MOD_NETWORK_IPADDR_BUF_SIZE] = {8, 8, 8, 8};
+ uint8_t *buf = m_new(uint8_t, MAX_DNS_BUF_SIZE);
+ DNS_init(0, buf);
+ mp_int_t ret = DNS_run(dns_ip, (uint8_t*)name, out_ip);
+ m_del(uint8_t, buf, MAX_DNS_BUF_SIZE);
+ if (ret == 1) {
+ // success
+ return 0;
+ } else {
+ // failure
+ return -2;
+ }
+}
+
+int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
+ if (socket->u_param.domain != MOD_NETWORK_AF_INET) {
+ *_errno = MP_EAFNOSUPPORT;
+ return -1;
+ }
+
+ switch (socket->u_param.type) {
+ case MOD_NETWORK_SOCK_STREAM: socket->u_param.type = Sn_MR_TCP; break;
+ case MOD_NETWORK_SOCK_DGRAM: socket->u_param.type = Sn_MR_UDP; break;
+ default: *_errno = MP_EINVAL; return -1;
+ }
+
+ if (socket->u_param.fileno == -1) {
+ // get first unused socket number
+ for (mp_uint_t sn = 0; sn < _WIZCHIP_SOCK_NUM_; sn++) {
+ if ((wiznet5k_obj.socket_used & (1 << sn)) == 0) {
+ wiznet5k_obj.socket_used |= (1 << sn);
+ socket->u_param.fileno = sn;
+ break;
+ }
+ }
+ if (socket->u_param.fileno == -1) {
+ // too many open sockets
+ *_errno = MP_EMFILE;
+ return -1;
+ }
+ }
+
+ // WIZNET does not have a concept of pure "open socket". You need to know
+ // if it's a server or client at the time of creation of the socket.
+ // So, we defer the open until we know what kind of socket we want.
+
+ // use "domain" to indicate that this socket has not yet been opened
+ socket->u_param.domain = 0;
+
+ return 0;
+}
+
+void wiznet5k_socket_close(mod_network_socket_obj_t *socket) {
+ uint8_t sn = (uint8_t)socket->u_param.fileno;
+ if (sn < _WIZCHIP_SOCK_NUM_) {
+ wiznet5k_obj.socket_used &= ~(1 << sn);
+ WIZCHIP_EXPORT(close)(sn);
+ }
+}
+
+int wiznet5k_socket_bind(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) {
+ // open the socket in server mode (if port != 0)
+ mp_int_t ret = WIZCHIP_EXPORT(socket)(socket->u_param.fileno, socket->u_param.type, port, 0);
+ if (ret < 0) {
+ wiznet5k_socket_close(socket);
+ *_errno = -ret;
+ return -1;
+ }
+
+ // indicate that this socket has been opened
+ socket->u_param.domain = 1;
+
+ // success
+ return 0;
+}
+
+int wiznet5k_socket_listen(mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno) {
+ mp_int_t ret = WIZCHIP_EXPORT(listen)(socket->u_param.fileno);
+ if (ret < 0) {
+ wiznet5k_socket_close(socket);
+ *_errno = -ret;
+ return -1;
+ }
+ return 0;
+}
+
+int wiznet5k_socket_accept(mod_network_socket_obj_t *socket, mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno) {
+ for (;;) {
+ int sr = getSn_SR((uint8_t)socket->u_param.fileno);
+ if (sr == SOCK_ESTABLISHED) {
+ socket2->u_param = socket->u_param;
+ getSn_DIPR((uint8_t)socket2->u_param.fileno, ip);
+ *port = getSn_PORT(socket2->u_param.fileno);
+
+ // WIZnet turns the listening socket into the client socket, so we
+ // need to re-bind and re-listen on another socket for the server.
+ // TODO handle errors, especially no-more-sockets error
+ socket->u_param.domain = MOD_NETWORK_AF_INET;
+ socket->u_param.fileno = -1;
+ int _errno2;
+ if (wiznet5k_socket_socket(socket, &_errno2) != 0) {
+ //printf("(bad resocket %d)\n", _errno2);
+ } else if (wiznet5k_socket_bind(socket, NULL, *port, &_errno2) != 0) {
+ //printf("(bad rebind %d)\n", _errno2);
+ } else if (wiznet5k_socket_listen(socket, 0, &_errno2) != 0) {
+ //printf("(bad relisten %d)\n", _errno2);
+ }
+
+ return 0;
+ }
+ if (sr == SOCK_CLOSED || sr == SOCK_CLOSE_WAIT) {
+ wiznet5k_socket_close(socket);
+ *_errno = MP_ENOTCONN; // ??
+ return -1;
+ }
+ mp_hal_delay_ms(1);
+ }
+}
+
+int wiznet5k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) {
+ // use "bind" function to open the socket in client mode
+ if (wiznet5k_socket_bind(socket, ip, 0, _errno) != 0) {
+ return -1;
+ }
+
+ // now connect
+ MP_THREAD_GIL_EXIT();
+ mp_int_t ret = WIZCHIP_EXPORT(connect)(socket->u_param.fileno, ip, port);
+ MP_THREAD_GIL_ENTER();
+
+ if (ret < 0) {
+ wiznet5k_socket_close(socket);
+ *_errno = -ret;
+ return -1;
+ }
+
+ // success
+ return 0;
+}
+
+mp_uint_t wiznet5k_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) {
+ MP_THREAD_GIL_EXIT();
+ mp_int_t ret = WIZCHIP_EXPORT(send)(socket->u_param.fileno, (byte*)buf, len);
+ MP_THREAD_GIL_ENTER();
+
+ // TODO convert Wiz errno's to POSIX ones
+ if (ret < 0) {
+ wiznet5k_socket_close(socket);
+ *_errno = -ret;
+ return -1;
+ }
+ return ret;
+}
+
+mp_uint_t wiznet5k_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) {
+ MP_THREAD_GIL_EXIT();
+ mp_int_t ret = WIZCHIP_EXPORT(recv)(socket->u_param.fileno, buf, len);
+ MP_THREAD_GIL_ENTER();
+
+ // TODO convert Wiz errno's to POSIX ones
+ if (ret < 0) {
+ wiznet5k_socket_close(socket);
+ *_errno = -ret;
+ return -1;
+ }
+ return ret;
+}
+
+mp_uint_t wiznet5k_socket_sendto(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) {
+ if (socket->u_param.domain == 0) {
+ // socket not opened; use "bind" function to open the socket in client mode
+ if (wiznet5k_socket_bind(socket, ip, 0, _errno) != 0) {
+ return -1;
+ }
+ }
+
+ MP_THREAD_GIL_EXIT();
+ mp_int_t ret = WIZCHIP_EXPORT(sendto)(socket->u_param.fileno, (byte*)buf, len, ip, port);
+ MP_THREAD_GIL_ENTER();
+
+ if (ret < 0) {
+ wiznet5k_socket_close(socket);
+ *_errno = -ret;
+ return -1;
+ }
+ return ret;
+}
+
+mp_uint_t wiznet5k_socket_recvfrom(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) {
+ uint16_t port2;
+ MP_THREAD_GIL_EXIT();
+ mp_int_t ret = WIZCHIP_EXPORT(recvfrom)(socket->u_param.fileno, buf, len, ip, &port2);
+ MP_THREAD_GIL_ENTER();
+ *port = port2;
+ if (ret < 0) {
+ wiznet5k_socket_close(socket);
+ *_errno = -ret;
+ return -1;
+ }
+ return ret;
+}
+
+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) {
+ // TODO
+ *_errno = MP_EINVAL;
+ return -1;
+}
+
+int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno) {
+ // TODO
+ *_errno = MP_EINVAL;
+ return -1;
+
+ /*
+ if (timeout_ms == 0) {
+ // set non-blocking mode
+ uint8_t arg = SOCK_IO_NONBLOCK;
+ WIZCHIP_EXPORT(ctlsocket)(socket->u_param.fileno, CS_SET_IOMODE, &arg);
+ }
+ */
+}
+
+int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno) {
+ if (request == MP_STREAM_POLL) {
+ int ret = 0;
+ if (arg & MP_STREAM_POLL_RD && getSn_RX_RSR(socket->u_param.fileno) != 0) {
+ ret |= MP_STREAM_POLL_RD;
+ }
+ if (arg & MP_STREAM_POLL_WR && getSn_TX_FSR(socket->u_param.fileno) != 0) {
+ ret |= MP_STREAM_POLL_WR;
+ }
+ return ret;
+ } else {
+ *_errno = MP_EINVAL;
+ return MP_STREAM_ERROR;
+ }
+}
+
+void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket) {
+ if (wiznet5k_obj.dhcp_active) {
+ DHCP_time_handler();
+ DHCP_run();
+ }
+}
+
+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;
+ }
+}
+
+void wiznet5k_stop_dhcp(void) {
+ if (wiznet5k_obj.dhcp_active) {
+ wiznet5k_obj.dhcp_active = 0;
+ DHCP_stop();
+ WIZCHIP_EXPORT(close)(0);
+ }
+}
+
+bool wiznet5k_check_dhcp(void) {
+ return wiznet5k_obj.dhcp_active;
+}
+
+/// 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) {
+
+ // init the wiznet5k object
+ wiznet5k_obj.base.type = (mp_obj_type_t*)&mod_network_nic_type_wiznet5k;
+ wiznet5k_obj.cris_state = 0;
+ wiznet5k_obj.spi = MP_OBJ_TO_PTR(spi_in);
+ common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.cs, cs_in);
+ common_hal_digitalio_digitalinout_construct(&wiznet5k_obj.rst, rst_in);
+ wiznet5k_obj.socket_used = 0;
+
+ /*!< SPI configuration */
+ // XXX probably should check if the provided SPI is already configured, and
+ // if so skip configuration?
+
+ common_hal_busio_spi_configure(wiznet5k_obj.spi,
+ 10000000, // BAUDRATE 10MHz
+ 1, // HIGH POLARITY
+ 1, // SECOND PHASE TRANSITION
+ 8 // 8 BITS
+ );
+
+ common_hal_digitalio_digitalinout_switch_to_output(&wiznet5k_obj.cs, 1, DRIVE_MODE_PUSH_PULL);
+ 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
+
+ reg_wizchip_cris_cbfunc(wiz_cris_enter, wiz_cris_exit);
+ reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
+ reg_wizchip_spi_cbfunc(wiz_spi_read, wiz_spi_write);
+
+ // 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);
+
+ wiz_NetInfo netinfo = {
+ .dhcp = NETINFO_DHCP,
+ };
+ network_module_create_random_mac_address(netinfo.mac);
+ ctlnetwork(CN_SET_NETINFO, (void*)&netinfo);
+
+ // seems we need a small delay after init
+ mp_hal_delay_ms(250);
+
+ wiznet5k_start_dhcp();
+
+ // register with network module
+ network_module_register_nic(&wiznet5k_obj);
+
+ // return wiznet5k object
+ return &wiznet5k_obj;
+}
+
+#endif // MICROPY_PY_WIZNET5K
diff --git a/shared-module/wiznet/wiznet5k.h b/shared-module/wiznet/wiznet5k.h
new file mode 100644
index 000000000..1284a44fd
--- /dev/null
+++ b/shared-module/wiznet/wiznet5k.h
@@ -0,0 +1,69 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_WIZNET_WIZNET5K_H
+#define MICROPY_INCLUDED_SHARED_MODULE_WIZNET_WIZNET5K_H
+
+#include "ethernet/wizchip_conf.h"
+#include "ethernet/socket.h"
+#include "internet/dns/dns.h"
+#include "internet/dhcp/dhcp.h"
+
+typedef struct _wiznet5k_obj_t {
+ mp_obj_base_t base;
+ mp_uint_t cris_state;
+ busio_spi_obj_t *spi;
+ 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);
+int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno);
+void wiznet5k_socket_close(mod_network_socket_obj_t *socket);
+int wiznet5k_socket_bind(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
+int wiznet5k_socket_listen(mod_network_socket_obj_t *socket, mp_int_t backlog, int *_errno);
+int wiznet5k_socket_accept(mod_network_socket_obj_t *socket, mod_network_socket_obj_t *socket2, byte *ip, mp_uint_t *port, int *_errno);
+int wiznet5k_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno);
+mp_uint_t wiznet5k_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno);
+mp_uint_t wiznet5k_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno);
+mp_uint_t wiznet5k_socket_sendto(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno);
+mp_uint_t wiznet5k_socket_recvfrom(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno);
+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);
+
+void wiznet5k_start_dhcp(void);
+void wiznet5k_stop_dhcp(void);
+bool wiznet5k_check_dhcp(void);
+
+extern const mod_network_nic_type_t mod_network_nic_type_wiznet5k;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_WIZNET_WIZNET5K_H