src/pdo.c
changeset 201 2966cd34162a
parent 149 fe50ada8020b
child 204 44ce74232ccb
equal deleted inserted replaced
200:930be51ddc24 201:2966cd34162a
    78     res = sendPDO(d, pdo, NOT_A_REQUEST);
    78     res = sendPDO(d, pdo, NOT_A_REQUEST);
    79 
    79 
    80     return res;
    80     return res;
    81 }
    81 }
    82 
    82 
    83 /**************************************************************************/
    83 #if 0
       
    84 /*********************************************************************/
       
    85 /* TODO : implement bit mapping                          			 */
       
    86 /*********************************************************************/
       
    87 
    84 UNS8 buildPDO(CO_Data* d, UNS16 index)
    88 UNS8 buildPDO(CO_Data* d, UNS16 index)
    85 { /* DO NOT USE MSG_ERR because the macro may send a PDO -> infinite loop if it fails. */	
    89 { /* DO NOT USE MSG_ERR because the macro may send a PDO -> infinite loop if it fails. */	
    86   UNS16 ind;
    90   UNS16 ind;
    87   UNS8      subInd;
    91   UNS8      subInd;
    88 
    92 
   124 	    pMappingParameter = (d->objdict + offsetObjdict + ind)->pSubindex[subInd + 1].pObject;
   128 	    pMappingParameter = (d->objdict + offsetObjdict + ind)->pSubindex[subInd + 1].pObject;
   125 	    MSG_WAR(0x3914, "Get the mapping      at index : ", (UNS16)0x1A00 + ind);
   129 	    MSG_WAR(0x3914, "Get the mapping      at index : ", (UNS16)0x1A00 + ind);
   126 	    MSG_WAR(0x3915, "                     subIndex : ", subInd + 1);
   130 	    MSG_WAR(0x3915, "                     subIndex : ", subInd + 1);
   127 	    MSG_WAR(0x3916, "                     value    : ", *(UNS32 *)pMappingParameter);
   131 	    MSG_WAR(0x3916, "                     value    : ", *(UNS32 *)pMappingParameter);
   128 	    /* Get the mapped variable */
   132 	    /* Get the mapped variable */
   129 	     Size = ((UNS8)(((*pMappingParameter) & 0xFF) >> 3));
   133 	    Size = ((UNS8)(((*pMappingParameter) & 0xFF) >> 3));
   130 	     objDict = getODentry(d, (UNS16)((*pMappingParameter) >> 16),
   134 	  	objDict = getODentry(d, (UNS16)((*pMappingParameter) >> 16),
   131 				    (UNS8)(((*pMappingParameter) >> 8 ) & 0x000000FF),
   135 				    (UNS8)(((*pMappingParameter) >> 8 ) & 0x000000FF),
   132 				    (void *)&d->process_var.data[offset], &Size, &dataType, 0 ); 
   136 				    (void *)&d->process_var.data[offset], &Size, &dataType, 0 );  
   133 
   137 
   134 	     if (objDict != OD_SUCCESSFUL) {
   138 	     if (objDict != OD_SUCCESSFUL) {
   135 	        MSG_WAR(0x2919, "error accessing to the mapped var : ", subInd + 1);  
   139 	        MSG_WAR(0x2919, "error accessing to the mapped var : ", subInd + 1);  
   136 		MSG_WAR(0x2920, "         Mapped at index : ", (*pMappingParameter) >> 16);
   140 		MSG_WAR(0x2920, "         Mapped at index : ", (*pMappingParameter) >> 16);
   137 		MSG_WAR(0x2921, "                subindex : ", ((*pMappingParameter) >> 8 ) & 0xFF);
   141 		MSG_WAR(0x2921, "                subindex : ", ((*pMappingParameter) >> 8 ) & 0xFF);
   143 	      subInd++;					
   147 	      subInd++;					
   144 	}/* end Loop on mapped variables  */
   148 	}/* end Loop on mapped variables  */
   145   }
   149   }
   146   return 0;
   150   return 0;
   147 }
   151 }
   148 
   152 #endif
   149 /**************************************************************************/
   153 /**************************************************************************/
   150 UNS8 sendPDOrequest( CO_Data* d, UNS32 cobId )
   154 UNS8 sendPDOrequest( CO_Data* d, UNS32 cobId )
   151 {		
   155 {		
   152   UNS32 *	 pwCobId;	
   156   UNS32 *	 pwCobId;	
   153   UNS16          offset;
   157   UNS16          offset;
   197   UNS8       offset;
   201   UNS8       offset;
   198   UNS8       status;
   202   UNS8       status;
   199   UNS32      objDict;
   203   UNS32      objDict;
   200   UNS16      offsetObjdict;
   204   UNS16      offsetObjdict;
   201   UNS16      lastIndex;
   205   UNS16      lastIndex;
       
   206   UNS16      Index;
       
   207   UNS8       Sindex;
   202   status = state1;
   208   status = state1;
   203 
   209  
   204   MSG_WAR(0x3935, "proceedPDO, cobID : ", ((*m).cob_id.w & 0x7ff)); 
   210   MSG_WAR(0x3935, "proceedPDO, cobID : ", ((*m).cob_id.w & 0x7ff)); 
   205   offset = 0x00;
   211   offset = 0x00;
   206   numPdo = 0;
   212   numPdo = 0;
   207   numMap = 0;
   213   numMap = 0;
   208   if((*m).rtr == NOT_A_REQUEST ) { /* The PDO received is not a request. */
   214   if((*m).rtr == NOT_A_REQUEST ) { /* The PDO received is not a request. */
   209     offsetObjdict = d->firstIndex->PDO_RCV;
   215     offsetObjdict = d->firstIndex->PDO_RCV;
   210     lastIndex = d->lastIndex->PDO_RCV;
   216     lastIndex = d->lastIndex->PDO_RCV;
   211 
   217     
   212     /* study of all the PDO stored in the dictionary */   
   218     /* study of all the PDO stored in the dictionary */   
   213     if(offsetObjdict)
   219     if(offsetObjdict)
   214 	    while (offsetObjdict <= lastIndex) {
   220 	    while (offsetObjdict <= lastIndex) {
   215 					
   221 			
   216 	      switch( status ) {
   222 	      switch( status ) {
   217 						
   223 						
   218 	        case state1:	/* data are stored in process_var array */
   224 	        case state1:	/* data are stored in process_var array */
   219 		  /* memcpy(&(process_var.data), &m->data, (*m).len); */
   225 		  /* memcpy(&(process_var.data), &m->data, (*m).len); */
   220 		  /* Ce memcpy devrait être portable. */
   226 		  /* Ce memcpy devrait être portable. */
   225 		  status = state2; 
   231 		  status = state2; 
   226 		  break;
   232 		  break;
   227 	
   233 	
   228 		case state2:
   234 		case state2:
   229 		  /* get CobId of the dictionary correspondant to the received PDO */
   235 		  /* get CobId of the dictionary correspondant to the received PDO */
   230 	          pwCobId = d->objdict[offsetObjdict].pSubindex[1].pObject;
   236 	          pwCobId = d->objdict[offsetObjdict].pSubindex[1].pObject; 
   231 		  /* check the CobId coherance */
   237 		  /* check the CobId coherance */
   232 		  /*pwCobId is the cobId read in the dictionary at the state 3 */
   238 		  /*pwCobId is the cobId read in the dictionary at the state 3 */
   233 		  if ( *pwCobId == (*m).cob_id.w ){
   239 		  if ( *pwCobId == (*m).cob_id.w ){
   234 		    /* The cobId is recognized */
   240 		    /* The cobId is recognized */
   235 		    status = state4;
   241 		    status = state4;
   249 		  offsetObjdict = d->firstIndex->PDO_RCV_MAP;
   255 		  offsetObjdict = d->firstIndex->PDO_RCV_MAP;
   250 		  lastIndex = d->lastIndex->PDO_RCV_MAP;
   256 		  lastIndex = d->lastIndex->PDO_RCV_MAP;
   251 		  pMappingCount = (d->objdict + offsetObjdict + numPdo)->pSubindex[0].pObject;	  
   257 		  pMappingCount = (d->objdict + offsetObjdict + numPdo)->pSubindex[0].pObject;	  
   252 		  numMap = 0;
   258 		  numMap = 0;
   253 		  while (numMap < *pMappingCount) {
   259 		  while (numMap < *pMappingCount) {
       
   260 			UNS8 tmp[]= {0,0,0,0,0,0,0,0};
       
   261 		  	UNS8 ByteSize;
   254 		    pMappingParameter = (d->objdict + offsetObjdict + numPdo)->pSubindex[numMap + 1].pObject;
   262 		    pMappingParameter = (d->objdict + offsetObjdict + numPdo)->pSubindex[numMap + 1].pObject;
   255 		    if (pMappingParameter == NULL) {
   263 		    if (pMappingParameter == NULL) {
   256 		      MSG_ERR(0x1937, "Couldn't get mapping parameter : ", numMap + 1); 
   264 		      MSG_ERR(0x1937, "Couldn't get mapping parameter : ", numMap + 1); 
   257 		      return 0xFF;
   265 		      return 0xFF;
   258 		    }
   266 		    }
   259 		    /* Get the addresse of the mapped variable. */
   267 			/* Get the addresse of the mapped variable. */
   260 		    /* detail of *pMappingParameter : */
   268 		    /* detail of *pMappingParameter : */
   261 	            /* The 16 hight bits contains the index, the medium 8 bits contains the subindex, */
   269 	            /* The 16 hight bits contains the index, the medium 8 bits contains the subindex, */
   262 		    /* and the lower 8 bits contains the size of the mapped variable. */
   270 		    /* and the lower 8 bits contains the size of the mapped variable. */
   263 
   271 		    
   264 		    Size = ((UNS8)(((*pMappingParameter) & 0xFF) >> 3));
   272 		    Size = (UNS8)(*pMappingParameter);
   265 
   273 			
       
   274 			/* copy bit per bit in little endian */
       
   275 			CopyBits(Size, (UNS8*)&d->process_var.data[offset>>3], offset%8, 0, ((UNS8*)tmp), 0, 0);
       
   276 		  	
       
   277 		  	ByteSize = 1 + ((Size - 1) >> 3); /*1->8 => 1 ; 9->16 => 2, ... */
       
   278 		  	
   266 		    objDict = setODentry(d, (UNS16)((*pMappingParameter) >> 16),
   279 		    objDict = setODentry(d, (UNS16)((*pMappingParameter) >> 16),
   267 				            (UNS8)(((*pMappingParameter) >> 8 ) & 0xFF),
   280 				            (UNS8)(((*pMappingParameter) >> 8 ) & 0xFF),
   268 					    (void *)&d->process_var.data[offset], &Size, 0 );
   281 					    	tmp, &ByteSize, 0 );
   269 
   282 					    	
   270 		    if(objDict != OD_SUCCESSFUL) {
   283 		    if(objDict != OD_SUCCESSFUL) {
   271 		      MSG_ERR(0x1938, "error accessing to the mapped var : ", numMap + 1);  
   284 		      MSG_ERR(0x1938, "error accessing to the mapped var : ", numMap + 1);  
   272 		      MSG_WAR(0x2939, "         Mapped at index : ", (*pMappingParameter) >> 16);
   285 		      MSG_WAR(0x2939, "         Mapped at index : ", (*pMappingParameter) >> 16);
   273 		      MSG_WAR(0x2940, "                subindex : ", ((*pMappingParameter) >> 8 ) & 0xFF);
   286 		      MSG_WAR(0x2940, "                subindex : ", ((*pMappingParameter) >> 8 ) & 0xFF);
   274 		      return 0xFF;
   287 		      return 0xFF;
   334 	  pMappingCount = (d->objdict + offsetObjdict + numPdo)->pSubindex[0].pObject;
   347 	  pMappingCount = (d->objdict + offsetObjdict + numPdo)->pSubindex[0].pObject;
   335 	  numMap = 0;
   348 	  numMap = 0;
   336 	  while (numMap < *pMappingCount) {
   349 	  while (numMap < *pMappingCount) {
   337 	    pMappingParameter = (d->objdict + offsetObjdict + numPdo)->pSubindex[numMap + 1].pObject;
   350 	    pMappingParameter = (d->objdict + offsetObjdict + numPdo)->pSubindex[numMap + 1].pObject;
   338 	    /* Get the mapped variable */
   351 	    /* Get the mapped variable */
   339 	    Size = ((UNS8)(((*pMappingParameter) & 0xFF) >> 3));
   352 	    Size = ((UNS8)(((*pMappingParameter) & 0xFF) >> 3)); 
   340 	    objDict = getODentry( d, (UNS16)((*pMappingParameter) >> (UNS8)16), 
   353 	    objDict = getODentry( d, (UNS16)((*pMappingParameter) >> (UNS8)16), 
   341 				     (UNS8)(( (*pMappingParameter) >> (UNS8)8 ) & 0xFF),
   354 				     (UNS8)(( (*pMappingParameter) >> (UNS8)8 ) & 0xFF),
   342 				     (void *)&d->process_var.data[offset], &Size, &dataType, 0 );
   355 				     (void *)&d->process_var.data[offset], &Size, &dataType, 0 );
   343 	    if (objDict != OD_SUCCESSFUL) {
   356 	    if (objDict != OD_SUCCESSFUL) {
   344 	      MSG_ERR(0x1948, "error accessing to the mapped var : ", numMap + 1);  
   357 	      MSG_ERR(0x1948, "error accessing to the mapped var : ", numMap + 1);  
   360 		
   373 		
   361   return 0;
   374   return 0;
   362 }
   375 }
   363 
   376 
   364 
   377 
   365 
   378 void CopyBits(UNS8 NbBits, UNS8* SrcByteIndex, UNS8 SrcBitIndex, UNS8 SrcBigEndian, UNS8* DestByteIndex, UNS8 DestBitIndex, UNS8 DestBigEndian)
       
   379 {
       
   380 	//This loop copy as many bits that it can each time, crossing successively bytes
       
   381 	// boundaries from LSB to MSB.
       
   382 	while(NbBits > 0)
       
   383 	{
       
   384 		// Bit missalignement between src and dest
       
   385 		INTEGER8 Vect = DestBitIndex - SrcBitIndex;
       
   386 		
       
   387 		// We can now get src and align it to dest
       
   388 		UNS8 Aligned = Vect>0 ? *SrcByteIndex << Vect : *SrcByteIndex >> -Vect;
       
   389 		
       
   390 		// Compute the nb of bit we will be able to copy
       
   391 		UNS8 BoudaryLimit = (Vect>0 ? 8 - DestBitIndex :  8 - SrcBitIndex );
       
   392 		UNS8 BitsToCopy = BoudaryLimit > NbBits ? NbBits : BoudaryLimit;
       
   393 
       
   394 		// Create a mask that will serve in:
       
   395 		UNS8 Mask = ((0xff << (DestBitIndex + BitsToCopy)) | (0xff >> (8 - DestBitIndex)));
       
   396 
       
   397 		// - Filtering src
       
   398 		UNS8 Filtered = Aligned & ~Mask;
       
   399 
       
   400 		// - and erase bits where we write, preserve where we don't
       
   401 		*DestByteIndex &= Mask;
       
   402 
       
   403 		// Then write.
       
   404 		*DestByteIndex |= Filtered ;
       
   405 
       
   406 		//Compute next time cursors for src
       
   407 		if((SrcBitIndex += BitsToCopy)>7)	// cross boundary ?
       
   408 		{
       
   409 			SrcBitIndex = 0;							// First bit
       
   410 			SrcByteIndex += (SrcBigEndian ? -1 : 1);	// Next byte
       
   411 		}
       
   412 
       
   413 		//Compute next time cursors for dest
       
   414 		if((DestBitIndex += BitsToCopy)>7)
       
   415 		{
       
   416 			DestBitIndex = 0;							// First bit
       
   417 			DestByteIndex += (DestBigEndian ? -1 : 1);// Next byte
       
   418 		}
       
   419 		
       
   420 		//And decrement counter.
       
   421 		NbBits -= BitsToCopy;
       
   422 	}
       
   423 
       
   424 }
   366 
   425 
   367 #if 0
   426 #if 0
   368 
   427 
   369 /*********************************************************************/
   428 /*********************************************************************/
   370 /* TODO : reimplement this using CallBacks                           */
   429 /* TODO : reimplement this using CallBacks                           */
   412 	      ind++;
   471 	      ind++;
   413 	      offsetObjdict++;  
   472 	      offsetObjdict++;  
   414 	      offsetObjdictPrm++;
   473 	      offsetObjdictPrm++;
   415 	      continue;
   474 	      continue;
   416 	    }
   475 	    }
   417 	    pMappingCount = d->objdict[offsetObjdict].pSubindex[0].pObject;
   476 	    pMappingCount = d->objdict[offsetObjdict].pSubindex[0].pObject; 
   418 	    numMap = 1; /* mapped variable */
   477 	    numMap = 1; /* mapped variable */
   419 	    while (numMap <= *pMappingCount) {
   478 	    while (numMap <= *pMappingCount) {
   420 	      pMappingParameter = d->objdict[offsetObjdict].pSubindex[numMap].pObject;
   479 	      pMappingParameter = d->objdict[offsetObjdict].pSubindex[numMap].pObject;
   421 	      /* Get the variable */
   480 	      /* Get the variable */
   422 	      objDict = getODentry( d,
   481 	      objDict = getODentry( d,