关于KS22的flexcan的demo移植问题

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

关于KS22的flexcan的demo移植问题

2,008 Views
yongyan
Contributor II

官方的flexcan示例是基于KS22官方定制开发板的示例程序,定制开发板使用的是MKS22FN256VLL12,现在要将官方demo往MKS22FN256VLH12芯片中移植,只更改芯片的引脚初始化定义BOARD_InitPins()可以吗?

MKS22FN256VLH12芯片的RxD和TxD引脚已通过CAN收发器与CAN总线分析仪相连接,通过上位机软件CANtools对分析仪进行控制与显示,按照附件的程序运行,在第140行 while (!rxComplete)时暂停,表示不能成功完成发送,不能引起发送成功中断,请问原因为何?

使用的是SDK_2.2_MKS22FN256xxx12开发包中的例程,更改后的例程在附件中。

 

The demo of the flexcan is based on the customed board that released by the nxp,the chip's product type used on the board is MKS22FN256VLL12.Now I want to apply the program to the chip MKS22FN256VLH12,the only difference between the two chips is the numbers of the pins.So,I change the definition of the pins about flexcan'RxD and TxD.

Now,the RxD pin and TxD pin is connect to a  can transceiver and a can bus analyser,I use the software Ecantools to control the can bus analyser and receive the message of the MCU,however,the program is blocked in the 140th line ,what means the rxComplete can't be set,the callback function can't be called,the interrupt can't be triggled,I'm really confused of the question.Pray to God for help!

 

The enclosure is the project and the source code.

Original Attachment has been moved to: interrupt_transfer---副本.rar

Labels (1)
6 Replies

1,736 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi  Yong Yan,

    你那边除了接收有问题,那么你用CAN 发送出来,你的CAN分析仪是否能接到数据?

   另外,把你需要用的CAN引脚号告诉我下,我这边有空会测试下。


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

1,736 Views
yongyan
Contributor II

现在遇到的问题1:这样配置下来只有一路CAN可以正常收发工作,另一路不能收发,请问这是为什么?

问题2 :在FLEXCAN_TransferCreateHandle这个函数中,定义的s_flexcanIsr = FLEXCAN_TransferHandleIRQ;这一行代码对应的这个中断函数是什么意思?是接收或发送成功对应的中断函数吗?和用中断向号CAN1_ORed_Message_buffer_IRQn定义的函数有何区别?

问题3  :您能否给给我配置一个CAN0用于接收,CAN1将CAN0接收的数据发送出去的函数?

0 Kudos

1,736 Views
kerryzhou
NXP TechSupport
NXP TechSupport

你好!

1. 那么,你有没有单独分别测试,如果只有CAN0,CAN0是否正常,如果只有CAN1,CAN1是否正常?

2. 这个其实就是定义为CAN的中断函数,FLEXCAN_TransferHandleIRQ作为真正执行的中断函数,包含了FlexCAN Error, the Message Buffer, and the Rx FIFO IRQ request。
  如果仔细查看FLEXCAN_TransferHandleIRQ函数,可以看到里面对MB发送以及接受,错误的处理。
3. 我觉得你可以先单个的CAN运行,如果两个都能成功了,再加到一起,把接收到的数据放到buffer里面,然后再用另外一个CAN发送。


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

1,736 Views
yongyan
Contributor II

谢谢您,我的问题已经解决了

0 Kudos

1,736 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello Yong Yan,

     Thank you for your updated information.

     If your question is solved, please mark the correct answer to close this question.

     Thank you and have a nice day!



Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

1,736 Views
yongyan
Contributor II

谢谢您,我的这个我问题已经解决了。但现在又遇到了一个新问题,就是对着例程配置两个CAN同时通信时,一路CAN的配置收发可以正常工作,但是配置两路CAN的时候,会有一个CAN不能收发。我使用的SDK_2.2_MKS22FN256xxx12开发包中的例程interrupt transfer example,对应另一路的配置就是重新定义一个新的配置变量,对着单路CAN的例程完全对应配置,配置代码如下:

#include "fsl_debug_console.h"
#include "fsl_flexcan.h"
#include "board.h"
#include "canlib.h"

#include "fsl_device_registers.h"
#include "fsl_common.h"
#include "pin_mux.h"
#include "clock_config.h"

/*******************************************************************************
* Definitions
******************************************************************************/
#define EXAMPLE_CAN CAN0 //CAN0µØÖ·Ö¸Õë
#define EXAMPLE_CAN1 CAN1 //CAN1µØÖ·Ö¸Õë
#define EXAMPLE_CAN_CLKSRC kCLOCK_BusClk
#define EXAMPLE_CAN_CLK_FREQ CLOCK_GetFreq(kCLOCK_BusClk) //can×ÜÏßʱÖÓƵÂÊ
#define RX_MESSAGE_BUFFER_NUM (1) //½ÓÊÜÐÅÏ¢»º³åÇøÊý×Ö±àºÅ
#define TX_MESSAGE_BUFFER_NUM (2) // ·¢ËÍÐÅÏ¢»º³åÇøÊý×Ö±àºÅ


