Commit Iniziale, progetto funzionante caricato su box ETcontroller in

cantina
This commit is contained in:
2019-10-09 23:25:44 +02:00
commit c6e4e461ca
555 changed files with 118433 additions and 0 deletions

View File

@@ -0,0 +1,403 @@
/* Copyright (C) 2012 mbed.org, MIT License
*
* 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 <mbed.h>
#include "mbed_debug.h"
#include "wiznet.h"
#include "DNSClient.h"
#ifdef USE_W5100
//Debug is disabled by default
#if 0
#define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
//#define DBG(x, ...) debug("[WIZnet_Chip:DBG]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) debug("[WIZnet_Chip:WARN]"x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) debug("[WIZnet_Chip:ERR]"x"\r\n", ##__VA_ARGS__);
#else
#define DBG(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#endif
#if 1
#define INFO(x, ...) debug("[WIZnet_Chip:INFO]"x"\r\n", ##__VA_ARGS__);
#else
#define INFO(x, ...)
#endif
#define DBG_SPI 0
WIZnet_Chip* WIZnet_Chip::inst;
WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
cs(_cs), reset_pin(_reset)
{
spi = new SPI(mosi, miso, sclk);
spi->format(8,0);
spi->frequency(2000000);
cs = 1;
reset_pin = 1;
inst = this;
}
WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset):
cs(_cs), reset_pin(_reset)
{
this->spi = spi;
this->spi->format(8,0);
this->spi->frequency(2000000);
cs = 1;
reset_pin = 1;
inst = this;
}
bool WIZnet_Chip::setip()
{
reg_wr<uint32_t>(SIPR, ip);
reg_wr<uint32_t>(GAR, gateway);
reg_wr<uint32_t>(SUBR, netmask);
return true;
}
bool WIZnet_Chip::setProtocol(int socket, Protocol p)
{
if (socket < 0) {
return false;
}
sreg<uint8_t>(socket, Sn_MR, p);
return true;
}
bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms)
{
if (socket < 0) {
return false;
}
sreg<uint8_t>(socket, Sn_MR, TCP);
scmd(socket, OPEN);
sreg_ip(socket, Sn_DIPR, host);
sreg<uint16_t>(socket, Sn_DPORT, port);
sreg<uint16_t>(socket, Sn_PORT, new_port());
scmd(socket, eCONNECT);
Timer t;
t.reset();
t.start();
while(!is_connected(socket)) {
if (t.read_ms() > timeout_ms) {
return false;
}
}
return true;
}
bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip)
{
uint32_t addr = str_to_ip(host);
char buf[17];
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
if (strcmp(buf, host) == 0) {
*ip = addr;
return true;
}
DNSClient client;
if(client.lookup(host)) {
*ip = client.ip;
return true;
}
return false;
}
bool WIZnet_Chip::disconnect()
{
return true;
}
bool WIZnet_Chip::is_connected(int socket)
{
if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
return true;
}
return false;
}
bool WIZnet_Chip::is_fin_received(int socket)
{
uint8_t tmpSn_SR;
tmpSn_SR = sreg<uint8_t>(socket, Sn_SR);
// packet sending is possible, when state is SOCK_CLOSE_WAIT.
if (tmpSn_SR == SOCK_CLOSE_WAIT) {
return true;
}
return false;
}
void WIZnet_Chip::reset()
{
reset_pin = 1;
reset_pin = 0;
wait_us(2); // 2us
reset_pin = 1;
wait_ms(150); // 150ms
reg_wr<uint8_t>(MR, 1<<7);
reg_wr_mac(SHAR, mac);
}
bool WIZnet_Chip::close(int socket)
{
if (socket < 0) {
return false;
}
// if not connected, return
if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
return true;
}
scmd(socket, CLOSE);
sreg<uint8_t>(socket, Sn_IR, 0xff);
return true;
}
int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size)
{
if (socket < 0) {
return -1;
}
Timer t;
t.reset();
t.start();
while(1) {
int size = 0; int size1 = 0;
do {
size = sreg<uint16_t>(socket, Sn_RX_RSR);
if (size != 0) size1 = sreg<uint16_t>(socket, Sn_RX_RSR);
}while(size != size1);
if (size > req_size) {
return size;
}
if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
break;
}
}
return -1;
}
int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size)
{
if (socket < 0) {
return -1;
}
Timer t;
t.reset();
t.start();
while(1) {
//int size = sreg<uint16_t>(socket, Sn_TX_FSR);
// during the reading Sn_TX_FSR, it has the possible change of this register.
// so read twice and get same value then use size information.
int size, size2;
do {
size = sreg<uint16_t>(socket, Sn_TX_FSR);
size2 = sreg<uint16_t>(socket, Sn_TX_FSR);
} while (size != size2);
if (size > req_size) {
return size;
}
if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
break;
}
}
return -1;
}
int WIZnet_Chip::send(int socket, const char * str, int len)
{
if (socket < 0) {
return -1;
}
uint16_t base = 0x4000 + socket * 0x800; // each socket has 2K buffer
uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
uint16_t dst = base + (ptr&(0x800-1));
if ((dst + len) > (base+0x800)) {
int len2 = base + 0x800 - dst;
spi_write(dst, (uint8_t*)str, len2);
spi_write(base, (uint8_t*)str+len2, len-len2);
} else {
spi_write(dst, (uint8_t*)str, len);
}
sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
scmd(socket, SEND);
return len;
}
int WIZnet_Chip::recv(int socket, char* buf, int len)
{
if (socket < 0) {
return -1;
}
uint16_t base = 0x6000 + socket * 0x800; // each socket has 2K buffer
uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
uint16_t src = base + (ptr&(0x800-1));
if ((src + len) > (base+0x800)) {
int len2 = base + 0x800 - src;
spi_read(src, (uint8_t*)buf, len2);
spi_read(base, (uint8_t*)buf+len2, len-len2);
} else {
spi_read(src, (uint8_t*)buf, len);
}
sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
scmd(socket, RECV);
return len;
}
int WIZnet_Chip::new_socket()
{
for(int s = 0; s < MAX_SOCK_NUM; s++) {
if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
return s;
}
}
return -1;
}
uint16_t WIZnet_Chip::new_port()
{
uint16_t port = rand();
port |= 49152;
return port;
}
void WIZnet_Chip::scmd(int socket, Command cmd)
{
sreg<uint8_t>(socket, Sn_CR, cmd);
while(sreg<uint8_t>(socket, Sn_CR));
}
void WIZnet_Chip::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len)
{
for(int i = 0; i < len; i++) {
cs = 0;
spi->write(0xf0);
spi->write(addr >> 8);
spi->write(addr & 0xff);
addr++;
spi->write(buf[i]);
cs = 1;
}
#if DBG_SPI
debug("[SPI]W %04x(%d)", addr, len);
for(int i = 0; i < len; i++) {
debug(" %02x", buf[i]);
if (i > 16) {
debug(" ...");
break;
}
}
debug("\r\n");
#endif
}
void WIZnet_Chip::spi_read(uint16_t addr, uint8_t *buf, uint16_t len)
{
for(int i = 0; i < len; i++) {
cs = 0;
spi->write(0x0f);
spi->write(addr >> 8);
spi->write(addr & 0xff);
addr++;
buf[i] = spi->write(0);
cs = 1;
}
#if DBG_SPI
debug("[SPI]R %04x(%d)", addr, len);
for(int i = 0; i < len; i++) {
debug(" %02x", buf[i]);
if (i > 16) {
debug(" ...");
break;
}
}
debug("\r\n");
if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
wait_ms(200);
}
#endif
}
uint32_t str_to_ip(const char* str)
{
uint32_t ip = 0;
char* p = (char*)str;
for(int i = 0; i < 4; i++) {
ip |= atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
ip <<= 8;
p++;
}
return ip;
}
void printfBytes(char* str, uint8_t* buf, int len)
{
printf("%s %d:", str, len);
for(int i = 0; i < len; i++) {
printf(" %02x", buf[i]);
}
printf("\n");
}
void printHex(uint8_t* buf, int len)
{
for(int i = 0; i < len; i++) {
if ((i%16) == 0) {
printf("%p", buf+i);
}
printf(" %02x", buf[i]);
if ((i%16) == 15) {
printf("\n");
}
}
printf("\n");
}
void debug_hex(uint8_t* buf, int len)
{
for(int i = 0; i < len; i++) {
if ((i%16) == 0) {
debug("%p", buf+i);
}
debug(" %02x", buf[i]);
if ((i%16) == 15) {
debug("\n");
}
}
debug("\n");
}
#endif

View File

@@ -0,0 +1,269 @@
/* Copyright (C) 2012 mbed.org, MIT License
*
* 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.
*
*/
#pragma once
#include "mbed.h"
#include "mbed_debug.h"
#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
#define DEFAULT_WAIT_RESP_TIMEOUT 500
enum Protocol {
CLOSED = 0,
TCP = 1,
UDP = 2,
};
enum Command {
OPEN = 0x01,
LISTEN = 0x02,
eCONNECT = 0x04,
DISCON = 0x08,
CLOSE = 0x10,
SEND = 0x20,
SEND_MAC = 0x21,
SEND_KEEP = 0x22,
RECV = 0x40,
};
enum Interrupt {
INT_CON = 0x01,
INT_DISCON = 0x02,
INT_RECV = 0x04,
INT_TIMEOUT = 0x08,
INT_SEND_OK = 0x10,
};
enum Status {
SOCK_CLOSED = 0x00,
SOCK_INIT = 0x13,
SOCK_LISTEN = 0x14,
SOCK_ESTABLISHED = 0x17,
SOCK_CLOSE_WAIT = 0x1c,
SOCK_UDP = 0x22,
};
#define MAX_SOCK_NUM 4
#define MR 0x0000
#define GAR 0x0001
#define SUBR 0x0005
#define SHAR 0x0009
#define SIPR 0x000f
// W5100 socket
#define Sn_MR 0x0400
#define Sn_CR 0x0401
#define Sn_IR 0x0402
#define Sn_SR 0x0403
#define Sn_PORT 0x0404
#define Sn_DIPR 0x040c
#define Sn_DPORT 0x0410
//#define Sn_RXBUF_SIZE 0x401e
//#define Sn_TXBUF_SIZE 0x401f
#define Sn_TX_FSR 0x0420
#define Sn_TX_WR 0x0424
#define Sn_RX_RSR 0x0426
#define Sn_RX_RD 0x0428
class WIZnet_Chip {
public:
/*
* Constructor
*
* @param spi spi class
* @param cs cs of the W5200
* @param reset reset pin of the W5200
*/
WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
/*
* Connect the W5200 module to the ssid contained in the constructor.
*
* @return true if connected, false otherwise
*/
bool setip();
/*
* Disconnect the W5200 module from the access point
*
* @ returns true if successful
*/
bool disconnect();
/*
* Open a tcp connection with the specified host on the specified port
*
* @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
* @param port port
* @ returns true if successful
*/
bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
/*
* Set the protocol (UDP or TCP)
*
* @param p protocol
* @ returns true if successful
*/
bool setProtocol(int socket, Protocol p);
/*
* Reset the W5100 module
*/
void reset();
int wait_readable(int socket, int wait_time_ms, int req_size = 0);
int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
/*
* Check if a tcp link is active
*
* @returns true if successful
*/
bool is_connected(int socket);
/*
* Check if FIN received.
*
* @returns true if successful
*/
bool is_fin_received(int socket);
/*
* Close a tcp connection
*
* @ returns true if successful
*/
bool close(int socket);
/*
* @param str string to be sent
* @param len string length
*/
int send(int socket, const char * str, int len);
int recv(int socket, char* buf, int len);
/*
* Return true if the module is using dhcp
*
* @returns true if the module is using dhcp
*/
bool isDHCP() {
return dhcp;
}
bool gethostbyname(const char* host, uint32_t* ip);
static WIZnet_Chip * getInstance() {
return inst;
};
int new_socket();
uint16_t new_port();
void scmd(int socket, Command cmd);
template<typename T>
void sreg(int socket, uint16_t addr, T data) {
reg_wr<T>(addr+0x100*socket, data);
}
template<typename T>
T sreg(int socket, uint16_t addr) {
return reg_rd<T>(addr+0x100*socket);
}
template<typename T>
void reg_wr(uint16_t addr, T data) {
uint8_t buf[sizeof(T)];
*reinterpret_cast<T*>(buf) = data;
for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian
uint8_t t = buf[i];
buf[i] = buf[sizeof(buf)-1-i];
buf[sizeof(buf)-1-i] = t;
}
spi_write(addr, buf, sizeof(buf));
}
template<typename T>
T reg_rd(uint16_t addr) {
uint8_t buf[sizeof(T)];
spi_read(addr, buf, sizeof(buf));
for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
uint8_t t = buf[i];
buf[i] = buf[sizeof(buf)-1-i];
buf[sizeof(buf)-1-i] = t;
}
return *reinterpret_cast<T*>(buf);
}
void reg_rd_mac(uint16_t addr, uint8_t* data) {
spi_read(addr, data, 6);
}
void reg_wr_ip(uint16_t addr, const char* ip) {
uint8_t buf[4];
char* p = (char*)ip;
for(int i = 0; i < 4; i++) {
buf[i] = atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
p++;
}
spi_write(addr, buf, sizeof(buf));
}
void sreg_ip(int socket, uint16_t addr, const char* ip) {
reg_wr_ip(addr+0x100*socket, ip);
}
protected:
uint8_t mac[6];
uint32_t ip;
uint32_t netmask;
uint32_t gateway;
uint32_t dnsaddr;
bool dhcp;
static WIZnet_Chip* inst;
void reg_wr_mac(uint16_t addr, uint8_t* data) {
spi_write(addr, data, 6);
}
void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len);
void spi_read(uint16_t addr, uint8_t *buf, uint16_t len);
SPI* spi;
DigitalOut cs;
DigitalOut reset_pin;
};
extern uint32_t str_to_ip(const char* str);
extern void printfBytes(char* str, uint8_t* buf, int len);
extern void printHex(uint8_t* buf, int len);
extern void debug_hex(uint8_t* buf, int len);

