STM32F207 Ethernet problem with size of the page

mikroC, mikroBasic and mikroPascal for PRO ARM® MCUs, supporting STM32, Tiva, Kinetis, & CEC devices
Post Reply
Author
Message
antonp
Posts: 3
Joined: 05 Nov 2019 14:28

STM32F207 Ethernet problem with size of the page

#1 Post by antonp » 06 Apr 2020 12:22

Hello,

I've been using STM32F207 board (with LAN8720 chip) on a EasyMX PRO v7, especially the Ethernet_Demo example. I managed to get the demo running and then managed to rewrite it to suit my project.
After a while I discovered that the site cannot be open (but is seen on arp and it responds to ping) if it is to big (in my case not even that big, just a basic 5 x 11 table and some text).
So my questions are:
-Is there any limit on length of string that I can pass to "Ethernet_Intern_UserTCP" function?
-I've read that the size limit for package is around 1536 (it is not specified what, probably bits?). What happens if I go over the limit? Will the data be lost or will it be processe in the next "Ethernet_Intern_doPacket()" routine?

I am attaching my code for you to point out the bugs.

Thank you for your answers.

Code: Select all

#include        "__Ethernet_Intern.h"

int N = 0;
const int measurementsSaved = 1024;
const int measurementsShown = 10;


//Measured values
unsigned char co2[5];
unsigned char ch4[4];
unsigned char n2o[4];
unsigned char rh[5];
unsigned char temp[5];
unsigned char flow[4];


int co2ValueShown[measurementsShown];
int ch4ValueShown[measurementsShown];
int n2oValueShown[measurementsShown];

float rhValueShown[measurementsShown];
float tempValueShown[measurementsShown];
float flowValueShown[measurementsShown];

int co2ValueSaved[measurementsSaved];
int ch4ValueSaved[measurementsSaved];
int n2oValueSaved[measurementsSaved];

float rhValueSaved[measurementsSaved];
float tempValueSaved[measurementsSaved];
float flowValueSaved[measurementsSaved];


const code unsigned char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: ";  // HTTP header
const code unsigned char httpMimeTypeHTML[] = "text/html\n\n";              // HTML MIME type
const code unsigned char httpMimeTypeScript[] = "text/plain\n\n";           // TEXT MIME type
unsigned char httpMethod[] = "GET /";

/*
 * this HTML page calls the boards to get its status, and builds itself with javascript
 */
 const code char *pageHeader = "<meta http-equiv=\"refresh\" content=\"3;url=http://192.168.1.60\">\
<HTML><HEAD></HEAD><BODY>\
 ";
 const code char *pageInfo = "<h1>Some info:</h1>\
 <p>Dev: Someone</p>\
 <p>Email: someones.email@emailofsomeone.si</p>\
 <a href=/>Reload</a>\
<script src=/s></script>\
 ";

const code char *pageMeasurement1 = "<table border=1 style=font-size:20px ;font-family: terminal>\
    <tr>\
            <th>Measurement</th>\
            <script>\
                    for(var i = 1; i < 11; i++){\
                            var str = \"<td> <strong>#\" +i+ \"</strong></td>\";\
                            document.write(str);\
                    }\
            </script>\
    </tr>\
    <tr>\
            <td><strong>CO<sub>2</sub> [ppm]</strong></td>\
            <script>\
                    for (var i = 0; i < 10; i++) {\
                            var str =\"<td>\"+CO2[i]+\"</td>\";\
                            document.write(str);\
                    }\
            </script>\
    </tr>\
    <tr>\
            <td><strong>CH<sub>4</sub> [ppm]</strong></td>\
            <script>\
                    for (var i = 0; i < 10; i++) {\
                            var str =\"<td>\"+CH4[i]+\"</td>\";\
                            document.write(str);\
                    }\
            </script>\
    </tr>\
    <tr>\
            <td><strong>N<sub>2</sub>O [ppm]</strong></td>\
            <script>\
                    for (var i = 0; i < 10; i++) {\
                            var str =\"<td>\"+N2O[i]+\"</td>\";\
                            document.write(str);\
                    }\
            </script>\
    </tr>\
        <tr>\
            <td><strong>N<sub>2</sub>O [ppm]</strong></td>\
            <script>\
                    for (var i = 0; i < 10; i++) {\
                            var str =\"<td>\"+N2O[i]+\"</td>\";\
                            document.write(str);\
                    }\
            </script>\
    </tr>\
</table>\
";


const code char *pageFooter = "This is HTTP request #<script>document.write(REQ)</script></BODY></HTML>\
";

/***********************************
 * RAM variables
 */
unsigned char   myMacAddr[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x01};   // my MAC address
unsigned char   myIpAddr[4]  = {192, 168,  1, 60 };                   // my IP address
unsigned char   gwIpAddr[4]  = {192, 168,  1, 254};                   // gateway (router) IP address
unsigned char   ipMask[4]    = {255, 255, 255,  0 };                   // network mask (for example : 255.255.255.0)
unsigned char   dnsIpAddr[4] = {8, 8,  8, 8 };                   // DNS server IP address

