/* * Copyright (c) 2010 by Cristian Maglie * * This file is free software; you can redistribute it and/or modify * it under the terms of either the GNU General Public License version 2 * or the GNU Lesser General Public License version 2.1, both as * published by the Free Software Foundation. */ #include #include #include "utility/w5100.h" // W5100 controller instance W5100Class W5100; #define SPI_CS 10 #define TX_RX_MAX_BUF_SIZE 2048 #define TX_BUF 0x1100 #define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE) #define TXBUF_BASE 0x4000 #define RXBUF_BASE 0x6000 void W5100Class::init(void) { delay(300); SPI.begin(SPI_CS); // Set clock to 4Mhz (W5100 should support up to about 14Mhz) SPI.setClockDivider(SPI_CS, 21); SPI.setDataMode(SPI_CS, SPI_MODE0); writeMR(1< SSIZE) { // Wrap around circular buffer uint16_t size = SSIZE - offset; write(dstAddr, data, size); write(SBASE[s], data + size, len - size); } else { write(dstAddr, data, len); } ptr += len; writeSnTX_WR(s, ptr); } void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek) { uint16_t ptr; ptr = readSnRX_RD(s); read_data(s, ptr, data, len); if (!peek) { ptr += len; writeSnRX_RD(s, ptr); } } void W5100Class::read_data(SOCKET s, volatile uint16_t src, volatile uint8_t *dst, uint16_t len) { uint16_t size; uint16_t src_mask; uint16_t src_ptr; src_mask = src & RMASK; src_ptr = RBASE[s] + src_mask; if( (src_mask + len) > RSIZE ) { size = RSIZE - src_mask; read(src_ptr, (uint8_t *)dst, size); dst += size; read(RBASE[s], (uint8_t *) dst, len - size); } else read(src_ptr, (uint8_t *) dst, len); } uint8_t W5100Class::write(uint16_t _addr, uint8_t _data) { SPI.transfer(SPI_CS, 0xF0, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); SPI.transfer(SPI_CS, _data); return 1; } uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len) { for (uint16_t i=0; i<_len; i++) { SPI.transfer(SPI_CS, 0xF0, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); SPI.transfer(SPI_CS, _buf[i]); _addr++; } return _len; } uint8_t W5100Class::read(uint16_t _addr) { SPI.transfer(SPI_CS, 0x0F, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); uint8_t _data = SPI.transfer(SPI_CS, 0); return _data; } uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len) { for (uint16_t i=0; i<_len; i++) { SPI.transfer(SPI_CS, 0x0F, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); _buf[i] = SPI.transfer(SPI_CS, 0); _addr++; } return _len; } void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) { // Send command to socket writeSnCR(s, _cmd); // Wait for command to complete while (readSnCR(s)) ; }