got rid of extra servers for eiger, converted to c and it works

This commit is contained in:
Maliakal Dhanya 2014-08-29 16:40:27 +02:00
parent 367bdd9c2f
commit 10ca7c10e8
6 changed files with 2893 additions and 0 deletions

View File

@ -0,0 +1,557 @@
/**
* @author Ian Johnson
* @version 1.0
*/
//return reversed 1 means good, 0 means failed
#include <stdio.h>
#include <stdlib.h>
#include "xfs_types.h"
#include "xparameters.h"
#include "Beb.h"
struct BebInfo beb_infos[10];
int bebInfoSize = 0;
struct LocalLinkInterface ll_beb_local,* ll_beb;
struct LocalLinkInterface ll_beb_new_memory_local,* ll_beb_new_memory;
struct udp_header_type udp_header;
int Beb_send_ndata;
unsigned int Beb_send_buffer_size;
unsigned int* Beb_send_data_raw;
unsigned int* Beb_send_data;
int Beb_recv_ndata;
unsigned int Beb_recv_buffer_size;
unsigned int* Beb_recv_data_raw;
unsigned int* Beb_recv_data;
short Beb_bit_mode;
void BebInfo_BebInfo(struct BebInfo* bebInfo, unsigned int beb_num){
bebInfo->beb_number=beb_num;
bebInfo->serial_address=0;
strcpy(bebInfo->src_mac_1GbE,"");
strcpy(bebInfo->src_mac_10GbE,"");
strcpy(bebInfo->src_ip_1GbE,"");
strcpy(bebInfo->src_ip_10GbE,"");
bebInfo->src_port_1GbE=bebInfo->src_port_10GbE=0;
}
int BebInfo_SetSerialAddress(struct BebInfo* bebInfo, unsigned int a){
//address pre shifted
if(a>0xff) return 0;
bebInfo->serial_address = 0x04000000 | ((a&0xff)<<16);
return 1;
}
int BebInfo_SetHeaderInfo(struct BebInfo* bebInfo, int ten_gig, char* src_mac, char* src_ip, unsigned int src_port){
if(ten_gig){ strcpy(bebInfo->src_mac_10GbE,src_mac); strcpy(bebInfo->src_ip_10GbE,src_ip); bebInfo->src_port_10GbE = src_port;}
else { strcpy(bebInfo->src_mac_1GbE,src_mac); strcpy(bebInfo->src_ip_1GbE,src_ip); bebInfo->src_port_1GbE = src_port;}
return 1;
}
unsigned int BebInfo_GetBebNumber(struct BebInfo* bebInfo) {return bebInfo->beb_number;}
unsigned int BebInfo_GetSerialAddress(struct BebInfo* bebInfo) {return bebInfo->serial_address;}
char* BebInfo_GetSrcMAC(struct BebInfo* bebInfo, int ten_gig) {return ten_gig ? bebInfo->src_mac_10GbE : bebInfo->src_mac_1GbE;}
char* BebInfo_GetSrcIP(struct BebInfo* bebInfo, int ten_gig) {return ten_gig ? bebInfo->src_ip_10GbE : bebInfo->src_ip_1GbE;}
unsigned int BebInfo_GetSrcPort(struct BebInfo* bebInfo, int ten_gig) {return ten_gig ? bebInfo->src_port_10GbE : bebInfo->src_port_1GbE;}
void BebInfo_Print(struct BebInfo* bebInfo){
printf("\t%d) Beb Info.:\n",bebInfo->beb_number);
printf("\t\tSerial Add: 0x%x\n",bebInfo->serial_address);
printf("\t\tMAC 1GbE: %s\n",bebInfo->src_mac_1GbE);
printf("\t\tIP 1GbE: %s\n",bebInfo->src_ip_1GbE);
printf("\t\tport 1GbE: %d\n",bebInfo->src_port_1GbE);
printf("\t\tMAC 10GbE: %s\n",bebInfo->src_mac_10GbE);
printf("\t\tIP 10GbE: %s\n",bebInfo->src_ip_10GbE);
printf("\t\tport 10GbE: %d\n",bebInfo->src_port_10GbE);
}
void Beb_Beb(int arg1){
Beb_send_ndata = 0;
Beb_send_buffer_size = 1026;
Beb_send_data_raw = malloc((Beb_send_buffer_size+1) * sizeof(unsigned int));
Beb_send_data = &Beb_send_data_raw[1];
Beb_recv_ndata = 0;
Beb_recv_buffer_size = 1026;
Beb_recv_data_raw = malloc((Beb_recv_buffer_size+1) * sizeof(unsigned int));
Beb_recv_data = &Beb_recv_data_raw[1];
udp_header= (struct udp_header_type){
{0x00, 0x50, 0xc5, 0xb2, 0xcb, 0x46}, // DST MAC
{0x00, 0x50, 0xc2, 0x46, 0xd9, 0x02}, // SRC MAC
{0x08, 0x00},
{0x45},
{0x00},
{0x00, 0x00},
{0x00, 0x00},
{0x40},
{0x00},
{0xff},
{0x11},
{0x00, 0x00},
{129, 205, 205, 128}, // Src IP
{129, 205, 205, 122}, // Dst IP
{0x0f, 0xa1},
{0x13, 0x89},
{0x00, 0x00}, //{0x00, 0x11},
{0x00, 0x00}
};
if(!Beb_InitBebInfos()) exit(1);
printf("Printing Beb infos:\n");
unsigned int i;
for(i=1;i<bebInfoSize;i++) BebInfo_Print(&beb_infos[i]);
printf("\n");
Beb_bit_mode = 4;
ll_beb = &ll_beb_local;
Local_LocalLinkInterface1(ll_beb,XPAR_PLB_LL_FIFO_AURORA_DUAL_CTRL_FEB_LEFT_BASEADDR);
Beb_SetByteOrder();
ll_beb_new_memory = &ll_beb_new_memory_local;
Local_LocalLinkInterface(ll_beb_new_memory);
if(!Local_InitNewMemory(ll_beb_new_memory,XPAR_PLB_LL_NEW_MEMORY, arg1))
printf("New Memory FAIL\n");
else
printf("New Memory OK\n");
}
void Beb_ClearBebInfos(){
unsigned int i;
//for(i=0;i<bebInfoSize;i++) free(beb_infos[i]);
bebInfoSize=0;
}
int Beb_InitBebInfos(){//file name at some point
Beb_ClearBebInfos();
struct BebInfo b0;
BebInfo_BebInfo(&b0,0);
if(BebInfo_SetSerialAddress(&b0,0xff)) { //all bebs for reset and possibly get request data?
beb_infos[bebInfoSize] = b0;
bebInfoSize++;
}
if(!Beb_ReadSetUpFromFile("/home/root/executables/setup_beb.txt")) return 0;
/*
//loop through file to fill vector.
BebInfo* b = new BebInfo(26);
b->SetSerialAddress(0); //0xc4000000
b->SetHeaderInfo(0,"00:50:c2:46:d9:34","129.129.205.78",42000 + 26); // 1 GbE, ip address can be acquire from the network "arp"
b->SetHeaderInfo(1,"00:50:c2:46:d9:35","10.0.26.1",52000 + 26); //10 GbE, everything calculable/setable
beb_infos.push_back(b);
*/
return Beb_CheckSourceStuffBebInfo();
}
int Beb_SetBebSrcHeaderInfos(unsigned int beb_number, int ten_gig, char* src_mac, char* src_ip,unsigned int src_port){
//so that the values can be reset externally for now....
unsigned int i = Beb_GetBebInfoIndex(beb_number);
if(!i){ printf("returning 000\n");return 0;} //i must be greater than 0, zero is the global send
BebInfo_SetHeaderInfo(&beb_infos[i],ten_gig,src_mac,src_ip,src_port);
printf("Printing Beb info number (%d) :\n",i);
BebInfo_Print(&beb_infos[i]);
printf("\n");
return 1;
}
int Beb_ReadSetUpFromFile(char* file_name){
char line[100];
char str[100];
int i0,i1;
char mac0[50],mac1[50],ip0[50],ip1[0];
FILE* fp = fopen(file_name, "r");
if( fp == NULL ){
perror("Error while opening the beb setup file.\n");
return 0;
}
printf("Setting up beb side of detector:\n");
while ( fgets (line , 255 , fp) != NULL ){
if(strlen(line)<=1)
continue;
sscanf (line, "%s", str);
if (str[0]=='#')
continue;
if(!strcmp(str,"add_beb")){
if( sscanf (line,"%s %d %d %s %s %s %s",str,&i0,&i1,mac0,ip0,mac1,ip1) < 7){
printf("Error adding beb from %s.\n",file_name);
exit(0);
}
printf ("Read: %s %d %d %s %s %s %s\n", str,i0,i1,mac0,ip0,mac1,ip1);
if(Beb_GetBebInfoIndex(i0)){
printf("Error adding beb from %s, beb number %d already added.\n",file_name,i0);
exit(0);
}
struct BebInfo b0;
BebInfo_BebInfo(&b0,i0);
BebInfo_SetSerialAddress(&b0,i1);
BebInfo_SetHeaderInfo(&b0,0,mac0,ip0,42000+i0);
BebInfo_SetHeaderInfo(&b0,1,mac1,ip1,52000+i0);
beb_infos[bebInfoSize] = b0;
bebInfoSize++;
}
}
fclose(fp);
return 1;
}
int Beb_CheckSourceStuffBebInfo(){
unsigned int i;
for(i=1;i<bebInfoSize;i++){ //header stuff always starts from 1
if(!Beb_SetHeaderData(BebInfo_GetBebNumber(&beb_infos[i]),0,"00:00:00:00:00:00","10.0.0.1",20000)||!Beb_SetHeaderData(BebInfo_GetBebNumber(&beb_infos[i]),1,"00:00:00:00:00:00","10.0.0.1",20000)){
printf("Error in BebInfo for module number %d.\n",BebInfo_GetBebNumber(&beb_infos[i]));
BebInfo_Print(&beb_infos[i]);
return 0;
}
}
return 1;
}
unsigned int Beb_GetBebInfoIndex(unsigned int beb_numb){
if(!beb_numb) return 0;
unsigned int i;
for(i=1;i<bebInfoSize;i++)
if(beb_numb==BebInfo_GetBebNumber(&beb_infos[i]))
return i;
return 0;
}
int Beb_WriteTo(unsigned int index){
if(index>=bebInfoSize){
printf("WriteTo index error.\n");
return 0;
}
Beb_send_data_raw[0] = 0x90000000 | BebInfo_GetSerialAddress(&beb_infos[index]);
if(Local_Write(ll_beb,4,Beb_send_data_raw)!=4) return 0;
Beb_send_data_raw[0] = 0xc0000000;
if((Beb_send_ndata+1)*4!=Local_Write(ll_beb,(Beb_send_ndata+1)*4,Beb_send_data_raw)) return 0;
return 1;
}
void Beb_SwapDataFun(int little_endian, unsigned int n, unsigned int *d){
unsigned int i;
if(little_endian) for(i=0;i<n;i++) d[i] = (((d[i]&0xff)<<24) | ((d[i]&0xff00)<<8) | ((d[i]&0xff0000)>>8) | ((d[i]&0xff000000)>>24)); //little_endian
else for(i=0;i<n;i++) d[i] = (((d[i]&0xffff)<<16) | ((d[i]&0xffff0000)>>16));
}
int Beb_SetByteOrder(){
Beb_send_data_raw[0] = 0x8fff0000;
if(Local_Write(ll_beb,4,Beb_send_data_raw)!=4) return 0;
while((Local_Read(ll_beb,Beb_recv_buffer_size*4,Beb_recv_data_raw)/4)>0) printf("\t) Cleanning buffer ...\n");
if(bebInfoSize<2) return 0;
Beb_send_ndata = 3;
Beb_send_data[0] = 0x000c0000;
Beb_send_data[1] = 0;
Beb_send_data[2] = 0;
Beb_WriteTo(0);
//using little endian for data, big endian not fully tested, swap on 16 bit boundary.
Beb_send_ndata = 3;
Beb_send_data[0] = 0x000c0000;
Beb_send_data[1] = 1;
Beb_send_data[2] = 0;
Beb_SwapDataFun(0,2,&(Beb_send_data[1]));
Beb_WriteTo(0);
printf("\tSetting Byte Order .............. ok\n");
return 1;
}
int Beb_SetUpUDPHeader(unsigned int beb_number, int ten_gig, unsigned int header_number, char* dst_mac, char* dst_ip, unsigned int dst_port){
unsigned int i = Beb_GetBebInfoIndex(beb_number);
if(!i) return 0; //i must be greater than 0, zero is the global send
Beb_send_ndata = 14;
Beb_send_data[0] = ten_gig ? 0x00020000 : 0x00010000; //write to fanout numbers 1 or 2
Beb_send_data[1] = ((header_number*8)<<16);
if(!Beb_SetHeaderData(beb_number,ten_gig,dst_mac,dst_ip,dst_port)) return 0;
Beb_SwapDataFun(1,12,&(Beb_send_data[2]));
if(!Beb_WriteTo(i)) return 0;
return 1;
}
int Beb_SetHeaderData(unsigned int beb_number, int ten_gig, char* dst_mac, char* dst_ip, unsigned int dst_port){
unsigned int i = Beb_GetBebInfoIndex(beb_number);
if(!i) return 0; //i must be greater than 0, zero is the global send
return Beb_SetHeaderData1(BebInfo_GetSrcMAC(&beb_infos[i],ten_gig),BebInfo_GetSrcIP(&beb_infos[i],ten_gig),BebInfo_GetSrcPort(&beb_infos[i],ten_gig),dst_mac,dst_ip,dst_port);
}
int Beb_SetHeaderData1(char* src_mac, char* src_ip, unsigned int src_port, char* dst_mac, char* dst_ip, unsigned int dst_port){
/* example header*/
//static unsigned int* word_ptr = new unsigned int [16];
/*static*/
/*
udp_header_type udp_header = {
{0x00, 0x50, 0xc5, 0xb2, 0xcb, 0x46}, // DST MAC
{0x00, 0x50, 0xc2, 0x46, 0xd9, 0x02}, // SRC MAC
{0x08, 0x00},
{0x45},
{0x00},
{0x00, 0x00},
{0x00, 0x00},
{0x40},
{0x00},
{0xff},
{0x11},
{0x00, 0x00},
{129, 205, 205, 128}, // Src IP
{129, 205, 205, 122}, // Dst IP
{0x0f, 0xa1},
{0x13, 0x89},
{0x00, 0x00}, //{0x00, 0x11},
{0x00, 0x00}
};
*/
if(!Beb_SetMAC(src_mac,&(udp_header.src_mac[0]))) return 0;
if(!Beb_SetIP(src_ip,&(udp_header.src_ip[0]))) return 0;
if(!Beb_SetPortNumber(src_port,&(udp_header.src_port[0]))) return 0;
if(!Beb_SetMAC(dst_mac,&(udp_header.dst_mac[0]))) return 0;
if(!Beb_SetIP(dst_ip,&(udp_header.dst_ip[0]))) return 0;
if(!Beb_SetPortNumber(dst_port,&(udp_header.dst_port[0]))) return 0;
Beb_AdjustIPChecksum(&udp_header);
unsigned int* base_ptr = (unsigned int *) &udp_header;
unsigned int num_words = ( sizeof(struct udp_header_type) + 3 ) / 4;
// for(unsigned int i=0; i<num_words; i++) word_ptr[i] = base_ptr[i];
// for(unsigned int i=num_words; i<16; i++) word_ptr[i] = 0;
// return word_ptr;
unsigned int i;
for(i=0; i<num_words; i++) Beb_send_data[i+2] = base_ptr[i];
for(i=num_words; i<16; i++) Beb_send_data[i+2] = 0;
return 1;
}
int Beb_SetMAC(char* mac, uint8_t* dst_ptr){
char macVal[50];strcpy(macVal,mac);
int i = 0;
char *pch = strtok (macVal,":");
while (pch != NULL){
if(strlen(pch)!=2){
printf("Error: in mac address -> %s\n",macVal);
return 0;
}
int itemp;
sscanf(pch,"%x",&itemp);
dst_ptr[i] = (u_int8_t)itemp;
pch = strtok (NULL, ":");
i++;
}
return 1;
}
int Beb_SetIP(char* ip, uint8_t* dst_ptr){
char ipVal[50];strcpy(ipVal,ip);
int i = 0;
char *pch = strtok (ipVal,".");
while (pch != NULL){
if(((i!=3) && ((strlen(pch)>3) || (strlen(pch)<1))) || ((i==3)&&((strlen(pch)<1) || (strlen(pch) > 3)))){
printf("Error: in ip address -> %s\n",ipVal);
return 0;
}
int itemp;
sscanf(pch,"%d",&itemp);
dst_ptr[i] = (u_int8_t)itemp;
pch = strtok (NULL, ".");
i++;
}
return 1;
}
int Beb_SetPortNumber(unsigned int port_number, uint8_t* dst_ptr){
dst_ptr[0] = (port_number >> 8) & 0xff ;
dst_ptr[1] = port_number & 0xff;
return 1;
}
void Beb_AdjustIPChecksum(struct udp_header_type *ip){
unsigned char *cptr = (unsigned char *) ip->ver_headerlen;
ip->ip_header_checksum[0] = 0;
ip->ip_header_checksum[1] = 0;
ip->total_length[0] = 0;
ip->total_length[1] = 28; // IP + UDP Header Length
// calc ip checksum
unsigned int ip_checksum = 0;
unsigned int i;
for(i=0; i<10; i++){
ip_checksum += ( (cptr[2*i] << 8) + (cptr[2*i + 1]) );
if (ip_checksum & 0x00010000) ip_checksum = (ip_checksum + 1) & 0x0000ffff;
}
ip->ip_header_checksum[0] = (ip_checksum >> 8) & 0xff ;
ip->ip_header_checksum[1] = ip_checksum & 0xff ;
}
int Beb_SendMultiReadRequest(unsigned int beb_number, unsigned int left_right, int ten_gig, unsigned int dst_number, unsigned int npackets, unsigned int packet_size, int stop_read_when_fifo_empty){
unsigned int i = Beb_GetBebInfoIndex(beb_number); //zero is the global send
Beb_send_ndata = 3;
if(left_right == 1) Beb_send_data[0] = 0x00040000;
else if(left_right == 2) Beb_send_data[0] = 0x00080000;
else if(left_right == 3) Beb_send_data[0] = 0x000c0000;
else return 0;
//packet_size/=2;
if(dst_number>0x3f) return 0;
if(packet_size>0x3ff) return 0;
if(npackets==0||npackets>0x100) return 0;
npackets--;
Beb_send_data[1] = 0x62000000 | (!stop_read_when_fifo_empty) << 27 | (ten_gig==1) << 24 | packet_size << 14 | dst_number << 8 | npackets;
Beb_send_data[2] = 0;
Beb_SwapDataFun(0,2,&(Beb_send_data[1]));
if(!Beb_WriteTo(i)) return 0;
return 1;
}
int Beb_SetUpTransferParameters(short the_bit_mode){
if(the_bit_mode!=4&&the_bit_mode!=8&&the_bit_mode!=16&&the_bit_mode!=32) return 0;
Beb_bit_mode = the_bit_mode;
//nimages = the_number_of_images;
// on_dst = 0;
return 1;
}
int Beb_RequestNImages(unsigned int beb_number, unsigned int left_right, int ten_gig, unsigned int dst_number, unsigned int nimages, int test_just_send_out_packets_no_wait){
if(dst_number>64) return 0;
unsigned int header_size = 4; //4*64 bits
unsigned int packet_size = ten_gig ? 0x200 : 0x80; // 4k or 1k packets
unsigned int npackets = ten_gig ? Beb_bit_mode*4 : Beb_bit_mode*16;
int in_two_requests = (!ten_gig&&Beb_bit_mode==32);
if(in_two_requests) npackets/=2;
printf("here: %d %d %d %d %d %d %d\n",beb_number,left_right,ten_gig,dst_number,nimages, header_size,test_just_send_out_packets_no_wait);
//printf("here: "<<beb_number<<","<<left_right<<","<<ten_gig<<","<<dst_number<<","<<1<<","<<header_size<<","<<test_just_send_out_packets_no_wait\n");
unsigned int i;
for(i=0;i<nimages;i++){
//header then data request
if(!Beb_SendMultiReadRequest(beb_number,left_right,ten_gig,dst_number,1,header_size,test_just_send_out_packets_no_wait) ||
!Beb_SendMultiReadRequest(beb_number,left_right,ten_gig,dst_number,npackets,packet_size,test_just_send_out_packets_no_wait) ||
(in_two_requests&&!Beb_SendMultiReadRequest(beb_number,left_right,ten_gig,dst_number,npackets,packet_size,test_just_send_out_packets_no_wait)))
return 0;
}
return 1;
}
int Beb_Test(unsigned int beb_number){
printf("Testing module number: %d\n",beb_number);
//int SetUpUDPHeader(unsigned int beb_number, int ten_gig, unsigned int header_number, string dst_mac, string dst_ip, unsigned int dst_port){
//SetUpUDPHeader(26,0,0,"60:fb:42:f4:e3:d2","129.129.205.186",22000);
unsigned int index = Beb_GetBebInfoIndex(beb_number);
if(!index){
printf("Error beb number (%d)not in list????\n",beb_number);
return 0;
}
unsigned int i;
for(i=0;i<64;i++){
if(!Beb_SetUpUDPHeader(beb_number,0,i,"60:fb:42:f4:e3:d2","129.129.205.186",22000+i)){
printf("Error setting up header table....\n");
return 0;
}
}
// SendMultiReadRequest(unsigned int beb_number, unsigned int left_right, int ten_gig, unsigned int dst_number, unsigned int npackets, unsigned int packet_size, int stop_read_when_fifo_empty=1);
for(i=0;i<64;i++){
if(!Beb_SendMultiReadRequest(beb_number,i%3+1,0,i,1,0,1)){
printf("Error requesting data....\n");
return 0;
}
}
return 1;
}