View File

@@ -0,0 +1,402 @@
/* Copyright (C) 2012 mbed.org, MIT License
*
* 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 "mbed.h"
#include "mbed_debug.h"
#include "wiznet.h"
#include "DNSClient.h"
#ifdef USE_W5200
//Debug is disabled by default
#if 0
#define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
//#define DBG(x, ...) debug("[WIZnet_Chip:DBG]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) debug("[WIZnet_Chip:WARN]"x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) debug("[WIZnet_Chip:ERR]"x"\r\n", ##__VA_ARGS__);
#else
#define DBG(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#endif
#if 1
#define INFO(x, ...) debug("[WIZnet_Chip:INFO]"x"\r\n", ##__VA_ARGS__);
#else
#define INFO(x, ...)
#endif
#define DBG_SPI 0
WIZnet_Chip* WIZnet_Chip::inst;
WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
cs(_cs), reset_pin(_reset)
{
spi = new SPI(mosi, miso, sclk);
cs = 1;
reset_pin = 1;
inst = this;
}
WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset):
cs(_cs), reset_pin(_reset)
{
this->spi = spi;
cs = 1;
reset_pin = 1;
inst = this;
}
bool WIZnet_Chip::setip()
{
reg_wr<uint32_t>(SIPR, ip);
reg_wr<uint32_t>(GAR, gateway);
reg_wr<uint32_t>(SUBR, netmask);
return true;
}
bool WIZnet_Chip::setProtocol(int socket, Protocol p)
{
if (socket < 0) {
return false;
}
sreg<uint8_t>(socket, Sn_MR, p);
return true;
}
bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms)
{
if (socket < 0) {
return false;
}
sreg<uint8_t>(socket, Sn_MR, TCP);
scmd(socket, OPEN);
sreg_ip(socket, Sn_DIPR, host);
sreg<uint16_t>(socket, Sn_DPORT, port);
sreg<uint16_t>(socket, Sn_PORT, new_port());
scmd(socket, CONNECT);
Timer t;
t.reset();
t.start();
while(!is_connected(socket)) {
if (t.read_ms() > timeout_ms) {
return false;
}
}
return true;
}
bool WIZnet_Chip::is_fin_received(int socket)
{
uint8_t tmpSn_SR;
tmpSn_SR = sreg<uint8_t>(socket, Sn_SR);
// packet sending is possible, when state is SOCK_CLOSE_WAIT.
if (tmpSn_SR == SOCK_CLOSE_WAIT) {
return true;
}
return false;
}
bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip)
{
uint32_t addr = str_to_ip(host);
char buf[17];
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
if (strcmp(buf, host) == 0) {
*ip = addr;
return true;
}
DNSClient client;
if(client.lookup(host)) {
*ip = client.ip;
return true;
}
return false;
}
bool WIZnet_Chip::disconnect()
{
return true;
}
bool WIZnet_Chip::is_connected(int socket)
{
if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
return true;
}
return false;
}
void WIZnet_Chip::reset()
{
reset_pin = 1;
reset_pin = 0;
wait_us(2); // 2us
reset_pin = 1;
wait_ms(150); // 150ms
reg_wr<uint8_t>(MR, 1<<7);
reg_wr_mac(SHAR, mac);
}
bool WIZnet_Chip::close(int socket)
{
if (socket < 0) {
return false;
}
// if not connected, return
if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
return true;
}
if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
scmd(socket, DISCON);
}
scmd(socket, CLOSE);
sreg<uint8_t>(socket, Sn_IR, 0xff);
return true;
}
int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size)
{
if (socket < 0) {
return -1;
}
Timer t;
t.reset();
t.start();
while(1) {
//int size = sreg<uint16_t>(socket, Sn_RX_RSR);
// during the reading Sn_RX_RXR, it has the possible change of this register.
// so read twice and get same value then use size information.
int size, size2;
do {
size = sreg<uint16_t>(socket, Sn_RX_RSR);
size2 = sreg<uint16_t>(socket, Sn_RX_RSR);
} while (size != size2);
if (size > req_size) {
return size;
}
if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
break;
}
}
return -1;
}
int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size)
{
if (socket < 0) {
return -1;
}
Timer t;
t.reset();
t.start();
while(1) {
//int size = sreg<uint16_t>(socket, Sn_TX_FSR);
// during the reading Sn_TX_FSR, it has the possible change of this register.
// so read twice and get same value then use size information.
int size, size2;
do {
size = sreg<uint16_t>(socket, Sn_TX_FSR);
size2 = sreg<uint16_t>(socket, Sn_TX_FSR);
} while (size != size2);
if (size > req_size) {
return size;
}
if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
break;
}
}
return -1;
}
int WIZnet_Chip::send(int socket, const char * str, int len)
{
if (socket < 0) {
return -1;
}
uint16_t base = 0x8000 + socket * 0x800;
uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
uint16_t dst = base + (ptr&(0x800-1));
if ((dst + len) > (base+0x800)) {
int len2 = base + 0x800 - dst;
spi_write(dst, (uint8_t*)str, len2);
spi_write(base, (uint8_t*)str+len2, len-len2);
} else {
spi_write(dst, (uint8_t*)str, len);
}
sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
scmd(socket, SEND);
return len;
}
int WIZnet_Chip::recv(int socket, char* buf, int len)
{
if (socket < 0) {
return -1;
}
uint16_t base = 0xc000 + socket * 0x800;
uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
uint16_t src = base + (ptr&(0x800-1));
if ((src + len) > (base+0x800)) {
int len2 = base + 0x800 - src;
spi_read(src, (uint8_t*)buf, len2);
spi_read(base, (uint8_t*)buf+len2, len-len2);
} else {
spi_read(src, (uint8_t*)buf, len);
}
sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
scmd(socket, RECV);
return len;
}
int WIZnet_Chip::new_socket()
{
for(int s = 0; s < 8; s++) {
if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
return s;
}
}
return -1;
}
uint16_t WIZnet_Chip::new_port()
{
uint16_t port = rand();
port |= 49152;
return port;
}
void WIZnet_Chip::scmd(int socket, Command cmd)
{
sreg<uint8_t>(socket, Sn_CR, cmd);
while(sreg<uint8_t>(socket, Sn_CR));
}
void WIZnet_Chip::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len)
{
cs = 0;
spi->write(addr >> 8);
spi->write(addr & 0xff);
spi->write((0x80 | ((len & 0x7f00) >> 8)));
spi->write(len & 0xff);
for(int i = 0; i < len; i++) {
spi->write(buf[i]);
}
cs = 1;
#if DBG_SPI
debug("[SPI]W %04x(%d)", addr, len);
for(int i = 0; i < len; i++) {
debug(" %02x", buf[i]);
if (i > 16) {
debug(" ...");
break;
}
}
debug("\r\n");
#endif
}
void WIZnet_Chip::spi_read(uint16_t addr, uint8_t *buf, uint16_t len)
{
cs = 0;
spi->write(addr >> 8);
spi->write(addr & 0xff);
spi->write((0x00 | ((len & 0x7f00) >> 8)));
spi->write(len & 0xff);
for(int i = 0; i < len; i++) {
buf[i] = spi->write(0);
}
cs = 1;
#if DBG_SPI
debug("[SPI]R %04x(%d)", addr, len);
for(int i = 0; i < len; i++) {
debug(" %02x", buf[i]);
if (i > 16) {
debug(" ...");
break;
}
}
debug("\r\n");
if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
wait_ms(200);
}
#endif
}
uint32_t str_to_ip(const char* str)
{
uint32_t ip = 0;
char* p = (char*)str;
for(int i = 0; i < 4; i++) {
ip |= atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
ip <<= 8;
p++;
}
return ip;
}
void printfBytes(char* str, uint8_t* buf, int len)
{
printf("%s %d:", str, len);
for(int i = 0; i < len; i++) {
printf(" %02x", buf[i]);
}
printf("\n");
}
void printHex(uint8_t* buf, int len)
{
for(int i = 0; i < len; i++) {
if ((i%16) == 0) {
printf("%p", buf+i);
}
printf(" %02x", buf[i]);
if ((i%16) == 15) {
printf("\n");
}
}
printf("\n");
}
void debug_hex(uint8_t* buf, int len)
{
for(int i = 0; i < len; i++) {
if ((i%16) == 0) {
debug("%p", buf+i);
}
debug(" %02x", buf[i]);
if ((i%16) == 15) {
debug("\n");
}
}
debug("\n");
}
#endif

View File

@@ -0,0 +1,271 @@
/* Copyright (C) 2012 mbed.org, MIT License
*
* 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.
*
*/
#pragma once
#include "mbed.h"
#include "mbed_debug.h"
#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
#define DEFAULT_WAIT_RESP_TIMEOUT 500
enum Protocol {
CLOSED = 0,
TCP = 1,
UDP = 2,
};
enum Command {
OPEN = 0x01,
LISTEN = 0x02,
CONNECT = 0x04,
DISCON = 0x08,
CLOSE = 0x10,
SEND = 0x20,
SEND_MAC = 0x21,
SEND_KEEP = 0x22,
RECV = 0x40,
};
enum Interrupt {
INT_CON = 0x01,
INT_DISCON = 0x02,
INT_RECV = 0x04,
INT_TIMEOUT = 0x08,
INT_SEND_OK = 0x10,
};
enum Status {
SOCK_CLOSED = 0x00,
SOCK_INIT = 0x13,
SOCK_LISTEN = 0x14,
SOCK_SYNSENT = 0x15,
SOCK_ESTABLISHED = 0x17,
SOCK_CLOSE_WAIT = 0x1c,
SOCK_UDP = 0x22,
};
#define MAX_SOCK_NUM 8
#define MR 0x0000
#define GAR 0x0001
#define SUBR 0x0005
#define SHAR 0x0009
#define SIPR 0x000f
#define PHYSTATUS 0x0035
// W5200 socket
#define Sn_MR 0x4000
#define Sn_CR 0x4001
#define Sn_IR 0x4002
#define Sn_SR 0x4003
#define Sn_PORT 0x4004
#define Sn_DIPR 0x400c
#define Sn_DPORT 0x4010
#define Sn_RXBUF_SIZE 0x401e
#define Sn_TXBUF_SIZE 0x401f
#define Sn_TX_FSR 0x4020
#define Sn_TX_WR 0x4024
#define Sn_RX_RSR 0x4026
#define Sn_RX_RD 0x4028
class WIZnet_Chip {
public:
/*
* Constructor
*
* @param spi spi class
* @param cs cs of the W5200
* @param reset reset pin of the W5200
*/
WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
/*
* Connect the W5200 module to the ssid contained in the constructor.
*
* @return true if connected, false otherwise
*/
bool setip();
/*
* Disconnect the W5200 module from the access point
*
* @ returns true if successful
*/
bool disconnect();
/*
* Open a tcp connection with the specified host on the specified port
*
* @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
* @param port port
* @ returns true if successful
*/
bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
/*
* Set the protocol (UDP or TCP)
*
* @param p protocol
* @ returns true if successful
*/
bool setProtocol(int socket, Protocol p);
/*
* Reset the W5200 module
*/
void reset();
int wait_readable(int socket, int wait_time_ms, int req_size = 0);
int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
/*
* Check if a tcp link is active
*
* @returns true if successful
*/
bool is_connected(int socket);
/*
* Check if FIN received.
*
* @returns true if successful
*/
bool is_fin_received(int socket);
/*
* Close a tcp connection
*
* @ returns true if successful
*/
bool close(int socket);
/*
* @param str string to be sent
* @param len string length
*/
int send(int socket, const char * str, int len);
int recv(int socket, char* buf, int len);
/*
* Return true if the module is using dhcp
*
* @returns true if the module is using dhcp
*/
bool isDHCP() {
return dhcp;
}
bool gethostbyname(const char* host, uint32_t* ip);
static WIZnet_Chip * getInstance() {
return inst;
};
int new_socket();
uint16_t new_port();
void scmd(int socket, Command cmd);
template<typename T>
void sreg(int socket, uint16_t addr, T data) {
reg_wr<T>(addr+0x100*socket, data);
}
template<typename T>
T sreg(int socket, uint16_t addr) {
return reg_rd<T>(addr+0x100*socket);
}
template<typename T>
void reg_wr(uint16_t addr, T data) {
uint8_t buf[sizeof(T)];
*reinterpret_cast<T*>(buf) = data;
for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian
uint8_t t = buf[i];
buf[i] = buf[sizeof(buf)-1-i];
buf[sizeof(buf)-1-i] = t;
}
spi_write(addr, buf, sizeof(buf));
}
template<typename T>
T reg_rd(uint16_t addr) {
uint8_t buf[sizeof(T)];
spi_read(addr, buf, sizeof(buf));
for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
uint8_t t = buf[i];
buf[i] = buf[sizeof(buf)-1-i];
buf[sizeof(buf)-1-i] = t;
}
return *reinterpret_cast<T*>(buf);
}
void reg_rd_mac(uint16_t addr, uint8_t* data) {
spi_read(addr, data, 6);
}
void reg_wr_ip(uint16_t addr, const char* ip) {
uint8_t buf[4];
char* p = (char*)ip;
for(int i = 0; i < 4; i++) {
buf[i] = atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
p++;
}
spi_write(addr, buf, sizeof(buf));
}
void sreg_ip(int socket, uint16_t addr, const char* ip) {
reg_wr_ip(addr+0x100*socket, ip);
}
protected:
uint8_t mac[6];
uint32_t ip;
uint32_t netmask;
uint32_t gateway;
uint32_t dnsaddr;
bool dhcp;
static WIZnet_Chip* inst;
void reg_wr_mac(uint16_t addr, uint8_t* data) {
spi_write(addr, data, 6);
}
void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len);
void spi_read(uint16_t addr, uint8_t *buf, uint16_t len);
SPI* spi;
DigitalOut cs;
DigitalOut reset_pin;
};
extern uint32_t str_to_ip(const char* str);
extern void printfBytes(char* str, uint8_t* buf, int len);
extern void printHex(uint8_t* buf, int len);
extern void debug_hex(uint8_t* buf, int len);

