2014-09-01 17:48:57 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-09-01 17:48:57 -04:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/objlist.h"
|
|
|
|
#include "py/runtime.h"
|
2017-10-17 01:34:10 -04:00
|
|
|
#include "py/stream.h"
|
2016-05-10 18:45:33 -04:00
|
|
|
#include "py/mperrno.h"
|
2015-10-30 19:03:58 -04:00
|
|
|
#include "py/mphal.h"
|
2021-07-09 00:19:15 -04:00
|
|
|
#include "shared/netutils/netutils.h"
|
2014-09-30 17:29:49 -04:00
|
|
|
#include "modnetwork.h"
|
2014-09-01 17:48:57 -04:00
|
|
|
#include "pin.h"
|
|
|
|
#include "spi.h"
|
|
|
|
|
stm32: Add network driver for Wiznet5k using MACRAW mode and lwIP.
The Wiznet5k series of chips support a MACRAW mode which allows the host to
send and receive Ethernet frames directly. This can be hooked into the
lwIP stack to provide a full "socket" implementation using this Wiznet
Ethernet device. This patch adds support for this feature.
To enable the feature one must add the following to mpconfigboard.mk, or
mpconfigport.mk:
MICROPY_PY_WIZNET5K = 5500
and the following to mpconfigboard.h, or mpconfigport.h:
#define MICROPY_PY_LWIP (1)
After wiring up the module (X5=CS, X4=RST), usage on a pyboard is:
import time, network
nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
nic.active(1)
while not nic.isconnected():
time.sleep_ms(50) # needed to poll the NIC
print(nic.ifconfig())
Then use the socket module as usual.
Compared to using the built-in TCP/IP stack on the Wiznet module, some
performance is lost in MACRAW mode: with a lot of memory allocated to lwIP
buffers, lwIP gives Around 750,000 bytes/sec max TCP download, compared
with 1M/sec when using the TCP/IP stack on the Wiznet module.
2018-06-01 00:21:38 -04:00
|
|
|
#if MICROPY_PY_WIZNET5K && !MICROPY_PY_LWIP
|
|
|
|
|
2014-09-01 17:48:57 -04:00
|
|
|
#include "ethernet/wizchip_conf.h"
|
|
|
|
#include "ethernet/socket.h"
|
|
|
|
#include "internet/dns/dns.h"
|
|
|
|
|
2014-10-30 21:11:55 -04:00
|
|
|
/// \moduleref network
|
2014-09-01 17:58:22 -04:00
|
|
|
|
2014-09-01 17:48:57 -04:00
|
|
|
typedef struct _wiznet5k_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_uint_t cris_state;
|
2018-02-04 21:45:08 -05:00
|
|
|
const spi_t *spi;
|
2014-09-01 17:48:57 -04:00
|
|
|
const pin_obj_t *cs;
|
|
|
|
const pin_obj_t *rst;
|
|
|
|
uint8_t socket_used;
|
|
|
|
} wiznet5k_obj_t;
|
|
|
|
|
|
|
|
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) {
|
2016-10-17 23:32:42 -04:00
|
|
|
mp_hal_pin_low(wiznet5k_obj.cs);
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void wiz_cs_deselect(void) {
|
2016-10-17 23:32:42 -04:00
|
|
|
mp_hal_pin_high(wiznet5k_obj.cs);
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void wiz_spi_read(uint8_t *buf, uint32_t len) {
|
2018-02-04 21:45:08 -05:00
|
|
|
HAL_StatusTypeDef status = HAL_SPI_Receive(wiznet5k_obj.spi->spi, buf, len, 5000);
|
2014-09-01 17:48:57 -04:00
|
|
|
(void)status;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void wiz_spi_write(const uint8_t *buf, uint32_t len) {
|
2018-02-04 21:45:08 -05:00
|
|
|
HAL_StatusTypeDef status = HAL_SPI_Transmit(wiznet5k_obj.spi->spi, (uint8_t *)buf, len, 5000);
|
2014-09-01 17:48:57 -04:00
|
|
|
(void)status;
|
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
STATIC 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
|
2017-10-15 22:19:11 -04:00
|
|
|
return -2;
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
STATIC int wiznet5k_socket_socket(mod_network_socket_obj_t *socket, int *_errno) {
|
|
|
|
if (socket->u_param.domain != MOD_NETWORK_AF_INET) {
|
2016-05-10 18:45:33 -04:00
|
|
|
*_errno = MP_EAFNOSUPPORT;
|
2014-11-29 08:48:45 -05:00
|
|
|
return -1;
|
2014-09-30 17:29:49 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
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;
|
2016-05-10 18:45:33 -04:00
|
|
|
default:
|
|
|
|
*_errno = MP_EINVAL;
|
|
|
|
return -1;
|
2014-09-30 17:29:49 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
if (socket->u_param.fileno == -1) {
|
2014-09-30 17:29:49 -04:00
|
|
|
// get first unused socket number
|
|
|
|
for (mp_uint_t sn = 0; sn < _WIZCHIP_SOCK_NUM_; sn++) {
|
|
|
|
if ((wiznet5k_obj.socket_used & (1 << sn)) == 0) {
|
2014-11-29 08:48:45 -05:00
|
|
|
wiznet5k_obj.socket_used |= (1 << sn);
|
|
|
|
socket->u_param.fileno = sn;
|
2014-09-30 17:29:49 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
2014-11-29 08:48:45 -05:00
|
|
|
if (socket->u_param.fileno == -1) {
|
2014-09-30 17:29:49 -04:00
|
|
|
// too many open sockets
|
2016-05-10 18:45:33 -04:00
|
|
|
*_errno = MP_EMFILE;
|
2014-11-29 08:48:45 -05:00
|
|
|
return -1;
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
// 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;
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
STATIC 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);
|
2014-09-30 17:29:49 -04:00
|
|
|
}
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
STATIC 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;
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
STATIC 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC 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;
|
2017-09-27 10:04:42 -04:00
|
|
|
getSn_DIPR((uint8_t)socket2->u_param.fileno, ip);
|
2014-11-29 08:48:45 -05:00
|
|
|
*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);
|
2016-05-10 18:45:33 -04:00
|
|
|
*_errno = MP_ENOTCONN; // ??
|
2014-11-29 08:48:45 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(1);
|
2014-11-29 08:48:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC 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
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-11-29 08:48:45 -05:00
|
|
|
mp_int_t ret = WIZCHIP_EXPORT(connect)(socket->u_param.fileno, ip, port);
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
if (ret < 0) {
|
|
|
|
wiznet5k_socket_close(socket);
|
|
|
|
*_errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// success
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t wiznet5k_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) {
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-11-29 08:48:45 -05:00
|
|
|
mp_int_t ret = WIZCHIP_EXPORT(send)(socket->u_param.fileno, (byte *)buf, len);
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
// TODO convert Wiz errno's to POSIX ones
|
|
|
|
if (ret < 0) {
|
|
|
|
wiznet5k_socket_close(socket);
|
|
|
|
*_errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t wiznet5k_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) {
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-11-29 08:48:45 -05:00
|
|
|
mp_int_t ret = WIZCHIP_EXPORT(recv)(socket->u_param.fileno, buf, len);
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
// TODO convert Wiz errno's to POSIX ones
|
|
|
|
if (ret < 0) {
|
|
|
|
wiznet5k_socket_close(socket);
|
|
|
|
*_errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-11-29 08:48:45 -05:00
|
|
|
mp_int_t ret = WIZCHIP_EXPORT(sendto)(socket->u_param.fileno, (byte *)buf, len, ip, port);
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_ENTER();
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
if (ret < 0) {
|
|
|
|
wiznet5k_socket_close(socket);
|
|
|
|
*_errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC 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;
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_EXIT();
|
2014-11-29 08:48:45 -05:00
|
|
|
mp_int_t ret = WIZCHIP_EXPORT(recvfrom)(socket->u_param.fileno, buf, len, ip, &port2);
|
2017-09-07 22:23:33 -04:00
|
|
|
MP_THREAD_GIL_ENTER();
|
2014-11-29 08:48:45 -05:00
|
|
|
*port = port2;
|
|
|
|
if (ret < 0) {
|
|
|
|
wiznet5k_socket_close(socket);
|
|
|
|
*_errno = -ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC 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
|
2016-05-10 18:45:33 -04:00
|
|
|
*_errno = MP_EINVAL;
|
2014-11-29 08:48:45 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int wiznet5k_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno) {
|
|
|
|
// TODO
|
2016-05-10 18:45:33 -04:00
|
|
|
*_errno = MP_EINVAL;
|
2014-11-29 08:48:45 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno) {
|
2017-10-17 01:34:10 -04:00
|
|
|
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;
|
|
|
|
}
|
2014-11-29 08:48:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
STATIC void wiznet5k_socket_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
wiznet5k_socket_obj_t *self = self_in;
|
|
|
|
print(env, "<WIZNET5K.socket sn=%u MR=0x%02x>", self->sn, getSn_MR(self->sn));
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
|
|
|
|
mp_int_t ret = WIZCHIP_EXPORT(disconnect)(self->sn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 03:22:17 -04:00
|
|
|
// MicroPython bindings
|
2014-11-29 08:48:45 -05:00
|
|
|
|
2014-09-30 17:29:49 -04:00
|
|
|
/// \classmethod \constructor(spi, pin_cs, pin_rst)
|
2014-11-29 08:48:45 -05:00
|
|
|
/// Create and return a WIZNET5K object.
|
2017-01-04 08:10:42 -05:00
|
|
|
STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2014-09-01 17:48:57 -04:00
|
|
|
// check arguments
|
2014-09-30 17:29:49 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 3, 3, false);
|
2014-09-01 17:48:57 -04:00
|
|
|
|
|
|
|
// init the wiznet5k object
|
2014-09-30 17:29:49 -04:00
|
|
|
wiznet5k_obj.base.type = (mp_obj_type_t *)&mod_network_nic_type_wiznet5k;
|
2014-09-01 17:48:57 -04:00
|
|
|
wiznet5k_obj.cris_state = 0;
|
2018-02-04 21:45:08 -05:00
|
|
|
wiznet5k_obj.spi = spi_from_mp_obj(args[0]);
|
2014-09-30 17:29:49 -04:00
|
|
|
wiznet5k_obj.cs = pin_find(args[1]);
|
|
|
|
wiznet5k_obj.rst = pin_find(args[2]);
|
2014-09-01 17:48:57 -04:00
|
|
|
wiznet5k_obj.socket_used = 0;
|
|
|
|
|
|
|
|
/*!< SPI configuration */
|
2018-02-04 21:45:08 -05:00
|
|
|
SPI_InitTypeDef *init = &wiznet5k_obj.spi->spi->Init;
|
|
|
|
init->Mode = SPI_MODE_MASTER;
|
|
|
|
init->Direction = SPI_DIRECTION_2LINES;
|
|
|
|
init->DataSize = SPI_DATASIZE_8BIT;
|
|
|
|
init->CLKPolarity = SPI_POLARITY_LOW; // clock is low when idle
|
|
|
|
init->CLKPhase = SPI_PHASE_1EDGE; // data latched on first edge, which is rising edge for low-idle
|
|
|
|
init->NSS = SPI_NSS_SOFT;
|
|
|
|
init->BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; // clock freq = f_PCLK / this_prescale_value; Wiz820i can do up to 80MHz
|
|
|
|
init->FirstBit = SPI_FIRSTBIT_MSB;
|
|
|
|
init->TIMode = SPI_TIMODE_DISABLED;
|
|
|
|
init->CRCCalculation = SPI_CRCCALCULATION_DISABLED;
|
|
|
|
init->CRCPolynomial = 7; // unused
|
2014-09-30 17:29:49 -04:00
|
|
|
spi_init(wiznet5k_obj.spi, false);
|
2014-09-01 17:48:57 -04:00
|
|
|
|
2016-10-17 23:32:42 -04:00
|
|
|
mp_hal_pin_output(wiznet5k_obj.cs);
|
|
|
|
mp_hal_pin_output(wiznet5k_obj.rst);
|
2014-09-01 17:48:57 -04:00
|
|
|
|
2016-10-17 23:32:42 -04:00
|
|
|
mp_hal_pin_low(wiznet5k_obj.rst);
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(1); // datasheet says 2us
|
2016-10-17 23:32:42 -04:00
|
|
|
mp_hal_pin_high(wiznet5k_obj.rst);
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(160); // datasheet says 150ms
|
2014-09-01 17:48:57 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2k buffer for each socket
|
|
|
|
ctlwizchip(CW_INIT_WIZCHIP, sn_size);
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
// set some sensible default values; they are configurable using ifconfig method
|
2014-09-01 17:48:57 -04:00
|
|
|
wiz_NetInfo netinfo = {
|
|
|
|
.mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef},
|
|
|
|
.ip = {192, 168, 0, 18},
|
|
|
|
.sn = {255, 255, 255, 0},
|
|
|
|
.gw = {192, 168, 0, 1},
|
|
|
|
.dns = {8, 8, 8, 8}, // Google public DNS
|
|
|
|
.dhcp = NETINFO_STATIC,
|
|
|
|
};
|
|
|
|
ctlnetwork(CN_SET_NETINFO, (void *)&netinfo);
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
// seems we need a small delay after init
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(250);
|
2014-11-29 08:48:45 -05:00
|
|
|
|
2014-09-30 17:29:49 -04:00
|
|
|
// register with network module
|
|
|
|
mod_network_register_nic(&wiznet5k_obj);
|
|
|
|
|
2014-09-01 17:48:57 -04:00
|
|
|
// return wiznet5k object
|
|
|
|
return &wiznet5k_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \method regs()
|
2014-11-29 08:48:45 -05:00
|
|
|
/// Dump WIZNET5K registers.
|
2014-09-01 17:48:57 -04:00
|
|
|
STATIC mp_obj_t wiznet5k_regs(mp_obj_t self_in) {
|
|
|
|
// wiznet5k_obj_t *self = self_in;
|
|
|
|
printf("Wiz CREG:");
|
|
|
|
for (int i = 0; i < 0x50; ++i) {
|
|
|
|
if (i % 16 == 0) {
|
|
|
|
printf("\n %04x:", i);
|
|
|
|
}
|
2017-10-16 00:36:57 -04:00
|
|
|
#if MICROPY_PY_WIZNET5K == 5200
|
|
|
|
uint32_t reg = i;
|
|
|
|
#else
|
|
|
|
uint32_t reg = _W5500_IO_BASE_ | i << 8;
|
|
|
|
#endif
|
|
|
|
printf(" %02x", WIZCHIP_READ(reg));
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
for (int sn = 0; sn < 4; ++sn) {
|
|
|
|
printf("\nWiz SREG[%d]:", sn);
|
|
|
|
for (int i = 0; i < 0x30; ++i) {
|
|
|
|
if (i % 16 == 0) {
|
|
|
|
printf("\n %04x:", i);
|
|
|
|
}
|
2017-10-16 00:36:57 -04:00
|
|
|
#if MICROPY_PY_WIZNET5K == 5200
|
|
|
|
uint32_t reg = WIZCHIP_SREG_ADDR(sn, i);
|
|
|
|
#else
|
|
|
|
uint32_t reg = _W5500_IO_BASE_ | i << 8 | WIZCHIP_SREG_BLOCK(sn) << 3;
|
|
|
|
#endif
|
|
|
|
printf(" %02x", WIZCHIP_READ(reg));
|
2014-09-01 17:48:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_regs_obj, wiznet5k_regs);
|
|
|
|
|
2017-10-15 22:16:00 -04:00
|
|
|
STATIC mp_obj_t wiznet5k_isconnected(mp_obj_t self_in) {
|
|
|
|
(void)self_in;
|
|
|
|
return mp_obj_new_bool(wizphy_getphylink() == PHY_LINK_ON);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wiznet5k_isconnected_obj, wiznet5k_isconnected);
|
|
|
|
|
2014-11-29 08:48:45 -05:00
|
|
|
/// \method ifconfig([(ip, subnet, gateway, dns)])
|
2014-09-01 17:48:57 -04:00
|
|
|
/// Get/set IP address, subnet mask, gateway and DNS.
|
2017-08-29 20:59:58 -04:00
|
|
|
STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) {
|
2014-09-01 17:48:57 -04:00
|
|
|
wiz_NetInfo netinfo;
|
|
|
|
ctlnetwork(CN_GET_NETINFO, &netinfo);
|
|
|
|
if (n_args == 1) {
|
|
|
|
// get
|
|
|
|
mp_obj_t tuple[4] = {
|
2015-05-03 14:26:08 -04:00
|
|
|
netutils_format_ipv4_addr(netinfo.ip, NETUTILS_BIG),
|
|
|
|
netutils_format_ipv4_addr(netinfo.sn, NETUTILS_BIG),
|
|
|
|
netutils_format_ipv4_addr(netinfo.gw, NETUTILS_BIG),
|
|
|
|
netutils_format_ipv4_addr(netinfo.dns, NETUTILS_BIG),
|
2014-09-01 17:48:57 -04:00
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(4, tuple);
|
|
|
|
} else {
|
|
|
|
// set
|
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_get_array_fixed_n(args[1], 4, &items);
|
2015-05-03 14:26:08 -04:00
|
|
|
netutils_parse_ipv4_addr(items[0], netinfo.ip, NETUTILS_BIG);
|
|
|
|
netutils_parse_ipv4_addr(items[1], netinfo.sn, NETUTILS_BIG);
|
|
|
|
netutils_parse_ipv4_addr(items[2], netinfo.gw, NETUTILS_BIG);
|
|
|
|
netutils_parse_ipv4_addr(items[3], netinfo.dns, NETUTILS_BIG);
|
2014-09-01 17:48:57 -04:00
|
|
|
ctlnetwork(CN_SET_NETINFO, &netinfo);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
2014-11-29 08:48:45 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_ifconfig_obj, 1, 2, wiznet5k_ifconfig);
|
2014-09-01 17:48:57 -04:00
|
|
|
|
2017-05-06 03:03:40 -04:00
|
|
|
STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_regs), MP_ROM_PTR(&wiznet5k_regs_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) },
|
2017-10-15 22:16:00 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&wiznet5k_isconnected_obj) },
|
2014-09-01 17:48:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(wiznet5k_locals_dict, wiznet5k_locals_dict_table);
|
|
|
|
|
2014-09-30 17:29:49 -04:00
|
|
|
const mod_network_nic_type_t mod_network_nic_type_wiznet5k = {
|
|
|
|
.base = {
|
|
|
|
{ &mp_type_type },
|
2014-11-29 08:48:45 -05:00
|
|
|
.name = MP_QSTR_WIZNET5K,
|
2014-09-30 17:29:49 -04:00
|
|
|
.make_new = wiznet5k_make_new,
|
2017-05-06 03:03:40 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&wiznet5k_locals_dict,
|
2014-09-30 17:29:49 -04:00
|
|
|
},
|
|
|
|
.gethostbyname = wiznet5k_gethostbyname,
|
2014-11-29 08:48:45 -05:00
|
|
|
.socket = wiznet5k_socket_socket,
|
|
|
|
.close = wiznet5k_socket_close,
|
|
|
|
.bind = wiznet5k_socket_bind,
|
|
|
|
.listen = wiznet5k_socket_listen,
|
|
|
|
.accept = wiznet5k_socket_accept,
|
|
|
|
.connect = wiznet5k_socket_connect,
|
|
|
|
.send = wiznet5k_socket_send,
|
|
|
|
.recv = wiznet5k_socket_recv,
|
|
|
|
.sendto = wiznet5k_socket_sendto,
|
|
|
|
.recvfrom = wiznet5k_socket_recvfrom,
|
|
|
|
.setsockopt = wiznet5k_socket_setsockopt,
|
|
|
|
.settimeout = wiznet5k_socket_settimeout,
|
|
|
|
.ioctl = wiznet5k_socket_ioctl,
|
2014-09-01 17:48:57 -04:00
|
|
|
};
|
stm32: Add network driver for Wiznet5k using MACRAW mode and lwIP.
The Wiznet5k series of chips support a MACRAW mode which allows the host to
send and receive Ethernet frames directly. This can be hooked into the
lwIP stack to provide a full "socket" implementation using this Wiznet
Ethernet device. This patch adds support for this feature.
To enable the feature one must add the following to mpconfigboard.mk, or
mpconfigport.mk:
MICROPY_PY_WIZNET5K = 5500
and the following to mpconfigboard.h, or mpconfigport.h:
#define MICROPY_PY_LWIP (1)
After wiring up the module (X5=CS, X4=RST), usage on a pyboard is:
import time, network
nic = network.WIZNET5K(pyb.SPI(1), pyb.Pin.board.X5, pyb.Pin.board.X4)
nic.active(1)
while not nic.isconnected():
time.sleep_ms(50) # needed to poll the NIC
print(nic.ifconfig())
Then use the socket module as usual.
Compared to using the built-in TCP/IP stack on the Wiznet module, some
performance is lost in MACRAW mode: with a lot of memory allocated to lwIP
buffers, lwIP gives Around 750,000 bytes/sec max TCP download, compared
with 1M/sec when using the TCP/IP stack on the Wiznet module.
2018-06-01 00:21:38 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_PY_WIZNET5K && !MICROPY_PY_LWIP
|