flexcan

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

flexcan

1,182 Views
zouxueping
Contributor II

大家好,

我使用的Freescale_MQX_4_1_TWRK64F120M中的flexcan例程,两个节点(A、B)都能够接收到收到对方的信息。但我想再增加一个节点(C),我想让C节点用一个邮箱能够接收到A、B两个节点所发送的数据,程序对C节点如何设置?请大家帮忙解答一下?

多谢!

4 Replies

942 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi zou xueping:

应该参考例程, 增加一个节点NODE 3.

然后定义对应的RX_identifier  和  TX_identifier。

设置正确的 global standard ID filtering mask, C 不能屏蔽A 和 B的ID

请参考API   FLEXCAN_Set_global_stdmask()

pastedImage_1.png

Regards

Daniel

942 Views
zouxueping
Contributor II

hi Daniel,

首先感谢您的回复。

我在 程序中 设置如下:

#if NODE==1
RX_identifier = 0x123;
TX_identifier = 0x321;
RX_remote_identifier = 0x0F0;
TX_remote_identifier = 0x00F;
#elif NODE==2
RX_identifier = 0x123;
TX_identifier = 0x322;
RX_remote_identifier = 0x00F;
TX_remote_identifier = 0x0F0;
#else
RX_identifier = 0x323;
TX_identifier = 0x123;
RX_remote_identifier = 0x00F;
TX_remote_identifier = 0x0F0;
#endif 

result = FLEXCAN_Select_mode( CAN_DEVICE, flexcan_mode);
printf("\nFLEXCAN mode selected. result: 0x%lx", result);

result = FLEXCAN_Set_global_stdmask (CAN_DEVICE, 0x0);
printf("\nFLEXCAN global mask. result: 0x%lx", result);

我设置FLEXCAN_Set_global_stdmask (CAN_DEVICE, 0x7ff);或者FLEXCAN_Set_global_stdmask (CAN_DEVICE, 0);时,

节点C(RX_identifier = 0x323; )时,连邮箱中断服务函数都进不去。

如果C(RX_identifier = 0x321或者RX_identifier = 0x322)时,都会 接到相应节点的数据。似乎跟FLEXCAN_Set_global_stdmask 的设置没什么关系。能否帮我分析一下什么问题吗?

您那是否有这方面的例程?

我的程序如下: 帮我分析一下哪有问题,多谢!

