COSA
An Object-Oriented Platform for Arduino Programming
W5200.cpp
Go to the documentation of this file.
1 
21 #include "W5200.hh"
22 #include <W5X00.h>
23 #include <DNS.h>
24 #include <DHCP.h>
25 
26 #if !defined(BOARD_ATTINY)
27 
28 #define M_CREG(name) uint16_t(&m_creg->name)
29 #define M_SREG(name) uint16_t(&m_sreg->name)
30 
31 #define W5X00 W5200
32 #include <W5X00.inc>
33 
34 void
35 W5200::write(uint16_t addr, const void* buf, size_t len, bool progmem)
36 {
37  if (len == 0) return;
38  const uint8_t* bp = (const uint8_t*) buf;
39  spi.acquire(this);
40  spi.begin();
41  spi.transfer_start(addr >> 8);
42  spi.transfer_next(addr);
43  spi.transfer_next((OP_WRITE | ((len & 0x7F00) >> 8)));
44  spi.transfer_next(len & 0x00FF);
45  for (size_t i = 0; i < len; i++, bp++)
46  spi.transfer_next(progmem ? pgm_read_byte(bp) : *bp);
48  spi.end();
49  spi.release();
50 }
51 
52 void
53 W5200::read(uint16_t addr, void* buf, size_t len)
54 {
55  if (len == 0) return;
56  uint8_t* bp = (uint8_t*) buf;
57  spi.acquire(this);
58  spi.begin();
59  spi.transfer_start(addr >> 8);
60  spi.transfer_next(addr);
61  spi.transfer_next((OP_READ | ((len & 0x7F00) >> 8)));
62  spi.transfer_next(len & 0x00FF);
64  for (size_t i = 0; i < len; i++, bp++)
65  *bp = spi.transfer(0);
66  spi.end();
67  spi.release();
68 }
69 
70 bool
71 W5200::begin(uint8_t ip[4], uint8_t subnet[4], uint16_t timeout)
72 {
73  // Initiate socket structure; buffer allocation and socket register pointer
74  for (uint8_t i = 0; i < SOCK_MAX; i++) {
76  m_sock[i].m_proto = 0;
77  m_sock[i].m_sreg = sreg;
80  m_sock[i].m_dev = this;
81  }
82 
83  // Check for default network address
84  uint8_t BROADCAST[4] = { 0, 0, 0, 0 };
85  if (ip == NULL || subnet == NULL) {
86  subnet = BROADCAST;
87  ip = BROADCAST;
88  }
89 
90  // Adjust timeout period to 100 us scale
91  timeout = swap(timeout * 10);
92 
93  // Read hardware address from program memory
94  uint8_t mac[6];
95  memcpy_P(mac, m_mac, sizeof(mac));
96 
97  // Reset and setup registers
98  write(M_CREG(MR), MR_RST);
99  write(M_CREG(SHAR), mac, sizeof(m_creg->SHAR));
100  write(M_CREG(RTR), &timeout, sizeof(m_creg->RTR));
101 
102  // Set source network address, subnet mask and default gateway
103  bind(ip, subnet);
104 
105  // TODO: Attach interrupt handler
106  // spi.attach(this);
107 
108  return (true);
109 }
110 #endif
static const uint16_t RX_MEMORY_BASE
Definition: W5200.hh:349
uint8_t transfer(uint8_t data)
Definition: SOFT_SPI.cpp:87
void addr(uint8_t ip[4], uint8_t subnet[4])
void acquire(Driver *dev)
Definition: SOFT_SPI.cpp:43
#define NULL
Definition: Types.h:101
static const uint16_t TX_MEMORY_BASE
Definition: W5200.hh:345
uint16_t m_rx_buf
Definition: W5200.hh:550
CommonRegister * m_creg
Definition: W5200.hh:638
void addr(uint8_t ip[4], uint8_t subnet[4])
SocketRegister * m_sreg
Definition: W5200.hh:535
static const size_t BUF_MAX
Definition: W5200.hh:353
const uint8_t * m_mac
Definition: W5200.hh:644
uint8_t transfer_next(uint8_t data)
Definition: SPI.hh:261
int bind(uint8_t ip[4], uint8_t subnet[4], uint8_t gateway[4]=NULL)
W5200 * m_dev
Definition: W5200.hh:538
S/W Reset.
Definition: W5200.hh:166
uint8_t SHAR[6]
Source Hardware Address Register.
Definition: W5200.hh:140
Driver m_sock[SOCK_MAX]
Definition: W5200.hh:635
void begin()
Definition: SPI.hh:216
#define swap(a, b)
Definition: Canvas.cpp:164
#define M_CREG(name)
Definition: W5200.cpp:28
void end()
Definition: SPI.hh:226
uint16_t m_tx_buf
Definition: W5200.hh:541
uint8_t m_proto
Definition: Socket.hh:305
uint8_t transfer_await()
Definition: SPI.hh:249
static const uint8_t SOCK_MAX
Definition: W5200.hh:360
uint8_t read(uint16_t addr)
void transfer_start(uint8_t data)
Definition: SPI.hh:238
SPI spi
Definition: SPI.cpp:29
uint16_t RTR
Retry Time Register.
Definition: W5200.hh:145
void release()
Definition: SOFT_SPI.cpp:64
static const uint16_t SOCKET_REGISTER_BASE
Definition: W5200.hh:341
bool begin(uint8_t ip[4]=NULL, uint8_t subnet[4]=NULL, uint16_t timeout=500)
Definition: W5200.cpp:71
void write(uint16_t addr, uint8_t data)
Definition: W5200.hh:660