View File

@@ -0,0 +1,427 @@
/* Copyright (C) 2012 mbed.org, MIT License
*
* 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 "mbed.h"
#include "mbed_debug.h"
#include "wiznet.h"
#include "DNSClient.h"
#ifdef USE_W5500
//Debug is disabled by default
#if 0
#define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
//#define DBG(x, ...) debug("[W5500:DBG]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) debug("[W5500:WARN]"x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) debug("[W5500:ERR]"x"\r\n", ##__VA_ARGS__);
#else
#define DBG(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#endif
#if 1
#define INFO(x, ...) debug("[W5500:INFO]"x"\r\n", ##__VA_ARGS__);
#else
#define INFO(x, ...)
#endif
#define DBG_SPI 0
WIZnet_Chip* WIZnet_Chip::inst;
WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
cs(_cs), reset_pin(_reset)
{
spi = new SPI(mosi, miso, sclk);
cs = 1;
reset_pin = 1;
inst = this;
}
WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset):
cs(_cs), reset_pin(_reset)
{
this->spi = spi;
cs = 1;
reset_pin = 1;
inst = this;
}
// Set the IP
bool WIZnet_Chip::setip()
{
reg_wr<uint32_t>(SIPR, ip);
reg_wr<uint32_t>(GAR, gateway);
reg_wr<uint32_t>(SUBR, netmask);
return true;
}
bool WIZnet_Chip::setProtocol(int socket, Protocol p)
{
if (socket < 0) {
return false;
}
sreg<uint8_t>(socket, Sn_MR, p);
return true;
}
bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms)
{
if (socket < 0) {
return false;
}
sreg<uint8_t>(socket, Sn_MR, TCP);
scmd(socket, OPEN);
sreg_ip(socket, Sn_DIPR, host);
sreg<uint16_t>(socket, Sn_DPORT, port);
sreg<uint16_t>(socket, Sn_PORT, new_port());
scmd(socket, CONNECT);
Timer t;
t.reset();
t.start();
while(!is_connected(socket)) {
if (t.read_ms() > timeout_ms) {
return false;
}
}
return true;
}
bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip)
{
uint32_t addr = str_to_ip(host);
char buf[17];
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
if (strcmp(buf, host) == 0) {
*ip = addr;
return true;
}
DNSClient client;
if(client.lookup(host)) {
*ip = client.ip;
return true;
}
return false;
}
bool WIZnet_Chip::disconnect()
{
return true;
}
bool WIZnet_Chip::is_connected(int socket)
{
uint8_t tmpSn_SR;
tmpSn_SR = sreg<uint8_t>(socket, Sn_SR);
// packet sending is possible, when state is SOCK_CLOSE_WAIT.
if ((tmpSn_SR == SOCK_ESTABLISHED) || (tmpSn_SR == SOCK_CLOSE_WAIT)) {
return true;
}
return false;
}
bool WIZnet_Chip::is_fin_received(int socket)
{
uint8_t tmpSn_SR;
tmpSn_SR = sreg<uint8_t>(socket, Sn_SR);
// packet sending is possible, when state is SOCK_CLOSE_WAIT.
if (tmpSn_SR == SOCK_CLOSE_WAIT) {
return true;
}
return false;
}
// Reset the chip & set the buffer
void WIZnet_Chip::reset()
{
reset_pin = 1;
reset_pin = 0;
wait_us(500); // 500us (w5500)
reset_pin = 1;
wait_ms(400); // 400ms (w5500)
#if defined(USE_WIZ550IO_MAC)
reg_rd_mac(SHAR, mac); // read the MAC address inside the module
#endif
reg_wr_mac(SHAR, mac);
// set RX and TX buffer size
for (int socket = 0; socket < MAX_SOCK_NUM; socket++) {
sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2);
sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2);
}
}
bool WIZnet_Chip::close(int socket)
{
if (socket < 0) {
return false;
}
// if not connected, return
if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
return true;
}
if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
scmd(socket, DISCON);
}
scmd(socket, CLOSE);
sreg<uint8_t>(socket, Sn_IR, 0xff);
return true;
}
int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size)
{
if (socket < 0) {
return -1;
}
Timer t;
t.reset();
t.start();
while(1) {
//int size = sreg<uint16_t>(socket, Sn_RX_RSR);
// during the reading Sn_RX_RXR, it has the possible change of this register.
// so read twice and get same value then use size information.
int size, size2;
do {
size = sreg<uint16_t>(socket, Sn_RX_RSR);
size2 = sreg<uint16_t>(socket, Sn_RX_RSR);
} while (size != size2);
if (size > req_size) {
return size;
}
if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
break;
}
}
return -1;
}
int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size)
{
if (socket < 0) {
return -1;
}
Timer t;
t.reset();
t.start();
while(1) {
//int size = sreg<uint16_t>(socket, Sn_TX_FSR);
// during the reading Sn_TX_FSR, it has the possible change of this register.
// so read twice and get same value then use size information.
int size, size2;
do {
size = sreg<uint16_t>(socket, Sn_TX_FSR);
size2 = sreg<uint16_t>(socket, Sn_TX_FSR);
} while (size != size2);
if (size > req_size) {
return size;
}
if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
break;
}
}
return -1;
}
int WIZnet_Chip::send(int socket, const char * str, int len)
{
if (socket < 0) {
return -1;
}
uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
uint8_t cntl_byte = (0x14 + (socket << 5));
spi_write(ptr, cntl_byte, (uint8_t*)str, len);
sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
scmd(socket, SEND);
uint8_t tmp_Sn_IR;
while (( (tmp_Sn_IR = sreg<uint8_t>(socket, Sn_IR)) & INT_SEND_OK) != INT_SEND_OK) {
// @Jul.10, 2014 fix contant name, and udp sendto function.
switch (sreg<uint8_t>(socket, Sn_SR)) {
case SOCK_CLOSED :
close(socket);
return 0;
//break;
case SOCK_UDP :
// ARP timeout is possible.
if ((tmp_Sn_IR & INT_TIMEOUT) == INT_TIMEOUT) {
sreg<uint8_t>(socket, Sn_IR, INT_TIMEOUT);
return 0;
}
break;
default :
break;
}
}
/*
while ((sreg<uint8_t>(socket, Sn_IR) & INT_SEND_OK) != INT_SEND_OK) {
if (sreg<uint8_t>(socket, Sn_SR) == CLOSED) {
close(socket);
return 0;
}
}
*/
sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK);
return len;
}
int WIZnet_Chip::recv(int socket, char* buf, int len)
{
if (socket < 0) {
return -1;
}
uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
uint8_t cntl_byte = (0x18 + (socket << 5));
spi_read(ptr, cntl_byte, (uint8_t*)buf, len);
sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
scmd(socket, RECV);
return len;
}
int WIZnet_Chip::new_socket()
{
for(int s = 0; s < MAX_SOCK_NUM; s++) {
if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
return s;
}
}
return -1;
}
uint16_t WIZnet_Chip::new_port()
{
uint16_t port = rand();
port |= 49152;
return port;
}
void WIZnet_Chip::scmd(int socket, Command cmd)
{
sreg<uint8_t>(socket, Sn_CR, cmd);
while(sreg<uint8_t>(socket, Sn_CR));
}
void WIZnet_Chip::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len)
{
cs = 0;
spi->write(addr >> 8);
spi->write(addr & 0xff);
spi->write(cb);
for(int i = 0; i < len; i++) {
spi->write(buf[i]);
}
cs = 1;
#if DBG_SPI
debug("[SPI]W %04x(%02x %d)", addr, cb, len);
for(int i = 0; i < len; i++) {
debug(" %02x", buf[i]);
if (i > 16) {
debug(" ...");
break;
}
}
debug("\r\n");
#endif
}
void WIZnet_Chip::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len)
{
cs = 0;
spi->write(addr >> 8);
spi->write(addr & 0xff);
spi->write(cb);
for(int i = 0; i < len; i++) {
buf[i] = spi->write(0);
}
cs = 1;
#if DBG_SPI
debug("[SPI]R %04x(%02x %d)", addr, cb, len);
for(int i = 0; i < len; i++) {
debug(" %02x", buf[i]);
if (i > 16) {
debug(" ...");
break;
}
}
debug("\r\n");
if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
wait_ms(200);
}
#endif
}
uint32_t str_to_ip(const char* str)
{
uint32_t ip = 0;
char* p = (char*)str;
for(int i = 0; i < 4; i++) {
ip |= atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
ip <<= 8;
p++;
}
return ip;
}
void printfBytes(char* str, uint8_t* buf, int len)
{
printf("%s %d:", str, len);
for(int i = 0; i < len; i++) {
printf(" %02x", buf[i]);
}
printf("\n");
}
void printHex(uint8_t* buf, int len)
{
for(int i = 0; i < len; i++) {
if ((i%16) == 0) {
printf("%p", buf+i);
}
printf(" %02x", buf[i]);
if ((i%16) == 15) {
printf("\n");
}
}
printf("\n");
}
void debug_hex(uint8_t* buf, int len)
{
for(int i = 0; i < len; i++) {
if ((i%16) == 0) {
debug("%p", buf+i);
}
debug(" %02x", buf[i]);
if ((i%16) == 15) {
debug("\n");
}
}
debug("\n");
}
#endif

