STM32 107 CAN过滤器和掩码 设置函数
[复制链接]
芯片资料没看到如何设置过滤器ID和掩码,结果弄了好久才搞清楚,原来固件库的接口还需要自己加工一下才行。
CP_VOID CanSetFilter(CAN_NUM_TypeDef byDevNo,CP_BYTE byFilterNumber,CP_DWORD dwId,CP_DWORD dwMask,CP_BYTE bIsStdId)
{
assert_param(byFilterNumber<14);
assert_param(IS_CAN_IDTYPE(bIsStdId));
CAN_FilterInitTypeDef CAN_FilterInitStructure;
if(byDevNo == CAN_1)
{
CAN_FilterInitStructure.CAN_FilterNumber=byFilterNumber;//CPU默认CAN2使用0-13号过滤器
}
else
{
CAN_FilterInitStructure.CAN_FilterNumber=byFilterNumber+14;//CPU默认CAN2使用14-27号过滤器
}
dwId = (dwId << 3) & 0xFFFFFFF8;//0位预留0,1位表示数据帧or远程帧,2位表示标准ID or 扩展ID
dwId |= (((CP_DWORD)bIsStdId) & 0x00000007);
dwMask = (dwMask << 3) & 0xFFFFFFF8;//0位预留0,1位表示数据帧or远程帧,2位表示标准ID or 扩展ID
dwMask |= (((CP_DWORD)bIsStdId) & 0x00000007);
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=(CP_WORD)((dwId >> 16) & 0x0000FFFF);
CAN_FilterInitStructure.CAN_FilterIdLow=(CP_WORD)(dwId & 0x0000FFFF);
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=(CP_WORD)((dwMask >> 16) & 0x0000FFFF);
CAN_FilterInitStructure.CAN_FilterMaskIdLow=(CP_WORD)(dwMask & 0x0000FFFF);
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
}
[ 本帖最后由 likeqi2599 于 2011-6-24 09:31 编辑 ]
|