Mateusz Jaskula
/
KL25Z_TangoIO
KL25 driver for Tango Control System
Revision 0:5d27c333afa6, committed 2014-08-25
- Comitter:
- jskl
- Date:
- Mon Aug 25 12:00:15 2014 +0000
- Child:
- 1:f1ee978773d3
- Commit message:
- Initial Release
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHCPClient/DHCPClient.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,212 @@ +// DHCPClient.cpp 2013/4/10 +#include "mbed.h" +#include "mbed_debug.h" +#include "UDPSocket.h" +#include "DHCPClient.h" + +#define DBG_DHCP 0 + +#if DBG_DHCP +#define DBG(...) do{debug("[%s:%d]", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0); +#define DBG_HEX(A,B) do{debug("[%s:%d]\r\n", __PRETTY_FUNCTION__,__LINE__);debug_hex(A,B);} while(0); +#else +#define DBG(...) while(0); +#define DBG_HEX(A,B) while(0); +#endif + +int DHCPClient::discover() +{ + m_pos = 0; + const uint8_t header[] = {0x01,0x01,0x06,0x00}; + add_buf((uint8_t*)header, sizeof(header)); + uint32_t x = time(NULL) + rand(); + xid[0] = x>>24; xid[1] = x>>16; xid[2] = x>>8; xid[3] = x; + add_buf(xid, 4); + fill_buf(20, 0x00); + add_buf(chaddr, 6); + fill_buf(10+192, 0x00); + const uint8_t options[] = {0x63,0x82,0x53,0x63, // magic cookie + 53,1,DHCPDISCOVER, // DHCP option 53: DHCP Discover + 55,4,1,3,15,6, + 255}; + add_buf((uint8_t*)options, sizeof(options)); + return m_pos; +} + +int DHCPClient::request() +{ + m_pos = 0; + const uint8_t header[] = {0x01,0x01,0x06,0x00}; + add_buf((uint8_t*)header, sizeof(header)); + add_buf(xid, 4); + fill_buf(12, 0x00); + add_buf(siaddr, 4); + fill_buf(4, 0x00); // giaddr + add_buf(chaddr, 6); + fill_buf(10+192, 0x00); + const uint8_t options[] = {0x63,0x82,0x53,0x63, // magic cookie + 53,1,DHCPREQUEST, // DHCP option 53: DHCP Request + 55,4,1,3,15,6, // DHCP option 55: + }; + add_buf((uint8_t*)options, sizeof(options)); + add_option(50, yiaddr, 4); + add_option(54, siaddr, 4); + add_option(255); + return m_pos; +} + +int DHCPClient::offer(uint8_t buf[], int size) { + memcpy(yiaddr, buf+DHCP_OFFSET_YIADDR, 4); + memcpy(siaddr, buf+DHCP_OFFSET_SIADDR, 4); + uint8_t *p; + int msg_type = -1; + p = buf + DHCP_OFFSET_OPTIONS; + while(*p != 255 && p < (buf+size)) { + uint8_t code = *p++; + if (code == 0) { // Pad Option + continue; + } + int len = *p++; + + DBG("DHCP option: %d\r\n", code); + DBG_HEX(p, len); + + switch(code) { + case 53: + msg_type = *p; + break; + case 1: + memcpy(netmask, p, 4); // Subnet mask address + break; + case 3: + memcpy(gateway, p, 4); // Gateway IP address + break; + case 6: // DNS server + memcpy(dnsaddr, p, 4); + break; + case 51: // IP lease time + break; + case 54: // DHCP server + memcpy(siaddr, p, 4); + break; + } + p += len; + } + return msg_type; +} + +bool DHCPClient::verify(uint8_t buf[], int len) { + if (len < DHCP_OFFSET_OPTIONS) { + return false; + } + if (buf[DHCP_OFFSET_OP] != 0x02) { + return false; + } + if (memcmp(buf+DHCP_OFFSET_XID, xid, 4) != 0) { + return false; + } + return true; +} + +void DHCPClient::callback() +{ + Endpoint host; + int recv_len = m_udp->receiveFrom(host, (char*)m_buf, sizeof(m_buf)); + if (recv_len < 0) { + return; + } + if (!verify(m_buf, recv_len)) { + return; + } + int r = offer(m_buf, recv_len); + if (r == DHCPOFFER) { + int send_size = request(); + m_udp->sendTo(m_server, (char*)m_buf, send_size); + } else if (r == DHCPACK) { + exit_flag = true; + } +} + +void DHCPClient::add_buf(uint8_t c) +{ + m_buf[m_pos++] = c; +} + +void DHCPClient::add_buf(uint8_t* buf, int len) +{ + for(int i = 0; i < len; i++) { + add_buf(buf[i]); + } +} + +void DHCPClient::fill_buf(int len, uint8_t data) +{ + while(len-- > 0) { + add_buf(data); + } +} + +void DHCPClient::add_option(uint8_t code, uint8_t* buf, int len) +{ + add_buf(code); + if (len > 0) { + add_buf((uint8_t)len); + add_buf(buf, len); + } +} + +int DHCPClient::setup(int timeout_ms) +{ + eth = WIZnet_Chip::getInstance(); + if (eth == NULL) { + return -1; + } + eth->reg_rd_mac(SHAR, chaddr); + int interval_ms = 5*1000; // 5000msec + if (timeout_ms < interval_ms) { + interval_ms = timeout_ms; + } + m_udp = new UDPSocket; + m_udp->init(); + m_udp->set_blocking(false); + eth->reg_wr<uint32_t>(SIPR, 0x00000000); // local ip "0.0.0.0" + m_udp->bind(68); // local port + m_server.set_address("255.255.255.255", 67); // DHCP broadcast + exit_flag = false; + int err = 0; + int seq = 0; + int send_size; + while(!exit_flag) { + switch(seq) { + case 0: + m_retry = 0; + seq++; + break; + case 1: + send_size = discover(); + m_udp->sendTo(m_server, (char*)m_buf, send_size); + m_interval.reset(); + m_interval.start(); + seq++; + break; + case 2: + callback(); + if (m_interval.read_ms() > interval_ms) { + DBG("m_retry: %d\n", m_retry); + if (++m_retry >= (timeout_ms/interval_ms)) { + err = -1; + exit_flag = true; + } + seq--; + } + break; + } + } + DBG("m_retry: %d, m_interval: %d\n", m_retry, m_interval.read_ms()); + delete m_udp; + return err; +} + +DHCPClient::DHCPClient() { +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHCPClient/DHCPClient.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,55 @@ +// DHCPClient.h 2013/4/10 +#ifndef DHCPCLIENT_H +#define DHCPCLIENT_H +#include "wiznet.h" +#include "UDPSocket.h" + +#define DHCP_OFFSET_OP 0 +#define DHCP_OFFSET_XID 4 +#define DHCP_OFFSET_YIADDR 16 +#define DHCP_OFFSET_SIADDR 20 +#define DHCP_OFFSET_OPTIONS 240 +#define DHCP_MAX_PACKET_SIZE 600 + +// DHCP Message Type +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 +#define DHCPINFORM 8 + +class DHCPClient { +public: + DHCPClient(); + int setup(int timeout_ms = 15*1000); + uint8_t chaddr[6]; // MAC + uint8_t yiaddr[4]; // IP + uint8_t dnsaddr[4]; // DNS + uint8_t gateway[4]; + uint8_t netmask[4]; + uint8_t siaddr[4]; +private: + int discover(); + int request(); + int offer(uint8_t buf[], int size); + void add_buf(uint8_t* buf, int len); + void fill_buf(int len, uint8_t data = 0x00); + void add_buf(uint8_t c); + void add_option(uint8_t code, uint8_t* buf = NULL, int len = 0); + bool verify(uint8_t buf[], int len); + void callback(); + UDPSocket* m_udp; + Endpoint m_server; + uint8_t xid[4]; + bool exit_flag; + Timer m_interval; + int m_retry; + uint8_t m_buf[DHCP_MAX_PACKET_SIZE]; + int m_pos; + WIZnet_Chip* eth; +}; +#endif //DHCPCLIENT_H +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DNSClient/DNSClient.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,174 @@ +// DNSClient.cpp 2013/8/27 +#include "mbed.h" +#include "mbed_debug.h" +#include "DNSClient.h" +#include "UDPSocket.h" +#include "dnsname.h" +#include "wiznet.h" + +#define DBG_DNS 0 + +#if DBG_DNS +#define DBG2(...) do{debug("[DNS]%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0); +#else +#define DBG2(...) while(0); +#endif + +DNSClient::DNSClient(const char* hostname) : m_state(MYNETDNS_START), m_udp(NULL) { + m_hostname = hostname; +} + +DNSClient::DNSClient(Endpoint* pHost) : m_state(MYNETDNS_START), m_udp(NULL) { +} + +DNSClient::~DNSClient() { + if (m_udp) { + delete m_udp; + } +} + +void DNSClient::callback() +{ + uint8_t buf[512]; + Endpoint host; + int len = m_udp->receiveFrom(host, (char*)buf, sizeof(buf)); + if (len < 0) { + return; + } + if (memcmp(buf+0, m_id, 2) != 0) { //verify + return; + } + int rcode = response(buf, len); + if (rcode == 0) { + m_state = MYNETDNS_OK; + } else { + m_state = MYNETDNS_NOTFOUND; + } +} + +int DNSClient::response(uint8_t buf[], int size) { + int rcode = buf[3] & 0x0f; + if (rcode != 0) { + return rcode; + } + int qdcount = buf[4]<<8|buf[5]; + int ancount = buf[6]<<8|buf[7]; + int pos = 12; + while(qdcount-- > 0) { + dnsname qname(buf); + pos = qname.decode(pos); // qname + pos += 4; // qtype qclass + } + while(ancount-- > 0) { + dnsname name(buf); + pos = name.decode(pos); // name + int type = buf[pos]<<8|buf[pos+1]; + pos += 8; // type class TTL + int rdlength = buf[pos]<<8|buf[pos+1]; pos += 2; + int rdata_pos = pos; + pos += rdlength; + if (type == 1) { // A record + ip = (buf[rdata_pos]<<24) | (buf[rdata_pos+1]<<16) | (buf[rdata_pos+2]<<8) | buf[rdata_pos+3]; + } +#if DBG_DNS + printf("%s", name.str.c_str()); + if (type == 1) { + printf(" A %d.%d.%d.%d\n", + buf[rdata_pos],buf[rdata_pos+1],buf[rdata_pos+2],buf[rdata_pos+3]); + } else if (type == 5) { + dnsname rdname(buf); + rdname.decode(rdata_pos); + printf(" CNAME %s\n", rdname.str.c_str()); + } else { + printf(" TYPE:%d", type); + printfBytes(" RDATA:", &buf[rdata_pos], rdlength); + } +#endif + } + return rcode; +} + +int DNSClient::query(uint8_t buf[], int size, const char* hostname) { + const uint8_t header[] = { + 0x00,0x00,0x01,0x00, // id=0x0000 QR=0 rd=1 opcode=0 rcode=0 + 0x00,0x01,0x00,0x00, // qdcount=1 ancount=0 + 0x00,0x00,0x00,0x00};// nscount=0 arcount=0 + const uint8_t tail[] = {0x00,0x01,0x00,0x01}; // qtype=A qclass=IN + memcpy(buf, header, sizeof(header)); + int t = rand(); + m_id[0] = t>>8; + m_id[1] = t; + memcpy(buf, m_id, 2); + dnsname qname(buf); + int pos = qname.encode(sizeof(header), (char*)hostname); + memcpy(buf+pos, tail, sizeof(tail)); + pos += sizeof(tail); + return pos; +} + +void DNSClient::resolve(const char* hostname) { + if (m_udp == NULL) { + m_udp = new UDPSocket; + } + m_udp->init(); + m_udp->set_blocking(false); + Endpoint server; + server.set_address("8.8.8.8", 53); // DNS + m_udp->bind(rand()&0x7fff); + uint8_t buf[256]; + int size = query(buf, sizeof(buf), hostname); +#if DBG_DNS + printf("hostname:[%s]\n", hostname); + printHex(buf, size); +#endif + m_udp->sendTo(server, (char*)buf, size); + m_interval.reset(); + m_interval.start(); +} + +void DNSClient::poll() { +#if DBG_DNS + printf("%p m_state: %d, m_udp: %p\n", this, m_state, m_udp); + wait_ms(400); +#endif + switch(m_state) { + case MYNETDNS_START: + m_retry = 0; + resolve(m_hostname); + m_state = MYNETDNS_PROCESSING; + break; + case MYNETDNS_PROCESSING: + break; + case MYNETDNS_NOTFOUND: + break; + case MYNETDNS_ERROR: + break; + case MYNETDNS_OK: + DBG2("m_retry=%d, m_interval=%d\n", m_retry, m_interval.read_ms()); + break; + } + if (m_interval.read_ms() > 1000) { + m_interval.stop(); + DBG2("timeout m_retry=%d\n", m_retry); + if (++m_retry >= 2) { + m_state = MYNETDNS_ERROR; + } else { + resolve(m_hostname); + m_state = MYNETDNS_PROCESSING; + } + } +} + +bool DNSClient::lookup(const char* hostname) { + m_hostname = hostname; + m_state = MYNETDNS_START; + while(1) { + poll(); + callback(); + if (m_state != MYNETDNS_PROCESSING) { + break; + } + } + return m_state == MYNETDNS_OK; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DNSClient/DNSClient.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,35 @@ +// DNSClient.h 2013/4/5 +#pragma once + +#include "UDPSocket.h" + +class DNSClient { +public: + DNSClient(const char* hostname = NULL); + DNSClient(Endpoint* pHost); + virtual ~DNSClient(); + bool lookup(const char* hostname = NULL); + uint32_t ip; +protected: + void poll(); + void callback(); + int response(uint8_t buf[], int size); + int query(uint8_t buf[], int size, const char* hostname); + void resolve(const char* hostname); + uint8_t m_id[2]; + Timer m_interval; + int m_retry; + const char* m_hostname; +private: + enum MyNetDnsState + { + MYNETDNS_START, + MYNETDNS_PROCESSING, //Req has not completed + MYNETDNS_NOTFOUND, + MYNETDNS_ERROR, + MYNETDNS_OK + }; + MyNetDnsState m_state; + UDPSocket *m_udp; +}; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DNSClient/dnsname.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,52 @@ +// dnsname.h 2013/8/27 +#pragma once +//#include <string> +#include "pico_string.h" +class dnsname { +public: + uint8_t *buf; + pico_string str; + dnsname(uint8_t *s) { + buf = s; + } + int decode(int pos) { + while(1) { + int len = buf[pos++]; + if (len == 0x00) { + break; + } + if ((len&0xc0) == 0xc0) { //compress + int offset = (len&0x3f)<<8|buf[pos]; + decode(offset); + return pos+1; + } + if (!str.empty()) { + str.append("."); + } + str.append((const char*)(buf+pos), len); + pos += len; + } + return pos; + } + + int encode(int pos, char* s) { + while(*s) { + char *f = strchr(s, '.'); + if (f == NULL) { + int len = strlen(s); + buf[pos++] = len; + memcpy(buf+pos, s, len); + pos += len; + break; + } + int len = f - s; + buf[pos++] = len; + memcpy(buf+pos, s, len); + s = f+1; + pos += len; + } + buf[pos++] = 0x00; + return pos; + } +}; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GY80/GY80.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,127 @@ +#include "GY80.h" + +Serial pc2(USBTX, USBRX); + +GY80::GY80() : Wire( SDA,SCL) +{ + Wire.frequency(I2C_FREQ); + Accel_Init(); + Gyro_Init(); + Magn_Init(); +} +GY80::~GY80() +{ +} +void GY80::Accel_Init() +{ + byte data[2]; + data[0] = 0x2D; // Power register + data[1] = 0x08; //Measurement mode + Wire.write(ACCEL_ADDRESS, data, 2); + wait_ms(1); + + data[0] = 0x31; // Data format register + data[1] = 0x08; //Set to full resolution + Wire.write(ACCEL_ADDRESS, data, 2); + wait_ms(1); + + // + data[0] = 0x2C; // Rate + data[1] = 0x0D; //Set to 800Hz, normal operation, 0x0A 100hz + Wire.write(ACCEL_ADDRESS, data, 2); + wait_ms(1); +} + +void GY80::Gyro_Init() +{ + byte data[2]; + + data[0] = 0x20; //L3G4200D_CTRL_REG1 + data[1] = 0xCF; // normal power mode, all axes enable, 8:20 9:25 A:50 B:110 + Wire.write(GYRO_ADDRESS, data, 2); + wait_ms(1); + + + data[0] = 0x23; // L3G4200D_CTRL_REG4 + data[1] = 0x20; //2000 dps full scale + Wire.write(GYRO_ADDRESS, data, 2); + wait_ms(1); + + + data[0] = 0x24; // L3G4200D_CTRL_REG5 + data[1] = 0x02; //Low Pass Filter + Wire.write(GYRO_ADDRESS, data, 2); +} + +void GY80::Magn_Init() +{ + byte data[2]; + data[0] = 0x02; + data[1] = 0x00; // 00000000 Set continuous mode (default 10Hz) + Wire.write(MAGN_ADDRESS, data, 2); + wait_ms(1); + + data[0] = 0x00; + data[1] = 0x50; // 01010000 + Wire.write(MAGN_ADDRESS, data, 2); + wait_ms(1); +} + +void GY80::Read_Accel(float* accel_v) +{ + byte buff[6]; + buff[0] = 0x32; // Send address to read from + Wire.write(ACCEL_ADDRESS, buff, 1); + + int accel[3]; + if (Wire.read(ACCEL_ADDRESS, buff,6) == 0) // All bytes received? + { + accel[0] = (short) ((uint16_t) buff[1] << 8 | buff[0]); + accel[1] = (short) ((uint16_t) buff[3] << 8 | buff[2]); + accel[2] = (short) ((uint16_t) buff[5] << 8 | buff[4]); + } + accel_v[0] = (accel[0] - ACCEL_X_OFFSET) * ACCEL_X_SCALE; + accel_v[1] = (accel[1] - ACCEL_Y_OFFSET) * ACCEL_Y_SCALE; + accel_v[2] = (accel[2] - ACCEL_Z_OFFSET) * ACCEL_Z_SCALE; +} + + +void GY80::Read_Gyro(float* gyro_v) +{ + byte buff[6]; + + buff[0] = 0xA8; // 0x28 | (1 << 7) Send address to read from + Wire.write(GYRO_ADDRESS, buff, 1); + // Request 6 bytes + int gyro[3]; + if (Wire.read(GYRO_ADDRESS, buff,6) == 0) // All bytes received? + { + gyro[0] = (short) ((uint16_t) buff[1] << 8 | buff[0]); + gyro[1] = (short) ((uint16_t) buff[3] << 8 | buff[2]); + gyro[2] = (short) ((uint16_t) buff[5] << 8 | buff[4]); + } + gyro_v[0] = DEG2RAD((gyro[0] - GYRO_X_OFFSET) * GYRO_GAIN_X); + gyro_v[1] = DEG2RAD((gyro[1] - GYRO_Y_OFFSET) * GYRO_GAIN_Y); + gyro_v[2] = DEG2RAD((gyro[2] - GYRO_Z_OFFSET) * GYRO_GAIN_Z); +} + +void GY80::Read_Magn(float* magn_v) +{ + byte buff[6]; + + buff[0] = 0x03; // Send address to read from + Wire.write(MAGN_ADDRESS, buff, 1); + + // Request 6 bytes + int mag[3]; + if (Wire.read(MAGN_ADDRESS, buff,6) == 0) // All bytes received? + { + mag[0] = (short) ((uint16_t) buff[1] << 8 | buff[0]); + mag[1] = (short) ((uint16_t) buff[3] << 8 | buff[2]); + mag[2] = (short) ((uint16_t) buff[5] << 8 | buff[4]); + } + magn_v[0] = (mag[0] - MAGN_X_OFFSET) * MAGN_X_SCALE; + magn_v[1] = (mag[1] - MAGN_Y_OFFSET) * MAGN_Y_SCALE; + magn_v[2] = (mag[2] - MAGN_Z_OFFSET) * MAGN_Z_SCALE; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GY80/GY80.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,90 @@ +#ifndef SENSORS_H +#define SENSORS_H + +#include "mbed.h" +#define I2C_FREQ 400000 +#define SCL PTC8 +#define SDA PTC9 + +#define ACCEL_ADDRESS (0xA6) // 0x53 = 0xA6 / 2 +#define MAGN_ADDRESS (0x3C) // 0x1E = 0x3C / 2 +#define GYRO_ADDRESS (0xD2) // 0x68 = 0xD0 / 2 + +// SENSOR CALIBRATION +/*****************************************************************/ +// How to calibrate? Read the tutorial at http://dev.qu.tu-berlin.de/projects/sf-razor-9dof-ahrs +// Put MIN/MAX and OFFSET readings for your board here! +// Accelerometer +// "accel x,y,z (min/max) = X_MIN/X_MAX Y_MIN/Y_MAX Z_MIN/Z_MAX" +#define ACCEL_X_MIN (-255.0f) +#define ACCEL_X_MAX (260.0f) +#define ACCEL_Y_MIN (-267.0f) +#define ACCEL_Y_MAX (255.0f) +#define ACCEL_Z_MIN (-278.0f) +#define ACCEL_Z_MAX (238.0f) + +// Magnetometer +// "magn x,y,z (min/max) = X_MIN/X_MAX Y_MIN/Y_MAX Z_MIN/Z_MAX" +#define MAGN_X_MIN (-520.0f) +#define MAGN_X_MAX (850.0f) +#define MAGN_Y_MIN (-640.0f) +#define MAGN_Y_MAX (684.0f) +#define MAGN_Z_MIN (-213.0f) +#define MAGN_Z_MAX (850.0f) + +// Gyroscope +// "gyro x,y,z (current/average) = .../OFFSET_X .../OFFSET_Y .../OFFSET_Z +#define GYRO_X_OFFSET (-20.5f) +#define GYRO_Y_OFFSET (0.5f) +#define GYRO_Z_OFFSET (10.5f) + +//*****************************************************************************/ + +#define GRAVITY 255.0f //this equivalent to 1G in the raw data coming from the accelerometer + +#define ACCEL_X_OFFSET ((ACCEL_X_MIN + ACCEL_X_MAX) / 2.0f) +#define ACCEL_Y_OFFSET ((ACCEL_Y_MIN + ACCEL_Y_MAX) / 2.0f) +#define ACCEL_Z_OFFSET ((ACCEL_Z_MIN + ACCEL_Z_MAX) / 2.0f) +#define ACCEL_X_SCALE (GRAVITY / (ACCEL_X_MAX - ACCEL_X_OFFSET)) +#define ACCEL_Y_SCALE (GRAVITY / (ACCEL_Y_MAX - ACCEL_Y_OFFSET)) +#define ACCEL_Z_SCALE (GRAVITY / (ACCEL_Z_MAX - ACCEL_Z_OFFSET)) + +#define MAGN_X_OFFSET ((MAGN_X_MIN + MAGN_X_MAX) / 2.0f) +#define MAGN_Y_OFFSET ((MAGN_Y_MIN + MAGN_Y_MAX) / 2.0f) +#define MAGN_Z_OFFSET ((MAGN_Z_MIN + MAGN_Z_MAX) / 2.0f) +#define MAGN_X_SCALE (100.0f / (MAGN_X_MAX - MAGN_X_OFFSET)) +#define MAGN_Y_SCALE (100.0f / (MAGN_Y_MAX - MAGN_Y_OFFSET)) +#define MAGN_Z_SCALE (100.0f / (MAGN_Z_MAX - MAGN_Z_OFFSET)) + +// Gyro gain (conversion from raw to degree per seconds) +#define GYRO_GAIN 0.061035156f +#define GYRO_GAIN_X 0.061035156f //X axis Gyro gain +#define GYRO_GAIN_Y 0.061035156f //Y axis Gyro gain +#define GYRO_GAIN_Z 0.061035156f //Z axis Gyro gain + +#define DEG2RAD(x) (x * 0.01745329252) // *pi/180 +#define RAD2DEG(x) (x * 57.2957795131) // *180/pi + +typedef char byte; + +class GY80 +{ +public: + I2C Wire; + GY80(); + ~GY80(); + void Read_Accel(float* accel_v); + void Read_Gyro(float* ); + void Read_Magn(float* ); + +private: + short accel[3]; + short gyro[3]; + short mag[3]; + void Accel_Init(); + void Gyro_Init(); + void Magn_Init(); + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Endpoint.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,53 @@ +/* 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 "Socket.h" +#include "Endpoint.h" + +Endpoint::Endpoint() { + reset_address(); +} +Endpoint::~Endpoint() {} + +void Endpoint::reset_address(void) { + _ipAddress[0] = '\0'; + _port = 0; +} + +int Endpoint::set_address(const char* host, const int port) { + //Resolve DNS address or populate hard-coded IP address + WIZnet_Chip* eth = WIZnet_Chip::getInstance(); + if (eth == NULL) { + return -1; + } + uint32_t addr; + if (!eth->gethostbyname(host, &addr)) { + return -1; + } + snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); + _port = port; + return 0; +} + +char* Endpoint::get_address() { + return _ipAddress; +} + +int Endpoint::get_port() { + return _port; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Endpoint.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,65 @@ +/* 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. + */ +#ifndef ENDPOINT_H +#define ENDPOINT_H + +#include "wiznet.h" + +class UDPSocket; + +/** +IP Endpoint (address, port) +*/ +class Endpoint { + friend class UDPSocket; + +public: + /** IP Endpoint (address, port) + */ + Endpoint(void); + + ~Endpoint(void); + + /** Reset the address of this endpoint + */ + void reset_address(void); + + /** Set the address of this endpoint + \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS). + \param port The endpoint port + \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). + */ + int set_address(const char* host, const int port); + + /** Get the IP address of this endpoint + \return The IP address of this endpoint. + */ + char* get_address(void); + + /** Get the port of this endpoint + \return The port of this endpoint + */ + int get_port(void); + +protected: + char _ipAddress[16]; + int _port; +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Socket.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,40 @@ +/* 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 "Socket.h" + +Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500) { + eth = WIZnet_Chip::getInstance(); + if (eth == NULL) { + error("Socket constructor error: no W5500 instance available!\r\n"); + } +} + +void Socket::set_blocking(bool blocking, unsigned int timeout) { + _blocking = blocking; + _timeout = timeout; +} + +int Socket::close() { + return (eth->close(_sock_fd)) ? 0 : -1; +} + +Socket::~Socket() { + close(); //Don't want to leak +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Socket.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,59 @@ +/* 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. + */ +#ifndef SOCKET_H_ +#define SOCKET_H_ + +#include "wiznet.h" + +#define htons(x) __REV16(x) +#define ntohs(x) __REV16(x) +#define htonl(x) __REV(x) +#define ntohl(x) __REV(x) + +/** Socket file descriptor and select wrapper + */ +class Socket { +public: + /** Socket + */ + Socket(); + + /** Set blocking or non-blocking mode of the socket and a timeout on + blocking socket operations + \param blocking true for blocking mode, false for non-blocking mode. + \param timeout timeout in ms [Default: (1500)ms]. + */ + void set_blocking(bool blocking, unsigned int timeout=1500); + + /** Close the socket file descriptor + */ + int close(); + + ~Socket(); + +protected: + int _sock_fd; + bool _blocking; + int _timeout; + + WIZnet_Chip* eth; +}; + + +#endif /* SOCKET_H_ */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,113 @@ +/* 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 "TCPSocketConnection.h" + +TCPSocketConnection::TCPSocketConnection() +{ +} + +int TCPSocketConnection::connect(const char* host, const int port) +{ + if (_sock_fd < 0) { + _sock_fd = eth->new_socket(); + if (_sock_fd < 0) { + return -1; + } + } + if (set_address(host, port) != 0) { + return -1; + } + if (!eth->connect(_sock_fd, get_address(), port)) { + return -1; + } + return 0; +} + +bool TCPSocketConnection::is_connected(void) +{ + return eth->is_connected(_sock_fd); +} + +int TCPSocketConnection::send(char* data, int length) +{ + int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); + if (size < 0) { + return -1; + } + if (size > length) { + size = length; + } + return eth->send(_sock_fd, data, size); +} + +// -1 if unsuccessful, else number of bytes written +int TCPSocketConnection::send_all(char* data, int length) +{ + int writtenLen = 0; + while (writtenLen < length) { + int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); + if (size < 0) { + return -1; + } + if (size > (length-writtenLen)) { + size = (length-writtenLen); + } + int ret = eth->send(_sock_fd, data + writtenLen, size); + if (ret < 0) { + return -1; + } + writtenLen += ret; + } + return writtenLen; +} + +// -1 if unsuccessful, else number of bytes received +int TCPSocketConnection::receive(char* data, int length) +{ + int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); + if (size < 0) { + return -1; + } + if (size > length) { + size = length; + } + return eth->recv(_sock_fd, data, size); +} + +// -1 if unsuccessful, else number of bytes received +int TCPSocketConnection::receive_all(char* data, int length) +{ + int readLen = 0; + while (readLen < length) { + int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout); + if (size <= 0) { + break; + } + if (size > (length - readLen)) { + size = length - readLen; + } + int ret = eth->recv(_sock_fd, data + readLen, size); + if (ret < 0) { + return -1; + } + readLen += ret; + } + return readLen; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,78 @@ +/* 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. + */ + +#ifndef TCPSOCKET_H +#define TCPSOCKET_H + +#include "Socket.h" +#include "Endpoint.h" + +/** +TCP socket connection +*/ +class TCPSocketConnection: public Socket, public Endpoint { + friend class TCPSocketServer; + +public: + /** TCP socket connection + */ + TCPSocketConnection(); + + /** Connects this TCP socket to the server + \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. + \param port The host's port to connect to. + \return 0 on success, -1 on failure. + */ + int connect(const char* host, const int port); + + /** Check if the socket is connected + \return true if connected, false otherwise. + */ + bool is_connected(void); + + /** Send data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send(char* data, int length); + + /** Send all the data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send_all(char* data, int length); + + /** Receive data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive(char* data, int length); + + /** Receive all the data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive_all(char* data, int length); +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketServer.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,94 @@ +/* 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 "TCPSocketServer.h" + +TCPSocketServer::TCPSocketServer() {} + +// Server initialization +int TCPSocketServer::bind(int port) +{ + + if (_sock_fd < 0) { + _sock_fd = eth->new_socket(); + if (_sock_fd < 0) { + return -1; + } + } + // set the listen_port for next connection. + listen_port = port; + // set TCP protocol + eth->setProtocol(_sock_fd, TCP); + // set local port + eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); + // connect the network + eth->scmd(_sock_fd, OPEN); + return 0; +} + +int TCPSocketServer::listen(int backlog) +{ + if (_sock_fd < 0) { + return -1; + } + if (backlog != 1) { + return -1; + } + eth->scmd(_sock_fd, LISTEN); + return 0; +} + + +int TCPSocketServer::accept(TCPSocketConnection& connection) +{ + if (_sock_fd < 0) { + return -1; + } + Timer t; + t.reset(); + t.start(); + while(1) { + if (t.read_ms() > _timeout && _blocking == false) { + return -1; + } + if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) { + break; + } + } + uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR); + char host[16]; + snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); + uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT); + // change this server socket to connection socket. + connection._sock_fd = _sock_fd; + connection.set_address(host, port); + + // and then, for the next connection, server socket should be assigned new one. + _sock_fd = -1; // want to assign new available _sock_fd. + if(bind(listen_port) < 0) { + error("No more socket for listening"); + } else { + //return -1; + if(listen(1) < 0) { + error("No more socket for listening"); + } + } + + return 0; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketServer.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,57 @@ +/* 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. + */ +#ifndef TCPSOCKETSERVER_H +#define TCPSOCKETSERVER_H + +#include "Socket.h" +#include "TCPSocketConnection.h" + +/** TCP Server. + */ +class TCPSocketServer : public Socket +{ +public: + /** Instantiate a TCP Server. + */ + TCPSocketServer(); + + /** Bind a socket to a specific port. + \param port The port to listen for incoming connections on. + \return 0 on success, -1 on failure. + */ + int bind(int port); + + /** Start listening for incoming connections. + \param backlog number of pending connections that can be queued up at any + one time [Default: 1]. + \return 0 on success, -1 on failure. + */ + int listen(int backlog=1); + + /** Accept a new connection. + \param connection A TCPSocketConnection instance that will handle the incoming connection. + \return 0 on success, -1 on failure. + */ + int accept(TCPSocketConnection& connection); + +private : + int listen_port; +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/UDPSocket.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,105 @@ +/* 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 "UDPSocket.h" + +static int udp_local_port; + +UDPSocket::UDPSocket() +{ +} + +// After init function, bind() should be called. +int UDPSocket::init(void) +{ + if (_sock_fd < 0) { + _sock_fd = eth->new_socket(); + } + if (eth->setProtocol(_sock_fd, UDP) == false) return -1; + return 0; +} + +// Server initialization +int UDPSocket::bind(int port) +{ + if (_sock_fd < 0) { + _sock_fd = eth->new_socket(); + if (_sock_fd < 0) { + return -1; + } + } + // set local port + if (port != 0) { + eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); + } else { + udp_local_port++; + eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port); + } + // set udp protocol + eth->setProtocol(_sock_fd, UDP); + eth->scmd(_sock_fd, OPEN); + return 0; +} + +// -1 if unsuccessful, else number of bytes written +int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) +{ + int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1); + if (size < 0) { + return -1; + } + confEndpoint(remote); + int ret = eth->send(_sock_fd, packet, length); + return ret; +} + +// -1 if unsuccessful, else number of bytes received +int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) +{ + uint8_t info[8]; + int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info)); + if (size < 0) { + return -1; + } + eth->recv(_sock_fd, (char*)info, sizeof(info)); + readEndpoint(remote, info); + int udp_size = info[6]<<8|info[7]; + //TEST_ASSERT(udp_size <= (size-sizeof(info))); + if (udp_size > (size-sizeof(info))) { + return -1; + } + return eth->recv(_sock_fd, buffer, udp_size); +} + +void UDPSocket::confEndpoint(Endpoint & ep) +{ + char * host = ep.get_address(); + // set remote host + eth->sreg_ip(_sock_fd, Sn_DIPR, host); + // set remote port + eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port()); +} + +void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[]) +{ + char addr[17]; + snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]); + uint16_t port = info[4]<<8|info[5]; + ep.set_address(addr, port); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/UDPSocket.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,69 @@ +/* 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. + */ + +#ifndef UDPSOCKET_H +#define UDPSOCKET_H + +#include "Endpoint.h" +#include "Socket.h" + +/** +UDP Socket +*/ +class UDPSocket: public Socket { + +public: + /** Instantiate an UDP Socket. + */ + UDPSocket(); + + /** Init the UDP Client Socket without binding it to any specific port + \return 0 on success, -1 on failure. + */ + int init(void); + + /** Bind a UDP Server Socket to a specific port + \param port The port to listen for incoming connections on + \return 0 on success, -1 on failure. + */ + int bind(int port = -1); + + /** Send a packet to a remote endpoint + \param remote The remote endpoint + \param packet The packet to be sent + \param length The length of the packet to be sent + \return the number of written bytes on success (>=0) or -1 on failure + */ + int sendTo(Endpoint &remote, char *packet, int length); + + /** Receive a packet from a remote endpoint + \param remote The remote endpoint + \param buffer The buffer for storing the incoming packet data. If a packet + is too long to fit in the supplied buffer, excess bytes are discarded + \param length The length of the buffer + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receiveFrom(Endpoint &remote, char *buffer, int length); + +private: + void confEndpoint(Endpoint & ep); + void readEndpoint(Endpoint & ep, uint8_t info[]); +}; + +#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet/W5100.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,383 @@ +/* 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, 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) +{ + 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; + } + + 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); + 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet/W5100.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,262 @@ +/* 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_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); + + /* + * 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);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet/W5200.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,374 @@ +/* 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::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); + 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); + 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet/W5200.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,264 @@ +/* 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); + + /* + * 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);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet/W5500.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,421 @@ +/* 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) +{ + /* + if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) { + return true; + } + */ + 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; +} + +// 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet/W5500.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,273 @@ +/* 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); + + /* + * 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);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnet/wiznet.h Mon Aug 25 12:00:15 2014 +0000 @@ -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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnetInterface.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,123 @@ +/* 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 "WIZnetInterface.h" +#include "DHCPClient.h" + +WIZnetInterface::WIZnetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) : + WIZnet_Chip(mosi, miso, sclk, cs, reset) +{ + ip_set = false; +} + +WIZnetInterface::WIZnetInterface(SPI* spi, PinName cs, PinName reset) : + WIZnet_Chip(spi, cs, reset) +{ + ip_set = false; +} + +int WIZnetInterface::init(uint8_t * mac) +{ + dhcp = true; + // + for (int i =0; i < 6; i++) this->mac[i] = mac[i]; + // + reset(); + return 0; +} + +int WIZnetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway) +{ + dhcp = false; + // + for (int i =0; i < 6; i++) this->mac[i] = mac[i]; + // + this->ip = str_to_ip(ip); + strcpy(ip_string, ip); + ip_set = true; + this->netmask = str_to_ip(mask); + this->gateway = str_to_ip(gateway); + reset(); + return 0; +} + +// Connect Bring the interface up, start DHCP if needed. +int WIZnetInterface::connect() +{ + if (dhcp) { + int r = IPrenew(); + if (r < 0) { + return r; + } + } + + if (WIZnet_Chip::setip() == false) return -1; + return 0; +} + +// Disconnect Bring the interface down. +int WIZnetInterface::disconnect() +{ + if (WIZnet_Chip::disconnect() == false) return -1; + return 0; +} + +char* WIZnetInterface::getIPAddress() +{ + uint32_t ip = reg_rd<uint32_t>(SIPR); + snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); + return ip_string; +} + +char* WIZnetInterface::getNetworkMask() +{ + uint32_t ip = reg_rd<uint32_t>(SUBR); + snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); + return mask_string; +} + +char* WIZnetInterface::getGateway() +{ + uint32_t ip = reg_rd<uint32_t>(GAR); + snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); + return gw_string; +} + +char* WIZnetInterface::getMACAddress() +{ + uint8_t mac[6]; + reg_rd_mac(SHAR, mac); + snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return mac_string; + +} + +int WIZnetInterface::IPrenew(int timeout_ms) +{ + DHCPClient dhcp; + int err = dhcp.setup(timeout_ms); + if (err == (-1)) { + return -1; + } +// printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); + ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3]; + gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3]; + netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3]; + dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3]; + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnetInterface.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,88 @@ +/* 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 "wiznet.h" + + /** Interface using Wiznet chip to connect to an IP-based network + * + */ +class WIZnetInterface: public WIZnet_Chip { +public: + + /** + * Constructor + * + * \param mosi mbed pin to use for SPI + * \param miso mbed pin to use for SPI + * \param sclk mbed pin to use for SPI + * \param cs chip select of the WIZnet_Chip + * \param reset reset pin of the WIZnet_Chip + */ + WIZnetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); + WIZnetInterface(SPI* spi, PinName cs, PinName reset); + + /** Initialize the interface with DHCP. + * Initialize the interface and configure it to use DHCP (no connection at this point). + * \return 0 on success, a negative number on failure + */ + int init(uint8_t * mac); //With DHCP + + /** Initialize the interface with a static IP address. + * Initialize the interface and configure it with the following static configuration (no connection at this point). + * \param ip the IP address to use + * \param mask the IP address mask + * \param gateway the gateway to use + * \return 0 on success, a negative number on failure + */ + int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway); + + /** Connect + * Bring the interface up, start DHCP if needed. + * \return 0 on success, a negative number on failure + */ + int connect(); + + /** Disconnect + * Bring the interface down + * \return 0 on success, a negative number on failure + */ + int disconnect(); + + /** Get IP address & MAC address + * + * @ returns ip address + */ + char* getIPAddress(); + char* getNetworkMask(); + char* getGateway(); + char* getMACAddress(); + + int IPrenew(int timeout_ms = 15*1000); + +private: + char ip_string[20]; + char mask_string[20]; + char gw_string[20]; + char mac_string[20]; + bool ip_set; +}; + +#include "TCPSocketConnection.h" +#include "TCPSocketServer.h" +#include "UDPSocket.h"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,287 @@ + +#include "mbed.h" +#include "WIZnetInterface.h" +#include "GY80.h" +#include "tsi_sensor.h" +#include "main.h" + + +int main() /// Main Function: +{ + + + pc.attach(&SerialInterHandler, Serial::RxIrq); /// - configures serial port interrupts & choose handler + menu_prop(); /// - shows 5 sec menu + pc.attach(NULL, Serial::RxIrq); /// - disables serial port interrupts after 5s + tick1.attach_us(&read_data, 10000); /// - reads data from sensors in 10ms intervals + if(init_eth() == 1) { /// - initializes ethernet communication + wait(0.001); // wait + eth_comm(); /// - and tcp/ip server if no error occured + } else { // + return -1; /// - or returns an error + } /// + while(1); // infinit loop +} + +int init_eth() +{ + /** + * Initializes ethernet communication by configuring WIZ550io via SPI interface. + * DHCP or Static IP configuration could be used. + * Use of them is user configurable via serial port. + */ + int ret; // error code variable + + // MAC Adress setting: + mac[0] = 0x00; + mac[1] = 0x08; + mac[2] = 0xDC; + mac[3] = 0x1D; + mac[4] = 0x2E; + mac[5] = 0x74; + + pc.printf("\r \n Ethernet Initialization \n"); + + if (USE_DHCP){ + ret = eth.init(mac); // Use DHCP + } + else{ + ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static configuration + } + if (!ret) { // if error don't happen + pc.printf("\r \n Initialized, MAC: %s\n", eth.getMACAddress()); + } else { // else show error code + pc.printf("\r \n Error eth.init() - ret = %d\n", ret); + return -1; + } + + ret = eth.connect(); + if (!ret) { + pc.printf("\r \nIP: %s, MASK: %s, GW: %s\n", + eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); + } else { + pc.printf("\r \n Error eth.connect() - ret = %d\n", ret); + return -1; + } + return 1; + +} + +void eth_comm() +{ + /** + * Creates server and waits for client to connect. + * Sends reveiced buffer with commands to comm_handler. + * And sends reply created by comm_handler to client. + */ + TCPSocketServer server; // TCP/IP server object + server.bind(ECHO_SERVER_PORT); // binding listen port + server.listen(); // start listening + + while (true) { // infinit loop + pc.printf("\n \r Wait for new connection...\n"); // pc info + TCPSocketConnection client; // TCP/IP client object + server.accept(client); // accept connection + client.set_blocking(false, 1500); // no blocking connections, timeout after (1.5)s + pc.printf("\n \r Connection from: %s\n", client.get_address()); // pc info + char buffer[256]; // Data buffer + while (true) { // loop + for(int i =0; i < 256; i++) buffer[i] = '\0'; // clear buffer + int n = client.receive(buffer, sizeof(buffer)); // receive data & save it to buffer + if (n <= 0) break; // break if buffer is empty or error occured + comm_handler(buffer); // send received command to communication handler + n = strlen(buffer); // check buffer length + pc.printf("\n \r BUFF: %s, strlen: %d, size: %d, n: %d", buffer, strlen(buffer), sizeof(buffer), n); // pc info, just for debug + client.send(buffer, n); // send reply + for(int i =0; i < 256; i++) buffer[i] = '\0'; // clear buffer + if (n <= 0) break; // if error occured send break + } // + client.close(); // close connection + } // +} + +void read_data() +{ + /** + * Reads sensor data in 10ms intervals. + * And saves them. + */ + sensor.Read_Magn(magn); // read magnitude + sensor.Read_Accel(accel); // read acceleration + sensor.Read_Gyro(gyro); // read gyroscope + TSI_pos = tsi.readPercentage(); // read percentage position of touch sensor +} + +void comm_handler(char* command) +{ + /** This function gets data buffer with command + * and prepares answer to be sent. + */ + + pc.printf("\n \r %s \n \r", command); // send received command to pc (DEBUG) + if(strcmp(command,"read_sensor")==0) { // data read from sensor + sprintf(command, "Sensor data: %10f,%10f,%10f,%10f,%10f,%10f,%10f,%10f,%10f, %10f", magn[0], magn[1], magn[2], accel[0], accel[1], accel[2], gyro[0], gyro[1], gyro[2], TSI_pos); // enter data to buffer + wait(0.001); // wait + pc.printf("\n \r sending sensor data!"); // pc info (debug) + } else if (strcmp(command,"check_ports")==0) { // check I/O ports current values + sprintf(command, "Ports values: P1: %d, P2: %d, P3: %d, P4: %d, P5: %d, P6: %d", P1.read(), P2.read(), P3.read(), P4.read(), P5.read(), P6.read()); + } else if (strcmp(command,"get_P1")==0) { // get P1 value + sprintf(command, "P1 value: %d", P1.read()); // + } else if(strcmp(command,"set_P1_o")==0) { // set P1 as output + P1.output(); // + sprintf(command, "P1 is now set as output"); // + } else if (strcmp(command,"set_P1_i")==0) { // set P1 as input + P1.input(); // + sprintf(command, "P1 is now set as input"); // + } else if (strcmp(command,"set_P1_1")==0) { // set P1 as 1 + P1.output(); // set to output + P1.write(1); // write 1 to P1 + sprintf(command, "P1 value is now: %d ", P1.read()); // reply with current value to check + } else if (strcmp(command,"set_P1_0")==0) { // set P1 as 0 + P1.output(); // + P1.write(0); + sprintf(command, "P1 value is now: %d ", P1.read()); + } else if (strcmp(command,"get_P2")==0) { + sprintf(command, "P2 value: %d", P2.read()); + } else if (strcmp(command,"set_P2_o")==0) { + P2.output(); + sprintf(command, "P2 is now set as output"); + } else if (strcmp(command,"set_P2_i")==0) { + P2.input(); + sprintf (command, "P2 is now set as input"); + } else if (strcmp(command,"set_P2_1")==0) { + P2.output(); + P2.write(1); + sprintf(command, "P2 value is now: %d ", P2.read()); + } else if (strcmp(command,"set_P2_0")==0) { + P2.output(); + P2.write(0); + sprintf(command, "P2 value is now: %d ", P2.read()); + } else if (strcmp(command,"get_P3")==0) { + sprintf(command, "P3 value: %d", P3.read()); + } else if (strcmp(command,"set_P3_o")==0) { + P3.output(); + sprintf(command, "P3 is now set as output"); + } else if (strcmp(command,"set_P3_i")==0) { + P3.input(); + sprintf (command, "P3 is now set as input"); + } else if (strcmp(command,"set_P3_1")==0) { + P3.output(); + P3.write(1); + sprintf(command, "P3 value is now: %d ", P3.read()); + } else if (strcmp(command,"set_P3_0")==0) { + P3.output(); + P3.write(0); + sprintf(command, "P3 value is now: %d ", P3.read()); + } else if (strcmp(command,"get_P4")==0) { + sprintf(command, "P4 value: %d", P4.read()); + } else if (strcmp(command,"set_P4_o")==0) { + P4.output(); + sprintf(command, "P4 is now set as output"); + } else if (strcmp(command,"set_P4_i")==0) { + P4.input(); + sprintf (command, "P4 is now set as input"); + } else if (strcmp(command,"set_P4_1")==0) { + P4.output(); + P4.write(1); + sprintf(command, "P4 value is now: %d ", P4.read()); + } else if (strcmp(command,"set_P4_0")==0) { + P4.output(); + P4.write(0); + sprintf(command, "P4 value is now: %d ", P4.read()); + } else if (strcmp(command,"get_P5")==0) { + sprintf(command, "P5 value: %d", P5.read()); + } else if (strcmp(command,"set_P5_o")==0) { + P5.output(); + sprintf(command, "P5 is now set as output"); + } else if (strcmp(command,"set_P5_i")==0) { + P5.input(); + sprintf (command, "P5 is now set as input"); + } else if (strcmp(command,"set_P5_1")==0) { + P5.output(); + P5.write(1); + sprintf(command, "P5 value is now: %d ", P5.read()); + } else if (strcmp(command,"set_P5_0")==0) { + P5.output(); + P5.write(0); + sprintf(command, "P5 value is now: %d ", P5.read()); + } else if (strcmp(command,"get_P6")==0) { + sprintf(command, "P6 value: %d", P6.read()); + } else if (strcmp(command,"set_P6_o")==0) { + P6.output(); + sprintf(command, "P6 is now set as output"); + } else if (strcmp(command,"set_P6_i")==0) { + P6.input(); + sprintf (command, "P6 is now set as input"); + } else if (strcmp(command,"set_P6_1")==0) { + P6.output(); + P6.write(1); + sprintf(command, "P6 value is now: %d ", P6.read()); + } else if (strcmp(command,"set_P6_0")==0) { + P6.output(); + P6.write(0); + sprintf(command, "P6 value is now: %d ", P6.read()); + } else sprintf(command, "Unknown Command!"); +} + +void SerialInterHandler() +{ + /** + Enables to configure Ethernet card via serial port in first 5 seconds from KL25Z startup. + */ + pc.getc(); // gets 1 char from buffer and clears it + pc.printf("\n \rUse DHCP? \n \ry-yes "); + char from_serial; + while (true) { + if (pc.readable()) { + from_serial= pc.getc(); + break; + } + } + pc.printf("\n \r%c", from_serial); + if (from_serial=='y') { + USE_DHCP = true; + pc.printf("\n \rUsing DHCP "); + return; + } + else if (from_serial=='n') { + USE_DHCP = false; + pc.printf("\n \rStatic IP configuration "); + pc.printf("\n \rCurrent config:"); + pc.printf("\r \nIP: %s, MASK: %s, GW: %s\n", IP_Addr, IP_Subnet, IP_Gateway); + pc.printf("\n \rDo you want to change it?"); + if(pc.getc()=='y'){ + pc.printf("\n \rIP: \n \r Press 'enter and then backspace' to end and save \r \n Remember to Enter all 15 characters!\n \r"); + pc.scanf("%s" ,IP_Addr); + pc.printf("new IP: %s", IP_Addr); + pc.printf("\n \r Enter Subnet Mask:"); + pc.scanf("%s", IP_Subnet); + pc.printf("\n \r new Mask: %s", IP_Subnet); + pc.printf("\n \r Enter Gateway:"); + pc.scanf("%s", IP_Gateway); + pc.printf("\r \n IP: %s, MASK: %s, GW: %s\n", IP_Addr, IP_Subnet, IP_Gateway); + } + else return; + return; + } + else pc.printf("\n \rWrong key! Nothing to do"); +} + +void menu_prop() +{ + /** + Counting down time left to enter menu. + */ + pc.printf("\n \r Press any key to enter menu \n \r 5"); + wait(1); + pc.printf("\n \r 4"); + wait(1); + pc.printf("\n \r 3"); + wait(1); + pc.printf("\n \r 2"); + wait(1); + pc.printf("\n \r 1"); + wait(1); + pc.printf("\n \r 0 \n \r Starting!"); + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,133 @@ +/** @file main.cpp */ +/** @file main.h +* +* \brief +* Program main features: +* - enables communication via ethernet +* - reads data from GY-80 sensor +* - reads data from built-in touch sensor +* - enables configuration of Wiz550io ethernet card via PC serial port +* +* +* Peripherals connection details: +* +* WIZ550io pins are connected to: +* - MOSI -> PTD2 +* - MISO -> PTD3 +* - SCLK -> PTD1 +* - CS -> PTD0 +* - RST -> PTA20 +* - VCC -> +3.3V +* - GND -> GND +* +* IMU GY-80 pins: +* - VCC -> +5V +* - GND -> GND +* - SDA -> PTC9 +* - SCL -> PTC8 +* - Connection to other I2C port may not work!!! +* +* Serial port communication is available via virtual OpenSDA COM port. +* Detailed description about configuring OpenSDA available here: https://mbed.org/handbook/Windows-serial-configuration . +* +* Digital I/O ports pins and names: +* - PTE20 -> P1 +* - PTE21 -> P2 +* - PTE22 -> P3 +* - PTE23 -> P4 +* - PTE29 -> P5 +* - PTE30 -> P6 +* +* Ethernet commands: +* Available commands: +* - "read_sensor" - reads all sensor data - Reply form: "Sensor data: magnX, magnY, magnZ, accelX, accelY, accelZ, gyroX, gyroY, gyroZ, TSIposition" +* - "check_ports" - reads ports P1 to P6 status - Reply form: "Port values: P1: %d, P2: %d, P3: %d, P4: %d, P5: %d, P6: %d" +* Single port commands: +* - "set_P$_i" - sets port as input - Reply: "P1 is now set as input" +* - "set_P$_o" - sets port as output - Reply: "P1 is now set as output" +* - "set_P$_1" - sets port as 1 (High) - Reply: "P1 value is now: %d " +* - "set_P$_0" - sets port as 0 (low) - Reply: "P1 value is now: %d " +* - "get_P$" - gets port value - Reply: "P1 value: %d" +* - $ mark is port number from 1-6 +* +* @author Mateusz Jaskula +* +*/ + + + +// Set server communication port to 22 +#define ECHO_SERVER_PORT 22 + + +/// SPI communication with WIZ550io initialization +SPI spi(PTD2, PTD3, PTD1); /// mosi, miso, sclk + +/// Ethernet communication via WIZ550io +WIZnetInterface eth(&spi, PTD0, PTA20); /// spi, cs, reset + +/// Serial communication init +Serial pc(USBTX,USBRX); + +/// TSI Electrodes definition: +#define ELEC0 9 +/// TSI Electrodes definition: +#define ELEC1 10 + + +/// port P1 is connected to PTE20 pin +DigitalInOut P1(PTE20); +/// port P1 is connected to PTE21 pin +DigitalInOut P2(PTE21); +/// port P2 is connected to PTE22 pin +DigitalInOut P3(PTE22); +/// port P3 is connected to PTE23 pin +DigitalInOut P4(PTE23); +/// port P4 is connected to PTE29 pin +DigitalInOut P5(PTE29); +/// port P5 is connected to PTE30 pin +DigitalInOut P6(PTE30); + + +/// Defines use of DHCP protocol or Static IP Configuration +bool USE_DHCP = true; + +// Static IP setting. Using MAC stored in WIZ550io + char IP_Addr[16] = "192.168.133.015"; /// IP + char IP_Subnet[16] = "255.255.255.000"; /// Subnet Mask + char IP_Gateway[16] = "192.168.133.001"; /// Gateway + +/// MAC Adress global variable +uint8_t mac[6]; + +/// GY80 IMU sensor Object +GY80 sensor; + +// GY80 Data is being hold here +float magn[3]; /// Current magnitude +float accel[3]; /// Current acceleration +float gyro[3]; /// current gyroscope + +/// TSI Slider object: +TSIAnalogSlider tsi(ELEC0, ELEC1, 100); + +/// TSI Slider position: +float TSI_pos; + +/// Ticker generates time based interrups to read data from sensors in 10ms intervals. +Ticker tick1; + + +//Functions declaration: +// Initializes eth communication with MAC, IP, Subnet Mask asd Gateway +int init_eth(); +// Handles ethernet communication +void eth_comm(); +// Reads data from sensor with 1ms freq +void read_data(); +// Reads commands and takes action +void comm_handler(char* command); +// Enables configuration mode +void SerialInterHandler(); +// 5 sec serial menu with info how to enter configuration mode +void menu_prop();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pico_string.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,46 @@ +// pico_string.h 2013/8/27 +#pragma once +class pico_string { +public: + pico_string(){ + _len = 0; + _buf = (char*)malloc(1); + if (_buf) { + _buf[0] = '\0'; + } + } + ~pico_string() { + if (_buf) { + free(_buf); + } + } + bool empty() { + return _len == 0; + } + void append(const char* s, int len) { + if (_buf == NULL) { + return; + } + char* p = (char*)malloc(_len+len+1); + if (p == NULL) { + return; + } + memcpy(p, _buf, _len); + memcpy(p+_len, s, len); + p[_len+len] = '\0'; + free(_buf); + _buf = p; + } + void append(const char* s) { + append(s, strlen(s)); + } + char* c_str() { + if (_buf) { + return _buf; + } + return ""; + } +private: + char* _buf; + int _len; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tsi_sensor/tsi_sensor.cpp Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,148 @@ +/* Freescale Semiconductor Inc. + * mbed Microcontroller Library + * (c) Copyright 2014 ARM Limited. + * + * 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 "tsi_sensor.h" + +void tsi_irq(void); +TSIAnalogSlider *TSIAnalogSlider::_instance; + +TSIAnalogSlider::TSIAnalogSlider(uint32_t elec0, uint32_t elec1, + uint32_t range) +: _elec0(elec0), _elec1(elec1), _range(range) { + _instance = this; + _current_elec = &_elec0; + SIM->SCGC5 |= SIM_SCGC5_TSI_MASK; + + TSI0->GENCS |= (TSI_GENCS_ESOR_MASK | TSI_GENCS_MODE(0) | TSI_GENCS_REFCHRG(4) + | TSI_GENCS_DVOLT(0) | TSI_GENCS_EXTCHRG(7) | TSI_GENCS_PS(4) + | TSI_GENCS_NSCN(11) | TSI_GENCS_TSIIEN_MASK | TSI_GENCS_STPE_MASK); + TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; + + NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq); + NVIC_EnableIRQ(TSI0_IRQn); + + selfCalibration(); +} + +static void initBaseline(TSIElectrode& elec) +{ + uint32_t channel0 = elec.getChannel(); + TSI0->DATA = ((channel0 << TSI_DATA_TSICH_SHIFT) ); + TSI0->DATA |= TSI_DATA_SWTS_MASK; + while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK)); + TSI0->GENCS |= TSI_GENCS_EOSF_MASK; + elec.setBaseline(TSI0->DATA & TSI_DATA_TSICNT_MASK); +} + +void TSIAnalogSlider::selfCalibration(void) +{ + TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag + TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module + + uint32_t trigger_backup; + if(TSI0->GENCS & TSI_GENCS_STM_MASK) { // Back-up TSI Trigger mode from Application + trigger_backup = 1; + } else { + trigger_backup = 0; + } + + TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger + TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts + TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module + + initBaseline(_elec0); + initBaseline(_elec1); + + TSI0->DATA = ((_elec0.getChannel() << TSI_DATA_TSICH_SHIFT)); + + TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module + TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt + if (trigger_backup) { // Restore trigger mode + TSI0->GENCS |= TSI_GENCS_STM_MASK; + } else { + TSI0->GENCS &= ~TSI_GENCS_STM_MASK; + } + + TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module + TSI0->DATA |= TSI_DATA_SWTS_MASK; +} + + +void TSIAnalogSlider::sliderRead(void ) { + if (_scan_in_progress) { + _scan_in_progress = 0; + uint32_t delta0 = _elec0.getDelta(); + uint32_t delta1 = _elec1.getDelta(); + + if ((delta0 > _elec0.getThreshold()) || (delta1 > _elec1.getThreshold())) { + uint32_t perc_pos0 = (delta0 * 100) / (delta0 + delta1); + uint32_t perc_pos1 = (delta1 * 100) / (delta0 + delta1); + setSliderPercPosition(0, perc_pos0); + setSliderPercPosition(1, perc_pos1); + uint32_t dist_pos0 = (perc_pos0 * _range) / 100; + uint32_t dist_pos1 = (perc_pos0 * _range) / 100; + setSliderDisPosition(0, dist_pos0); + setSliderDisPosition(1, dist_pos1); + + setAbsolutePosition(((100 - perc_pos0) + perc_pos1) / 2); + setAbsoluteDistance(((_range - dist_pos0) + dist_pos1) / 2); + } else { + setSliderPercPosition(0, 0); + setSliderPercPosition(1, 0); + setSliderDisPosition(0, 0); + setSliderDisPosition(1, 0); + setAbsolutePosition(0); + setAbsoluteDistance(0); + } + } +} + +float TSIAnalogSlider::readPercentage() { + sliderRead(); + return (float)getAbsolutePosition() / 100.0; +} + +uint32_t TSIAnalogSlider::readDistance() { + sliderRead(); + return getAbsoluteDistance(); +} + +static void changeElectrode(TSIAnalogSlider *analog_slider) +{ + TSIElectrode* elec = analog_slider->getCurrentElectrode(); + uint32_t signal = (TSI0->DATA & TSI_DATA_TSICNT_MASK); + elec->setSignal(signal); + + TSIElectrode *next_elec = analog_slider->getNextElectrode(elec); + + analog_slider->setCurrentElectrode(next_elec); + TSI0->DATA = ((next_elec->getChannel() << TSI_DATA_TSICH_SHIFT) ); + TSI0->DATA |= TSI_DATA_SWTS_MASK; +} + + +void tsi_irq(void) +{ + TSIAnalogSlider *analog_slider = TSIAnalogSlider::getInstance(); + analog_slider->setScan(1); + TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag + changeElectrode(analog_slider); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tsi_sensor/tsi_sensor.h Mon Aug 25 12:00:15 2014 +0000 @@ -0,0 +1,198 @@ +/* Freescale Semiconductor Inc. + * + * mbed Microcontroller Library + * (c) Copyright 2009-2012 ARM Limited. + * + * 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 TSISENSOR_H +#define TSISENSOR_H + +/** +* TSISensor example +* +* @code +* #include "mbed.h" +* #include "TSISensor.h" +* +* int main(void) { +* DigitalOut led(LED_GREEN); +* TSIElectrode elec0(9); +* TSIElectrode elec1(10); +* TSIAnalogSlider tsi(elec0, elec1, 40); +* +* while (true) { +* printf("slider percentage: %f%\r\n", tsi.readPercentage()); +* printf("slider distance: %dmm\r\n", tsi.readDistance()); +* wait(1); +* led = !led; +* } +* } +* @endcode +*/ +#define NO_TOUCH 0 + +/** TSI Electrode with simple data required for touch detection. + */ +class TSIElectrode { +public: + /** Initialize electrode. + */ + TSIElectrode(uint32_t tsi_channel) : _threshold(100) { + _channel = (uint8_t)tsi_channel; + } + /** Set baseline. + */ + void setBaseline(uint32_t baseline) { + _baseline = (uint16_t)baseline; + } + /** Set threshold. + */ + void setThreshold(uint32_t threshold) { + _threshold = (uint16_t)threshold; + } + /** Set signal. + */ + void setSignal(uint32_t signal) { + _signal = (uint16_t)signal; + } + /** Get baseline. + */ + uint32_t getBaseline() { + return _baseline; + } + /** Get delta. + */ + uint32_t getDelta() { + int32_t delta = getSignal() - getBaseline(); + if (delta < 0) { + return 0; + } else { + return delta; + } + } + /** Get signal. + */ + uint32_t getSignal() { + return _signal; + } + /** Get threshold. + */ + uint32_t getThreshold() { + return _threshold; + } + /** Get channel. + */ + uint32_t getChannel() { + return _channel; + } +private: + uint8_t _channel; + uint16_t _signal; + uint16_t _baseline; + uint16_t _threshold; +}; + +/** Analog slider which consists of two electrodes. + */ +class TSIAnalogSlider { +public: + /** + * Initialize the TSI Touch Sensor + */ + TSIAnalogSlider(uint32_t elec0, uint32_t elec1, uint32_t range); + /** + * Read Touch Sensor percentage value + * + * @returns percentage value between [0 ... 1] + */ + float readPercentage(); + /** + * Read Touch Sensor distance + * + * @returns distance in mm. The value is between [0 ... _range] + */ + uint32_t readDistance(); + /** Get current electrode. + */ + TSIElectrode* getCurrentElectrode() { + return _current_elec; + } + /** Set current electrode which is being measured. + */ + void setCurrentElectrode(TSIElectrode *elec){ + _current_elec = elec; + } + /** Get next electrode. + */ + TSIElectrode* getNextElectrode(TSIElectrode* electrode) { + if (electrode->getChannel() == _elec0.getChannel()) { + return &_elec1; + } else { + return &_elec0; + } + } + /** Return absolute distance position. + */ + uint32_t getAbsoluteDistance() { + return _absolute_distance_pos; + } + /** Return absolute precentage position. + */ + uint32_t getAbsolutePosition() { + return _absolute_percentage_pos; + } + /** Set value to the scan in progress flag. + */ + void setScan(uint32_t scan) { + _scan_in_progress = scan; + } + /** Return instance to Analog slider. Used in tsi irq. + */ + static TSIAnalogSlider *getInstance() { + return _instance; + } +private: + void sliderRead(void); + void selfCalibration(void); + void setSliderPercPosition(uint32_t elec_num, uint32_t position) { + _percentage_position[elec_num] = position; + } + void setSliderDisPosition(uint32_t elec_num, uint32_t position) { + _distance_position[elec_num] = position; + } + void setAbsolutePosition(uint32_t position) { + _absolute_percentage_pos = position; + } + void setAbsoluteDistance(uint32_t distance) { + _absolute_distance_pos = distance; + } +private: + TSIElectrode _elec0; + TSIElectrode _elec1; + uint8_t _scan_in_progress; + TSIElectrode* _current_elec; + uint8_t _percentage_position[2]; + uint8_t _distance_position[2]; + uint32_t _absolute_percentage_pos; + uint32_t _absolute_distance_pos; + uint8_t _range; +protected: + static TSIAnalogSlider *_instance; +}; + +#endif