ublo
bogdan's (micro)blog

bogdanel » pic32-pinguino-otg enc28j60 example

11:10 pm on Nov 1, 2012 | #more | tags:

for a week or so i'm searching the internet for a simple example (that can be easily expanded) on how to use the pic32-pinguino-otg with a UEXT connected enc28j60 module (both from olimex). and of course i didn't wanted to use the "universal" microchip tcp/ip stack that requires MPlab. after digging quite a lot trough the pinguino repositories, i managed to compile a set of working header files (which you can find here) and a small program from which you can ping your boards. for the library to work, copy it under %pinguino/p32/include/pinguino/libraries/ethernet, where %pinguino is the path to your pinguino installation folder. i used pinguino X.3 and it compiled fine.

/*----------------------------------------------------- 
Author:  --<>
Date: 28/Oct/2012
Description:

-----------------------------------------------------*/
#include <ethernet/ip_arp_udp.h>
#define BUFFER_MAX    224

static u8 buf[BUFFER_MAX+1]; // the received message
u16 len; // the length of the received messages

void setup() {
	// put your setup code here, to run once:
	// this is the enc28j60 ip address
	u8 myip[4] = { 192, 168, 2, 2 };
	// this is the enc28j60 mac address
	u8 mymac[6] = { 0x02, 0x04, 0x08, 0x10, 0x12, 0x14 };
	init_ip_arp_udp (mymac, myip);
	enc28j60Init (mymac);
	}

void loop() {
	len = enc28j60PacketReceive(BUFFER_MAX, buf);
	CDC.printf("received! len: %d\n", len);
    
	if (len == 0) {
		return;
		}
	if(eth_type_is_arp_and_my_ip(buf, len)) {
		make_arp_answer_from_request(buf, len);
		return;
		}
	if(eth_type_is_ip_and_my_ip(buf, len)==0) {
                return;
                }

	if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V) {
		// the ping reply
		make_echo_reply_from_request(buf, len);
		return;
		}
	}
Creampie