/*TASK*-----------------------------------------------------------
*
* Task Name : Main_Task
* Comments :
*
*
*END*-----------------------------------------------------------*/
void Can_Main_Task(uint32_t parameter)
{ /* Body */
_task_id created_task;
uint32_t result;

// _int_install_unexpected_isr();
// Uart3_RTS();
if (_bsp_flexcan_io_init(CAN_DEVICE) != 0)
{
printf ("\nError initializing pins for FlexCAN device %d!\n", CAN_DEVICE);
_task_block();
}

printf("\n*********FLEXCAN TEST PROGRAM.*********");
printf("\n Message format: Standard (11 bit id)");
printf("\n Message buffer 0 used for Tx and Rx.");
printf("\n Interrupt Mode: Enabled");
printf("\n Operation Mode: TX and RX --> Normal");
printf("\n***************************************\n");

frequency = 125;
printf("\nselected frequency (Kbps) is: %d", frequency);

data_len_code = 8;
printf("\nData length: %d", data_len_code);

/* Select message format */
format = FLEXCAN_STANDARD;

/* Select mailbox number */
RX_mailbox_num = 0;
TX_mailbox_num = 1;
RX_remote_mailbox_num = 2;
TX_remote_mailbox_num = 3;

#if NODE==1
RX_identifier = 0x123;
TX_identifier = 0x321;
RX_remote_identifier = 0x0F0;
TX_remote_identifier = 0x00F;
#elif NODE==2
RX_identifier = 0x123;
TX_identifier = 0x322;
RX_remote_identifier = 0x00F;
TX_remote_identifier = 0x0F0;
#else
RX_identifier = 0x323;
TX_identifier = 0x123;
RX_remote_identifier = 0x00F;
TX_remote_identifier = 0x0F0;
#endif

/* We use default settings */
bit_timing0 = bit_timing1 = 0;

/* Select operation mode */
flexcan_mode = FLEXCAN_NORMAL_MODE;

/* Enable interrupt */
interrupt = FLEXCAN_ENABLE;

/* Enable error interrupt */
flexcan_error_interrupt = 1;

/* Reset FLEXCAN device */
result = FLEXCAN_Softreset ( CAN_DEVICE);
printf("\nFLEXCAN reset. result: 0x%lx", result);

/* Initialize FLEXCAN device */
result = FLEXCAN_Initialize ( CAN_DEVICE, bit_timing0, bit_timing1, frequency, FLEXCAN_IPBUS_CLK);
printf("\nFLEXCAN initilization. result: 0x%lx", result);

/* Select mode */
result = FLEXCAN_Select_mode( CAN_DEVICE, flexcan_mode);
printf("\nFLEXCAN mode selected. result: 0x%lx", result);

result = FLEXCAN_Set_global_stdmask (CAN_DEVICE, 0x7ff);//0x222 );
printf("\nFLEXCAN global mask. result: 0x%lx", result);

/* Enable error interrupts */
if(flexcan_error_interrupt == 1)
{
result = FLEXCAN_Install_isr_err_int( CAN_DEVICE, MY_FLEXCAN_ISR );
printf("\nFLEXCAN Error ISR install, result: 0x%lx", result);

result = FLEXCAN_Install_isr_boff_int( CAN_DEVICE, MY_FLEXCAN_ISR );
printf("\nFLEXCAN Bus off ISR install, result: 0x%lx", result);

result = FLEXCAN_Error_int_enable(CAN_DEVICE);
printf("\nFLEXCAN error interrupt enable. result: 0x%lx", result);
}

/* Set up an event group */
result = _lwevent_create(&event, LWEVENT_AUTO_CLEAR);
if (result != MQX_OK) {
printf("\nCannot create lwevent");
}

created_task = _task_create(0, CAN_RX_TASK, 0);
if (created_task == MQX_NULL_TASK_ID)
{
printf("\nRx task: task creation failed.");
}

created_task = _task_create(0, CAN_TX_TASK, 0);
if (created_task == MQX_NULL_TASK_ID)
{
printf("\nTx task: task creation failed.");
}

/* Start FLEXCAN */
result = FLEXCAN_Start(CAN_DEVICE);
printf("\nFLEXCAN started. result: 0x%lx", result);

// start the ping pong

_task_block();
} /* EndBody */

/*TASK*-----------------------------------------------------------
*
* Task Name : Tx_Task
* Comments :
*
*
*END*-----------------------------------------------------------*/
void Can_Tx_Task(uint32_t parameter)
{/* Body */
unsigned char data = 0;
uint32_t result;

unsigned char string[] =
{
0x2a, 0x2b, 0x2e, 0x2c, 0x2e, 0x2d, 0x2a, 0x0
};

/* Initialize mailbox */
result = FLEXCAN_Initialize_mailbox( CAN_DEVICE, TX_mailbox_num, TX_identifier,
data_len_code, FLEXCAN_TX, format,
interrupt);
printf("\nFLEXCAN tx mailbox initialization. result: 0x%lx", result);

result = FLEXCAN_Activate_mailbox(CAN_DEVICE, TX_mailbox_num, FLEXCAN_TX_MSG_BUFFER_NOT_ACTIVE);
printf("\nFLEXCAN tx mailbox activation. result: 0x%lx", result);

/* Install ISR */
if(interrupt == FLEXCAN_ENABLE)
{
result = FLEXCAN_Install_isr( CAN_DEVICE, TX_mailbox_num, MY_FLEXCAN_ISR );
printf("\nFLEXCAN TX ISR install. result: 0x%lx", result);

result = FLEXCAN_Install_isr( CAN_DEVICE, TX_remote_mailbox_num, MY_FLEXCAN_ISR );
printf("\nFLEXCAN TX remote ISR install. result: 0x%lx", result);
}

while(1)
{
/* Let Rx Task receive message */
_time_delay(1000);

 data++;
//
 result = FLEXCAN_Tx_mailbox(CAN_DEVICE, TX_mailbox_num,string);// &data);
 if(result != FLEXCAN_OK)
 printf("\nTransmit error. Error Code: 0x%lx", result);
 else
 printf("\nData transmit: %d", data);

 string[7] = data;

result = FLEXCAN_Update_message(CAN_DEVICE, string, 8, format, TX_remote_mailbox_num);
 printf("\nFLEXCAN tx update message. result: 0x%lx", result);

}
} /* EndBody */

