root/branches/follower/libw5100/src/libw5100/w5100_device.cpp

Revision 140, 3.1 KB (checked in by follower, 11 months ago)

Add author, license and copyright details. (Which weren't permitted in competition entry source.)

Line 
1/*
2
3  W5100 device configuration
4
5  Author:
6
7     Philip Lindsay <follower@rancidbacon.com>
8
9  License:
10
11     Copyright 2007-2008 // LGPL
12
13*/
14
15#include "w5100_device.h"
16
17W5100Device::W5100Device(int resetPin) {
18  /*
19
20    Store configuration and initialise the W5100 device
21
22  */
23
24  // TODO: We should really allow the chip-select pin to be set here?
25  //       Or require that it's defined. (Currently in the library file 'types.h'.)
26  _resetPin = resetPin;
27 
28  _init();
29}
30
31
32void W5100Device::_init(void) {
33  /*
34 
35     Initialise the W5100 device and driver.
36     
37   */
38
39  /*
40     Initialise the W5100 chip
41     
42     (Originally I thought it was possible for the chip
43      to function without a hardware reset but it
44      seems not to be the case.)
45   */   
46  pinMode(_resetPin, OUTPUT);
47 
48  // We rely on the time between function calls to
49  // be long enough for the chip to recognise the
50  // reset.
51  digitalWrite(_resetPin, HIGH);
52  digitalWrite(_resetPin, LOW); // reset
53  digitalWrite(_resetPin, HIGH);
54 
55  // Enable SPI bug fix
56  // (We only need to do this to enable additional optional
57  // devices on the SPI bus. The current version of the W5100
58  // requires SPI_EN to be low when you communicate with other
59  // devices on the SPI bus.)
60#define PIN_SPI_EN 8 // WIZnet module SPI_EN 
61  pinMode(PIN_SPI_EN, OUTPUT);
62  digitalWrite(PIN_SPI_EN, HIGH);
63 
64  // Chip initialisation by driver
65  // Might be redundant following the above reset,
66  // as this performs a software reset.
67  iinchip_init();
68 
69  // Initialise driver
70  // (This is required to configure some variables used
71  // internally by the driver--even if the default chip
72  // configuration is used.)
73  sysinit(0x55, 0x55); 
74}
75
76
77byte * W5100Device::_packBuffer(byte b0, byte b1, byte b2, byte b3) {
78  /*
79 
80     Utility function to pack four bytes into a buffer
81     in order to pass on to the lower-level drive functions.
82
83   */
84  return _packBuffer(b0, b1, b2, b3, 0, 0); // Adds two bytes of padding
85}
86
87
88byte * W5100Device::_packBuffer(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5) {
89  /*
90 
91     Utility function to pack six bytes into a buffer
92     in order to pass on to the lower-level drive functions.
93
94   */
95  _scratchBuffer[0] = b0;
96  _scratchBuffer[1] = b1;
97  _scratchBuffer[2] = b2;
98  _scratchBuffer[3] = b3;
99  _scratchBuffer[4] = b4;
100  _scratchBuffer[5] = b5;
101
102  return _scratchBuffer;
103}
104
105
106void W5100Device::setIp(byte b0, byte b1, byte b2, byte b3) {
107  /*
108
109     Set device IP address.
110 
111   */
112  setSIPR(_packBuffer(b0, b1, b2, b3)); 
113}
114
115
116void W5100Device::setMask(byte b0, byte b1, byte b2, byte b3) {
117  /*
118 
119     Set device net mask.
120
121   */
122  setSUBR(_packBuffer(b0, b1, b2, b3)); 
123}
124
125
126void W5100Device::setGateway(byte b0, byte b1, byte b2, byte b3) {
127  /*
128   
129     Set device gateway.
130
131     (Note: this is required to access the internet from within a LAN.)
132
133   */
134  setGAR(_packBuffer(b0, b1, b2, b3)); 
135}
136
137
138void W5100Device::setMac(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5) {
139  /*
140
141     Set device MAC address.
142 
143   */
144  setSHAR(_packBuffer(b0, b1, b2, b3, b4, b5)); 
145}
146
147// W5100Device W5100 = W5100Device(PIN_RESET);
148
Note: See TracBrowser for help on using the browser.