AnsweredAssumed Answered

Bug found in MQX FLEXCAN Driver

Question asked by SHULONG LI on Oct 25, 2015
Latest reply on Oct 26, 2015 by soledad

I'm trying the FLEXCAN driver in MQX latest release

What I wanted to do is to capture all the CAN bus traffics.

To simplify the development, need to set the filter to enable the single MB to receive both standard message and extended frame message.

During the test, I found that there are 2 functions have bugs and 1 function needs to be improved. I put the detail here and see if it can be accepted by MQX team.



The 2 functions - FLEXCAN_Write and FLEXCAN_Read are used to read and write 32bit registers of FLEXCAN module. But the source code of the functions return portion is like this.

FLEXCAN_Read : *data_ptr = (uint32_t) *(unsigned char *)read_ptr;

FLEXCAN_Write:  *(unsigned char *)write_ptr = (unsigned char)value;


So the write and read operation is just 8bit. I revised it to

FLEXCAN_Read: *data_ptr = *(uint32_t *)read_ptr;

FLEXCAN_Write: *(uint32_t *)write_ptr = value;

Now it can operator 32bit registers correctly.


Need to improve.

I found the function FLEXCAN_Rx_Message needs to be improve to match the CAN bus definition correctly.

Definition of the function:

uint32_t FLEXCAN_Rx_message(

uint8_t dev_num,

uint32_t mailbox_number,

uint32_t *identifier,

uint32_t format,

uint32_t *data_len_code,

void *data,

uint32_t int_enabled)


According to CAN bus protocol, the format should be reflected by the IDE field of the message buffer, so it should be and out type instead of in type.

During my test of using 1 MB to receive both standard and extended format messages. I met the issue - if the received message is standard and the format is set to FLEXCAN_EXTENDED, the return ID is incorrect, and vise vesa.

I think it should be defined like this.

uint32_t FLEXCAN_Rx_message(

uint8_t dev_num,

uint32_t mailbox_number,

uint32_t *identifier,

uint32_t *format,

uint32_t *data_len_code,

void *data,

uint32_t int_enabled)


I revised the code to reflect the definition change

    /* store data len */

    *format = can_reg_ptr->MB[mailbox_number].CS & CAN_CS_IDE_MASK?FLEXCAN_EXTENDED:FLEXCAN_STANDARD;

    dlen = can_reg_ptr->MB[mailbox_number].CS & CAN_CS_DLC_MASK;

    DLC = dlen >> CAN_CS_DLC_SHIFT;


    /* Reassemble the message ID */

    switch (*format)



Now the format output will be the current Message format and ID will be returned correctly.