2 This file is part of CanFestival, a library implementing CanOpen
5 Copyright (C): Edouard TISSERANT and Francis DUPIN
7 See COPYING file for copyrights details.
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include "canfestival.h"
31 ** @author Edouard TISSERANT and Francis DUPIN
32 ** @date Tue Jun 5 09:32:32 2007
43 ** @param TPDO_com TPDO communication parameters OD entry
44 ** @param TPDO_map TPDO mapping parameters OD entry
50 buildPDO (CO_Data * d, UNS8 numPdo, Message * pdo)
52 const indextable *TPDO_com = d->objdict + d->firstIndex->PDO_TRS + numPdo;
53 const indextable *TPDO_map =
54 d->objdict + d->firstIndex->PDO_TRS_MAP + numPdo;
58 const UNS8 *pMappingCount = (UNS8 *) TPDO_map->pSubindex[0].pObject;
60 pdo->cob_id = UNS16_LE(*(UNS32*)TPDO_com->pSubindex[1].pObject & 0x7FF);
61 pdo->rtr = NOT_A_REQUEST;
63 MSG_WAR (0x3009, " PDO CobId is : ",
64 *(UNS32 *) TPDO_com->pSubindex[1].pObject);
65 MSG_WAR (0x300D, " Number of objects mapped : ", *pMappingCount);
69 UNS8 dataType; /* Unused */
70 UNS8 tmp[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; /* temporary space to hold bits */
72 /* pointer fo the var which holds the mapping parameter of an mapping entry */
73 UNS32 *pMappingParameter =
74 (UNS32 *) TPDO_map->pSubindex[prp_j + 1].pObject;
75 UNS16 index = (UNS16) ((*pMappingParameter) >> 16);
76 UNS8 Size = (UNS8) (*pMappingParameter & (UNS32) 0x000000FF); /* Size in bits */
78 /* get variable only if Size != 0 and Size is lower than remaining bits in the PDO */
79 if (Size && ((offset + Size) <= 64))
81 UNS8 ByteSize = 1 + ((Size - 1) >> 3); /*1->8 => 1 ; 9->16 => 2, ... */
83 (UNS8) (((*pMappingParameter) >> (UNS8) 8) & (UNS32) 0x000000FF);
85 MSG_WAR (0x300F, " got mapping parameter : ", *pMappingParameter);
86 MSG_WAR (0x3050, " at index : ", TPDO_map->index);
87 MSG_WAR (0x3051, " sub-index : ", prp_j + 1);
89 if (getODentry (d, index, subIndex, tmp, &ByteSize, &dataType, 0) !=
93 " Couldn't find mapped variable at index-subindex-size : ",
94 (UNS16) (*pMappingParameter));
97 /* copy bit per bit in little endian */
98 CopyBits (Size, ((UNS8 *) tmp), 0, 0,
99 (UNS8 *) & pdo->data[offset >> 3], offset % 8, 0);
105 while (prp_j < *pMappingCount);
107 pdo->len = 1 + ((offset - 1) >> 3);
109 MSG_WAR (0x3015, " End scan mapped variable", 0);
123 sendPDOrequest (CO_Data * d, UNS16 RPDOIndex)
126 UNS16 offset = d->firstIndex->PDO_RCV;
127 UNS16 lastIndex = d->lastIndex->PDO_RCV;
129 /* Sending the request only if the cobid have been found on the PDO
131 /* part dictionary */
133 MSG_WAR (0x3930, "sendPDOrequest RPDO Index : ", RPDOIndex);
135 if (offset && RPDOIndex >= 0x1400)
137 offset += RPDOIndex - 0x1400;
138 if (offset <= lastIndex)
141 pwCobId = (UNS16 *) d->objdict[offset].pSubindex[1].pObject;
143 MSG_WAR (0x3930, "sendPDOrequest cobId is : ", *pwCobId);
146 pdo.cob_id = *pwCobId;
149 return canSend (d->canHandle, &pdo);
153 MSG_ERR (0x1931, "sendPDOrequest : RPDO Index not found : ", RPDOIndex);
167 proceedPDO (CO_Data * d, Message * m)
170 UNS8 numMap; /* Number of the mapped varable */
171 UNS8 *pMappingCount = NULL; /* count of mapped objects... */
172 /* pointer to the var which is mapped to a pdo... */
173 /* void * pMappedAppObject = NULL; */
174 /* pointer fo the var which holds the mapping parameter of an
176 UNS32 *pMappingParameter = NULL;
177 UNS8 *pTransmissionType = NULL; /* pointer to the transmission
179 UNS16 *pwCobId = NULL;
189 MSG_WAR (0x3935, "proceedPDO, cobID : ", ((*m).cob_id & UNS16_LE (0x7ff)));
193 if ((*m).rtr == NOT_A_REQUEST)
194 { /* The PDO received is not a
197 offsetObjdict = d->firstIndex->PDO_RCV;
198 lastIndex = d->lastIndex->PDO_RCV;
200 /* study of all the PDO stored in the dictionary */
202 while (offsetObjdict <= lastIndex)
209 /* get CobId of the dictionary correspondant to the received
212 (UNS16 *) d->objdict[offsetObjdict].pSubindex[1].pObject;
213 /* check the CobId coherance */
214 /*pwCobId is the cobId read in the dictionary at the state 3
216 if (*pwCobId == (*m).cob_id)
218 /* The cobId is recognized */
220 MSG_WAR (0x3936, "cobId found at index ",
226 /* cobId received does not match with those write in the
234 case state4: /* Get Mapped Objects Number */
235 /* The cobId of the message received has been found in the
237 offsetObjdict = d->firstIndex->PDO_RCV_MAP;
238 lastIndex = d->lastIndex->PDO_RCV_MAP;
240 (UNS8 *) (d->objdict + offsetObjdict +
241 numPdo)->pSubindex[0].pObject;
243 while (numMap < *pMappingCount)
245 UNS8 tmp[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
248 (UNS32 *) (d->objdict + offsetObjdict +
249 numPdo)->pSubindex[numMap + 1].pObject;
250 if (pMappingParameter == NULL)
252 MSG_ERR (0x1937, "Couldn't get mapping parameter : ",
256 /* Get the addresse of the mapped variable. */
257 /* detail of *pMappingParameter : */
258 /* The 16 hight bits contains the index, the medium 8 bits
259 contains the subindex, */
260 /* and the lower 8 bits contains the size of the mapped
263 Size = (UNS8) (*pMappingParameter & (UNS32) 0x000000FF);
265 /* set variable only if Size != 0 and Size is lower than remaining bits in the PDO */
266 if (Size && ((offset + Size) <= (m->len << 3)))
268 /* copy bit per bit in little endian */
269 CopyBits (Size, (UNS8 *) & m->data[offset >> 3],
270 offset % 8, 0, ((UNS8 *) tmp), 0, 0);
272 ByteSize = 1 + ((Size - 1) >> 3); /*1->8 => 1 ; 9->16 =>
276 setODentry (d, (UNS16) ((*pMappingParameter) >> 16),
277 (UNS8) (((*pMappingParameter) >> 8) &
278 0xFF), tmp, &ByteSize, 0);
280 if (objDict != OD_SUCCESSFUL)
283 "error accessing to the mapped var : ",
285 MSG_WAR (0x2939, " Mapped at index : ",
286 (*pMappingParameter) >> 16);
287 MSG_WAR (0x2940, " subindex : ",
288 ((*pMappingParameter) >> 8) & 0xFF);
293 "Variable updated with value received by PDO cobid : ",
295 MSG_WAR (0x3943, " Mapped at index : ",
296 (*pMappingParameter) >> 16);
297 MSG_WAR (0x3944, " subindex : ",
298 ((*pMappingParameter) >> 8) & 0xFF);
299 /* MSG_WAR(0x3945, " data : ",*((UNS32*)pMappedAppObject)); */
303 } /* end loop while on mapped variables */
309 } /* end switch status */
311 } /* end if Donnees */
312 else if ((*m).rtr == REQUEST)
314 MSG_WAR (0x3946, "Receive a PDO request cobId : ", m->cob_id);
316 offsetObjdict = d->firstIndex->PDO_TRS;
317 lastIndex = d->lastIndex->PDO_TRS;
319 while (offsetObjdict <= lastIndex)
321 /* study of all PDO stored in the objects dictionary */
326 case state1: /* check the CobId */
327 /* get CobId of the dictionary which match to the received PDO
330 (UNS16 *) (d->objdict +
331 offsetObjdict)->pSubindex[1].pObject;
332 if (*pwCobId == (*m).cob_id)
346 case state4: /* check transmission type */
348 (UNS8 *) d->objdict[offsetObjdict].pSubindex[2].pObject;
349 /* If PDO is to be sampled and send on RTR, do it */
350 if ((*pTransmissionType == TRANS_RTR))
354 /* RTR_SYNC mean data is prepared at SYNC, and transmitted on RTR */
356 else if ((*pTransmissionType == TRANS_RTR_SYNC))
358 if (d->PDO_status[numPdo].
359 transmit_type_parameter & PDO_RTR_SYNC_READY)
361 /*Data ready, just send */
362 canSend (d->canHandle,
363 &d->PDO_status[numPdo].last_message);
368 /* if SYNC did never occur, force emission with current data */
369 /* DS301 do not tell what to do in such a case... */
371 "Not ready RTR_SYNC TPDO send current data : ",
377 else if ((*pTransmissionType == TRANS_EVENT_PROFILE) ||
378 (*pTransmissionType == TRANS_EVENT_SPECIFIC))
380 /* Zap all timers and inhibit flag */
381 d->PDO_status[numPdo].event_timer =
382 DelAlarm (d->PDO_status[numPdo].event_timer);
383 d->PDO_status[numPdo].inhibit_timer =
384 DelAlarm (d->PDO_status[numPdo].inhibit_timer);
385 d->PDO_status[numPdo].transmit_type_parameter &=
387 /* Call PDOEventTimerAlarm for this TPDO, this will trigger emission et reset timers */
388 PDOEventTimerAlarm (d, numPdo);
393 /* The requested PDO is not to send on request. So, does
395 MSG_WAR (0x2947, "PDO is not to send on request : ",
400 case state5: /* build and send requested PDO */
403 if (buildPDO (d, numPdo, &pdo))
405 MSG_ERR (0x1948, " Couldn't build TPDO n°", numPdo);
408 canSend (d->canHandle, &pdo);
411 } /* end switch status */
413 } /* end if Requete */
422 ** @param SrcByteIndex
423 ** @param SrcBitIndex
424 ** @param SrcBigEndian
425 ** @param DestByteIndex
426 ** @param DestBitIndex
427 ** @param DestBigEndian
430 CopyBits (UNS8 NbBits, UNS8 * SrcByteIndex, UNS8 SrcBitIndex,
431 UNS8 SrcBigEndian, UNS8 * DestByteIndex, UNS8 DestBitIndex,
434 /* This loop copy as many bits that it can each time, crossing */
435 /* successively bytes */
436 // boundaries from LSB to MSB.
439 /* Bit missalignement between src and dest */
440 INTEGER8 Vect = DestBitIndex - SrcBitIndex;
442 /* We can now get src and align it to dest */
444 Vect > 0 ? *SrcByteIndex << Vect : *SrcByteIndex >> -Vect;
446 /* Compute the nb of bit we will be able to copy */
447 UNS8 BoudaryLimit = (Vect > 0 ? 8 - DestBitIndex : 8 - SrcBitIndex);
448 UNS8 BitsToCopy = BoudaryLimit > NbBits ? NbBits : BoudaryLimit;
450 /* Create a mask that will serve in: */
452 ((0xff << (DestBitIndex + BitsToCopy)) |
453 (0xff >> (8 - DestBitIndex)));
455 /* - Filtering src */
456 UNS8 Filtered = Aligned & ~Mask;
458 /* - and erase bits where we write, preserve where we don't */
459 *DestByteIndex &= Mask;
462 *DestByteIndex |= Filtered;
464 /*Compute next time cursors for src */
465 if ((SrcBitIndex += BitsToCopy) > 7) /* cross boundary ? */
467 SrcBitIndex = 0; /* First bit */
468 SrcByteIndex += (SrcBigEndian ? -1 : 1); /* Next byte */
472 /*Compute next time cursors for dest */
473 if ((DestBitIndex += BitsToCopy) > 7)
475 DestBitIndex = 0; /* First bit */
476 DestByteIndex += (DestBigEndian ? -1 : 1); /* Next byte */
479 /*And decrement counter. */
480 NbBits -= BitsToCopy;
494 sendPDOevent (CO_Data * d)
496 /* Calls _sendPDOevent specifying it is not a sync event */
497 return _sendPDOevent (d, 0);
502 PDOEventTimerAlarm (CO_Data * d, UNS32 pdoNum)
504 /* This is needed to avoid deletion of re-attribuated timer */
505 d->PDO_status[pdoNum].event_timer = TIMER_NONE;
506 /* force emission of PDO by artificially changing last emitted */
507 d->PDO_status[pdoNum].last_message.cob_id = 0;
508 _sendPDOevent (d, 0); /* not a Sync Event */
512 PDOInhibitTimerAlarm (CO_Data * d, UNS32 pdoNum)
514 /* This is needed to avoid deletion of re-attribuated timer */
515 d->PDO_status[pdoNum].inhibit_timer = TIMER_NONE;
516 /* Remove inhibit flag */
517 d->PDO_status[pdoNum].transmit_type_parameter &= ~PDO_INHIBITED;
518 _sendPDOevent (d, 0); /* not a Sync Event */
525 ** @param isSyncEvent
531 _sendPDOevent (CO_Data * d, UNS8 isSyncEvent)
533 UNS8 pdoNum = 0x00; /* number of the actual processed pdo-nr. */
534 UNS8 *pTransmissionType = NULL;
535 UNS8 status = state3;
536 UNS16 offsetObjdict = d->firstIndex->PDO_TRS;
537 UNS16 offsetObjdictMap = d->firstIndex->PDO_TRS_MAP;
538 UNS16 lastIndex = d->lastIndex->PDO_TRS;
540 /* study all PDO stored in the objects dictionary */
543 Message pdo = Message_Initializer;
544 while (offsetObjdict <= lastIndex)
549 if ( /*d->objdict[offsetObjdict].bSubCount < 5 || not necessary with objdictedit (always 5) */
550 /* check if TPDO is not valid */
551 *(UNS32 *) d->objdict[offsetObjdict].pSubindex[1].
552 pObject & 0x80000000)
554 MSG_WAR (0x3960, "Not a valid PDO ", 0x1800 + pdoNum);
559 /* get the PDO transmission type */
561 (UNS8 *) d->objdict[offsetObjdict].pSubindex[2].pObject;
562 MSG_WAR (0x3962, "Reading PDO at index : ", 0x1800 + pdoNum);
564 /* check if transmission type is SYNCRONOUS */
565 /* The message is transmited every n SYNC with n=TransmissionType */
567 (*pTransmissionType >= TRANS_SYNC_MIN) &&
568 (*pTransmissionType <= TRANS_SYNC_MAX) &&
569 (++d->PDO_status[pdoNum].transmit_type_parameter ==
572 /*Reset count of SYNC */
573 d->PDO_status[pdoNum].transmit_type_parameter = 0;
574 MSG_WAR (0x3964, " PDO is on SYNCHRO. Trans type : ",
577 Message msg_init = Message_Initializer;
580 if (buildPDO (d, pdoNum, &pdo))
582 MSG_ERR (0x1906, " Couldn't build TPDO number : ",
588 /* If transmission RTR, with data sampled on SYNC */
590 else if (isSyncEvent && (*pTransmissionType == TRANS_RTR_SYNC))
593 (d, pdoNum, &d->PDO_status[pdoNum].last_message))
595 MSG_ERR (0x1966, " Couldn't build TPDO number : ",
597 d->PDO_status[pdoNum].transmit_type_parameter &=
602 d->PDO_status[pdoNum].transmit_type_parameter |=
607 /* If transmission on Event and not inhibited, check for changes */
611 && (*pTransmissionType == TRANS_SYNC_ACYCLIC))
613 ((*pTransmissionType == TRANS_EVENT_PROFILE
614 || *pTransmissionType == TRANS_EVENT_SPECIFIC)
615 && !(d->PDO_status[pdoNum].
616 transmit_type_parameter & PDO_INHIBITED)))
618 MSG_WAR (0x3968, " PDO is on EVENT. Trans type : ",
621 Message msg_init = Message_Initializer;
624 if (buildPDO (d, pdoNum, &pdo))
626 MSG_ERR (0x3907, " Couldn't build TPDO number : ",
632 /*Compare new and old PDO */
633 if (d->PDO_status[pdoNum].last_message.cob_id == pdo.cob_id
634 && d->PDO_status[pdoNum].last_message.len == pdo.len &&
636 *(UNS64 *) (&d->PDO_status[pdoNum].last_message.
637 data[0]) == *(UNS64 *) (&pdo.data[0])
638 #else /* don't ALLOW_64BIT_OPS */
639 *(UNS32 *) (&d->PDO_status[pdoNum].last_message.
640 data[0]) == *(UNS32 *) (&pdo.data[0])
641 && *(UNS32 *) (&d->PDO_status[pdoNum].last_message.
642 data[4]) == *(UNS32 *) (&pdo.data[4])
646 /* No changes -> go to next pdo */
652 UNS16 EventTimerDuration;
653 UNS16 InhibitTimerDuration;
655 MSG_WAR (0x306A, "Changes TPDO number : ", pdoNum);
656 /* Changes detected -> transmit message */
658 *(UNS16 *) d->objdict[offsetObjdict].pSubindex[5].
660 InhibitTimerDuration =
661 *(UNS16 *) d->objdict[offsetObjdict].pSubindex[3].
666 /* Start both event_timer and inhibit_timer */
667 if (EventTimerDuration)
669 DelAlarm (d->PDO_status[pdoNum].event_timer);
670 d->PDO_status[pdoNum].event_timer =
671 SetAlarm (d, pdoNum, &PDOEventTimerAlarm,
672 MS_TO_TIMEVAL (EventTimerDuration), 0);
675 if (InhibitTimerDuration)
677 DelAlarm (d->PDO_status[pdoNum].inhibit_timer);
678 d->PDO_status[pdoNum].inhibit_timer =
679 SetAlarm (d, pdoNum, &PDOInhibitTimerAlarm,
680 US_TO_TIMEVAL (InhibitTimerDuration *
682 /* and inhibit TPDO */
683 d->PDO_status[pdoNum].transmit_type_parameter |=
692 " PDO is not on EVENT or synchro or not at this SYNC. Trans type : ",
697 case state5: /*Send the pdo */
698 /*store_as_last_message */
699 d->PDO_status[pdoNum].last_message = pdo;
700 MSG_WAR (0x396D, "sendPDO cobId :", pdo.cob_id);
701 MSG_WAR (0x396E, " Nb octets : ", pdo.len);
703 canSend (d->canHandle, &pdo);
706 case state11: /*Go to next TPDO */
710 MSG_WAR (0x3970, "next pdo index : ", pdoNum);
715 MSG_ERR (0x1972, "Unknown state has been reached : %d", status);
717 } /* end switch case */
734 TPDO_Communication_Parameter_Callback (CO_Data * d,
735 const indextable * OD_entry,
738 /* If PDO are actives */
739 if (d->CurrentCommunicationState.csPDO)
742 case 2: /* Changed transmition type */
743 case 3: /* Changed inhibit time */
744 case 5: /* Changed event time */
746 const indextable *TPDO_com = d->objdict + d->firstIndex->PDO_TRS;
747 UNS8 numPdo = OD_entry - TPDO_com; /* number of the actual processed pdo-nr. */
749 /* Zap all timers and inhibit flag */
750 d->PDO_status[numPdo].event_timer =
751 DelAlarm (d->PDO_status[numPdo].event_timer);
752 d->PDO_status[numPdo].inhibit_timer =
753 DelAlarm (d->PDO_status[numPdo].inhibit_timer);
754 d->PDO_status[numPdo].transmit_type_parameter = 0;
755 /* Call PDOEventTimerAlarm for this TPDO, this will trigger emission et reset timers */
756 PDOEventTimerAlarm (d, numPdo);
760 default: /* other subindex are ignored */
767 PDOInit (CO_Data * d)
769 /* For each TPDO mapping parameters */
770 UNS16 pdoIndex = 0x1800; /* OD index of TDPO */
772 UNS16 offsetObjdict = d->firstIndex->PDO_TRS;
773 UNS16 lastIndex = d->lastIndex->PDO_TRS;
775 while (offsetObjdict <= lastIndex)
777 /* Assign callbacks to sensible TPDO mapping subindexes */
779 ODCallback_t *CallbackList;
780 /* Find callback list */
781 scanIndexOD (d, pdoIndex, &errorCode, &CallbackList);
782 if (errorCode == OD_SUCCESSFUL && CallbackList)
784 /*Assign callbacks to corresponding subindex */
785 /* Transmission type */
786 CallbackList[2] = &TPDO_Communication_Parameter_Callback;
788 CallbackList[3] = &TPDO_Communication_Parameter_Callback;
790 CallbackList[5] = &TPDO_Communication_Parameter_Callback;
796 /* Trigger a non-sync event */
797 _sendPDOevent (d, 0);
801 PDOStop (CO_Data * d)
803 /* For each TPDO mapping parameters */
804 UNS8 pdoNum = 0x00; /* number of the actual processed pdo-nr. */
805 UNS16 offsetObjdict = d->firstIndex->PDO_TRS;
806 UNS16 lastIndex = d->lastIndex->PDO_TRS;
808 while (offsetObjdict <= lastIndex)
810 /* Delete TPDO timers */
811 d->PDO_status[pdoNum].event_timer =
812 DelAlarm (d->PDO_status[pdoNum].event_timer);
813 d->PDO_status[pdoNum].inhibit_timer =
814 DelAlarm (d->PDO_status[pdoNum].inhibit_timer);
815 /* Reset transmit type parameter */
816 d->PDO_status[pdoNum].transmit_type_parameter = 0;
817 d->PDO_status[pdoNum].last_message.cob_id = 0;