View File

@ -0,0 +1,334 @@
/**
* @author Ian Johnson
* @version 1.0
*/
/*#include <iostream>
#include <iomanip>*/
#include <stdio.h>
#include "xparameters.h"
#include "Feb.h"
Feb_Feb(){
Feb_nfebs = 0;
Feb_feb_numb = 0;
Feb_send_ndata = 0;
Feb_send_buffer_size = 1026;
Feb_send_data_raw = malloc((Feb_send_buffer_size+1)*sizeof(int));
Feb_send_data = &Feb_send_data_raw[1];
Feb_recv_ndata = 0;
Feb_recv_buffer_size = 1026;
Feb_recv_data_raw = malloc((Feb_recv_buffer_size+1)*sizeof(int));
Feb_recv_data = &Feb_recv_data_raw[1];
Local_LocalLinkInterface1(ll,XPAR_PLB_LL_FIFO_AURORA_DUAL_CTRL_FEB_RIGHT_BASEADDR);
}
/*
~Feb(){
delete ll;
if(feb_numb) delete [] feb_numb;
delete [] send_data_raw;
delete [] recv_data_raw;
}
*/
void Feb_SendCompleteFebList(unsigned int n,unsigned int* list){
unsigned int i;
if(Feb_feb_numb) free(Feb_feb_numb);
Feb_nfebs = n;
Feb_feb_numb = malloc(n*sizeof(unsigned int));
for(i=0;i<n;i++) Feb_feb_numb[i] = list[i];
}
int Feb_WriteTo(unsigned int ch){
if(ch>0xfff) return 0;
Feb_send_data_raw[0] = 0x90000000 | (ch<<16); //we
if(Local_Write(ll,4,Feb_send_data_raw)!=4) return 0;
Feb_send_data_raw[0] = 0xc0000000; //data
return 1;((Feb_send_ndata+1)*4==Local_Write(ll,(Feb_send_ndata+1)*4,Feb_send_data_raw));
}
int Feb_ReadFrom(unsigned int ch, unsigned int ntrys){
unsigned int t;
if(ch>=0xfff) return 0;
Feb_recv_data_raw[0] = 0xa0000000 | (ch<<16); //read data
Local_Write(ll,4,Feb_recv_data_raw);
usleep(20);
Feb_recv_ndata=-1;
for(t=0;t<ntrys;t++){
if((Feb_recv_ndata=Local_Read(ll,Feb_recv_buffer_size*4,Feb_recv_data_raw)/4)>0){
Feb_recv_ndata--;
break;
}
printf("\t Read try number: %d\n",t);
usleep(1000);
}
return (Feb_recv_ndata>=0);
}
void Feb_PrintData(){
int i;
printf("Sent data: %d\n",Feb_send_ndata);
for(i=0;i<Feb_send_ndata;i++) printf("\t%d)%d (0x%x)\n",i,Feb_send_data[i],Feb_send_data[i]);
printf("Receive data: %d\n",Feb_recv_ndata);
for(i=0;i<Feb_recv_ndata;i++) printf("\t%d)%d (0x%x)\n",i,Feb_recv_data[i],Feb_recv_data[i]);
printf("\n\n");
}
int Feb_CheckHeader(unsigned int valid_bit_mask, int print_error_info){
int header_returned_is_ok = (Feb_send_data[0] & valid_bit_mask)==(Feb_recv_data[0] & valid_bit_mask);
if(print_error_info && !header_returned_is_ok){
printf("Error: Command received not the same as command recieved.\n");
printf("\t\t Header sent: %d (0x%x) received: %d (0x%x)\n",Feb_send_data[0], Feb_send_data[0], Feb_recv_data[0], Feb_recv_data[0]);
if(Feb_send_ndata>1&&Feb_recv_ndata>1){
printf("\t\t Tail sent: %d (0x%x) receiver: %d (0x%x)\n",Feb_send_data[Feb_send_ndata-1],Feb_send_data[Feb_send_ndata-1],Feb_recv_data[Feb_recv_ndata-1],Feb_recv_data[Feb_recv_ndata-1]);
}else{
printf("Error printing tail, too little data nsent = 0x%x, nrecv = 0x%x.\n",Feb_send_ndata, Feb_recv_ndata);
}
Feb_PrintData();
}
return header_returned_is_ok;
}
int Feb_CheckTail(unsigned int valid_bit_mask){
if(Feb_send_ndata<=1&&Feb_recv_ndata<=1){
printf("Error checking tail, too little data nsent = %d, nrecv = %d.\n",Feb_send_ndata, Feb_recv_ndata);
return 0;
}
unsigned int the_tail = Feb_recv_data[Feb_recv_ndata-1]&valid_bit_mask;
if(the_tail!=0){
printf("Error returned in tail: 0x%x (%d)\n",the_tail,the_tail);
if(the_tail&0x10000000) printf("\t\tBusy flag address error.\n");
if(the_tail&0x20000000) printf("\t\tRead register address error.\n");
if(the_tail&0x40000000) printf("\t\tWrite register address error.\n");
if(the_tail&0x80000000) printf("\t\tBram number error.\n");
if(the_tail&0x08000000) printf("\t\tFifo to read from error.\n");
if(the_tail&0x3ff) printf("\t\tNumber of data send error.\n");
return 0; //error
}
return 1;
}
int Feb_CheckCommunication(){
Feb_send_data_raw[0] = 0x8fff0000; //rst-all serial coms and lls
if(Local_Write(ll,4,Feb_send_data_raw)!=4) return 0;
printf("CheckingCommunication ....\n");
while((Local_Read(ll,Feb_recv_buffer_size*4,Feb_recv_data_raw)/4)>0) printf("\t) Cleanning buffer ...\n");
return Feb_SetByteOrder();
}
int Feb_SetByteOrder(){
unsigned int i;
Feb_send_ndata = 2;
Feb_send_data[0] = 0; //header
Feb_send_data[1] = 0; //tail
unsigned int dst = 0xff;
for( i=0;i<Feb_nfebs;i++) dst = (dst | Feb_feb_numb[i]); //get sub dst bits (left right in this case)
int passed = Feb_WriteTo(dst);
for(i=0;i<Feb_nfebs;i++){
printf("\t%d) Set Byte Order .............. ",i);
unsigned int current_passed = Feb_ReadFrom(Feb_feb_numb[i],20)&&(Feb_recv_ndata==2)&&Feb_CheckHeader(0xffffffff,1);
if(current_passed) printf("passed.\n");
else printf("failed.\n");
passed&=current_passed;
}
printf("\n");
return passed;
}
/* feb_ needed
int Feb_CheckSubNumber(unsigned int Feb_sub_num){
if(sub_num>=nfebs){
cout<<"Error invalid sub number "<<sub_num<<" must be less than "<<nfebs<<"."<<endl;
return 0;
}
return 1;
}
int Feb_SetStartOnEndOnFebs(int sub_num_s, unsigned int& start_on, unsigned int& end_on){
// -1 means write to all
if(sub_num_s<=-2){
cout<<"Error bad subnumber "<<sub_num_s<<"."<<endl;
return 0;
}
start_on = sub_num_s!=-1 ? sub_num_s : 0;
end_on = sub_num_s!=-1 ? sub_num_s : nfebs - 1;
return Feb_CheckSubNumber(start_on);
}
*/
/*
int Feb_ReadRegister(unsigned int Feb_sub_num, unsigned int Feb_reg_num,unsigned int& Feb_value_read){
return Feb_ReadRegisters(Feb_sub_num,1,&Feb_reg_num,&Feb_value_read);
}
*/
int Feb_ReadRegister(unsigned int sub_num, unsigned int reg_num,unsigned int* value_read){
return Feb_ReadRegisters(sub_num,1,&reg_num,value_read);
}
int Feb_ReadRegisters(unsigned int sub_num, unsigned int nreads, unsigned int* reg_nums,unsigned int* values_read){
//here cout<<"Reading Register ...."<<endl;
unsigned int i;
nreads &= 0x3ff; //10 bits
if(!nreads||nreads>Feb_send_buffer_size-2) return 0;
Feb_send_ndata = nreads+2;
Feb_send_data[0] = 0x20000000 | nreads << 14; //cmd -> read "00" , nreads
for(i=0;i<nreads;i++) Feb_send_data[i+1]=reg_nums[i];
Feb_send_data[nreads+1] = 0; //tail
if(!Feb_WriteTo(sub_num)||!Feb_ReadFrom(sub_num,20)||Feb_recv_ndata!=(int)(nreads+2)||!Feb_CheckHeader(0xffffffff,1)||!Feb_CheckTail(0xffffffff)){
Feb_PrintData();
printf("Error reading register.\n");
return 0;
}
for(i=0;i<nreads;i++) values_read[i] = Feb_recv_data[i+1];
return 1;
}
int Feb_WriteRegister(unsigned int sub_num, unsigned int reg_num,unsigned int value, int wait_on, unsigned int wait_on_address){
return Feb_WriteRegisters(sub_num,1,&reg_num,&value,&wait_on,&wait_on_address);
}
int Feb_WriteRegisters(unsigned int sub_num, unsigned int nwrites, unsigned int* reg_nums, unsigned int* values, int* wait_ons, unsigned int* wait_on_addresses){
unsigned int i;
// sub_num == 0xfff means write to all
nwrites &= 0x3ff; //10 bits
if(!nwrites||nwrites>Feb_send_buffer_size-2) return 0;
//cout<<"Write register : "<<this<<" "<<s_num<<" "<<nwrites<<" "<<reg_nums<<" "<<values<<" "<<wait_ons<<" "<<wait_on_addresses<<endl;
Feb_send_ndata = 2*nwrites+2;
Feb_send_data[0] = 0x80000000 | nwrites << 14; //cmd -> write nwrites and how many
Feb_send_data[2*nwrites+1] = 0; //tail
for(i=0;i<nwrites;i++) Feb_send_data[2*i+1] = 0x3fff&reg_nums[i]; // register address data_in(13 downto 0)
for(i=0;i<nwrites;i++) Feb_send_data[2*i+2] = values[i]; // value is data_in(31 downto 0)
// wait on busy data(28), address of busy flag data(27 downto 14)
if(wait_ons&&wait_on_addresses) for(i=0;i<nwrites;i++) Feb_send_data[2*i+1] |= (wait_ons[i]<<28 | (0x3fff&wait_on_addresses[i])<<14);
if(!Feb_WriteTo(sub_num)){
printf("%d) Error writing register(s).\n",sub_num);
Feb_PrintData();
return 0;
}
int passed = 1;
unsigned int n = (sub_num&0xff)==0xff ? Feb_nfebs : 1;
unsigned int* nums = (sub_num&0xff)==0xff ? Feb_feb_numb : &sub_num;
for(i=0;i<n;i++){
if((sub_num&0xf00&(nums[i]))==0) continue;
if(!Feb_ReadFrom(nums[i],20)||Feb_recv_ndata!=2||!Feb_CheckHeader(0xffffffff,1)){
printf("%d) Error writing register(s) response.\n",nums[i]);
Feb_PrintData();
passed = 0;
}else{
passed = passed && Feb_CheckTail(0xffffffff);
}
}
return passed;
}
int Feb_WriteMemory(unsigned int sub_num, unsigned int mem_num, unsigned int start_address, unsigned int nwrites, unsigned int *values){
// -1 means write to all
unsigned int i;
mem_num &= 0x3f; //6 bits
start_address &= 0x3fff; //14 bits
nwrites &= 0x3ff; //10 bits
if(!nwrites||nwrites>Feb_send_buffer_size-2) return 0;
Feb_send_ndata = nwrites+2;
Feb_send_data[0] = 0xc0000000 | mem_num << 24 | nwrites << 14 | start_address; //cmd -> write to memory, nwrites, mem number, start address
Feb_send_data[nwrites+1] = 0; //tail
for(i=0;i<nwrites;i++) Feb_send_data[i+1] = values[i];
if(!Feb_WriteTo(sub_num)){
printf("%d) Error writing memory.\n",sub_num);
return 0;
}
int passed = 1;
unsigned int n = (sub_num&0xff)==0xff ? Feb_nfebs : 1;
unsigned int* nums = (sub_num&0xff)==0xff ? Feb_feb_numb : &sub_num;
for(i=0;i<n;i++){
if((sub_num&0xf00&(nums[i]))==0) continue;
if(!Feb_ReadFrom(nums[i],20)||Feb_recv_ndata!=2||!Feb_CheckHeader(0xffffffff,1)){
printf("%d) Error writing memory response. \n",nums[i]);
Feb_PrintData();
passed = 0;
}else{
passed = passed && Feb_CheckTail(0xffffffff);
}
}
// unsigned int n = sub_num==0xfff ? nfebs : 1;
// unsigned int* nums = sub_num==0xfff ? feb_numb : &sub_num;
// for(unsigned int i=0;i<n;i++){
return passed;
}
int Feb_Test(){//int sub_num_s, unsigned int mem_num, unsigned int start_address, unsigned int nwrites, unsigned int *values){
// -1 means write to all
unsigned int i;
unsigned int reg_nums[10]={0,1,2,3,1,2,3,1,2,3};
printf("Test\n\n\n\n");
unsigned int value = 0;
for(i=0;i<10;i++){
Feb_WriteRegister(0xfff,reg_nums[i%10],i,0,0);
Feb_ReadRegister(256,reg_nums[i%10],&value);
printf("%d %d\n",i,value);
Feb_ReadRegister(512,reg_nums[i%10],&value);
printf("%d %d\n",i,value);
Feb_WriteMemory(0xfff,0,0,10,reg_nums);
}
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,189 @@
/**
* @author Ian Johnson
* @version 1.0
*/
//#include <iostream>
//#include <iomanip>
//#include <unistd.h>
//#include <string.h>
//#include <sys/mman.h>
//#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "xparameters.h"
#include "FebInterface.h"
struct LocalLinkInterface ll_local,* ll;
unsigned int Feb_Interface_nfebs;
unsigned int* Feb_Interface_feb_numb;
int Feb_Interface_send_ndata;
unsigned int Feb_Interface_send_buffer_size;
unsigned int* Feb_Interface_send_data_raw;
unsigned int* Feb_Interface_send_data;
int Feb_Interface_recv_ndata;
unsigned int Feb_Interface_recv_buffer_size;
unsigned int* Feb_Interface_recv_data_raw;
unsigned int* Feb_Interface_recv_data;
void Feb_Interface_FebInterface(){
ll = &ll_local;
Feb_Interface_nfebs = 0;
Feb_Interface_feb_numb = 0;
Feb_Interface_send_ndata = 0;
Feb_Interface_send_buffer_size = 1026;
Feb_Interface_send_data_raw = malloc((Feb_Interface_send_buffer_size+1) * sizeof(unsigned int));
Feb_Interface_send_data = &Feb_Interface_send_data_raw[1];
Feb_Interface_recv_ndata = 0;
Feb_Interface_recv_buffer_size = 1026;
Feb_Interface_recv_data_raw = malloc((Feb_Interface_recv_buffer_size+1) * sizeof(unsigned int));
Feb_Interface_recv_data = &Feb_Interface_recv_data_raw[1];
Local_LocalLinkInterface1(ll,XPAR_PLB_LL_FIFO_AURORA_DUAL_CTRL_FEB_RIGHT_BASEADDR);
}
void Feb_Interface_SendCompleteList(unsigned int n,unsigned int* list){
unsigned int i;
if(Feb_Interface_feb_numb) free(Feb_Interface_feb_numb);
Feb_Interface_nfebs = n;
Feb_Interface_feb_numb = malloc(n * sizeof(unsigned int));
for(i=0;i<n;i++) Feb_Interface_feb_numb[i] = list[i];
}
int Feb_Interface_WriteTo(unsigned int ch){
if(ch>0xfff) return 0;
Feb_Interface_send_data_raw[0] = 0x8fff0000;
if(Local_Write(ll,4,Feb_Interface_send_data_raw)!=4) return 0;
Feb_Interface_send_data_raw[0] = 0x90000000 | (ch<<16);
if(Local_Write(ll,4,Feb_Interface_send_data_raw)!=4) return 0;
Feb_Interface_send_data_raw[0] = 0xc0000000;
return ((Feb_Interface_send_ndata+1)*4==Local_Write(ll,(Feb_Interface_send_ndata+1)*4,Feb_Interface_send_data_raw));
}
int Feb_Interface_ReadFrom(unsigned int ch, unsigned int ntrys){
unsigned int t;
if(ch>=0xfff) return 0;
Feb_Interface_recv_data_raw[0] = 0xa0000000 | (ch<<16);
Local_Write(ll,4,Feb_Interface_recv_data_raw);
usleep(20);
Feb_Interface_recv_ndata=-1;
for(t=0;t<ntrys;t++){
if((Feb_Interface_recv_ndata=Local_Read(ll,Feb_Interface_recv_buffer_size*4,Feb_Interface_recv_data_raw)/4)>0){
Feb_Interface_recv_ndata--;
break;
}
usleep(1000);
}
return (Feb_Interface_recv_ndata>=0);
}
int Feb_Interface_SetByteOrder(){
Feb_Interface_send_data_raw[0] = 0x8fff0000;
if(Local_Write(ll,4,Feb_Interface_send_data_raw)!=4) return 0;
Feb_Interface_send_ndata = 2;
Feb_Interface_send_data[0] = 0;
Feb_Interface_send_data[1] = 0;
unsigned int i;
unsigned int dst = 0xff;
for(i=0;i<Feb_Interface_nfebs;i++) dst = (dst | Feb_Interface_feb_numb[i]);
int passed = Feb_Interface_WriteTo(dst);
return passed;
}
int Feb_Interface_ReadRegister(unsigned int sub_num, unsigned int reg_num,unsigned int* value_read){
return Feb_Interface_ReadRegisters(sub_num,1,&reg_num,value_read);
}
int Feb_Interface_ReadRegisters(unsigned int sub_num, unsigned int nreads, unsigned int* reg_nums,unsigned int* values_read){
//here cout<<"Reading Register ...."<<endl;
unsigned int i;
nreads &= 0x3ff;
if(!nreads||nreads>Feb_Interface_send_buffer_size-2) return 0;
Feb_Interface_send_ndata = nreads+2;
Feb_Interface_send_data[0] = 0x20000000 | nreads << 14;
for(i=0;i<nreads;i++) Feb_Interface_send_data[i+1]=reg_nums[i];
Feb_Interface_send_data[nreads+1] = 0;
if(!Feb_Interface_WriteTo(sub_num)||!Feb_Interface_ReadFrom(sub_num,20)||Feb_Interface_recv_ndata!=(int)(nreads+2)) return 0;
for(i=0;i<nreads;i++) values_read[i] = Feb_Interface_recv_data[i+1];
return 1;
}
int Feb_Interface_WriteRegister(unsigned int sub_num, unsigned int reg_num,unsigned int value, int wait_on, unsigned int wait_on_address){
return Feb_Interface_WriteRegisters(sub_num,1,&reg_num,&value,&wait_on,&wait_on_address);
}
int Feb_Interface_WriteRegisters(unsigned int sub_num, unsigned int nwrites, unsigned int* reg_nums, unsigned int* values, int* wait_ons, unsigned int* wait_on_addresses){
unsigned int i;
nwrites &= 0x3ff; //10 bits
if(!nwrites||2*nwrites>Feb_Interface_send_buffer_size-2) return 0;
//cout<<"Write register : "<<this<<" "<<s_num<<" "<<nwrites<<" "<<reg_nums<<" "<<values<<" "<<wait_ons<<" "<<wait_on_addresses<<endl;
Feb_Interface_send_ndata = 2*nwrites+2;
Feb_Interface_send_data[0] = 0x80000000 | nwrites << 14;
Feb_Interface_send_data[2*nwrites+1] = 0;
for(i=0;i<nwrites;i++) Feb_Interface_send_data[2*i+1] = 0x3fff&reg_nums[i];
for(i=0;i<nwrites;i++) Feb_Interface_send_data[2*i+2] = values[i];
// wait on busy data(28), address of busy flag data(27 downto 14)
if(wait_ons&&wait_on_addresses) for(i=0;i<nwrites;i++) Feb_Interface_send_data[2*i+1] |= (wait_ons[i]<<28 | (0x3fff&wait_on_addresses[i])<<14);
if(!Feb_Interface_WriteTo(sub_num)) return 0;
return 1;
}
int Feb_Interface_WriteMemory(unsigned int sub_num, unsigned int mem_num, unsigned int start_address, unsigned int nwrites, unsigned int *values){
// -1 means write to all
unsigned int i;
mem_num &= 0x3f;
start_address &= 0x3fff;
nwrites &= 0x3ff;
if(!nwrites||nwrites>Feb_Interface_send_buffer_size-2) {printf("error herer: nwrites:%d\n",nwrites);return 0;}//*d-1026
Feb_Interface_send_ndata = nwrites+2;//*d-1025
Feb_Interface_send_data[0] = 0xc0000000 | mem_num << 24 | nwrites << 14 | start_address; //cmd -> write to memory, nwrites, mem number, start address
Feb_Interface_send_data[nwrites+1] = 0;
for(i=0;i<nwrites;i++) Feb_Interface_send_data[i+1] = values[i];
if(!Feb_Interface_WriteTo(sub_num)) return 0;
return 1;
}

View File

@ -0,0 +1,87 @@
//Class initially from Gerd and was called mmap_test.c
//return reversed 1 means good, 0 means failed
//#include <stdio.h>
//#include <unistd.h>
//#include <string.h>
//#include <sys/mman.h>
//#include <fcntl.h>
#include "HardwareIO.h"
xfs_u8 HWIO_xfs_in8(xfs_u32 InAddress)
{
/* read the contents of the I/O location and then synchronize the I/O
* such that the I/O operation completes before proceeding on
*/
xfs_u8 IoContents;
__asm__ volatile ("eieio; lbz %0,0(%1)":"=r" (IoContents):"b"
(InAddress));
return IoContents;
}
/*****************************************************************************/
xfs_u16 HWIO_xfs_in16(xfs_u32 InAddress)
{
/* read the contents of the I/O location and then synchronize the I/O
* such that the I/O operation completes before proceeding on
*/
xfs_u16 IoContents;
__asm__ volatile ("eieio; lhz %0,0(%1)":"=r" (IoContents):"b"
(InAddress));
return IoContents;
}
/*****************************************************************************/
xfs_u32 HWIO_xfs_in32(xfs_u32 InAddress)
{
/* read the contents of the I/O location and then synchronize the I/O
* such that the I/O operation completes before proceeding on
*/
xfs_u32 IoContents;
__asm__ volatile ("eieio; lwz %0,0(%1)":"=r" (IoContents):"b"
(InAddress));
return IoContents;
}
/*****************************************************************************/
void HWIO_xfs_out8(xfs_u32 OutAddress, xfs_u8 Value)
{
/* write the contents of the I/O location and then synchronize the I/O
* such that the I/O operation completes before proceeding on
*/
__asm__ volatile ("stb %0,0(%1); eieio"::"r" (Value), "b"(OutAddress));
}
/*****************************************************************************/
void HWIO_xfs_out16(xfs_u32 OutAddress, xfs_u16 Value)
{
/* write the contents of the I/O location and then synchronize the I/O
* such that the I/O operation completes before proceeding on
*/
__asm__ volatile ("sth %0,0(%1); eieio"::"r" (Value), "b"(OutAddress));
}
/*****************************************************************************/
void HWIO_xfs_out32(xfs_u32 OutAddress, xfs_u32 Value)
{
/* write the contents of the I/O location and then synchronize the I/O
* such that the I/O operation completes before proceeding on
*/
__asm__ volatile ("stw %0,0(%1); eieio"::"r" (Value), "b"(OutAddress));
}

View File

@ -0,0 +1,280 @@
//Class initially from Gerd and was called mmap_test.c
//return reversed 1 means good, 0 means failed
#include <stdio.h>
#include <unistd.h>
//#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "HardwareMMappingDefs.h"
#include "LocalLinkInterface.h"
Local_LocalLinkInterface1(struct LocalLinkInterface* ll,unsigned int ll_fifo_badr){
// printf("\n v 1 \n");
printf("Initialize PLB LL FIFOs\n");
ll->ll_fifo_base=0;
ll->ll_fifo_ctrl_reg=0;
if(Local_Init(ll,ll_fifo_badr)){
Local_Reset(ll);
printf("\tFIFO Status : 0x%08x\n",Local_StatusVector(ll));
}else printf("\tError LocalLink Mappping : 0x%08x\n",ll_fifo_badr);
printf("\n\n");
}
/*~LocalLinkInterface(){};*/
Local_LocalLinkInterface(struct LocalLinkInterface* ll){
printf("Initialize new memory\n");
}
int Local_InitNewMemory (struct LocalLinkInterface* ll,unsigned int addr, int ifg){
unsigned int CSP0BASE;
int fd;
/*fd = open("/dev/mem", O_RDWR | O_SYNC, 0);
if (fd == -1) {
printf("\nCan't find /dev/mem!\n");
return 0;
}
printf("/dev/mem opened\n");
CSP0BASE = (u_int32_t)mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, addr);
if (CSP0BASE == (u_int32_t)MAP_FAILED) {
printf("\nCan't map memmory area!!\n");
return 0;
}
printf("CSP0 mapped\n");
volatile u_int8_t *ptr1;
ptr1=(u_int8_t*)(CSP0BASE);
printf("pointer val=%x\n",(void*)ptr1);
printf("ifg_control=%02x\n",*ptr1);
*ptr1=ifg;
printf("ifg_control new=%02x\n",*ptr1);
close(fd);
*/
return 1;
}
int Local_Init(struct LocalLinkInterface* ll,unsigned int ll_fifo_badr){
int fd;
void *plb_ll_fifo_ptr;
if ((fd=open("/dev/mem", O_RDWR)) < 0){
fprintf(stderr, "Could not open /dev/mem\n");
return 0;
}
plb_ll_fifo_ptr = mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, ll_fifo_badr);
close(fd);
if (plb_ll_fifo_ptr == MAP_FAILED){
perror ("mmap");
return 0;
}
ll->ll_fifo_base = (xfs_u32) plb_ll_fifo_ptr;
ll->ll_fifo_ctrl_reg = 0;
return 1;
}
int Local_Reset(struct LocalLinkInterface* ll){
return Local_Reset1(ll,PLB_LL_FIFO_CTRL_RESET_STD);
}
int Local_Reset1(struct LocalLinkInterface* ll,unsigned int rst_mask){
ll->ll_fifo_ctrl_reg |= rst_mask;
printf("\tCTRL Register bits: 0x%08x\n",ll->ll_fifo_ctrl_reg);
HWIO_xfs_out32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_CTRL,ll->ll_fifo_ctrl_reg);
HWIO_xfs_out32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_CTRL,ll->ll_fifo_ctrl_reg);
HWIO_xfs_out32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_CTRL,ll->ll_fifo_ctrl_reg);
HWIO_xfs_out32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_CTRL,ll->ll_fifo_ctrl_reg);
ll->ll_fifo_ctrl_reg &= (~rst_mask);
HWIO_xfs_out32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_CTRL,ll->ll_fifo_ctrl_reg);
// printf("FIFO CTRL Address: 0x%08x\n FIFO CTRL Register: 0x%08x\n",PLB_LL_FIFO_REG_CTRL,plb_ll_fifo[PLB_LL_FIFO_REG_CTRL]);
return 1;
}
unsigned int Local_StatusVector(struct LocalLinkInterface* ll){
return HWIO_xfs_in32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_STATUS);
}
int Local_Write(struct LocalLinkInterface* ll,unsigned int buffer_len, void *buffer){
// note: buffer must be word (4 byte) aligned
// frame_len in byte
int vacancy=0;
int i;
int words_send = 0;
int last_word;
unsigned int *word_ptr;
unsigned int fifo_ctrl;
xfs_u32 status;
if (buffer_len < 1) return -1;
last_word = (buffer_len-1)/4;
word_ptr = (unsigned int *)buffer;
while (words_send <= last_word)
{
while (!vacancy)//wait for Fifo to be empty again
{
status = HWIO_xfs_in32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_STATUS);
if((status & PLB_LL_FIFO_STATUS_ALMOSTFULL) == 0) vacancy = 1;
}
//Just to know: #define PLB_LL_FIFO_ALMOST_FULL_THRESHOLD_WORDS 100
for (i=0; ((i<PLB_LL_FIFO_ALMOST_FULL_THRESHOLD_WORDS) && (words_send <= last_word)); i++)
{
fifo_ctrl = 0;
if (words_send == 0)
{
fifo_ctrl = PLB_LL_FIFO_CTRL_LL_SOF;//announce the start of file
}
if (words_send == last_word)
{
fifo_ctrl |= (PLB_LL_FIFO_CTRL_LL_EOF | (( (buffer_len-1)<<PLB_LL_FIFO_CTRL_LL_REM_SHIFT) & PLB_LL_FIFO_CTRL_LL_REM) );
}
Local_ctrl_reg_write_mask(ll,PLB_LL_FIFO_CTRL_LL_MASK,fifo_ctrl);
HWIO_xfs_out32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_FIFO,word_ptr[words_send++]);
}
}
return buffer_len;
}
int Local_Read(struct LocalLinkInterface* ll,unsigned int buffer_len, void *buffer){
static unsigned int buffer_ptr = 0;
// note: buffer must be word (4 byte) aligned
// frame_len in byte
int len;
unsigned int *word_ptr;
unsigned int status;
volatile unsigned int fifo_val;
int sof = 0;
word_ptr = (unsigned int *)buffer;
do
{
status = HWIO_xfs_in32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_STATUS);
if (!(status & PLB_LL_FIFO_STATUS_EMPTY))
{
if (status & PLB_LL_FIFO_STATUS_LL_SOF)
{
if (buffer_ptr)
{
buffer_ptr = 0;
return -1; // buffer overflow
}
// printf(">>>> SOF\n\r");
buffer_ptr = 0;
sof = 1;
}
fifo_val = HWIO_xfs_in32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_FIFO); //read from fifo
if ((buffer_ptr > 0) || sof)
{
if ( (buffer_len >> 2) > buffer_ptr)
{
word_ptr[buffer_ptr++] = fifo_val; //write to buffer
}
else
{
buffer_ptr = 0;
return -2; // buffer overflow
}
if (status & PLB_LL_FIFO_STATUS_LL_EOF)
{
len = (buffer_ptr << 2) -3 + ( (status & PLB_LL_FIFO_STATUS_LL_REM)>>PLB_LL_FIFO_STATUS_LL_REM_SHIFT );
// printf(">>>>status=0x%08x EOF len = %d \n\r\n\r",status, len);
buffer_ptr = 0;
return len;
}
}
}
}
while(!(status & PLB_LL_FIFO_STATUS_EMPTY));
return 0;
}
int Local_ctrl_reg_write_mask(struct LocalLinkInterface* ll,unsigned int mask, unsigned int val){
// printf("Fifo CTRL Reg(1): 0x%08x\n",plb_ll_fifo_ctrl_reg);
ll->ll_fifo_ctrl_reg &= (~mask);
//printf("Fifo CTRL Reg(2): 0x%08x\n",plb_ll_fifo_ctrl_reg);
ll->ll_fifo_ctrl_reg |= ( mask & val);
// printf("Fifo CTRL Reg: 0x%08x\n",plb_ll_fifo_ctrl_reg);
HWIO_xfs_out32(ll->ll_fifo_base+4*PLB_LL_FIFO_REG_CTRL,ll->ll_fifo_ctrl_reg);
// printf("Fifo STAT Reg: 0x%08x\n", plb_ll_fifo[PLB_LL_FIFO_REG_STATUS]);
return 1;
}
int Local_Test(struct LocalLinkInterface* ll,unsigned int buffer_len, void *buffer){
int len;
unsigned int rec_buff_len = 4096;
unsigned int rec_buffer[4097];
Local_Write(ll,buffer_len,buffer);
usleep(10000);
do{
len = Local_Read(ll,rec_buff_len,rec_buffer);
printf("receive length: %i\n",len);
if (len > 0){
rec_buffer[len]=0;
printf((char*) rec_buffer);
printf("\n");
}
} while(len > 0);
printf("\n\n\n\n");
return 1;
}
void Local_llfifo_print_frame(struct LocalLinkInterface* ll,unsigned char* fbuff, int len){
int i;
printf("\n\r----Frame of len : %d Byte\n\r",len);
for(i=0;i<len;i++){
printf("0x%02x ",fbuff[i] );
if ((i&0xf) == 0x7) printf(" ");
if ((i&0xf) == 0xf) printf("\n\r");
}
printf("\n\r");
}