- Timestamp:
- 12/07/07 04:40:54 (13 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/follower/wiz810mj/src/demo/WizDemo2/WizDemo2.pde
r69 r72 5 5 See: 6 6 7 <http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ>7 <http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ> 8 8 9 9 Current features: 10 10 11 * Initial W5100 driver port: 12 13 + new-style network configuration 11 * Initial W5100 driver port: 12 13 + new-style network configuration 14 15 + socket creation/listening/closing 16 17 + Sending/Receiving okay 18 19 + example "echo" server (no longer) 20 21 + example "web server" with LED flash. 22 23 * Terrible hacked-together code 24 25 Author: 26 27 follower@rancidbacon.com 14 28 15 + socket creation/listening/closing16 17 + Sending/Receiving okay18 19 + example "echo" server (no longer)20 21 + example "web server" with LED flash.22 23 * Terrible hacked-together code24 25 Author:26 27 follower@rancidbacon.com28 29 29 License: 30 31 LGPL32 30 31 LGPL 32 33 33 Version: 34 34 35 20071106-0005+36 35 20071106-0005+ 36 37 37 */ 38 38 39 39 #include <WIZ810MJ.h> 40 #include <SPrint/staticPrint.h> 40 41 41 42 42 … … 57 57 /* 58 58 59 Configure pins and registers required for SPI communication.59 Configure pins and registers required for SPI communication. 60 60 61 61 */ … … 70 70 71 71 /* 72 Configure SPI Control Register (SPCR) (All values initially 0)73 Bit Description74 7 SPI Interrupt Enable -- disable (SPIE --> 0)75 6 SPI Enable-- enable (SPE --> 1)76 5 Data Order-- MSB 1st (DORD --> 0) (Slave specific)77 4 Master/Slave Select -- master (MSTR --> 1)78 3 Clock Polarity --(CPOL --> 0) (Slave specific) ("Mode")79 2 Clock Phase --(CPHA --> 0) (Slave specific)80 1 SPI Clock Rate Select 1 -- }(SPR1 --> 0)81 0 SPI Clock Rate Select 0 -- } fOSC/4 (SPR0 --> 0) ("Fastest" but see SPI2X in SPSR)72 Configure SPI Control Register (SPCR) (All values initially 0) 73 Bit Description 74 7 SPI Interrupt Enable -- disable (SPIE --> 0) 75 6 SPI Enable -- enable (SPE --> 1) 76 5 Data Order -- MSB 1st (DORD --> 0) (Slave specific) 77 4 Master/Slave Select -- master (MSTR --> 1) 78 3 Clock Polarity -- (CPOL --> 0) (Slave specific) ("Mode") 79 2 Clock Phase -- (CPHA --> 0) (Slave specific) 80 1 SPI Clock Rate Select 1 -- } (SPR1 --> 0) 81 0 SPI Clock Rate Select 0 -- } fOSC/4 (SPR0 --> 0) ("Fastest" but see SPI2X in SPSR) 82 82 */ 83 83 SPCR = (1<<SPE)| (1<<MSTR); … … 96 96 /* 97 97 98 Initialise the WIZ810MJ module and driver.99 98 Initialise the WIZ810MJ module and driver. 99 100 100 */ 101 101 102 102 /* 103 Initialise the W5100 chip104 105 (Originally I thought it was possible for the chip106 to function without a hardware reset but it107 seems not to be the case.)108 */ 103 Initialise the W5100 chip 104 105 (Originally I thought it was possible for the chip 106 to function without a hardware reset but it 107 seems not to be the case.) 108 */ 109 109 pinMode(PIN_RESET, OUTPUT); 110 110 … … 132 132 133 133 void setup () { 134 Serial.begin( 19200);135 S Println("Setup enter...");136 137 configureSPI(); 134 Serial.begin(9600); 135 Serial.println("Setup enter..."); 136 137 configureSPI(); 138 138 139 139 initModule(); 140 140 141 S Println("Test W5100 configuration...");142 143 byte config_gateway[] = {192,168, 0.1};144 byte config_subnet_mask[] = {255,255, 0,0};141 Serial.println("Test W5100 configuration..."); 142 143 byte config_gateway[] = {192,168,2,101}; 144 byte config_subnet_mask[] = {255,255,255,0}; 145 145 byte config_mac_address[] = {0x02,0xDE,0xAD,0xBE,0xEF,0x00}; 146 byte config_ip_address[] = {192,168, 0,69};146 byte config_ip_address[] = {192,168,2,105}; 147 147 148 148 setGAR(config_gateway); … … 151 151 setSIPR(config_ip_address); 152 152 153 S Println("End test W5100 configuration...");154 155 156 S Println("Test W5100 driver code...");153 Serial.println("End test W5100 configuration..."); 154 155 156 Serial.println("Test W5100 driver code..."); 157 157 158 158 getGAR(ip); 159 159 160 S Print("Gateway IP read (first digit): ");160 Serial.print("Gateway IP read (first digit): "); 161 161 Serial.println(ip[0], DEC); 162 162 163 S Println("End test W5100 driver code...");163 Serial.println("End test W5100 driver code..."); 164 164 165 165 pinMode(PIN_LED, OUTPUT); 166 166 digitalWrite(PIN_LED, HIGH); 167 167 168 S Println("Setup exit...");168 Serial.println("Setup exit..."); 169 169 170 170 } … … 174 174 strcpy((char *) buffer, "w00t!> "); 175 175 176 S Print("send result: ");176 Serial.print("send result: "); 177 177 //Serial.println(send(testSocket, buffer, 6), DEC); 178 178 Serial.println(send(testSocket, buffer, strlen((char *)buffer)), DEC); … … 182 182 183 183 //strcpy((char *) buffer, "Content-Type: text/plain\n\nfoo!\n"); 184 strcpy((char *) buffer, "HTTP/1.1 200 OK\n\rContent-Type: text/html\n\r\n\r<html><body bgcolor='#000000'>foo!</body></html>\n\r"); 184 185 if (ledState) { 185 strcpy((char *) buffer, "HTTP/1.1 200 OK\n\rContent-Type: text/html\n\r\n\r<html><body bgcolor='#000000'>foo!</body></html>\n\r");186 186 buffer[66] = 'F'; 187 187 } else { 188 strcpy((char *) buffer, "HTTP/1.1 200 OK\n\rContent-Type: text/html\n\r\n\r<html><body bgcolor='#000000'>bar!</body></html>\n\r"); 189 buffer[68] = 'F'; 188 buffer[68] = 'F'; 190 189 } 191 192 S Print("send result: ");190 191 Serial.print("send result: "); 193 192 Serial.println(send(testSocket, buffer, strlen((char *)buffer)), DEC); 194 193 } … … 196 195 197 196 void loop() { 198 S Println("Test W5100 socket...");199 200 S Print("Create socket result: ");197 Serial.println("Test W5100 socket..."); 198 199 Serial.print("Create socket result: "); 201 200 Serial.println(socket(testSocket, Sn_MR_TCP, 80, 0), DEC); 202 201 203 S Print("Socket status: ");202 Serial.print("Socket status: "); 204 203 Serial.println(IINCHIP_READ(Sn_SR(testSocket)), HEX); 205 204 206 205 if (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) { 207 S Println("Socket still closed, waiting...");206 Serial.println("Socket still closed, waiting..."); 208 207 while (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) { 209 208 //pass … … 211 210 } 212 211 213 S Print("Listen on socket result: ");212 Serial.print("Listen on socket result: "); 214 213 Serial.println(listen(testSocket), DEC); 215 216 S Println("Waiting for connection...");214 215 Serial.println("Waiting for connection..."); 217 216 218 217 while (getSn_SR(testSocket) == SOCK_LISTEN) { … … 224 223 getSn_DIPR(testSocket, ip); 225 224 226 S Print("Destination IP read (last digit): ");225 Serial.print("Destination IP read (last digit): "); 227 226 Serial.println(ip[3], DEC); 228 227 … … 258 257 while (getSn_SR(testSocket) == SOCK_ESTABLISHED) { 259 258 while (getSn_RX_RSR(testSocket) > 0) { 260 recv(testSocket, bytesReceived, 1);261 theByte = bytesReceived[0];262 //Serial.print(bytesReceived[0], BYTE);263 Serial.print(theByte, BYTE);264 if ((state == STATE_G) && (theByte == 'G')) {265 state = STATE_E;266 } else if ((state == STATE_E) && (theByte == 'E')) {267 state = STATE_T;268 } else if ((state == STATE_T) && (theByte == 'T')) {269 state = STATE_SPACE;270 } else if ((state == STATE_SPACE) && (theByte == ' ')) {271 state = STATE_SLASH;272 } else if ((state == STATE_SLASH) && (theByte == '/')) {273 state = STATE_READ;274 } else if ((state == STATE_READ) && (theByte == '0')) {275 digitalWrite(PIN_LED, LOW);276 ledState = 0;277 //state = STATE_END;278 delay(100);279 } else if ((state == STATE_READ) && (theByte == '1')) {280 digitalWrite(PIN_LED, HIGH);281 ledState = 1;282 //state = STATE_END;283 delay(100);284 } else if (state == STATE_READ) {285 // It's not a valid byte.286 state = STATE_END;287 } else {288 state = STATE_ERR;289 }290 291 if ((state == STATE_ERR) || (state == STATE_END)) {292 SPrintln("");293 break;294 }295 296 /*297 dataLength = getSn_RX_RSR(testSocket);298 299 if (dataLength >= MAX_RX_BUFFER_SIZE) { // TODO: blah, blah...300 dataLength = MAX_RX_BUFFER_SIZE-1;301 }302 // SPrint("dataLength: "); Serial.println(dataLength, HEX);303 //SPrint("recv result: ");304 //Serial.println(recv(testSocket, bytesReceived, dataLength), DEC); // NOTE: Throws away unread portion? No?305 recv(testSocket, bytesReceived, dataLength); // TODO: Return length?306 bytesReceived[dataLength]=0x00;307 Serial.print((char *)bytesReceived);308 309 //SPrint("send result: ");310 //Serial.println(send(testSocket, bytesReceived, dataLength), DEC);311 312 //sendPrompt(bytesReceived);313 */314 259 recv(testSocket, bytesReceived, 1); 260 theByte = bytesReceived[0]; 261 //Serial.print(bytesReceived[0], BYTE); 262 Serial.print(theByte, BYTE); 263 if ((state == STATE_G) && (theByte == 'G')) { 264 state = STATE_E; 265 } else if ((state == STATE_E) && (theByte == 'E')) { 266 state = STATE_T; 267 } else if ((state == STATE_T) && (theByte == 'T')) { 268 state = STATE_SPACE; 269 } else if ((state == STATE_SPACE) && (theByte == ' ')) { 270 state = STATE_SLASH; 271 } else if ((state == STATE_SLASH) && (theByte == '/')) { 272 state = STATE_READ; 273 } else if ((state == STATE_READ) && (theByte == '0')) { 274 digitalWrite(PIN_LED, LOW); 275 ledState = 0; 276 //state = STATE_END; 277 delay(100); 278 } else if ((state == STATE_READ) && (theByte == '1')) { 279 digitalWrite(PIN_LED, HIGH); 280 ledState = 1; 281 //state = STATE_END; 282 delay(100); 283 } else if (state == STATE_READ) { 284 // It's not a valid byte. 285 state = STATE_END; 286 } else { 287 state = STATE_ERR; 288 } 289 290 if ((state == STATE_ERR) || (state == STATE_END)) { 291 Serial.println(""); 292 break; 293 } 294 295 /* 296 dataLength = getSn_RX_RSR(testSocket); 297 298 if (dataLength >= MAX_RX_BUFFER_SIZE) { // TODO: blah, blah... 299 dataLength = MAX_RX_BUFFER_SIZE-1; 300 } 301 // Serial.print("dataLength: "); Serial.println(dataLength, HEX); 302 //Serial.print("recv result: "); 303 //Serial.println(recv(testSocket, bytesReceived, dataLength), DEC); // NOTE: Throws away unread portion? No? 304 recv(testSocket, bytesReceived, dataLength); // TODO: Return length? 305 bytesReceived[dataLength]=0x00; 306 Serial.print((char *)bytesReceived); 307 308 //Serial.print("send result: "); 309 //Serial.println(send(testSocket, bytesReceived, dataLength), DEC); 310 311 //sendPrompt(bytesReceived); 312 */ 313 315 314 } 316 315 sendBanner(bytesToSend, ledState); … … 318 317 break; 319 318 } 320 319 320 disconnect(testSocket); 321 321 close(testSocket); 322 disconnect(testSocket); 323 324 SPrintln("End test W5100 socket..."); 325 326 327 328 } 329 322 323 Serial.println("End test W5100 socket..."); 324 325 326 327 } 328