コード例 #1
0
ファイル: cc1101.c プロジェクト: gefa/CC1101
/**
 * receiveData
 *
 * Read data packet from RX FIFO
 *
 * 'packet'     Container for the packet received
 *
 * Return:
 *      Amount of bytes received
 */
byte CC1101_receiveData(CCPACKET * packet)
{

	  byte val;

	  // Rx FIFO overflow?
	  if (((val=readStatusReg(CC1101_MARCSTATE)) & 0x1F) == 0x11)
	  {
	    setIdleState();       // Enter IDLE state
	    flushRxFifo();        // Flush Rx FIFO
	    //CC1101_cmdStrobe(CC1101_SFSTXON);
	    packet->length = 0;
	  }
	  // Any byte waiting to be read?
	  else if ((val=readStatusReg(CC1101_RXBYTES)) & 0x7F)
	  {
	    // Read data length
	    packet->length = readConfigReg(CC1101_RXFIFO);
	    // If packet is too long
	    if (packet->length > CC1101_DATA_LEN)
	      packet->length = 0;   // Discard packet
	    else
	    {
	      // Read data packet
	      CC1101_readBurstReg(packet->data, CC1101_RXFIFO, packet->length);
	      // Read RSSI
	      packet->rssi = readConfigReg(CC1101_RXFIFO);
	      // Read LQI and CRC_OK
	      val = readConfigReg(CC1101_RXFIFO);
	      packet->lqi = val & 0x7F;
	      packet->crc_ok = (val & 0x80)>>7;
	    }
	  }
コード例 #2
0
ファイル: cc1101.cpp プロジェクト: engkan2kit/WiTMon
/**
 * sendData
 * 
 * Send data packet via RF
 * 
 * 'packet'	Packet to be transmitted
 *
 *  Return:
 *    True if the transmission succeeds
 *    False otherwise
 */
boolean CC1101::sendData(CCPACKET packet)
{
  // Enter RX state
  setRxState();

  // Check that the RX state has been entered
  while (readStatusReg(CC1101_MARCSTATE) != 0x0D)
    delay(1);
  delayMicroseconds(500);

  // Set data length at the first position of the TX FIFO
  writeReg(CC1101_TXFIFO,  packet.length);
  // Write data into the TX FIFO
  writeBurstReg(CC1101_TXFIFO, packet.data, packet.length);

  // CCA enabled: will enter TX state only if the channel is clear
  cmdStrobe(CC1101_STX);

  // Check that TX state is being entered (state = RXTX_SETTLING)
  if(readStatusReg(CC1101_MARCSTATE) != 0x15)
    return false;

  // Wait for the sync word to be transmitted
  wait_GDO0_high();

  // Wait until the end of the packet transmission
  wait_GDO0_low();

  // Flush TX FIFO. Don't uncomment
  // cmdStrobe(CC1101_SFTX);
  
  // Enter back into RX state
  setRxState();

  // Check that the TX FIFO is empty
  if((readStatusReg(CC1101_TXBYTES) & 0x7F) == 0)
    return true;

  return false;
}
コード例 #3
0
ファイル: cc1101.cpp プロジェクト: hdo/arduino_panstamp
/**
 * receiveData
 * 
 * Read data packet from RX FIFO
 *
 * 'packet'	Container for the packet received
 * 
 * Return:
 * 	Amount of bytes received
 */
byte CC1101::receiveData(CCPACKET * packet)
{
  byte val;
  byte rxBytes = readStatusReg(CC1101_RXBYTES);

  // Any byte waiting to be read and no overflow?
  if (rxBytes & 0x7F && !(rxBytes & 0x80))
  {
    // Read data length
    packet->length = readConfigReg(CC1101_RXFIFO);
    // If packet is too long
    if (packet->length > CC1101_DATA_LEN)
      packet->length = 0;   // Discard packet
    else
    {
      // Read data packet
      readBurstReg(packet->data, CC1101_RXFIFO, packet->length);
      // Read RSSI
      packet->rssi = readConfigReg(CC1101_RXFIFO);
      // Read LQI and CRC_OK
      val = readConfigReg(CC1101_RXFIFO);
      packet->lqi = val & 0x7F;
      packet->crc_ok = bitRead(val, 7);
    }
  }
  else
    packet->length = 0;

  setIdleState();       // Enter IDLE state
  flushRxFifo();        // Flush Rx FIFO
  //cmdStrobe(CC1101_SCAL);

  // Back to RX state
  setRxState();

  return packet->length;
}
コード例 #4
0
ファイル: cc1101.cpp プロジェクト: engkan2kit/WiTMon
/**
 * receiveData
 * 
 * Read data packet from RX FIFO
 * 
 * Return:
 * 	Amount fo bytes received
 * 'packet'	Container for the packet received
 */
byte CC1101::receiveData(CCPACKET * packet)
{
  byte val;

  // Any byte waiting to be read?
  if (readStatusReg(CC1101_RXBYTES) & 0x7F)
  {
    // Read data length
    packet->length = readConfigReg(CC1101_RXFIFO);

    // If packewt is too long
    if (packet->length > CC1101_DATA_LEN)
      packet->length = 0;   // Discard packet
    else
    {
      // Read data packet
      readBurstReg(packet->data, CC1101_RXFIFO, packet->length);
      // Read RSSI
      packet->rssi = readConfigReg(CC1101_RXFIFO);
      // Read LQI and CRC_OK
      val = readConfigReg(CC1101_RXFIFO);
      packet->lqi = val & 0x7F;
      packet->crc_ok = bitRead(val, 7);
    }
  }
  else
    packet->length = 0;

  // Flush RX FIFO. Don't uncomment
  //cmdStrobe(CC1101_SFRX);

  // Enter back into RX state
  setRxState(); 

  return packet->length;
}
コード例 #5
0
ファイル: cc1101.cpp プロジェクト: hdo/arduino_panstamp
/**
 * sendData
 * 
 * Send data packet via RF
 * 
 * 'packet'	Packet to be transmitted. First byte is the destination address
 *
 *  Return:
 *    True if the transmission succeeds
 *    False otherwise
 */
boolean CC1101::sendData(CCPACKET packet)
{
  byte marcState;
  bool res = false;
 
  // Declare to be in Tx state. This will avoid receiving packets whilst
  // transmitting
  rfState = RFSTATE_TX;

  // Enter RX state
  setRxState();

  // Check that the RX state has been entered
  while (((marcState = readStatusReg(CC1101_MARCSTATE)) & 0x1F) != 0x0D)
  {
    if (marcState == 0x11)        // RX_OVERFLOW
      flushRxFifo();              // flush receive queue
  }

  delayMicroseconds(500);

  // Set data length at the first position of the TX FIFO
  writeReg(CC1101_TXFIFO,  packet.length);
  // Write data into the TX FIFO
  writeBurstReg(CC1101_TXFIFO, packet.data, packet.length);

  // CCA enabled: will enter TX state only if the channel is clear
  setTxState();

  // Check that TX state is being entered (state = RXTX_SETTLING)
  marcState = readStatusReg(CC1101_MARCSTATE) & 0x1F;
  if((marcState != 0x13) && (marcState != 0x14) && (marcState != 0x15))
  {
    setIdleState();       // Enter IDLE state
    flushTxFifo();        // Flush Tx FIFO
    setRxState();         // Back to RX state

    // Declare to be in Rx state
    rfState = RFSTATE_RX;
    return false;
  }

  // Wait for the sync word to be transmitted
  wait_GDO0_high();

  // Wait until the end of the packet transmission
  wait_GDO0_low();

  // Check that the TX FIFO is empty
  if((readStatusReg(CC1101_TXBYTES) & 0x7F) == 0)
    res = true;

  setIdleState();       // Enter IDLE state
  flushTxFifo();        // Flush Tx FIFO

  // Enter back into RX state
  setRxState();

  // Declare to be in Rx state
  rfState = RFSTATE_RX;

  return res;
}
コード例 #6
0
ファイル: framever.c プロジェクト: tkgunji/rcu2linuxtest
int main(int argc, char **argv){
   __u32 *pSrc;
   __u32 rawData;
   __u16 *pTgt, *pTgtoutput, *pRawData, *pErrFileDataoutput,*pErrFileData;
   __u32 *pRBData;
   //for the files that are opened
   int block, major, minor;
   FILE *frames;
   FILE *logfile;
   FILE *errfile;
   FILE *rbrawfile;
   FILE *rawfile;
   char pathandfilename[96]="";
   char errpathandfilename[96];
   char line[255];
   char putline[255];
   char foldername[96]="";
   int data[212];
   int frameaddress[3];
   int *pframeaddress;
   __u16 rawdata[212];
   int errfiledata[212];
   int cycles, result,n, linecounter, cyclestoreach, i,erroroccured, cycleserrors, linenumber, linenumbercount, overallerrors;
   //for the time
   struct tm *l_time;
   time_t now;
   char mytime[20];
   //for the Hardwareerrorregisters
   unsigned short hwnoferrorsreg;
   unsigned short hwnoflastframewitherr;
   unsigned short hwnoflastframe;
   unsigned short hwnofcycles;
   unsigned short hwstatusreg;
   unsigned short hwerrreg;
   char ReadbackRawFrameFilename[64];
   char RawFrameFilename[64];
   char ErrorFrameFilename[64];
   pframeaddress = &frameaddress[0];
   block = -1;
   major = -1;
   minor = -1;
   cycles = 0;
   cyclestoreach = 0;
   n = 0;
   i = 0;
   linecounter = 0;
   hwnoferrorsreg = 0;
   hwnoflastframewitherr = 0;
   hwnoflastframe = 0;
   hwnofcycles = 0;
   hwerrreg = 0;
   hwstatusreg = 0;
   erroroccured = 0;
   cycleserrors = 0;
   linenumber = 0;
   linenumbercount = 0;
   overallerrors = 0;
   
   //enable writing to the RCU
   initRcuAccess(NULL);

   //initialization of the registers
   result = init();
   if(result < 0){
	printf("Initialization failed: %d\n", result);
	return EXIT_FAILURE;
   }
   
   //create a logfile and write a header to it
   writeHeaderToLogfile();

   // test for the number of parameters
   if((argc != 2) && (argc != 3)) {
        perror("usage: framever <frames> [Cycles]\n");
        return EXIT_FAILURE;
   }
   
   // if no number of cycles is given use a default
   if(argv[2] == NULL){
	cyclestoreach = 2;
	printf("No Parameter for Cycles given, using Cycles = 2\n");
   }
   else{
	cyclestoreach = atoi(argv[2]);
	printf("Using given parameter %d for Cycles.\n", cyclestoreach);
   }
   printf("CyclestoReach: %d\n", cyclestoreach);

   // count the number of frames to be handled
   linenumbercount = linenumber = getLinesnumberFromFile(argv[1]);


   //open frames.txt for reading
   if((frames=fopen(argv[1],"r")) < 0){
        printf("Could not open %s for reading!\n",argv[1]);
        return EXIT_FAILURE;
   }
  

   //open logfile for writing	
   if((logfile=fopen("log.txt","a+b")) < 0){
       	printf("Could not open %s for reading!\n",argv[1]);
       	return EXIT_FAILURE;
   }
   else{
	printf("writing to logfile log.txt.\n");
   }
  
   sprintf(putline,"framever working on %d cycles.\n", cyclestoreach);
   fwrite(putline, strlen(putline), 1, logfile);

 
   //read and show the status registers
   hwnoferrorsreg = getErrorcounterReg();
   hwnoflastframewitherr = getLastErrorFramenumber();
   hwnoflastframe = getLastFramenumber();
   hwnofcycles = getNumberOfCycles();
   hwerrreg = readErrReg();
   hwstatusreg = readStatusReg();
		
   //printf("noferr: %d, lastframewitherr: %d, lastframe: %d, nofcycles: %d, ErrReg: %d, StatusReg: %d\n",hwnoferrorsreg, hwnoflastframewitherr, hwnoflastframe, hwnofcycles, hwerrreg, hwstatusreg);


   sprintf(putline,"Overview about the hardwareregisters before the actual work started:\n");
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);
   sprintf(putline, "Read Error Counter Register (0xb104)	: %d\n", hwnoferrorsreg);
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);
   sprintf(putline, "Last Frame with error (0xb105)	    	: %d\n", hwnoflastframewitherr);
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);
   sprintf(putline, "Last read out frame(0xb106)	    	: %d\n",hwnoflastframe);
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);
   sprintf(putline, "Error register (0xb101)		    	: %#x\n", hwerrreg);
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);
   sprintf(putline, "Status register (0xb100)		    : %#x\n\n", hwstatusreg);
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);
   
   sprintf(putline, "Number of Frames found in %s	 : %d\n\n", argv[1], linenumber);
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);

   //as long as there are frames to be read
   if(frames!=NULL){
        //and as long as we havent reached the number of cycles to reach
	while(cycles < cyclestoreach){

	   //read out time for for the folder generation
   	   time(&now);
   	   l_time = localtime(&now);
   	   strftime(mytime, sizeof mytime, "%T", l_time);
           
	   //generate the folder for this cycle
   	   sprintf(foldername,"cycle_%d_%s",cycles, mytime);
	   result = mkdir(foldername, S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IROTH | S_IWOTH | S_IXOTH);
   	   if(result < 0){
		printf("failed creating folder %s - Error %d\n",foldername, result);
   	   }


  	   sprintf(putline, "current cycle: %d\n=============================\n", cycles);
   	   //printf("%s",  putline);
	   fwrite(putline, strlen(putline), 1, logfile);
   	   strftime(mytime, sizeof mytime, "%D - %T%n", l_time);
	   
   	   sprintf(putline,"%s", mytime);
   	   //printf("%s",  putline);
	   fwrite(putline, strlen(putline), 1, logfile);

	   linenumbercount = linenumber;
	
	    //if there is something to readout
            while(fgets(line,255,frames)!=NULL){
	
	        //read line from framesfile
		pframeaddress = &frameaddress[0];
	        getFrameAddressFromLine(line, pframeaddress);
		pframeaddress = &frameaddress[0];
		block = frameaddress[0];
		major = frameaddress[1];
		minor = frameaddress[2];
		
	

	        if(block != -1){
		     //printf("processing %d, %d, %d\n",block, major, minor);
                     sprintf(ReadbackRawFrameFilename,"frame%d.%d.%d.hex",block,major,minor);
                     sprintf(ErrorFrameFilename,"Err_frame%d.%d.%d.hex",block,major,minor);
                     sprintf(RawFrameFilename,"raw/frame%d.%d.%d.hex",block,major,minor);
		
		     //prepare for reading out
		     result = step();
		     if(result < 0)
		         printf("rcuSingleWrite failed (0xb000, 0x0100), Error: %d\n",result);

		     //read the data
		     result = rcuMultipleRead(0xb41c, 212, data);
        	     //printf("read %d bytes from memory.\n",result);
	
		     //get rid of the unused 16bit
		     pSrc = (__u32*)&data[0];
        	     pTgtoutput = pTgt = (__u16*)malloc(212*sizeof(__u16));
        	     for(n=0;n<212;n++){
            	        *pTgt=*((__u16*)pSrc);
                        pTgt++;
                        pSrc++;
                     }

		     //printf("%s\n",foldername);
		     // generate the filename the data will be written to
		     strcpy(pathandfilename,foldername);
		     strcat(pathandfilename,"/");
		     strcat(pathandfilename,ReadbackRawFrameFilename);

		     //printf("\n%s\n",pathandfilename);
		
		     //write the readout data
                     if((rbrawfile=fopen(pathandfilename,"w+b")) < 0){
                        printf("Error opening %s for ascii writing!\n",pathandfilename);
                	return EXIT_FAILURE;
                     }
		     //printf("done writing raw data to %s.\n", pathandfilename);
		     //clear the filename
		     strcpy(pathandfilename,"\0");

		     //write the data to the file
	             fwrite(pTgtoutput, sizeof(__u16), 212, rbrawfile);
		     
   		     fclose(rbrawfile);

		     //open the raw File for reading and comparison
                     if((rawfile=fopen(RawFrameFilename,"r")) < 0){
                        printf("Error opening %s for reading!\n",RawFrameFilename);
                	return EXIT_FAILURE;
                     }
		     fread(rawdata, sizeof(__u16), 212, rawfile);
		     fclose(rawfile);

		     // do an XOR with the read out data and the correct framefiles
		     pRBData = (__u32*)&data[0];
		     pRawData = &rawdata[0];
		     pErrFileDataoutput = pErrFileData = (__u16*)&errfiledata[0];

		     for(i = 0; i < 212; i++){
			*pErrFileData = ((*pRawData++))^((*pRBData++));
			//analyze for errors and count them
			erroroccured += analyze16bit(*pErrFileData);
			*pErrFileData++;
			//fprintf(stderr, "data=%#x rawdata=%#x errordata=%#x\n", data[i], rawdata[i], errfiledata[i]);
		     }
		   
		     //if a bitflip was detected create an Errorfile wich contains the XOR data 
		     if(erroroccured > 0){
		     	//write the Errorfile	
		     	strcpy(errpathandfilename,foldername);
		     	strcat(errpathandfilename,"/");
		     	strcat(errpathandfilename,ErrorFrameFilename);
			
			printf("found error in frame %d,%d,%d, writing errors to file %s\n", block, major, minor, errpathandfilename);

                     	if((errfile=fopen(errpathandfilename,"w+b")) < 0){
                            printf("Error opening %s for reading!\n",ErrorFrameFilename);
                	    return EXIT_FAILURE;
                         }

		      	fwrite(pErrFileDataoutput, sizeof(__u16), 212, errfile);

		     	fclose(errfile);
		     }
			
		     //read the status registers
		     hwnoferrorsreg = getErrorcounterReg();
		     hwnoflastframewitherr = getLastErrorFramenumber();
		     hwnoflastframe = getLastFramenumber();
		     hwnofcycles = getNumberOfCycles();
		     hwerrreg = readErrReg();
		     hwstatusreg = readStatusReg();
/*
		if(hwerrreg != 0){
		    printf("Hardware Error Register shows %d! - aborting.",hwerrreg);
		    return EXIT_FAILURE;
		}
*/		
		     
  		     //print the Errorregisters to the logfile
  		     sprintf(putline, "current frame: %d, %d, %d - Framenumber: %d\n----------------------------\n", block, major, minor, linenumbercount-1);
		     fwrite(putline, strlen(putline), 1, logfile);
		     sprintf(putline, "Last read out frame(0xb106)	    : %d\n",hwnoflastframe);
		     fwrite(putline, strlen(putline), 1, logfile);
   		     sprintf(putline, "Read Error Counter Register (0xb104): %d\n", hwnoferrorsreg);
		     fwrite(putline, strlen(putline), 1, logfile);
		     sprintf(putline, "Last Frame with error (0xb105)	    : %d\n", hwnoflastframewitherr);
		     fwrite(putline, strlen(putline), 1, logfile);
		     //sprintf(putline, "Error register (0xb101)	 	    : %#x %s\n", hwerrreg, getMeaningOfErrReg(hwerrreg));
		     sprintf(putline, "Error register (0xb101)	 	    : %#x\n", hwerrreg);
		     fwrite(putline, strlen(putline), 1, logfile);
  		     sprintf(putline, "Status register (0xb100)            : %#x\n", hwstatusreg);
		     fwrite(putline, strlen(putline), 1, logfile);

		     
  		     sprintf(putline, "Bitflips found in this frame	    : %d\n\n", erroroccured);
		     fwrite(putline, strlen(putline), 1, logfile);

		     //add up the errors for the whole cycle
		     cycleserrors+=erroroccured;
		     erroroccured = 0;

	//	     printf("Cycle: %d, noferr: %d, lastframewitherr: %d, lastframe: %d, nofcycles: %d, ErrReg: %d, StatusReg: %d\n",cycles, hwnoferrorsreg, hwnoflastframewitherr, hwnoflastframe, hwnofcycles, hwerrreg, hwstatusreg);
	
		    //count the line Framenumbers like the hardware does
		    linenumbercount--;	
		    clearErrReg();

	       }

	   }

	//print detected bitflips to screen and logfile
  	sprintf(putline, "Bitflips found in cycle %d	    : %d\n", cycles, cycleserrors);
   	printf("%s",  putline);
	fwrite(putline, strlen(putline), 1, logfile);
        overallerrors+=cycleserrors;
	sprintf(putline, "Bitflips found so far               : %d\n",overallerrors);
   	printf("%s",  putline);
	
	//reset and start new cycle
	cycleserrors=0;
	cycles++;	    
	rewind(frames);
	}
   }

   //print all detected erorrs and clean up
   sprintf(putline, "Bitflips found during %d cycles with %d frames  : %d\n\n", cyclestoreach, linenumber, overallerrors);
   printf("%s",  putline);
   fwrite(putline, strlen(putline), 1, logfile);
   fclose(logfile);
   printf("closed logfile\n");
   free(pTgtoutput);
   printf("freed memory\n");
   fclose(frames);
   printf("closed framefile\n");
 //  printf("closed rawfile\n");

   releaseRcuAccess();

return EXIT_SUCCESS;
}