unsigned char   getRequest[15];                                        // HTTP request buffer
unsigned char   dyna[31] ;                                             // buffer for dynamic response
unsigned long   httpCounter = 0;                                       // counter of HTTP requests

unsigned char   remoteIpAddr[4]  = {192, 168,  1, 61 };                   // my IP address

/*******************************************
 * functions
 */

/*
 * this function is called by the library
 * the user accesses to the HTTP request by successive calls to Ethernet_Intern_getByte()
 * the user puts data in the transmit buffer by successive calls to Ethernet_Intern_putByte()
 * the function must return the length in bytes of the HTTP reply, or 0 if nothing to transmit
 *
 * if you don't need to reply to HTTP requests,
 * just define this function with a return(0) as single statement
 *
 */
 int k = 0;
unsigned int    Ethernet_Intern_UserTCP(unsigned char *remoteHost, unsigned int remotePort, unsigned int localPort, unsigned int reqLength, TEthInternPktFlags *flags)
        {
        unsigned int    len;            // my reply length
        unsigned int    len2;
        char tmp;
        // should we close tcp socket after response is sent?
        // library closes tcp socket by default if canCloseTCP flag is not reset here
        // flags->canCloseTCP = 0; // 0 - do not close socket
                                   // otherwise - close socke

        if(localPort != 80)                         // I listen only to web request on port 80
                {
                return(0);
                }


        // get 10 first bytes only of the request, the rest does not matter here
        for(len = 0; len < 10; len++)
        {
        getRequest[len] = Ethernet_Intern_getByte();
        }
        getRequest[len] = 0;
        len = 0;

        if(memcmp(getRequest, httpMethod, 5))       // only GET method is supported here
                {
                return(0);
                }

        httpCounter++;                             // one more request done

        if(getRequest[5] == 's')                    // if request path name starts with s, store dynamic data in transmit buffer
                {
                // the text string replied by this request can be interpreted as javascript statements
                // by browsers

                len = Ethernet_Intern_putConstString(httpHeader);              // HTTP header
                len += Ethernet_Intern_putConstString(httpMimeTypeScript);     // with text MIME type

                // add CO2 to reply
                len += Ethernet_Intern_putConstString("var CO2=[") ;

                for(k = 0; k< measurementsShown; k++){
                    if(k < measurementsShown - 1)
                    {
                        sprintf(dyna,"'%d',",co2ValueShown[k]);
                    }else
                    {
                        sprintf(dyna,"'%d'",co2ValueShown[k]);
                    }
                    len += Ethernet_Intern_putString(dyna) ;
                }
                len += Ethernet_Intern_putString("]") ;
                len += Ethernet_Intern_putConstString(";") ;
                
                 // add CH4 to reply
                //WordToStr(ch4Value[0], dyna) ;
                len += Ethernet_Intern_putConstString("var CH4=[") ;
                
                for(k = 0; k< measurementsShown; k++){
                    if(k < measurementsShown - 1)
                    {
                        sprintf(dyna,"'%d',",ch4ValueShown[k]);
                    }else
                    {
                        sprintf(dyna,"'%d'",ch4ValueShown[k]);
                    }
                    len += Ethernet_Intern_putString(dyna) ;
                }
                len += Ethernet_Intern_putString("]") ;
                len += Ethernet_Intern_putConstString(";") ;
                
                 // add N2O to reply
                //WordToStr(n2oValue[0], dyna) ;
                len += Ethernet_Intern_putConstString("var N2O=[") ;
                
                for(k = 0; k< measurementsShown; k++){
                    if(k < measurementsShown - 1)
                    {
                        sprintf(dyna,"'%d',",n2oValueShown[k]);
                    }else
                    {
                        sprintf(dyna,"'%d'",n2oValueShown[k]);
                    }
                    len += Ethernet_Intern_putString(dyna) ;
                }
                len += Ethernet_Intern_putString("]") ;
                
                len += Ethernet_Intern_putConstString(";") ;
                
                // add FLOW to reply
                //FloatToStr(flowValue[0], dyna) ;
                //sprintf(dyna,"%4.2f",flowValueShown[0]);
                len +=Ethernet_Intern_putConstString("var FLOW=[") ;
                for(k = 0; k< measurementsShown; k++){
                    if(k < measurementsShown - 1)
                    {
                        sprintf(dyna,"'%4.2f',",flowValueShown[k]);
                    }else
                    {
                        sprintf(dyna,"'%4.2f'",flowValueShown[k]);
                    }
                    len += Ethernet_Intern_putString(dyna) ;
                }
                len += Ethernet_Intern_putString("]") ;
                len += Ethernet_Intern_putConstString(";") ;
                
                // add RH to reply
                //sprintf(dyna,"%4.2f",rhValueShown[0]);
                len += Ethernet_Intern_putConstString("var RH=[") ;
                for(k = 0; k< measurementsShown; k++){
                    if(k < measurementsShown - 1)
                    {
                        sprintf(dyna,"'%4.2f',",rhValueShown[k]);
                    }else
                    {
                        sprintf(dyna,"'%4.2f'",rhValueShown[k]);
                    }
                    len += Ethernet_Intern_putString(dyna) ;
                }
                len += Ethernet_Intern_putString("]") ;
                len += Ethernet_Intern_putConstString(";") ;
                
                // add TEMPERATURE to reply
                //sprintf(dyna,"%4.2f",tempValueShown[0]);
                len += Ethernet_Intern_putConstString("var TEMP=[") ;
                for(k = 0; k< measurementsShown; k++){
                    if(k < measurementsShown - 1)
                    {
                        sprintf(dyna,"'%4.2f',",tempValueShown[k]);
                    }else
                    {
                        sprintf(dyna,"'%4.2f'",tempValueShown[k]);
                    }
                    len += Ethernet_Intern_putString(dyna) ;
                }
                len += Ethernet_Intern_putString("]") ;
                len += Ethernet_Intern_putConstString(";") ;
                


                // add HTTP requests counter to reply
                WordToStr(httpCounter, dyna);
                len += Ethernet_Intern_putConstString("var REQ=");
                len += Ethernet_Intern_putString(dyna);
                len += Ethernet_Intern_putConstString(";");
                }

        if(len == 0)                                            // what do to by default
                {
                len =  Ethernet_Intern_putConstString(httpHeader);             // HTTP header
                len += Ethernet_Intern_putConstString(httpMimeTypeHTML);       // with HTML MIME type
                len += Ethernet_Intern_putConstString(pageHeader);              // HTML page first part          // HTML page first part
                len += Ethernet_Intern_putConstString(pageInfo);
                len += Ethernet_Intern_putConstString(pageMeasurement1);
                len += Ethernet_Intern_putConstString(pageFooter);
                }

        return(len);                                           // return to the library with the number of bytes to transmit
        }

