Changeset 69 for branches

Show
Ignore:
Timestamp:
11/08/07 20:42:54 (14 months ago)
Author:
mlalondesvn
Message:

MODIF - Moved debug output to progmem functions
FIXED - Unexpected connection closing issue: close() before disconnect()

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/follower/wiz810mj/src/demo/WizDemo2/WizDemo2.pde

    r68 r69  
    55  See: 
    66 
    7      <http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ>      
     7   <http://code.rancidbacon.com/LearningAboutArduinoWIZ810MJ>    
    88 
    99  Current features: 
    1010   
    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 
     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 
    2424 
    2525  Author: 
    2626 
    27      follower@rancidbacon.com 
    28        
     27   follower@rancidbacon.com 
     28     
    2929  License: 
    30      
    31      LGPL 
    32       
     30   
     31   LGPL 
     32    
    3333  Version: 
    3434   
    35      20071106-0005+ 
    36       
     35   20071106-0005+ 
     36    
    3737*/ 
    3838 
    3939#include <WIZ810MJ.h> 
    40  
     40#include <SPrint/staticPrint.h> 
    4141 
    4242 
     
    5757  /* 
    5858 
    59      Configure pins and registers required for SPI communication. 
     59   Configure pins and registers required for SPI communication. 
    6060   
    6161  */ 
     
    7070   
    7171  /* 
    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) 
     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) 
    8282  */ 
    8383  SPCR = (1<<SPE)| (1<<MSTR); 
     
    9696  /* 
    9797   
    98      Initialise the WIZ810MJ module and driver. 
    99       
     98   Initialise the WIZ810MJ module and driver. 
     99    
    100100   */ 
    101101 
    102102  /* 
    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    */    
     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   */  
    109109  pinMode(PIN_RESET, OUTPUT); 
    110110   
     
    132132 
    133133void setup () { 
    134   Serial.begin(9600); 
    135   Serial.println("Setup enter..."); 
    136  
    137   configureSPI();     
     134  Serial.begin(19200); 
     135  SPrintln("Setup enter..."); 
     136 
     137  configureSPI();   
    138138   
    139139  initModule(); 
    140140 
    141   Serial.println("Test W5100 configuration...");         
    142    
    143   byte config_gateway[] = {192,168,2,101}; 
    144   byte config_subnet_mask[] = {255,255,255,0}; 
     141  SPrintln("Test W5100 configuration...");     
     142   
     143  byte config_gateway[] = {192,168,0.1}; 
     144  byte config_subnet_mask[] = {255,255,0,0}; 
    145145  byte config_mac_address[] = {0x02,0xDE,0xAD,0xBE,0xEF,0x00}; 
    146   byte config_ip_address[] = {192,168,2,105}; 
     146  byte config_ip_address[] = {192,168,0,69}; 
    147147    
    148148  setGAR(config_gateway); 
     
    151151  setSIPR(config_ip_address); 
    152152    
    153   Serial.println("End test W5100 configuration...");         
     153  SPrintln("End test W5100 configuration...");     
     154   
    154155     
    155          
    156   Serial.println("Test W5100 driver code..."); 
     156  SPrintln("Test W5100 driver code..."); 
    157157   
    158158  getGAR(ip); 
    159159   
    160   Serial.print("Gateway IP read (first digit): "); 
     160  SPrint("Gateway IP read (first digit): "); 
    161161  Serial.println(ip[0], DEC); 
    162162 
    163   Serial.println("End test W5100 driver code...");         
     163  SPrintln("End test W5100 driver code..."); 
    164164 
    165165  pinMode(PIN_LED, OUTPUT); 
    166166  digitalWrite(PIN_LED, HIGH); 
    167167   
    168   Serial.println("Setup exit..."); 
     168  SPrintln("Setup exit..."); 
    169169   
    170170} 
     
    174174  strcpy((char *) buffer, "w00t!> "); 
    175175 
    176   Serial.print("send result: ");  
     176  SPrint("send result: ");  
    177177  //Serial.println(send(testSocket, buffer, 6), DEC); 
    178178  Serial.println(send(testSocket, buffer, strlen((char *)buffer)), DEC); 
     
    182182 
    183183  //strcpy((char *) buffer, "Content-Type: text/plain\n\nfoo!\n"); 
     184  if (ledState) { 
    184185  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"); 
    185   if (ledState) { 
    186186   buffer[66] = 'F';  
    187187  } else { 
    188    buffer[68] = 'F';      
     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';    
    189190  } 
    190  
    191   Serial.print("send result: ");  
     191   
     192  SPrint("send result: ");  
    192193  Serial.println(send(testSocket, buffer, strlen((char *)buffer)), DEC); 
    193194} 
     
    195196 
    196197void loop() { 
    197   Serial.println("Test W5100 socket..."); 
    198    
    199   Serial.print("Create socket result: ");  
     198  SPrintln("Test W5100 socket..."); 
     199   
     200  SPrint("Create socket result: ");  
    200201  Serial.println(socket(testSocket, Sn_MR_TCP, 80, 0), DEC); 
    201202 
    202   Serial.print("Socket status: ");    
     203  SPrint("Socket status: ");    
    203204  Serial.println(IINCHIP_READ(Sn_SR(testSocket)), HEX); 
    204205   
    205206  if (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) { 
    206     Serial.println("Socket still closed, waiting..."); 
     207    SPrintln("Socket still closed, waiting..."); 
    207208    while (IINCHIP_READ(Sn_SR(testSocket)) == SOCK_CLOSED) { 
    208209      //pass 
     
    210211  } 
    211212   
    212   Serial.print("Listen on socket result: ");  
     213  SPrint("Listen on socket result: "); 
    213214  Serial.println(listen(testSocket), DEC); 
    214  
    215   Serial.println("Waiting for connection...");  
     215   
     216  SPrintln("Waiting for connection...");  
    216217 
    217218  while (getSn_SR(testSocket) == SOCK_LISTEN) { 
     
    223224  getSn_DIPR(testSocket, ip); 
    224225   
    225   Serial.print("Destination IP read (last digit): "); 
     226  SPrint("Destination IP read (last digit): "); 
    226227  Serial.println(ip[3], DEC); 
    227228 
     
    257258  while (getSn_SR(testSocket) == SOCK_ESTABLISHED) { 
    258259   while (getSn_RX_RSR(testSocket) > 0) { 
    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); 
     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   } 
    297290     
    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      
     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   
    314315   } 
    315316   sendBanner(bytesToSend, ledState); 
     
    317318   break; 
    318319 } 
    319  
     320   
     321  close(testSocket); 
    320322  disconnect(testSocket); 
    321   close(testSocket); 
    322                      
    323   Serial.println("End test W5100 socket...");         
    324  
    325    
    326    
    327 } 
    328  
     323           
     324  SPrintln("End test W5100 socket...");     
     325 
     326   
     327   
     328} 
     329