Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <built_in.h>
- // duplex config flags
- #define Spi_Ethernet_HALFDUPLEX 0x00
- #define Spi_Ethernet_FULLDUPLEX 0x01
- // mE ehternet NIC pinout
- sfr sbit SPI_Ethernet_Rst at GPIOE_ODR.B7;
- sfr sbit SPI_Ethernet_CS at GPIOE_ODR.B8;
- // TCP flags
- typedef struct {
- unsigned canCloseTCP: 1; // flag which closes TCP socket (not relevant to UDP)
- unsigned isBroadcast: 1; // flag which denotes that the IP package has been received via subnet broadcast address
- } TEthPktFlags;
- //ROM constant strings
- const unsigned char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: "; // HTTP header
- const unsigned char httpMimeTypeHTML[] = "text/html\n\n"; // HTML MIME type
- const unsigned char httpMimeTypeScript[] = "text/plain\n\n"; // TEXT MIME type
- unsigned char httpMethod[] = "GET /";
- /*
- * web page, splited into 2 parts :
- * when coming short of ROM, fragmented data is handled more efficiently by linker
- *
- * this HTML page calls the boards to get its status, and builds itself with javascript
- */
- const code char *indexPage = // Change the IP address of the page to be refreshed
- "<meta http-equiv=\"refresh\" content=\"3;url=http://192.168.0.107\">\
- <HTML><HEAD></HEAD><BODY>\
- <h1><center>ROV KONTROLA</h1> <center> \
- <center>\
- <a >Reload</a>\
- <script src=/s></script>\
- <table><tr><td valign=top><table border=1 style=\"font-size:20px ;font-family: terminal ;\">\
- </table></td><td><table border=1 style=\"font-size:20px ;font-family: terminal ;\">\
- <tr><th colspan=2></th></tr>\
- <script>\
- var str,i;\
- str=\"\";\
- for(i=0;i<8;i++)\
- {str+=\"<tr><td bgcolor=RED>SENZOR #\"+i+\"</td>\";\
- if(PORTD&(1<<i)){str+=\"<td bgcolor=red>ON\";}\
- else {str+=\"<td bgcolor=#cccccc>OFF\";}\
- str+=\"</td></tr>\";}\
- document.write(str) ;\
- function salji() { \
- //socket= new WebSocket('192.168.0.107:12345'); \
- socket.onopen= function() { \
- socket.send('hello'); \
- };\
- socket.onmessage= function(s) {\
- alert(s); \
- }; \
- } \
- </script>\
- " ;
- const code char *indexPage2 = "</table></td><td>\
- <table border=1 style=\"font-size:20px ;font-family: terminal ;\">\
- <tr><th colspan=3>LED</th></tr>\
- <script>\
- var str,i;\
- str=\"\";\
- for(i=1;i<8;i++)\
- {str+=\"<tr><td bgcolor=RED>LED\"+i+\"</td>\";\
- if(PORTE&(1<<i)){str+=\"<td bgcolor=GREEN>ON\";}\
- else {str+=\"<td onclick=""salji()"" bgcolor=RED>OFF\";}\
- str+=\"</td><td><a href=/t\"+i+\">AKTIVIRAJ</a></td></tr>\";}\
- document.write(str) ;\
- </script>\
- </table></td></tr></table>\
- " ;
- // RAM variables
- unsigned char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3f}; // my MAC address
- unsigned char myIpAddr[4] = {192, 168, 0, 107 }; // my IP address
- unsigned char gwIpAddr[4] = {192, 168, 20, 6 }; // gateway (router) IP address
- unsigned char ipMask[4] = {255, 255, 255, 0 }; // network mask (for example : 255.255.255.0)
- unsigned char dnsIpAddr[4] = {192, 168, 20, 1 }; // DNS server IP address
- unsigned char getRequest[15]; // HTTP request buffer
- unsigned char dyna[30]; // buffer for dynamic response
- unsigned long httpCounter = 0; // counter of HTTP requests
- #define putConstString SPI_Ethernet_putConstString
- #define putString SPI_Ethernet_putString
- unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort,
- unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags) {
- unsigned int len = 0 ; // my reply length
- unsigned int i ; // general purpose integer
- // should we close tcp socket after response is sent?
- // library closes tcp socket by default if canClose flag is not reset here
- // flags->canClose = 0; // 0 - do not close socket
- // otherwise - close socket
- if (localPort == 12345){
- for (i=0; i<reqLength;i++)
- getRequest[i] = SPI_Ethernet_getByte();
- len = putConstString("BACAM BURE!\n");
- len += putString(getRequest);
- return(len);
- }
- 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(i = 0 ; i < 10 ; i++) {
- getRequest[i] = SPI_Ethernet_getByte() ;
- }
- getRequest[i] = 0 ;
- i = 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 = putConstString(httpHeader) ; // HTTP header
- len += putConstString(httpMimeTypeScript); // with text MIME type
- // add AN2 value to reply
- IntToStr(ADC1_Get_Sample(3), dyna) ;
- len += putConstString("var AN3=") ;
- len += putString(dyna) ;
- len += putConstString(";") ;
- // add PORTB value (buttons) to reply
- len += putConstString("var PORTD=") ;
- IntToStr(GPIOD_IDR, dyna) ;
- len += putString(dyna) ;
- len += putConstString(";") ;
- // add PORTD value (LEDs) to reply
- len += putConstString("var PORTE=") ;
- WordToStr(GPIOE_ODR >> 8, dyna) ;
- len += putString(dyna) ;
- len += putConstString(";") ;
- // add HTTP requests counter to reply
- IntToStr(httpCounter, dyna) ;
- len += putConstString("var REQ=") ;
- len += putString(dyna) ;
- len += putConstString(";") ;
- }
- else if(getRequest[5] == 't') { // if request path name starts with t, toggle PORTD (LED) bit number that comes after
- unsigned long bitMask = 0 ; // for bit mask
- if(isdigit(getRequest[6])) { // if 0 <= bit number <= 9, bits 8 & 9 does not exist but does not matter
- bitMask = getRequest[6] - '0' ; // convert ASCII to integer
- bitMask = 1 << (bitMask + 8); // create bit mask
- GPIOE_ODR ^= bitMask ; // toggle PORTD with xor operator
- }
- }
- if(len == 0) { // what do to by default
- len = putConstString(httpHeader) ; // HTTP header
- len += putConstString(httpMimeTypeHTML); // with HTML MIME type
- len += putConstString(indexPage) ; // HTML page first part
- len += putConstString(indexPage2) ; // HTML page second part
- }
- return(len) ; // return to the library with the number of bytes to transmit
- }
- unsigned int SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort,
- unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags) {
- return 0; // back to the library with the length of the UDP reply
- }
- /**************************************************************************************************
- * MAIN PROGRAM
- **************************************************************************************************/
- void main(){
- GPIO_Digital_Input(&GPIOC_BASE, _GPIO_PINMASK_LOW);
- GPIO_Digital_Output(&GPIOE_BASE, _GPIO_PINMASK_HIGH);
- ADC1_Init();
- ADC_Set_Input_Channel(_ADC_CHANNEL_3);
- SPI3_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER | _SPI_8_BIT |
- _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
- _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
- &_GPIO_MODULE_SPI3_PC10_11_12);
- SPI_Ethernet_Init(myMacAddr, myIpAddr, Spi_Ethernet_FULLDUPLEX) ;
- while(1) { // do forever
- /*
- * if necessary, test the return value to get error code
- */
- SPI_Ethernet_doPacket() ; // process incoming Ethernet packets
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement