COSA
An Object-Oriented Platform for Arduino Programming
W5100.cpp
Go to the documentation of this file.
1 
21 #include "W5100.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 W5100
32 #include <W5X00.inc>
33 
34 void
35 W5100::write(uint16_t addr, const void* buf, size_t len, bool progmem)
36 {
37  const uint8_t* bp = (const uint8_t*) buf;
38  uint16_t last = addr + len;
39  spi.acquire(this);
40  spi.begin();
41  while (addr < last) {
43  spi.transfer_next(addr >> 8);
44  spi.transfer_next(addr++);
45  spi.transfer_next(progmem ? pgm_read_byte(bp++) : *bp++);
47  m_cs.set();
48  m_cs.clear();
49  }
50  spi.end();
51  spi.release();
52 }
53 
54 void
55 W5100::read(uint16_t addr, void* buf, size_t len)
56 {
57  uint8_t* bp = (uint8_t*) buf;
58  uint16_t last = addr + len;
59  spi.acquire(this);
60  spi.begin();
61  while (addr < last) {
63  spi.transfer_next(addr >> 8);
64  spi.transfer_next(addr++);
65  spi.transfer_next(0);
66  *bp++ = spi.transfer_await();
67  m_cs.set();
68  m_cs.clear();
69  }
70  spi.end();
71  spi.release();
72 }
73 
74 bool
75 W5100::begin(uint8_t ip[4], uint8_t subnet[4], uint16_t timeout)
76 {
77  // Initiate socket structure; buffer allocation and socket register pointer
78  for (uint8_t i = 0; i < SOCK_MAX; i++) {
80  m_sock[i].m_proto = 0;
81  m_sock[i].m_sreg = sreg;
84  m_sock[i].m_dev = this;
85  }
86 
87  // Check for default network address
88  uint8_t BROADCAST[4] = { 0, 0, 0, 0 };
89  if (ip == NULL || subnet == NULL) {
90  subnet = BROADCAST;
91  ip = BROADCAST;
92  }
93 
94  // Adjust timeout period to 100 us scale
95  timeout = swap(timeout * 10);
96 
97  // Read hardware address from program memory
98  uint8_t mac[6];
99  memcpy_P(mac, m_mac, sizeof(mac));
100 
101  // Reset and setup registers
102  write(M_CREG(MR), MR_RST);
103  write(M_CREG(SHAR), mac, sizeof(m_creg->SHAR));
104  write(M_CREG(RTR), &timeout, sizeof(m_creg->RTR));
105  write(M_CREG(TMSR), TX_MEMORY_SIZE);
106  write(M_CREG(RMSR), RX_MEMORY_SIZE);
107 
108  // Set source network address, subnet mask and default gateway
109  bind(ip, subnet);
110 
111  // TODO: Attach interrupt handler
112  // spi.attach(this);
113 
114  return (true);
115 }
116 #endif
uint16_t RTR
Retry Time Register.
Definition: W5100.hh:145
void addr(uint8_t ip[4], uint8_t subnet[4])
static const uint16_t SOCKET_REGISTER_BASE
Definition: W5100.hh:301
SocketRegister * m_sreg
Definition: W5100.hh:497
static const size_t BUF_MAX
Definition: W5100.hh:313
#define M_CREG(name)
Definition: W5100.cpp:28
void acquire(Driver *dev)
Definition: SOFT_SPI.cpp:43
#define NULL
Definition: Types.h:101
uint8_t read(uint16_t addr)
uint16_t m_rx_buf
Definition: W5100.hh:512
void set() const
Definition: OutputPin.hh:85
static const uint8_t RX_MEMORY_SIZE
Definition: W5100.hh:316
void write(uint16_t addr, uint8_t data)
Definition: W5100.hh:622
const uint8_t * m_mac
Definition: W5100.hh:606
uint8_t transfer_next(uint8_t data)
Definition: SPI.hh:261
uint8_t SHAR[6]
Source Hardware Address Register.
Definition: W5100.hh:140
CommonRegister * m_creg
Definition: W5100.hh:600
bool begin(uint8_t ip[4]=NULL, uint8_t subnet[4]=NULL, uint16_t timeout=500)
Definition: W5100.cpp:75
W5100 * m_dev
Definition: W5100.hh:500
static const uint8_t TX_MEMORY_SIZE
Definition: W5100.hh:315
static const uint16_t TX_MEMORY_BASE
Definition: W5100.hh:305
int bind(uint8_t ip[4], uint8_t subnet[4], uint8_t gateway[4]=NULL)
void begin()
Definition: SPI.hh:216
#define swap(a, b)
Definition: Canvas.cpp:164
void end()
Definition: SPI.hh:226
uint8_t m_proto
Definition: Socket.hh:305
uint8_t transfer_await()
Definition: SPI.hh:249
static const uint8_t SOCK_MAX
Definition: W5100.hh:322
uint16_t m_tx_buf
Definition: W5100.hh:503
S/W Reset.
Definition: W5100.hh:161
void transfer_start(uint8_t data)
Definition: SPI.hh:238
SPI spi
Definition: SPI.cpp:29
static const uint16_t RX_MEMORY_BASE
Definition: W5100.hh:309
void release()
Definition: SOFT_SPI.cpp:64
void clear() const
Definition: OutputPin.hh:124
OutputPin m_cs
Device chip select pin.
Definition: SPI.hh:166
Driver m_sock[SOCK_MAX]
Definition: W5100.hh:597