flexcan_handle_t flexcanHandle; //flexcan¾ä±ú½á¹¹£¬°üÀ¨»Øµ÷º¯Êý¼°²ÎÊý,MB¼°Rx FIFOÐÅÏ¢½ÓÊÕ»º³åÆ÷frame¼°×´Ì¬mbState
flexcan_handle_t flexcanHandle1; //flexcan1µÄ¾ä±ú½á¹¹
volatile bool txComplete = false;
volatile bool rxComplete = false;
volatile bool txComplete1 = false;
volatile bool rxComplete1 = false;

flexcan_mb_transfer_t txXfer, rxXfer; //´«Ê仺³åÇø , ÄÚÈÝΪframeºÍmbIdx
flexcan_frame_t frame; //flexcanµÄ±¨ÎĽṹÌ壬Ó뱨ÎÄ»º³åÇøµÄÊý¾Ý½á¹¹Ïà¶ÔÓ¦
flexcan_mb_transfer_t txXfer1, rxXfer1;
flexcan_frame_t frame1;

uint32_t txIdentifier; // ·¢ËÍ·½µÄID
uint32_t rxIdentifier; //½ÓÊÕ·½µÄID
uint32_t txIdentifier1; // ·¢ËÍ·½µÄID
uint32_t rxIdentifier1; //½ÓÊÕ·½µÄID

flexcan_config_t flexcanConfig; //flexcanÄ£¿éÅäÖñäÁ¿£¬°üÀ¨²¨ÌØÂÊ£¬Ð­ÒéÒýÇæʱÖÓÔ´±äÁ¿£¬×î´ó»º³åÇøÊýÁ¿£¬Ä£Ê½Ñ¡Ôñ
flexcan_config_t flexcanConfig1;
flexcan_rx_mb_config_t mbConfig; //½ÓÊÜ»º³åÇøÅäÖýṹÌ壬°üÀ¨±ê־룬±ê־λ¸ñʽ£¬Êý¾ÝÖ¡¸ñʽ
flexcan_rx_mb_config_t mbConfig1;

static void flexcan_callback(CAN_Type *base, flexcan_handle_t *handle, status_t status, uint32_t result, void *userData)
{
switch (status)
{
case kStatus_FLEXCAN_RxIdle:
if (RX_MESSAGE_BUFFER_NUM == result)
{
rxComplete = true;
}
break;

case kStatus_FLEXCAN_TxIdle:
if (TX_MESSAGE_BUFFER_NUM == result)
{
txComplete = true;
}
break;

default:
break;
}
}

static void flexcan_callback1(CAN_Type *base, flexcan_handle_t *handle, status_t status, uint32_t result, void *userData)
{
switch (status)
{
case kStatus_FLEXCAN_RxIdle:
if (RX_MESSAGE_BUFFER_NUM == result)
{
rxComplete1 = true;
}
break;

case kStatus_FLEXCAN_TxIdle:
if (TX_MESSAGE_BUFFER_NUM == result)
{
txComplete1 = true;
}
break;

default:
break;
}
}

void board_init(void)
{
/* Initialize board hardware. */
BOARD_InitPins();
BOARD_BootClockHSRUN();
BOARD_InitDebugConsole();
}


