static void _eth_spi_configure() { DBG(D_ETH, D_DEBUG, "ethspi spigen init\n"); SPI_DEV_GEN_init( &_enc28j60_spi_dev, SPIDEV_CONFIG_CPHA_1E | SPIDEV_CONFIG_CPOL_LO | SPIDEV_CONFIG_FBIT_MSB | SPIDEV_CONFIG_SPEED_9M, _SPI_BUS(1), SPI_ETH_GPIO_PORT, SPI_ETH_GPIO_PIN); DBG(D_ETH, D_DEBUG, "ethspi spigen open\n"); SPI_DEV_GEN_open(&_enc28j60_spi_dev); DBG(D_ETH, D_DEBUG, "ethspi enc28j60 init\n"); enc28j60Init(mac_address, EIE_PKTIE | EIE_TXIE | EIE_RXERIE | EIE_TXERIE); DBG(D_ETH, D_DEBUG, "ethspi enc28j60 init done, chip rev %02x\n", enc28j60getrev()); DBG(D_ETH, D_DEBUG, "ethspi mac readback: %02x:%02x:%02x:%02x:%02x:%02x\n", enc28j60Read(MAADR5), enc28j60Read(MAADR4), enc28j60Read(MAADR3), enc28j60Read(MAADR2), enc28j60Read(MAADR1), enc28j60Read(MAADR0) ); SYS_hardsleep_ms(20); enc28j60PhyWrite(PHLCON,0x476); SYS_hardsleep_ms(20); // init eth/ip layer init_ip_arp_udp_tcp(mac_address, ip_address, 80); DBG(D_ETH, D_INFO, "ethspi setup finished, ip %i.%i.%i.%i @ mac %02x.%02x.%02x.%02x.%02x.%02x\n", ip_address[0], ip_address[1], ip_address[2], ip_address[3], mac_address[0], mac_address[1], mac_address[2], mac_address[3], mac_address[4], mac_address[5]); }
/*----------------------------------------------------------*\ | MIAN ENTRY | \*----------------------------------------------------------*/ int main (void) { int rev = 0; stm32_Init (); // STM32 setup // GPIOD->ODR &= ~(1<<9);//GPIOA->BRR = ENC28J60_CS; // GPIOD->ODR |= 1<<9;//GPIOA->BSRR = ENC28J60_CS; printf ("SPI1_Init starting...\r\n"); SPI1_Init(); printf ("enc28j60 init...\r\n"); //enc28j60Init((unsigned char *)enc28j60_MAC); simple_server(); enc28j60Init((unsigned char *)enc28j60_MAC); rev = enc28j60getrev(); return rev; /* for(;;) { unsigned char c; printf ("Press a key. "); c = getchar (); printf ("\r\n"); printf ("You pressed '%c'.\r\n\r\n", c); } */ }
int main(void){ uint16_t dat_p; // set the clock speed to 8MHz // set the clock prescaler. First write CLKPCE to enable setting of clock the // next four instructions. CLKPR=(1<<CLKPCE); CLKPR=0; // 8 MHZ _delay_loop_1(0); // 60us //initialize the hardware driver for the enc28j60 enc28j60Init(mymac); //enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz _delay_loop_1(0); // 60us enc28j60PhyWrite(PHLCON,0x476); PORTB = 0xff; DDRB = 0xff; uart_init(); sei(); xfunc_out = (void (*)(char))uart_put; xputs(PSTR("AVR-Ethernet test monitor\n")); xprintf(PSTR("ENC28J60 Rev.%d\n"), enc28j60getrev()); //init the ethernet/ip layer: init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT); while(1){ // read packet, handle ping and wait for a tcp packet: dat_p=packetloop_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf)); /* dat_p will be unequal to zero if there is a valid * http get */ if(dat_p==0){ // no http request continue; } // tcp port 80 begin if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ // head, post and other methods: dat_p=http200ok(); dat_p=fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>")); goto SENDTCP; } // just one web page in the "root directory" of the web server if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){ dat_p=print_webpage(buf); goto SENDTCP; }else{ dat_p=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>")); goto SENDTCP; } SENDTCP: www_server_reply(buf,dat_p); // send web page data // tcp port 80 end } return (0); }
// prepare the webpage by writing the data to the tcp send buffer uint16_t print_webpage(uint8_t *buf,uint8_t rev) { char vstr[5]; uint16_t plen; plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")); plen=fill_tcp_data_p(buf,plen,PSTR("<center><p>ENC28J60 silicon rev is: B")); itoa((enc28j60getrev()),vstr,10); plen=fill_tcp_data(buf,plen,vstr); plen=fill_tcp_data_p(buf,plen,PSTR("</center><hr><br>tuxgraphics.org\n")); return(plen); }
int main(void){ uint16_t plen; uint16_t dat_p; //uint8_t cmd_pos=0; int8_t cmd; //uint8_t payloadlen=0; //char str[20]; //char cmdval; //char req[16]; //char i; DATA_DAIKIN daikin; // set the clock speed to "no pre-scaler" (8MHz with internal osc or // full external speed) // set the clock prescaler. First write CLKPCE to enable setting // of clock the next four instructions. // Note that the CKDIV8 Fuse determines the initial // value of the CKKPS bits. CLKPR=(1<<CLKPCE); // change enable CLKPR=0; // "no pre-scaler" _delay_loop_1(0); // 60us /*initialize enc28j60*/ enc28j60Init(mymac); //enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz _delay_loop_1(0); // 60us /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ // LEDB=yellow LEDA=green // // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10); enc28j60PhyWrite(PHLCON,0x476); //DDRB|= (1<<DDB1); // enable PB1, LED as output // the transistor on PD7: //DDRD|= (1<<DDD7); //PORTD &= ~(1<<PORTD7);// transistor off //PORTB = 0xff; //DDRB = 0xff; init_power_settings(); uart_init(); init_ir(); init_airController(&daikin); sei(); xfunc_out = (void (*)(char))uart_put; xputs(PSTR("AVR-Ethernet test monitor\n")); xprintf(PSTR("ENC28J60 Rev.%d\n"), enc28j60getrev()); xprintf(PSTR("PRR=%02X\n"),PRR); //init the web server ethernet/ip layer: init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT); while(1){ // handle ping and wait for a tcp packet plen=enc28j60PacketReceive(BUFFER_SIZE, buf); buf[BUFFER_SIZE]='\0'; dat_p=packetloop_icmp_tcp(buf,plen); if(dat_p==0){ // check if udp otherwise continue goto UDP; } // send data everytime we get a http request xprintf(PSTR("get http request\n")); if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ xprintf(PSTR("'GET \n'")); // head, post and other methods: // // for possible status codes see: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html plen=http200ok(); plen=fill_tcp_data_p(buf,plen,PSTR("<h1>200 OK</h1>")); goto SENDTCP; } if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){ plen=http200ok(); plen=fill_tcp_data_p(buf,plen,PSTR("<p>Usage: http://host_or_ip/pc or m</p>\n")); goto SENDTCP; } if ((strncmp("/pc ",(char *)&(buf[dat_p+4]),4)==0) || (strncmp("/pc/ ",(char *)&(buf[dat_p+4]),5)==0)){ plen=http200ok(); plen=fill_tcp_data_p(buf,plen,pc_html); goto SENDTCP; } if ((strncmp("/m ",(char *)&(buf[dat_p+4]),3)==0) || (strncmp("/m/ ",(char *)&(buf[dat_p+4]),4)==0)){ plen=http200ok(); plen=fill_tcp_data_p(buf,plen,mobile_html); goto SENDTCP; } // GET /ir?power=on&temp=28 if (strncmp("/ir?",(char *)&(buf[dat_p+4]),4)==0){ if(find_key_val( (char *)&(buf[dat_p+8]), gStrbuf, 5, "power" ) ) { xprintf(PSTR("power=%s\n"),gStrbuf); if(strncmp("on", gStrbuf, 2) == 0){ airController_on(&daikin); setData(DAIKIN, daikin.buf, 35*8); }else if(strncmp("off", gStrbuf, 3) == 0){ airController_off(&daikin); setData(DAIKIN, daikin.buf, 35*8); } } if(find_key_val( (char *)&(buf[dat_p+8]), gStrbuf, 4, "temp" ) ) { xprintf(PSTR("temp=%s\n"),gStrbuf); airController_setTemp(&daikin, atoi(gStrbuf)); setData(DAIKIN, daikin.buf, 35*8); } plen=http200ok(); plen=fill_tcp_data_p(buf,plen,PSTR("Done\n")); goto SENDTCP; } cmd=analyse_get_url((char *)&(buf[dat_p+4])); // for possible status codes see: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html if (cmd==-1){ plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>")); goto SENDTCP; } if (cmd==1){ //PORTD|= (1<<PORTD7);// transistor on } if (cmd==0){ //PORTD &= ~(1<<PORTD7);// transistor off } if (cmd==2){ // favicon: //plen=moved_perm(buf,0); goto SENDTCP; } if (cmd==-2){ // redirect to the right base url (e.g add a trailing slash): plen=moved_perm(buf,1); goto SENDTCP; } // if (cmd==-2) or any other value // just display the status: //plen=print_webpage(buf,/*(PORTD & (1<<PORTD7))*/0); // SENDTCP: www_server_reply(buf,plen); // send data continue; // tcp port www end // ------------------------------- // udp start, we listen on udp port 1200=0x4B0 UDP: // xprintf(PSTR("here is UDP:\n")); // check if ip packets are for us: if(eth_type_is_ip_and_my_ip(buf,plen)==0){ // xprintf(PSTR("here is eth_type_is_ip_andd_my_ip\n")); continue; } /*if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==(MYUDPPORT>>8)&&buf[UDP_DST_PORT_L_P]==(MYUDPPORT&0xff)){ payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; // you must sent a string starting with v // e.g udpcom version 10.0.0.24 if (verify_password((char *)&(buf[UDP_DATA_P]))){ // find the first comma which indicates // the start of a command: cmd_pos=0; while(cmd_pos<payloadlen){ cmd_pos++; if (buf[UDP_DATA_P+cmd_pos]==','){ cmd_pos++; // put on start of cmd break; } } // a command is one char and a value. At // least 3 characters long. It has an '=' on // position 2: if (cmd_pos<2 || cmd_pos>payloadlen-3 || buf[UDP_DATA_P+cmd_pos+1]!='='){ strcpy(str,"e=no_cmd"); goto ANSWER; } // supported commands are // t=1 t=0 t=? if (buf[UDP_DATA_P+cmd_pos]=='t'){ cmdval=buf[UDP_DATA_P+cmd_pos+2]; if(cmdval=='1'){ PORTD|= (1<<PORTD7);// transistor on strcpy(str,"t=1"); goto ANSWER; }else if(cmdval=='0'){ PORTD &= ~(1<<PORTD7);// transistor off strcpy(str,"t=0"); goto ANSWER; }else if(cmdval=='?'){ if (PORTD & (1<<PORTD7)){ strcpy(str,"t=1"); goto ANSWER; } strcpy(str,"t=0"); goto ANSWER; } } strcpy(str,"e=inv_cmd"); goto ANSWER; } strcpy(str,"e=inv_pw"); ANSWER: make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT); }*/ } return (0); }
int main(void){ uint16_t plen; uint16_t dat_p; uint8_t i=0; uint8_t payloadlen=0; char str[20]; // set the clock speed to 8MHz // set the clock prescaler. First write CLKPCE to enable setting of clock the // next four instructions. CLKPR=(1<<CLKPCE); CLKPR=0; // 8 MHZ _delay_loop_1(50); // 12ms /*initialize enc28j60*/ enc28j60Init(mymac); enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz _delay_loop_1(50); // 12ms // LED /* enable PB1, LED as output */ DDRB|= (1<<DDB1); /* set output to Vcc, LED off */ PORTB|= (1<<PORTB1); /* Magjack leds configuration, see enc28j60 datasheet, page 11 */ // LEDB=yellow LEDA=green // // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10); enc28j60PhyWrite(PHLCON,0x476); _delay_loop_1(50); // 12ms /* set output to GND, red LED on */ PORTB &= ~(1<<PORTB1); i=1; //init the ethernet/ip layer: init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT); while(1){ // get the next new packet: plen = enc28j60PacketReceive(BUFFER_SIZE, buf); /*plen will ne unequal to zero if there is a valid * packet (without crc error) */ if(plen==0){ continue; } // arp is broadcast if unknown but a host may also // verify the mac address by sending it to // a unicast address. if(eth_type_is_arp_and_my_ip(buf,plen)){ make_arp_answer_from_request(buf); continue; } // check if ip packets (icmp or udp) are for us: if(eth_type_is_ip_and_my_ip(buf,plen)==0){ continue; } if (i){ /* set output to Vcc, LED off */ PORTB|= (1<<PORTB1); i=0; }else{ /* set output to GND, LED on */ PORTB &= ~(1<<PORTB1); i=1; } if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){ // a ping packet, let's send pong make_echo_reply_from_request(buf,plen); continue; } // tcp port 80 start if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==MYWWWPORT){ if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){ make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack continue; } if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){ init_len_info(buf); // init some data structures // we can possibly have no data, just ack: dat_p=get_tcp_data_pointer(); if (dat_p==0){ if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){ // finack, answer with ack make_tcp_ack_from_any(buf); } // just an ack with no data, wait for next packet continue; } if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ // head, post and other methods: // // for possible status codes see: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>")); goto SENDTCP; } // any GET, any url: plen=print_webpage(buf,enc28j60getrev()); SENDTCP: make_tcp_ack_from_any(buf); // send ack for http get make_tcp_ack_with_data(buf,plen); // send data continue; } } // tcp port 80 end // // udp start, we listen on udp port 1200=0x4B0 if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==4&&buf[UDP_DST_PORT_L_P]==0xb0){ payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN; // you must sent a string starting with v // e.g udpcom version 10.0.0.24 if (buf[UDP_DATA_P]=='v' ){ str[0]='v'; str[1]='e'; str[2]='r'; str[3]='='; str[4]='B'; itoa((enc28j60getrev()),&(str[5]),10); make_udp_reply_from_request(buf,str,strnlen(str,10),MYUDPPORT); } } } return (0); }
uint8_t EtherShield::ES_enc28j60Revision(void) { return enc28j60getrev(); }