Add initial very-rough demo of 'echo' server.

git-svn-id: svn+ssh://oldsvn/home/mlalondesvn/svn/cral@58 3ee9b42a-b53c-0410-a25e-f0b6218d5d5b
master
follower 17 years ago
parent 8245c6d8be
commit cd1bdecdae

@ -0,0 +1,312 @@
/*
Code to test wiznet WIZ810MJ module
See:
<http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ>
Current features:
* Read register/address values
* Write register/address values
* Configure networking to enable ping (old style)
* Initial W5100 driver port:
+ new-style network configuration
+ socket creation/listening/closing
+ Sending/Receiving okay
+ example "echo" server
* Terrible hacked-together code
Author:
follower@rancidbacon.com
License:
LGPL
(Although note spi_transfer comes from the Playground originally.)
Version:
20071106-0005
*/
#include <WIZ810MJ.h>
// Define SPI-related pins
#define PIN_DATA_OUT 11 // MOSI (Master Out / Slave In)
#define PIN_DATA_IN 12 // MISO (Master In / Slave Out)
#define PIN_SPI_CLOCK 13 // SCK (Serial Clock)
#define PIN_SLAVE_SELECT 10 // SS (Slave Select)
// #define PIN_RESET 9 // WIZnet module /RESET
#define PIN_RESET 8 // WIZnet module /RESET
#define WIZNET_OPCODE_READ 0x0F
#define WIZNET_OPCODE_WRITE 0xF0
#define DUMMY_DATA 0xFF
SOCKET testSocket;
byte ip[6];
void setup () {
Serial.begin(9600);
Serial.println("Setup enter...");
Serial.print("SPCR: "); Serial.println(SPCR, BIN);
// Configure SPI
// Configure I/O pins
pinMode(PIN_DATA_OUT, OUTPUT);
pinMode(PIN_DATA_IN, INPUT);
pinMode(PIN_SPI_CLOCK, OUTPUT);
pinMode(PIN_SLAVE_SELECT, OUTPUT);
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)
SPCR = (1<<SPE)| (1<<MSTR);
Serial.print("SPCR: "); Serial.println(SPCR, BIN);
// Clear previous data and status (TODO: Determine if necessary/better way.)
// (Based on Playground SPI example.)
byte dummy;
dummy = SPSR;
dummy = SPDR;
delay(10);
// I thought this wasn't needed but seems like it is.
Serial.println("Triggering reset...");
pinMode(PIN_RESET, OUTPUT);
digitalWrite(PIN_RESET, HIGH); // no reset
//delay(10); // Pretty arbitrary length -- do we need it?
digitalWrite(PIN_RESET, LOW); // reset
//delay(10);
digitalWrite(PIN_RESET, HIGH); // no reset
//delay(10);
Serial.println("Reset triggered...");
/*
Serial.println("Configure device...");
// default gateway
writeAddressValue(0x00, 0x01, 0xC0);
writeAddressValue(0x00, 0x02, 0xA8);
writeAddressValue(0x00, 0x03, 0x02);
writeAddressValue(0x00, 0x04, 0x65);
// subnet mask
writeAddressValue(0x00, 0x05, 0xFF);
writeAddressValue(0x00, 0x06, 0xFF);
writeAddressValue(0x00, 0x07, 0xFF);
writeAddressValue(0x00, 0x08, 0x00);
// source hardware address (MAC?)
writeAddressValue(0x00, 0x09, 0x00);
writeAddressValue(0x00, 0x0A, 0xDE);
writeAddressValue(0x00, 0x0B, 0xAD);
writeAddressValue(0x00, 0x0C, 0xBE);
writeAddressValue(0x00, 0x0D, 0xEF);
writeAddressValue(0x00, 0x0E, 0x00);
// source ip address
writeAddressValue(0x00, 0x0F, 0xC0);
writeAddressValue(0x00, 0x10, 0xA8);
writeAddressValue(0x00, 0x11, 0x02);
writeAddressValue(0x00, 0x12, 0x69);
// Serial.println("SKIP: source ip address");
Serial.println("End configure device...");
*/
// driver chip init (Might be redundant with the above reset.)
Serial.println("Call iinchip_init...");
iinchip_init();
// driver sysinit
Serial.println("Call sysinit...");
sysinit(0x55, 0x55);
Serial.println("Test W5100 configuration...");
byte config_gateway[] = {192,168,2,101};
byte config_subnet_mask[] = {255,255,255,0};
byte config_mac_address[] = {0x02,0xDE,0xAD,0xBE,0xEF,0x00};
byte config_ip_address[] = {192,168,2,105};
setGAR(config_gateway);
setSUBR(config_subnet_mask);
setSHAR(config_mac_address);
setSIPR(config_ip_address);
Serial.println("End test W5100 configuration...");
Serial.println("Test W5100 driver code...");
getGAR(ip);
Serial.print("Gateway IP read (first digit): ");
Serial.println(ip[0], DEC);
Serial.println("End test W5100 driver code...");
Serial.println("Setup exit...");
}
byte readAddressValue(byte addressHiByte, byte addressLowByte) {
// TODO: use a word for the address instead
byte data = 0x00;
digitalWrite(PIN_SLAVE_SELECT, LOW); // Enable slave
// TODO: Check response values? e.g. 0x00, 0x01, 0x02
spi_transfer(WIZNET_OPCODE_READ);
spi_transfer(addressHiByte);
spi_transfer(addressLowByte);
data = spi_transfer(DUMMY_DATA);
digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave
return data;
}
void writeAddressValue(byte addressHiByte, byte addressLowByte, byte targetValue) {
// TODO: use a word for the address instead
digitalWrite(PIN_SLAVE_SELECT, LOW); // Enable slave
// TODO: Check response values? e.g. 0x00, 0x01, 0x02
spi_transfer(WIZNET_OPCODE_WRITE);
spi_transfer(addressHiByte);
spi_transfer(addressLowByte);
spi_transfer(targetValue);
digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave
}
// From Playground
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
{
};
return SPDR; // return the received byte
}
void sendPrompt(uint8_t *buffer) { // {Socket targetSocket, ) {
buffer[0] = 'w';
buffer[1] = '0';
buffer[2] = '0';
buffer[3] = 't';
buffer[4] = '>';
buffer[5] = ' ';
Serial.print("send result: ");
Serial.println(send(testSocket, buffer, 6), DEC);
}
void loop() {
Serial.println("Test W5100 socket...");
Serial.print("Create socket result: ");
Serial.println(socket(testSocket, Sn_MR_TCP, 5555, 0), DEC);
Serial.print("Socket status: ");
Serial.println(IINCHIP_READ(Sn_SR(testSocket)), HEX);
if (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) {
Serial.println("Socket still closed, waiting...");
while (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) {
//pass
}
}
Serial.print("Listen on socket result: ");
Serial.println(listen(testSocket), DEC);
Serial.println("Waiting for connection...");
while (getSn_SR(testSocket) == SOCK_LISTEN) {
delay(500);
}
Serial.println(getSn_SR(testSocket),HEX);
getSn_DIPR(testSocket, ip);
Serial.print("Destination IP read (last digit): ");
Serial.println(ip[3], DEC);
#define MAX_RX_BUFFER_SIZE 100
uint8_t bytesReceived[MAX_RX_BUFFER_SIZE]; // NOTE: Actual buffer should be larger.
sendPrompt(bytesReceived);
int dataLength = 0;
while (getSn_SR(testSocket) == SOCK_ESTABLISHED) {
while (getSn_RX_RSR(testSocket) > 0) {
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?
bytesReceived[dataLength]=0x00;
Serial.println((char *)bytesReceived);
Serial.print("send result: ");
Serial.println(send(testSocket, bytesReceived, dataLength), DEC);
sendPrompt(bytesReceived);
}}
disconnect(testSocket);
close(testSocket);
Serial.println("End test W5100 socket...");
}
Loading…
Cancel
Save