/*!
**功能:从机把第numPdo个TPDO对应的数据封装到Message 包中
**
** @param d
** @param numPdo TPDO_com TPDO communication parameters OD entry TPDO_map TPDO mapping parameters OD entry
** @param pdo can数据包
**
** @return
**/
UNS8 buildPDO (CO_Data * d, UNS8 numPdo, Message * pdo)
{
//TPDO 通讯参数 1800 h 的对象
const indextable *TPDO_com = d->objdict + d->firstIndex->PDO_TRS + numPdo;
//映射参数 1A00 h的对象
const indextable *TPDO_map = d->objdict + d->firstIndex->PDO_TRS_MAP + numPdo;
UNS8 prp_j = 0x00;
UNS32 offset = 0x00000000;
const UNS8 *pMappingCount = (UNS8 *) TPDO_map->pSubindex[0].pObject;//pdo映射有效对象数
pdo->cob_id = (UNS16) UNS16_LE(*(UNS32*)TPDO_com->pSubindex[1].pObject & 0x7FF);//支持的cob-id
pdo->rtr = NOT_A_REQUEST;//非遥控帧
MSG_WAR (0x3009, " PDO CobId is : ",
*(UNS32 *) TPDO_com->pSubindex[1].pObject);
MSG_WAR (0x300D, " Number of objects mapped : ", *pMappingCount);
do
{
UNS8 dataType; /* Unused */
UNS8 tmp[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; /* temporary space to hold bits */
/* pointer fo the var which holds the mapping parameter of an mapping entry */
UNS32 *pMappingParameter =
(UNS32 *) TPDO_map->pSubindex[prp_j + 1].pObject;
UNS16 index = (UNS16) ((*pMappingParameter) >> 16);//获取索引
UNS32 Size = (UNS32) (*pMappingParameter & (UNS32) 0x000000FF); /* Size in bits *///数据类型
/* get variable only if Size != 0 and Size is lower than remaining bits in the PDO */
if (Size && ((offset + Size) <= 64))
{
UNS32 ByteSize = 1 + ((Size - 1) >> 3); /*1->8 => 1 ; 9->16 => 2, ... */
UNS8 subIndex =
(UNS8) (((*pMappingParameter) >> (UNS8) 8) & (UNS32) 0x000000FF);//获取子索引
MSG_WAR (0x300F, " got mapping parameter : ", *pMappingParameter);
MSG_WAR (0x3050, " at index : ", TPDO_map->index);
MSG_WAR (0x3051, " sub-index : ", prp_j + 1);
if (getODentry (d, index, subIndex, tmp, &ByteSize, &dataType, 0) !=
OD_SUCCESSFUL)//取得数据
{
MSG_ERR (0x1013,
" Couldn't find mapped variable at index-subindex-size : ",
(UNS32) (*pMappingParameter));
return 0xFF;
}
/* copy bit per bit in little endian */
CopyBits ((UNS8) Size, ((UNS8 *) tmp), 0, 0,
(UNS8 *) & pdo->data[offset >> 3], (UNS8)(offset % 8), 0);
offset += Size;
}
prp_j++;
}
while (prp_j < *pMappingCount);
pdo->len = (UNS8)(1 + ((offset - 1) >> 3));
MSG_WAR (0x3015, " End scan mapped variable", 0);
return 0;
}