/* PL155-P controller 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 SAFE_LEN 402 // for 40ms timeout #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; /* Board control */ #define RPlength 652 #define MCA 0 #define LU 2 #define MC l #define MB O #define Reset 0 #define Write 1 #define Read 2 /***************************************/ /* 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); } } void WritePort(unsigned char *ComPtr) { unsigned char *Start; int i, ind; int LL = strlen(ComPtr); int Dsize; unsigned char CC; /* allow 50 ms settling time */ usleep(50000); /* reset pointer to beginning of the ethernet data field */ eth_data = buffer + ETH_HLEN; Start = eth_data; /* Loop over characters in the command */ for (ind=0; ind>i) & 0x01); *(eth_data++) = 0x01; // Stop bit *(eth_data++) = 0x01; // Safety delay } /* end for(LL) */ Dsize = (int)(eth_data - Start); if (Dsize