View File

@@ -0,0 +1,280 @@
/* Copyright (C) 2012 mbed.org, MIT License
*
* 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.
*
*/
#pragma once
#include "mbed.h"
#include "mbed_debug.h"
#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
#define DEFAULT_WAIT_RESP_TIMEOUT 500
enum Protocol {
CLOSED = 0,
TCP = 1,
UDP = 2,
};
enum Command {
OPEN = 0x01,
LISTEN = 0x02,
CONNECT = 0x04,
DISCON = 0x08,
CLOSE = 0x10,
SEND = 0x20,
SEND_MAC = 0x21,
SEND_KEEP = 0x22,
RECV = 0x40,
};
enum Interrupt {
INT_CON = 0x01,
INT_DISCON = 0x02,
INT_RECV = 0x04,
INT_TIMEOUT = 0x08,
INT_SEND_OK = 0x10,
};
enum Status {
SOCK_CLOSED = 0x00,
SOCK_INIT = 0x13,
SOCK_LISTEN = 0x14,
SOCK_SYNSENT = 0x15,
SOCK_ESTABLISHED = 0x17,
SOCK_CLOSE_WAIT = 0x1c,
SOCK_UDP = 0x22,
};
#define MAX_SOCK_NUM 8
#define MR 0x0000
#define GAR 0x0001
#define SUBR 0x0005
#define SHAR 0x0009
#define SIPR 0x000f
#define PHYSTATUS 0x0035
// W5500 socket register
#define Sn_MR 0x0000
#define Sn_CR 0x0001
#define Sn_IR 0x0002
#define Sn_SR 0x0003
#define Sn_PORT 0x0004
#define Sn_DIPR 0x000c
#define Sn_DPORT 0x0010
#define Sn_RXBUF_SIZE 0x001e
#define Sn_TXBUF_SIZE 0x001f
#define Sn_TX_FSR 0x0020
#define Sn_TX_WR 0x0024
#define Sn_RX_RSR 0x0026
#define Sn_RX_RD 0x0028
class WIZnet_Chip {
public:
/*
* Constructor
*
* @param spi spi class
* @param cs cs of the W5500
* @param reset reset pin of the W5500
*/
WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
/*
* Connect the W5500 to the ssid contained in the constructor.
*
* @return true if connected, false otherwise
*/
bool setip();
/*
* Disconnect the connection
*
* @ returns true
*/
bool disconnect();
/*
* Open a tcp connection with the specified host on the specified port
*
* @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
* @param port port
* @ returns true if successful
*/
bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
/*
* Set the protocol (UDP or TCP)
*
* @param p protocol
* @ returns true if successful
*/
bool setProtocol(int socket, Protocol p);
/*
* Reset the W5500
*/
void reset();
int wait_readable(int socket, int wait_time_ms, int req_size = 0);
int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
/*
* Check if a tcp link is active
*
* @returns true if successful
*/
bool is_connected(int socket);
/*
* Check if FIN received.
*
* @returns true if successful
*/
bool is_fin_received(int socket);
/*
* Close a tcp connection
*
* @ returns true if successful
*/
bool close(int socket);
/*
* @param str string to be sent
* @param len string length
*/
int send(int socket, const char * str, int len);
int recv(int socket, char* buf, int len);
/*
* Return true if the module is using dhcp
*
* @returns true if the module is using dhcp
*/
bool isDHCP() {
return dhcp;
}
bool gethostbyname(const char* host, uint32_t* ip);
static WIZnet_Chip * getInstance() {
return inst;
};
int new_socket();
uint16_t new_port();
void scmd(int socket, Command cmd);
template<typename T>
void sreg(int socket, uint16_t addr, T data) {
reg_wr<T>(addr, (0x0C + (socket << 5)), data);
}
template<typename T>
T sreg(int socket, uint16_t addr) {
return reg_rd<T>(addr, (0x08 + (socket << 5)));
}
template<typename T>
void reg_wr(uint16_t addr, T data) {
return reg_wr(addr, 0x04, data);
}
template<typename T>
void reg_wr(uint16_t addr, uint8_t cb, T data) {
uint8_t buf[sizeof(T)];
*reinterpret_cast<T*>(buf) = data;
for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian
uint8_t t = buf[i];
buf[i] = buf[sizeof(buf)-1-i];
buf[sizeof(buf)-1-i] = t;
}
spi_write(addr, cb, buf, sizeof(buf));
}
template<typename T>
T reg_rd(uint16_t addr) {
return reg_rd<T>(addr, 0x00);
}
template<typename T>
T reg_rd(uint16_t addr, uint8_t cb) {
uint8_t buf[sizeof(T)];
spi_read(addr, cb, buf, sizeof(buf));
for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
uint8_t t = buf[i];
buf[i] = buf[sizeof(buf)-1-i];
buf[sizeof(buf)-1-i] = t;
}
return *reinterpret_cast<T*>(buf);
}
void reg_rd_mac(uint16_t addr, uint8_t* data) {
spi_read(addr, 0x00, data, 6);
}
void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) {
uint8_t buf[4];
char* p = (char*)ip;
for(int i = 0; i < 4; i++) {
buf[i] = atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
p++;
}
spi_write(addr, cb, buf, sizeof(buf));
}
void sreg_ip(int socket, uint16_t addr, const char* ip) {
reg_wr_ip(addr, (0x0C + (socket << 5)), ip);
}
protected:
uint8_t mac[6];
uint32_t ip;
uint32_t netmask;
uint32_t gateway;
uint32_t dnsaddr;
bool dhcp;
static WIZnet_Chip* inst;
void reg_wr_mac(uint16_t addr, uint8_t* data) {
spi_write(addr, 0x04, data, 6);
}
void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len);
void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len);
SPI* spi;
DigitalOut cs;
DigitalOut reset_pin;
};
extern uint32_t str_to_ip(const char* str);
extern void printfBytes(char* str, uint8_t* buf, int len);
extern void printHex(uint8_t* buf, int len);
extern void debug_hex(uint8_t* buf, int len);

View File

@@ -0,0 +1,43 @@
/* Copyright (C) 2012 mbed.org, MIT License
*
* 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.
*
*/
#pragma once
#include "mbed.h"
#include "mbed_debug.h"
//#define USE_W5500
//#define USE_W5200
#define USE_W5100
#if defined(USE_W5500)
#include "W5500.h"
//#define USE_WIZ550IO_MAC // using the MAC address stored in the WIZ550io
#endif
#if defined(USE_W5200)
#include "W5200.h"
#endif
#if defined(USE_W5100)
#include "W5100.h"
#endif