|
|
|
@ -4,40 +4,40 @@
|
|
|
|
|
|
|
|
|
|
See:
|
|
|
|
|
|
|
|
|
|
<http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ>
|
|
|
|
|
<http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ>
|
|
|
|
|
|
|
|
|
|
Current features:
|
|
|
|
|
|
|
|
|
|
* Initial W5100 driver port:
|
|
|
|
|
* Initial W5100 driver port:
|
|
|
|
|
|
|
|
|
|
+ new-style network configuration
|
|
|
|
|
+ new-style network configuration
|
|
|
|
|
|
|
|
|
|
+ socket creation/listening/closing
|
|
|
|
|
+ socket creation/listening/closing
|
|
|
|
|
|
|
|
|
|
+ Sending/Receiving okay
|
|
|
|
|
+ Sending/Receiving okay
|
|
|
|
|
|
|
|
|
|
+ example "echo" server (no longer)
|
|
|
|
|
+ example "echo" server (no longer)
|
|
|
|
|
|
|
|
|
|
+ example "web server" with LED flash.
|
|
|
|
|
+ example "web server" with LED flash.
|
|
|
|
|
|
|
|
|
|
* Terrible hacked-together code
|
|
|
|
|
* Terrible hacked-together code
|
|
|
|
|
|
|
|
|
|
Author:
|
|
|
|
|
|
|
|
|
|
follower@rancidbacon.com
|
|
|
|
|
follower@rancidbacon.com
|
|
|
|
|
|
|
|
|
|
License:
|
|
|
|
|
|
|
|
|
|
LGPL
|
|
|
|
|
LGPL
|
|
|
|
|
|
|
|
|
|
Version:
|
|
|
|
|
|
|
|
|
|
20071106-0005+
|
|
|
|
|
20071106-0005+
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <WIZ810MJ.h>
|
|
|
|
|
|
|
|
|
|
#include <SPrint/staticPrint.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Define SPI-related pins
|
|
|
|
@ -56,7 +56,7 @@ byte ip[6];
|
|
|
|
|
void configureSPI() {
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Configure pins and registers required for SPI communication.
|
|
|
|
|
Configure pins and registers required for SPI communication.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
@ -69,16 +69,16 @@ void configureSPI() {
|
|
|
|
|
digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Configure SPI Control Register (SPCR) (All values initially 0)
|
|
|
|
|
Bit Description
|
|
|
|
|
7 SPI Interrupt Enable -- disable (SPIE --> 0)
|
|
|
|
|
6 SPI Enable -- enable (SPE --> 1)
|
|
|
|
|
5 Data Order -- MSB 1st (DORD --> 0) (Slave specific)
|
|
|
|
|
4 Master/Slave Select -- master (MSTR --> 1)
|
|
|
|
|
3 Clock Polarity -- (CPOL --> 0) (Slave specific) ("Mode")
|
|
|
|
|
2 Clock Phase -- (CPHA --> 0) (Slave specific)
|
|
|
|
|
1 SPI Clock Rate Select 1 -- } (SPR1 --> 0)
|
|
|
|
|
0 SPI Clock Rate Select 0 -- } fOSC/4 (SPR0 --> 0) ("Fastest" but see SPI2X in SPSR)
|
|
|
|
|
Configure SPI Control Register (SPCR) (All values initially 0)
|
|
|
|
|
Bit Description
|
|
|
|
|
7 SPI Interrupt Enable -- disable (SPIE --> 0)
|
|
|
|
|
6 SPI Enable -- enable (SPE --> 1)
|
|
|
|
|
5 Data Order -- MSB 1st (DORD --> 0) (Slave specific)
|
|
|
|
|
4 Master/Slave Select -- master (MSTR --> 1)
|
|
|
|
|
3 Clock Polarity -- (CPOL --> 0) (Slave specific) ("Mode")
|
|
|
|
|
2 Clock Phase -- (CPHA --> 0) (Slave specific)
|
|
|
|
|
1 SPI Clock Rate Select 1 -- } (SPR1 --> 0)
|
|
|
|
|
0 SPI Clock Rate Select 0 -- } fOSC/4 (SPR0 --> 0) ("Fastest" but see SPI2X in SPSR)
|
|
|
|
|
*/
|
|
|
|
|
SPCR = (1<<SPE)| (1<<MSTR);
|
|
|
|
|
|
|
|
|
@ -95,16 +95,16 @@ void configureSPI() {
|
|
|
|
|
void initModule() {
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Initialise the WIZ810MJ module and driver.
|
|
|
|
|
Initialise the WIZ810MJ module and driver.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Initialise the W5100 chip
|
|
|
|
|
Initialise the W5100 chip
|
|
|
|
|
|
|
|
|
|
(Originally I thought it was possible for the chip
|
|
|
|
|
to function without a hardware reset but it
|
|
|
|
|
seems not to be the case.)
|
|
|
|
|
(Originally I thought it was possible for the chip
|
|
|
|
|
to function without a hardware reset but it
|
|
|
|
|
seems not to be the case.)
|
|
|
|
|
*/
|
|
|
|
|
pinMode(PIN_RESET, OUTPUT);
|
|
|
|
|
|
|
|
|
@ -131,41 +131,41 @@ void initModule() {
|
|
|
|
|
#define PIN_LED 2
|
|
|
|
|
|
|
|
|
|
void setup () {
|
|
|
|
|
Serial.begin(9600);
|
|
|
|
|
Serial.println("Setup enter...");
|
|
|
|
|
Serial.begin(19200);
|
|
|
|
|
SPrintln("Setup enter...");
|
|
|
|
|
|
|
|
|
|
configureSPI();
|
|
|
|
|
|
|
|
|
|
initModule();
|
|
|
|
|
|
|
|
|
|
Serial.println("Test W5100 configuration...");
|
|
|
|
|
SPrintln("Test W5100 configuration...");
|
|
|
|
|
|
|
|
|
|
byte config_gateway[] = {192,168,2,101};
|
|
|
|
|
byte config_subnet_mask[] = {255,255,255,0};
|
|
|
|
|
byte config_gateway[] = {192,168,0.1};
|
|
|
|
|
byte config_subnet_mask[] = {255,255,0,0};
|
|
|
|
|
byte config_mac_address[] = {0x02,0xDE,0xAD,0xBE,0xEF,0x00};
|
|
|
|
|
byte config_ip_address[] = {192,168,2,105};
|
|
|
|
|
byte config_ip_address[] = {192,168,0,69};
|
|
|
|
|
|
|
|
|
|
setGAR(config_gateway);
|
|
|
|
|
setSUBR(config_subnet_mask);
|
|
|
|
|
setSHAR(config_mac_address);
|
|
|
|
|
setSIPR(config_ip_address);
|
|
|
|
|
|
|
|
|
|
Serial.println("End test W5100 configuration...");
|
|
|
|
|
SPrintln("End test W5100 configuration...");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Serial.println("Test W5100 driver code...");
|
|
|
|
|
SPrintln("Test W5100 driver code...");
|
|
|
|
|
|
|
|
|
|
getGAR(ip);
|
|
|
|
|
|
|
|
|
|
Serial.print("Gateway IP read (first digit): ");
|
|
|
|
|
SPrint("Gateway IP read (first digit): ");
|
|
|
|
|
Serial.println(ip[0], DEC);
|
|
|
|
|
|
|
|
|
|
Serial.println("End test W5100 driver code...");
|
|
|
|
|
SPrintln("End test W5100 driver code...");
|
|
|
|
|
|
|
|
|
|
pinMode(PIN_LED, OUTPUT);
|
|
|
|
|
digitalWrite(PIN_LED, HIGH);
|
|
|
|
|
|
|
|
|
|
Serial.println("Setup exit...");
|
|
|
|
|
SPrintln("Setup exit...");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -173,7 +173,7 @@ void sendPrompt(uint8_t *buffer) { // {Socket targetSocket, ) {
|
|
|
|
|
|
|
|
|
|
strcpy((char *) buffer, "w00t!> ");
|
|
|
|
|
|
|
|
|
|
Serial.print("send result: ");
|
|
|
|
|
SPrint("send result: ");
|
|
|
|
|
//Serial.println(send(testSocket, buffer, 6), DEC);
|
|
|
|
|
Serial.println(send(testSocket, buffer, strlen((char *)buffer)), DEC);
|
|
|
|
|
}
|
|
|
|
@ -181,38 +181,39 @@ void sendPrompt(uint8_t *buffer) { // {Socket targetSocket, ) {
|
|
|
|
|
void sendBanner(uint8_t *buffer, int ledState) { // {Socket targetSocket, ) {
|
|
|
|
|
|
|
|
|
|
//strcpy((char *) buffer, "Content-Type: text/plain\n\nfoo!\n");
|
|
|
|
|
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");
|
|
|
|
|
if (ledState) {
|
|
|
|
|
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");
|
|
|
|
|
buffer[66] = 'F';
|
|
|
|
|
} else {
|
|
|
|
|
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");
|
|
|
|
|
buffer[68] = 'F';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.print("send result: ");
|
|
|
|
|
SPrint("send result: ");
|
|
|
|
|
Serial.println(send(testSocket, buffer, strlen((char *)buffer)), DEC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
Serial.println("Test W5100 socket...");
|
|
|
|
|
SPrintln("Test W5100 socket...");
|
|
|
|
|
|
|
|
|
|
Serial.print("Create socket result: ");
|
|
|
|
|
SPrint("Create socket result: ");
|
|
|
|
|
Serial.println(socket(testSocket, Sn_MR_TCP, 80, 0), DEC);
|
|
|
|
|
|
|
|
|
|
Serial.print("Socket status: ");
|
|
|
|
|
SPrint("Socket status: ");
|
|
|
|
|
Serial.println(IINCHIP_READ(Sn_SR(testSocket)), HEX);
|
|
|
|
|
|
|
|
|
|
if (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) {
|
|
|
|
|
Serial.println("Socket still closed, waiting...");
|
|
|
|
|
SPrintln("Socket still closed, waiting...");
|
|
|
|
|
while (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) {
|
|
|
|
|
//pass
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.print("Listen on socket result: ");
|
|
|
|
|
SPrint("Listen on socket result: ");
|
|
|
|
|
Serial.println(listen(testSocket), DEC);
|
|
|
|
|
|
|
|
|
|
Serial.println("Waiting for connection...");
|
|
|
|
|
SPrintln("Waiting for connection...");
|
|
|
|
|
|
|
|
|
|
while (getSn_SR(testSocket) == SOCK_LISTEN) {
|
|
|
|
|
delay(500);
|
|
|
|
@ -222,7 +223,7 @@ void loop() {
|
|
|
|
|
|
|
|
|
|
getSn_DIPR(testSocket, ip);
|
|
|
|
|
|
|
|
|
|
Serial.print("Destination IP read (last digit): ");
|
|
|
|
|
SPrint("Destination IP read (last digit): ");
|
|
|
|
|
Serial.println(ip[3], DEC);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -256,60 +257,60 @@ void loop() {
|
|
|
|
|
|
|
|
|
|
while (getSn_SR(testSocket) == SOCK_ESTABLISHED) {
|
|
|
|
|
while (getSn_RX_RSR(testSocket) > 0) {
|
|
|
|
|
recv(testSocket, bytesReceived, 1);
|
|
|
|
|
theByte = bytesReceived[0];
|
|
|
|
|
//Serial.print(bytesReceived[0], BYTE);
|
|
|
|
|
Serial.print(theByte, BYTE);
|
|
|
|
|
if ((state == STATE_G) && (theByte == 'G')) {
|
|
|
|
|
state = STATE_E;
|
|
|
|
|
} else if ((state == STATE_E) && (theByte == 'E')) {
|
|
|
|
|
state = STATE_T;
|
|
|
|
|
} else if ((state == STATE_T) && (theByte == 'T')) {
|
|
|
|
|
state = STATE_SPACE;
|
|
|
|
|
} else if ((state == STATE_SPACE) && (theByte == ' ')) {
|
|
|
|
|
state = STATE_SLASH;
|
|
|
|
|
} else if ((state == STATE_SLASH) && (theByte == '/')) {
|
|
|
|
|
state = STATE_READ;
|
|
|
|
|
} else if ((state == STATE_READ) && (theByte == '0')) {
|
|
|
|
|
digitalWrite(PIN_LED, LOW);
|
|
|
|
|
ledState = 0;
|
|
|
|
|
//state = STATE_END;
|
|
|
|
|
delay(100);
|
|
|
|
|
} else if ((state == STATE_READ) && (theByte == '1')) {
|
|
|
|
|
digitalWrite(PIN_LED, HIGH);
|
|
|
|
|
ledState = 1;
|
|
|
|
|
//state = STATE_END;
|
|
|
|
|
delay(100);
|
|
|
|
|
} else if (state == STATE_READ) {
|
|
|
|
|
// It's not a valid byte.
|
|
|
|
|
state = STATE_END;
|
|
|
|
|
} else {
|
|
|
|
|
state = STATE_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((state == STATE_ERR) || (state == STATE_END)) {
|
|
|
|
|
Serial.println("");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
dataLength = getSn_RX_RSR(testSocket);
|
|
|
|
|
|
|
|
|
|
if (dataLength >= MAX_RX_BUFFER_SIZE) { // TODO: blah, blah...
|
|
|
|
|
dataLength = MAX_RX_BUFFER_SIZE-1;
|
|
|
|
|
}
|
|
|
|
|
// Serial.print("dataLength: "); Serial.println(dataLength, HEX);
|
|
|
|
|
//Serial.print("recv result: ");
|
|
|
|
|
//Serial.println(recv(testSocket, bytesReceived, dataLength), DEC); // NOTE: Throws away unread portion? No?
|
|
|
|
|
recv(testSocket, bytesReceived, dataLength); // TODO: Return length?
|
|
|
|
|
bytesReceived[dataLength]=0x00;
|
|
|
|
|
Serial.print((char *)bytesReceived);
|
|
|
|
|
recv(testSocket, bytesReceived, 1);
|
|
|
|
|
theByte = bytesReceived[0];
|
|
|
|
|
//Serial.print(bytesReceived[0], BYTE);
|
|
|
|
|
Serial.print(theByte, BYTE);
|
|
|
|
|
if ((state == STATE_G) && (theByte == 'G')) {
|
|
|
|
|
state = STATE_E;
|
|
|
|
|
} else if ((state == STATE_E) && (theByte == 'E')) {
|
|
|
|
|
state = STATE_T;
|
|
|
|
|
} else if ((state == STATE_T) && (theByte == 'T')) {
|
|
|
|
|
state = STATE_SPACE;
|
|
|
|
|
} else if ((state == STATE_SPACE) && (theByte == ' ')) {
|
|
|
|
|
state = STATE_SLASH;
|
|
|
|
|
} else if ((state == STATE_SLASH) && (theByte == '/')) {
|
|
|
|
|
state = STATE_READ;
|
|
|
|
|
} else if ((state == STATE_READ) && (theByte == '0')) {
|
|
|
|
|
digitalWrite(PIN_LED, LOW);
|
|
|
|
|
ledState = 0;
|
|
|
|
|
//state = STATE_END;
|
|
|
|
|
delay(100);
|
|
|
|
|
} else if ((state == STATE_READ) && (theByte == '1')) {
|
|
|
|
|
digitalWrite(PIN_LED, HIGH);
|
|
|
|
|
ledState = 1;
|
|
|
|
|
//state = STATE_END;
|
|
|
|
|
delay(100);
|
|
|
|
|
} else if (state == STATE_READ) {
|
|
|
|
|
// It's not a valid byte.
|
|
|
|
|
state = STATE_END;
|
|
|
|
|
} else {
|
|
|
|
|
state = STATE_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((state == STATE_ERR) || (state == STATE_END)) {
|
|
|
|
|
SPrintln("");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Serial.print("send result: ");
|
|
|
|
|
//Serial.println(send(testSocket, bytesReceived, dataLength), DEC);
|
|
|
|
|
/*
|
|
|
|
|
dataLength = getSn_RX_RSR(testSocket);
|
|
|
|
|
|
|
|
|
|
//sendPrompt(bytesReceived);
|
|
|
|
|
*/
|
|
|
|
|
if (dataLength >= MAX_RX_BUFFER_SIZE) { // TODO: blah, blah...
|
|
|
|
|
dataLength = MAX_RX_BUFFER_SIZE-1;
|
|
|
|
|
}
|
|
|
|
|
// SPrint("dataLength: "); Serial.println(dataLength, HEX);
|
|
|
|
|
//SPrint("recv result: ");
|
|
|
|
|
//Serial.println(recv(testSocket, bytesReceived, dataLength), DEC); // NOTE: Throws away unread portion? No?
|
|
|
|
|
recv(testSocket, bytesReceived, dataLength); // TODO: Return length?
|
|
|
|
|
bytesReceived[dataLength]=0x00;
|
|
|
|
|
Serial.print((char *)bytesReceived);
|
|
|
|
|
|
|
|
|
|
//SPrint("send result: ");
|
|
|
|
|
//Serial.println(send(testSocket, bytesReceived, dataLength), DEC);
|
|
|
|
|
|
|
|
|
|
//sendPrompt(bytesReceived);
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
sendBanner(bytesToSend, ledState);
|
|
|
|
@ -317,10 +318,10 @@ void loop() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disconnect(testSocket);
|
|
|
|
|
close(testSocket);
|
|
|
|
|
disconnect(testSocket);
|
|
|
|
|
|
|
|
|
|
Serial.println("End test W5100 socket...");
|
|
|
|
|
SPrintln("End test W5100 socket...");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|