unsigned int    Ethernet_Intern_UserUDP(unsigned char *remoteHost, unsigned int remotePort, unsigned int destPort, unsigned int reqLength, TEthInternPktFlags *flags) {
        unsigned int    len;                           // my reply length
        char tmp;

        if(destPort != 10001)
          return 0;
        // reply is made of the remote host IP address in human readable format
        ByteToStr(remoteHost[0], dyna);                // first IP address byte
        dyna[3] = '.';
        ByteToStr(remoteHost[1], dyna + 4);            // second
        dyna[7] = '.';
        ByteToStr(remoteHost[2], dyna + 8);            // third
        dyna[11] = '.';
        ByteToStr(remoteHost[3], dyna + 12);           // fourth

        dyna[15] = ':';                                // add separator

        // then remote host port number
        WordToStr(remotePort, dyna + 16);
        dyna[21] = '[';
        WordToStr(destPort, dyna + 22);
        dyna[27] = ']';
        dyna[28] = 0;

        // the total length of the request is the length of the dynamic string plus the text of the request
        len = 28 + reqLength; //28
        
        // puts the dynamic string into the transmit buffer
        Ethernet_Intern_putBytes(dyna, 28);

        // then puts the request string converted into upper char into the transmit buffer
        while(reqLength--)
                {
                tmp = Ethernet_Intern_getByte();
                Ethernet_Intern_putByte(toupper(tmp));
                }

        return(len);           // back to the library with the length of the UDP reply
}

void main() {
  
  //Init ETHERNET
  Ethernet_Intern_Init(myMacAddr, myIpAddr, _ETHERNET_AUTO_NEGOTIATION, &_GPIO_MODULE_ETHERNET);
  Ethernet_Intern_confNetwork(ipMask, gwIpAddr, dnsIpAddr);



  while(1) {
    /*
     * if necessary, test the return value to get error code
     */
     
    Ethernet_Intern_doPacket();   // process incoming Ethernet packets

    /*
     * add your stuff here if needed
     * Ethernet_Intern_doPacket() must be called as often as possible
     * otherwise packets could be lost
     */
     }


User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: STM32F207 Ethernet problem with size of the page

#2 Post by stefan.filipovic » 07 Apr 2020 08:05

Hi,

The maximum packet size is set to 1536 in the Ethernet library from the System Libraries.
I suggest you use the Network Ethernet library instead. Here is the download link.
In this library, the user can define the size of the Tx buffer which is used in session handling. The only restriction is that the buffer size must be the power of 2 (1, 2, 4, ... 512, ...).

You can find the TCP_TX_SIZE_Intern constant in the __Lib_NetEthInternal_Defs_STM32.c file on the following path:
c:\Users\Public\Documents\Mikroelektronika\mikroC PRO for ARM\Packages\Network_Ethernet_ARM\Uses\

Kind regards,
Stefan Filipović

antonp
Posts: 3
Joined: 05 Nov 2019 14:28

Re: STM32F207 Ethernet problem with size of the page

#3 Post by antonp » 07 Apr 2020 11:14

Hi,

thank you I tried it and I think it is what I need.

Post Reply

Return to “ARM PRO Compilers”