void can1init(uint32_t txid1,uint32_t rxid1)
{
/* Select mailbox ID. */ //¸ù¾Ý½ÚµãÀàÐÍÑ¡ÔñÏàÓ¦ÓÊÏäID
txIdentifier1 = txid1;
rxIdentifier1 = rxid1;

/* Get FlexCAN module default Configuration. */ //CAN Ä£¿éĬÈÏÅäÖÃ
/*
* flexcanConfig.clkSrc = kFLEXCAN_ClkSrcOsc;
* flexcanConfig.baudRate = 800000U;
* flexcanConfig.maxMbNum = 16;
* flexcanConfig.enableLoopBack = false;
* flexcanConfig.enableSelfWakeup = false;
* flexcanConfig.enableIndividMask = false;
* flexcanConfig.enableDoze = false;
*/
FLEXCAN_GetDefaultConfig(&flexcanConfig1); //ĬÈÏÅäÖã¬ÐèÒª¸ü¸Äµ½º¯Êý¶¨Òå´¦¸ü¸ÄÔ´´úÂë

/* Init FlexCAN module. */
#if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
flexcanConfig.clkSrc = kFLEXCAN_ClkSrcPeri; //Ñ¡ÔñʱÖÓԴЭÒéÒýÇæʱÖÓԴΪÍâÉèʱÖÓÔ´
#endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
FLEXCAN_Init(EXAMPLE_CAN1, &flexcanConfig1, EXAMPLE_CAN_CLK_FREQ); //³õʼ»¯£¬ÅäÖÃMCR£¬CTRL1,CTRL2¼Ä´æÆ÷³õʼ»¯µÈÏà¹Ø²Ù×÷

/* Create FlexCAN handle structure and set call back function. */ //Óëmainº¯ÊýÇ°µÄÖжϷµ»Øº¯ÊýÓйØ
FLEXCAN_TransferCreateHandle(EXAMPLE_CAN1, &flexcanHandle1, flexcan_callback1, NULL);

/* Set Rx Masking mechanism. */ //½ÓÊÕ»º³åÆ÷È«¾ÖÑÚÂëÉèÖ㬴ó¸Å¾ÍÊÇÉèÖü¸¸öÑÚÂë¼Ä´æÆ÷½«RXµÄIDÆ¥Åä
FLEXCAN_SetRxMbGlobalMask(EXAMPLE_CAN1, FLEXCAN_RX_MB_STD_MASK(rxIdentifier1, 0, 0));

/* Setup Rx Message Buffer. */ //½ÓÊÕ»º³åÇø³õʼ»¯ÉèÖÃ
mbConfig1.format = kFLEXCAN_FrameFormatStandard; //Ôں궨Òå·Ö±ð¶ÔӦλµÄ0»ò1
mbConfig1.type = kFLEXCAN_FrameTypeData;
mbConfig1.id = FLEXCAN_ID_STD(rxIdentifier1); //ͨ¹ý¸Ãº¯Êý½«rxIDÒƵ½¶ÔӦλÖÃ
FLEXCAN_SetRxMbConfig(EXAMPLE_CAN1, RX_MESSAGE_BUFFER_NUM, &mbConfig1, true); //¶Ô½ÓÊÕ»º³åÇø¼Ä´æÆ÷½øÐÐÅäÖÃ

/* Setup Tx Message Buffer. */ //·¢ËÍ»º³åÇø³õʼ»¯ÉèÖÃ
FLEXCAN_SetTxMbConfig(EXAMPLE_CAN1, TX_MESSAGE_BUFFER_NUM, true);
}

void caninit(uint32_t txid,uint32_t rxid) //ÐβÎÉèÖÃΪ·¢ËÍÓÊÏäºÍ½ÓÊÕÓÊÏäµÄID
{
/* Select mailbox ID. */ //¸ù¾Ý½ÚµãÀàÐÍÑ¡ÔñÏàÓ¦ÓÊÏäID
txIdentifier = txid;
rxIdentifier = rxid;

/* Get FlexCAN module default Configuration. */ //CAN Ä£¿éĬÈÏÅäÖÃ
/*
* flexcanConfig.clkSrc = kFLEXCAN_ClkSrcOsc;
* flexcanConfig.baudRate = 800000U;
* flexcanConfig.maxMbNum = 16;
* flexcanConfig.enableLoopBack = false;
* flexcanConfig.enableSelfWakeup = false;
* flexcanConfig.enableIndividMask = false;
* flexcanConfig.enableDoze = false;
*/
FLEXCAN_GetDefaultConfig(&flexcanConfig); //ĬÈÏÅäÖã¬ÐèÒª¸ü¸Äµ½º¯Êý¶¨Òå´¦¸ü¸ÄÔ´´úÂë

/* Init FlexCAN module. */
#if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
flexcanConfig.clkSrc = kFLEXCAN_ClkSrcPeri; //Ñ¡ÔñʱÖÓԴЭÒéÒýÇæʱÖÓԴΪÍâÉèʱÖÓÔ´
#endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
FLEXCAN_Init(EXAMPLE_CAN, &flexcanConfig, EXAMPLE_CAN_CLK_FREQ); //³õʼ»¯£¬ÅäÖÃMCR£¬CTRL1,CTRL2¼Ä´æÆ÷³õʼ»¯µÈÏà¹Ø²Ù×÷

/* Create FlexCAN handle structure and set call back function. */ //Óëmainº¯ÊýÇ°µÄÖжϷµ»Øº¯ÊýÓйØ
FLEXCAN_TransferCreateHandle(EXAMPLE_CAN, &flexcanHandle, flexcan_callback, NULL);

/* Set Rx Masking mechanism. */ //½ÓÊÕ»º³åÆ÷È«¾ÖÑÚÂëÉèÖ㬴ó¸Å¾ÍÊÇÉèÖü¸¸öÑÚÂë¼Ä´æÆ÷½«RXµÄIDÆ¥Åä
FLEXCAN_SetRxMbGlobalMask(EXAMPLE_CAN, FLEXCAN_RX_MB_STD_MASK(rxIdentifier, 0, 0));

/* Setup Rx Message Buffer. */ //½ÓÊÕ»º³åÇø³õʼ»¯ÉèÖÃ
mbConfig.format = kFLEXCAN_FrameFormatStandard; //Ôں궨Òå·Ö±ð¶ÔӦλµÄ0»ò1
mbConfig.type = kFLEXCAN_FrameTypeData;
mbConfig.id = FLEXCAN_ID_STD(rxIdentifier); //ͨ¹ý¸Ãº¯Êý½«rxIDÒƵ½¶ÔӦλÖÃ
FLEXCAN_SetRxMbConfig(EXAMPLE_CAN, RX_MESSAGE_BUFFER_NUM, &mbConfig, true); //¶Ô½ÓÊÕ»º³åÇø¼Ä´æÆ÷½øÐÐÅäÖÃ

/* Setup Tx Message Buffer. */ //·¢ËÍ»º³åÇø³õʼ»¯ÉèÖÃ
FLEXCAN_SetTxMbConfig(EXAMPLE_CAN, TX_MESSAGE_BUFFER_NUM, true);
}