/*TASK*-----------------------------------------------------------
*
* Task Name : Rx_Task
* Comments :
*
*
*END*-----------------------------------------------------------*/
unsigned char tempbuf[1024];
unsigned char tempbuf1[1024];
void Can_Rx_Task(uint32_t parameter)
{/* Body */
unsigned char dptr[8];
uint32_t result;
uint32_t DLC = 0;
uint32_t ID = 0;
uint32_t len = 0,len1 = 0;

/* Initialize mailbox */
result = FLEXCAN_Initialize_mailbox( CAN_DEVICE, RX_mailbox_num, RX_identifier,
data_len_code, FLEXCAN_RX, format,
interrupt);
printf("\nFLEXCAN rx mailbox initialization. result: 0x%lx", result);

result = FLEXCAN_Activate_mailbox(CAN_DEVICE, RX_mailbox_num, FLEXCAN_RX_MSG_BUFFER_EMPTY);
printf("\nFLEXCAN mailbox activation. result: 0x%lx", result);


/* Install ISR */
if(interrupt == FLEXCAN_ENABLE)
{
result = FLEXCAN_Install_isr( CAN_DEVICE, RX_mailbox_num, MY_FLEXCAN_ISR );
printf("\nFLEXCAN RX ISR install. result: 0x%lx", result);

result = FLEXCAN_Install_isr( CAN_DEVICE, RX_remote_mailbox_num, MY_FLEXCAN_ISR );
printf("\nFLEXCAN RX remote ISR install. result: 0x%lx", result);
}
while(1)
{

if (_lwevent_wait_ticks(&event, 1 << RX_mailbox_num, FALSE, 0) != MQX_OK) {
printf("\nEvent Wait failed");
}

result = FLEXCAN_Lock_mailbox (CAN_DEVICE, RX_mailbox_num);
if(result != FLEXCAN_OK)
{
printf("\nLock mailbox failed. Error Code: 0x%lx", result);
}

result = FLEXCAN_Rx_message(CAN_DEVICE, RX_mailbox_num, &ID, format,
&DLC, &dptr, interrupt);
if(result != FLEXCAN_OK)
printf("\nReceived error. Error Code: 0x%lx", result);
else
{
if(ID == 0x321)
{
printf("\nReceived data: ");
for (result = 0; result < DLC; result++)
{
printf ("0x%x ", dptr[result]);
tempbuf[len++] = dptr[result];
if(len == 1024)
{
len = 0;
}
}
}
else if(ID == 0x322)
{
for (result = 0; result < DLC; result++)
{
printf ("0x%x ", dptr[result]);
tempbuf1[len++] = dptr[result];
if(len1 == 1024)
{
len1 = 0;
}
}
}
printf("\nID is: 0x%x", ID);
printf("\nDLC is: 0x%x\n", DLC);
}

result = FLEXCAN_Unlock_mailbox (CAN_DEVICE);
if(result != FLEXCAN_OK)
{
printf("\nUnlock mailbox failed. Error Code: 0x%lx", result);
}

// FLEXCAN_Request_message (CAN_DEVICE, RX_remote_mailbox_num, format);

}
} /* EndBody */

0 Kudos
Reply

942 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi Xueping:

需要发送TX_identifier = 0x323的帧,  才会进入中断服务程序。 而节点1和2  tx_identifer 为 321, 322.

Regards

Daniel

0 Kudos
Reply

942 Views
zouxueping
Contributor II

hi Daniel,

主要我想接收到上述两个节点(A、B)发送的数据,那么C节点如何配置才能实现?

多谢你的回复!

邹雪平

0 Kudos
Reply