Hallo everyone.
I have implemented a code for CAN bus communication using the microZED. Basically I have used the given example functions, except for the receiving of the CAN Messages, for which I used a function of my own..
I am currently trying to activate the Acceptance Flters. The Problem is that, although everything seems to work fine in the debbuging, I am still receiving each and every message from the CAN bus, no matter the identifier.
Does someone know how to correctly activate the Acceptance Filters??
If a message gets discarded, it means that it will not make it to the Rx buffer in the first place, right?? (Hardware level)
Thanks a lot in advance..
Here is (a part of) my code:
static u32 MaskValue = 0x2000;
static u32 IdValue = 0x2000;
static u32 MaskValue2 = 0x4000;
static u32 IdValue2 = 0x4000;
int CAN_initialize()
{
tint Status;
txil_printf("initialize CAN... ");
tStatus = CAN_init(&IntcInstance, &CanInstance,
tttt CAN_DEVICE_ID, CAN_INTR_VEC_ID);
tif (Status != XST_SUCCESS) {
ttxil_printf("CAN Initialization Failedr
");
ttreturn XST_FAILURE;
t}
txil_printf("r
");
treturn XST_SUCCESS;
}
int CAN_init(XScuGic *IntcInstPtr, XCanPs *CanInstPtr,
tttu16 CanDeviceId, u16 CanIntrId)
{
tint Status;
tXCanPs_Config *ConfigPtr;
t/*
t * Initialize the Can device.
t */
tConfigPtr = XCanPs_LookupConfig(CanDeviceId);
tif (ConfigPtr == NULL) {
ttreturn XST_FAILURE;
t}
tXCanPs_CfgInitialize(CanInstPtr,
ttttConfigPtr,
ttttConfigPtr->BaseAddr);
t/*
t * Run self-test on the device, which verifies basic sanity of the
t * device and the driver.
t */
tStatus = XCanPs_SelfTest(CanInstPtr);
tif (Status != XST_SUCCESS) {
ttreturn XST_FAILURE;
t}
t/*
t * Configure CAN device.
t */
tConfig(CanInstPtr);
t/*
t * Set interrupt handlers.
t */
tXCanPs_SetHandler(CanInstPtr, XCANPS_HANDLER_SEND,
ttt(void *)SendHandler, (void *)CanInstPtr);
tXCanPs_SetHandler(CanInstPtr, XCANPS_HANDLER_RECV,
ttt(void *)RecvHandler, (void *)CanInstPtr);
tXCanPs_SetHandler(CanInstPtr, XCANPS_HANDLER_ERROR,
ttt(void *)ErrorHandler, (void *)CanInstPtr);
tXCanPs_SetHandler(CanInstPtr, XCANPS_HANDLER_EVENT,
ttt(void *)EventHandler, (void *)CanInstPtr);
t/*
t * Initialize the flags.
t */
tSendDone = FALSE;
tRecvDone = FALSE;
tLoopbackError = FALSE;
t/*
t * Connect to the interrupt controller.
t */
tStatus = SetupInterruptSystem(IntcInstPtr,
tttttCanInstPtr,
tttttCanIntrId);
tif (Status != XST_SUCCESS) {
ttreturn XST_FAILURE;
t}
t/*
t * Enable all interrupts in CAN device.
t */
tXCanPs_IntrEnable(CanInstPtr, XCANPS_IXR_ALL);
t
t/*
t * Enter Normal Mode.
t */
// tXCanPs_EnterMode(CanInstPtr, XCANPS_MODE_LOOPBACK);
// twhile(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_LOOPBACK);
tXCanPs_EnterMode(CanInstPtr, XCANPS_MODE_NORMAL);
twhile(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_NORMAL);
/*
* Configure Acceptance Filters
*/
t Access_Filters(CanInstPtr, MaskValue, IdValue, MaskValue2, IdValue2);
t/*if (Status != XST_SUCCESS){
ttxil_printf("Access Filter Function FAILEDr
");
ttreturn XST_FAILURE;
t}*/
t treturn XST_SUCCESS;
}
int Access_Filters(XCanPs *CanInstPtr,u32 MaskValue, u32 IdValue,u32 MaskValue2,u32 IdValue2)
{
int Status;
ttt u32 *MaskValuePtr = &MaskValue;
ttt u32 *MaskValue2Ptr = &MaskValue2;
ttt u32 *IdValuePtr = &IdValue;
ttt u32 *IdValue2Ptr = &IdValue2;
t /*
t tt * Acceptance Filter Initialization and configuration
t tt */
tttt// disable all filters
t tt tXCanPs_AcceptFilterDisable(CanInstPtr, XCANPS_AFR_UAF_ALL_MASK);
t tt tStatus = XCanPs_AcceptFilterGetEnabled(CanInstPtr);
t tt tif (Status != 0){
t tt t treturn XST_FAILURE;
t tt t tt t}
t tt t/*
t tt t * Wait for filter to be not busy
t tt t */
t tt t//Status = XCanPs_GetStatus(CanInstPtr);
t tt twhile(XCanPs_IsAcceptFilterBusy(CanInstPtr));
t tt t/*
t tt t * Configure Acceptance Filter No.1
t tt t */
t tt tStatus = XCanPs_AcceptFilterSet(CanInstPtr, XCANPS_AFR_UAF1_MASK,
t tt tttt MaskValue,IdValue);
t tt tif (Status != XST_SUCCESS){
t tt ttreturn XST_FAILURE;
t tt t}
t tt t/*
t tt t * Wait for filter to be not busy
t tt t */
t tt t while(XCanPs_IsAcceptFilterBusy(CanInstPtr));
t tt t/*
t tt t * Configure Acceptance Filter No.1
t tt t */
t tt t Status = XCanPs_AcceptFilterSet(CanInstPtr, XCANPS_AFR_UAF2_MASK,
t tt t tttt MaskValue2,IdValue2);
t tt t tif (Status != XST_SUCCESS){
t tt t ttreturn XST_FAILURE;
t tt t t}
t tt t tXCanPs_AcceptFilterGet(CanInstPtr, XCANPS_AFR_UAF1_MASK,
t tt t tttt MaskValuePtr, IdValuePtr);
t tt t tXCanPs_AcceptFilterGet(CanInstPtr, XCANPS_AFR_UAF2_MASK,
t tt t tt tt t tttt MaskValue2Ptr, IdValue2Ptr);
t tt t t/*
t tt t t * Configure Acceptance Filter No.2
t tt t t */
t tt t tXCanPs_AcceptFilterEnable(CanInstPtr, XCANPS_AFR_UAF2_MASK);
t tt t tStatus = XCanPs_AcceptFilterGetEnabled(CanInstPtr);
t tt t tt tt t tif (Status != 0x2){
t tt t tt tt t t t return XST_FAILURE;
t tt t tt tt t t t t}
t tt t tt tt //xil_printf("
Acceptance Filter 2 enabledr");
t tt t tXCanPs_AcceptFilterEnable(CanInstPtr, XCANPS_AFR_UAF1_MASK);
t tt t t/*
t tt t t* Check if they have been enabled. if not set failure flag
t tt t t */
t tt t tStatus = XCanPs_AcceptFilterGetEnabled(CanInstPtr);
t tt t tif (Status != 0x3){
t tt t t t return XST_FAILURE;
t tt t t t t}
t tt //xil_printf("
Acceptance Filters enabledr");
t tt return XST_SUCCESS;
t}
I am terribly sorry for the lame form of the code :(