/*Receive data through Rx Message Buffer. */
void Receive(void)
{
rxXfer.frame = &frame;
rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM;
FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN, &flexcanHandle, &rxXfer);
/* Wait until Rx receive full. */
while (!rxComplete)
{
};
rxComplete = false;
}

void Receive1(void)
{
rxXfer1.frame = &frame1;
rxXfer1.mbIdx = RX_MESSAGE_BUFFER_NUM;
FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_CAN1, &flexcanHandle1, &rxXfer1);

/* Wait until Rx receive full. */
while (!rxComplete1)
{
};
rxComplete1= false;
}

/*Send data through Tx Message Buffer. */
void Send(uint8_t datalength) //datalength<=8,Êý¾Ý×Ö½Ú³¤¶ÈСÓÚµÈÓÚ8
{
frame.id = FLEXCAN_ID_STD(txIdentifier); //±¨ÎÄÉèÖ÷¢ËÍ·½ID,¶ÔÓ¦»º³åÆ÷¼Ä´æÆ÷
frame.format = kFLEXCAN_FrameFormatStandard; //±¨Îĸñʽ
frame.type = kFLEXCAN_FrameTypeData; // ±¨ÎÄÖ¡ÀàÐÍ
frame.length = datalength;
txXfer.frame = &frame; //ÉèÖ÷¢ËÍÏà¹ØÄÚÈÝ
txXfer.mbIdx = TX_MESSAGE_BUFFER_NUM; //·¢ËÍ»º³åÇøµÄË÷ÒýºÅ
FLEXCAN_TransferSendNonBlocking(EXAMPLE_CAN, &flexcanHandle, &txXfer); //ÎÞÕÏ°­·¢ËÍ´«Ê亯Êý

while (!txComplete) //µÈ´ý´«ÊäÍê³É£¬Ôڻص÷º¯ÊýÖн«txcompleteÖÃλ
{
};
txComplete = false;
}
/*Send data through Tx1 Message Buffer. */
void Send1(uint8_t datalength1) //datalength1<=8,Êý¾Ý×Ö½Ú³¤¶ÈСÓÚµÈÓÚ8
{
frame1.id = FLEXCAN_ID_STD(txIdentifier); //±¨ÎÄÉèÖ÷¢ËÍ·½ID,¶ÔÓ¦»º³åÆ÷¼Ä´æÆ÷
frame1.format = kFLEXCAN_FrameFormatStandard; //±¨Îĸñʽ
frame1.type = kFLEXCAN_FrameTypeData; // ±¨ÎÄÖ¡ÀàÐÍ
frame1.length = datalength1;
txXfer1.frame = &frame; //ÉèÖ÷¢ËÍÏà¹ØÄÚÈÝ
txXfer1.mbIdx = TX_MESSAGE_BUFFER_NUM; //·¢ËÍ»º³åÇøµÄË÷ÒýºÅ
FLEXCAN_TransferSendNonBlocking(EXAMPLE_CAN1, &flexcanHandle1, &txXfer1); //ÎÞÕÏ°­·¢ËÍ´«Ê亯Êý

while (!txComplete1) //µÈ´ý´«ÊäÍê³É£¬Ôڻص÷º¯ÊýÖн«txcompleteÖÃλ
{
};
txComplete1 = false;
}

0 Kudos