/* Temperature logger based on PIC18F4550 via ethernet using */ /* Spartan 3E board. /* Copyright I.Tsurin Liverpool University V1.0 (30.11.2011) */ /* */ /* Acknowledgment: Paulo Pedreiras, Out.2005 RCAI 05/06 */ /* http://www.ieeta.pt/~pedreiras/resources/rcai/raw-sr.c */ /* Includes */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include // the L2 protocols #include #define ETH_SAFE_LEN (ETH_DATA_LEN - 100) #define DEVICE "eth0" int s = 0; // Socket descriptor unsigned char *eth_head; // byte pointer to ethenet header unsigned char *eth_data; // byte pointer to ethernet data field struct ethhdr *eh; // structured pointer to ethernet header struct sockaddr_ll socket_address; void *buffer = NULL; /* Local host NIC MAC address */ unsigned char src_mac[6] = {0x00, 0x1D, 0x09, 0xB1, 0x46, 0x6F}; /* Front end address template */ unsigned char dest_mac[6] = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00}; void sigint(int signum); int eth_raw_init(); unsigned char l=1; unsigned char O=0; unsigned ePackID; unsigned eCommID; #define MCA 0 #define LU 2 #define MC l #define MB O #define Reset 0 #define Write 1 #define Read 2 #define Nt 13 /***************************************/ /* Ethernet packet sender */ /***************************************/ void PING(int Device, int Address, int Command, int Dsize) { int i; ePackID = (rand()&0x7fff) | ((rand()&0x7fff)<<15); /* Re-build ethernet header */ dest_mac[0] = ( MCA<<4 | Device<<3 | LU)&0xFF; dest_mac[1] = (Address<<4 | Command)&0xFF; dest_mac[2] = (ePackID>>24)&0xFF; dest_mac[3] = (ePackID>>16)&0xFF; dest_mac[4] = (ePackID>>8)&0xFF; dest_mac[5] = (ePackID>>0)&0xFF; eCommID = dest_mac[0]<<8 | (dest_mac[1]&0xF3); memcpy((void *)eh->h_dest, (void*)dest_mac, ETH_ALEN); memcpy((void *)eh->h_source,(void*)src_mac, ETH_ALEN); /* Update data size */ eh->h_proto = htons(Dsize); /* Output data string */ i = sendto(s, buffer, (Dsize + ETHER_HDR_LEN), 0, ((struct sockaddr*)&socket_address), sizeof(socket_address)); if (i == -1) { perror("sendto():"); exit(1); } } /***************************************/ /* Ethernet packet receiver */ /***************************************/ int PONG(int Dsize) { int i; int TimeOut = 100; while(TimeOut) { unsigned CommID; unsigned PackID; /* Read NC buffer */ i = recvfrom(s, buffer, ETHER_MAX_LEN, 0, NULL, NULL); // Dsize (?) if (i == -1) { perror("recfrom():"); exit(1); } CommID = eh->h_source[0]<<8 | (eh->h_source[1]&0xF3); PackID = eh->h_source[2]<<24 | eh->h_source[3]<<16 | eh->h_source[4]<<8 | eh->h_source[5]; if ((CommID == eCommID)&&(PackID == ePackID)) break; TimeOut--; } /* end while(TimeOut) */ return TimeOut; } /***************************************/ /* main module */ /* */ /* send configuration to motherboard N */ /***************************************/ int main(int argc, char **argv) { int ind; /* Create socket */ eth_raw_init(); buffer = (void*)malloc(ETHER_MAX_LEN); /* set structurised pointer to beginning of ethernet header */ eth_head = buffer; eh = (struct ethhdr *)eth_head; /* set pointer to beginning of the ethernet data field */ eth_data = buffer + ETH_HLEN; while(1) //for (ind=0; ind<100; ind++) { int i; unsigned char *Wpt; int Tmp[Nt]; int Temp[Nt]; for (i=0; i