Hi,
I'm using S32K148 Microcontroller. I've enabled the CAN (not CAN FD) able to send and receive 1 frame (8 bytes) properly.
The issue is when we try to receive CAN multiframe we are missing some frames. It is implemented for UDS.
The logic we implemented is shown below:
Gen_CAN0_write( ID , Buffer, 8); //Request frame
if ((IP_FLEXCAN0->IFLAG1 >> 4) & 1)
{
Gen_CAN0_read(buff); //Response frame
}
Gen_CAN0_write( 0x782 , fc , 8); //Flow control frame
if ((IP_FLEXCAN0->IFLAG1 >> 4) & 1)
{
Gen_CAN0_read(buff1); //Read remaining frame 1
}
if ((IP_FLEXCAN0->IFLAG1 >> 4) & 1)
{
Gen_CAN0_read(buff2); //Read remaining frame 2
}
if ((IP_FLEXCAN0->IFLAG1 >> 4) & 1)
{
Gen_CAN0_read(buff3); //Read remaining frame 3
}
Is this the correct way to implement the logic. Please suggest.
Thanks,
Rak
Hi,
from this code it is hard to comment, if it will work or not. It is not clear what write/read functions do. It is not clear how many MBs are used for TX and RX operation. I can guess you have one TX MB and one RX MB. Then a logic for RX process is simple. The MB flag indicates message was successfully received into the RX MB and its content can be read. You should read it before new message will come and will be moved into this MB, if it is free to receive, means it is not locked. If you do not ensure that you may lost some messages.
I assume using while loop will work better, but depends on application
Gen_CAN0_write( ID , Buffer, 8); //Request frame
while (!((IP_FLEXCAN0->IFLAG1 >> 4) & 1)) {}; //wait till RX MB receive message, MB flag is set
Gen_CAN0_read(buff); // Read Response frame
Gen_CAN0_write( 0x782 , fc , 8); //Flow control frame
while (!((IP_FLEXCAN0->IFLAG1 >> 4) & 1)) {};
Gen_CAN0_read(buff1); //Read remaining frame 1
while (!((IP_FLEXCAN0->IFLAG1 >> 4) & 1)) {};
Gen_CAN0_read(buff2); //Read remaining frame 2
while (!((IP_FLEXCAN0->IFLAG1 >> 4) & 1)) {};
Gen_CAN0_read(buff3); //Read remaining frame 3
BR, Petr
Hi @PetrS ,
Thanks for the quick response.
Replacing while in place of if condition didn't work.
Please find the code of init, read and write functions.
Is there any code for implementing UDS over CAN in S32K148?
void Gen_FLEXCAN0_receive_msg(void)
{
/*! Receive msg from ID 0x556 using msg buffer 4
* =============================================
*/
uint8_t j;
uint32_t dummy;
RxCODE = (IP_FLEXCAN0->RAMn[ 4*MSG_BUF_SIZE + 0] & 0x07000000) >> 24; /* Read CODE field */
RxID = (IP_FLEXCAN0->RAMn[ 4*MSG_BUF_SIZE + 1] & FLEXCAN_WMBn_ID_ID_MASK) >> FLEXCAN_WMBn_ID_ID_SHIFT; /* Read ID */
RxLENGTH = (IP_FLEXCAN0->RAMn[ 4*MSG_BUF_SIZE + 0] & FLEXCAN_WMBn_CS_DLC_MASK) >> FLEXCAN_WMBn_CS_DLC_SHIFT; /* Read Message Length */
for (j=0; j<2; j++)
{ /* Read two words of data (8 bytes) */
RxDATA[j] = IP_FLEXCAN0->RAMn[ 4*MSG_BUF_SIZE + 2 + j];
}
RxTIMESTAMP = (IP_FLEXCAN0->RAMn[ 0*MSG_BUF_SIZE + 0] & 0x000FFFF);
dummy = IP_FLEXCAN0->TIMER; /* Read TIMER to unlock message buffers */
IP_FLEXCAN0->IFLAG1 = 0x00000010; /* Clear CAN 0 MB 4 flag without clearing others*/
}
Thanks
Hi,
code looks normal, even if different functions are used in previous. I do not see cause why it does not work. An UDS demo is available in https://community.nxp.com/t5/S32K-Knowledge-Base/Unified-bootloader-Demo/ta-p/1423099
BR, Petr
Hi @PetrS ,
The flexcan example is working fine but can I get the flexcan implementation with FreeRTOS as the available example is for baremetal which uses Osif.
Hi,
I am not aware of such demo for S32K1. There is some for MPC5748G; https://community.nxp.com/t5/MPC5xxx-Knowledge-Base/Example-MPC5748G-FlexCAN-TX-RX-FreeRTOS-S32DS2-1...
BR, Petr