S32k CAN Communication Trouble Shooting

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

S32k CAN Communication Trouble Shooting

Jump to solution
1,171 Views
akhilranga
Contributor IV

I have been trying to develope a code where i am trying to Transmit and Receive Extended CAN Data at 250kbps baud rate from Battery Control Unit (BMS). I am using CAN 0 instance on s32k146 to perform this action. here is the code below. You can notice i am trying to transmit dummy message CAN frames with an Extended CAN ID. On the successully sending this CAN frames the BMS will transmitt the actual battery information which i am  trying to receive as an Extended CAN frame and then convert that CAN frame to a Std Frame by just replacing the CAN ID to Standard ID and mapping all the received Ext CAN frame Data from BMS as same payload to the Std CAN. Which is being transmitted via CAN 1instance. 

 

/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "clockMan1.h"
#include "pin_mux.h"
#if CPU_INIT_CONFIG
#include "Init_Config.h"
#endif

#include "LPUART.h"

volatile int exit_code = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */

#include <stdint.h>
#include <stdbool.h>

/* This example is setup to work by default with EVB. To use it with other boards
please comment the following line
*/


#define LED_PORT 	PORTD
#define GPIO_PORT	PTD
#define PCC_CLOCK	PCC_PORTD_CLOCK
#define LED1		15U
#define LED2		16U
#define Bat_Lock_Switch     2U  //PTE


//CAN Tx Frames ID and MAILBOX Setup
#define TX_MAILBOX_1 (1UL)
#define TX_MSG_ID_1  (0X18900140)
#define TX_MAILBOX_2 (2UL)
#define TX_MSG_ID_2  (0X18910140)
#define TX_MAILBOX_3 (3UL)
#define TX_MSG_ID_3  (0X18920140)
#define TX_MAILBOX_4 (4UL)
#define TX_MSG_ID_4 (0X18940140)
#define TX_MAILBOX_5 (5UL)
#define TX_MSG_ID_5 (0X18980140)

#define TX_MAILBOX_11 (11UL)
#define TX_MSG_ID_11  (0x310)
#define TX_MAILBOX_12 (12UL)
#define TX_MSG_ID_12  (0X311)
#define TX_MAILBOX_13 (13UL)
#define TX_MSG_ID_13  (0X312)
#define TX_MAILBOX_14 (14UL)
#define TX_MSG_ID_14  (0X314)
#define TX_MAILBOX_15 (15UL)
#define TX_MSG_ID_15  (0X318)


//CAN RX Frame ID and MAILBOX Setup
#define RX_MAILBOX_1 (1UL)
#define RX_MSG_ID_1  (0x18904001)
#define RX_MAILBOX_2 (2UL)
#define RX_MSG_ID_2  (0x18914001)
#define RX_MAILBOX_3 (3UL)
#define RX_MSG_ID_3  (0x18924001)
#define RX_MAILBOX_4 (4UL)
#define RX_MSG_ID_4  (0x18944001)
#define RX_MAILBOX_5 (5UL)
#define RX_MSG_ID_5  (0x18984001)





void BoardInit(void)
{

/* Initialize and configure clocks
*  -   Setup system clocks, dividers
*  -   Configure FlexCAN clock, GPIO, LPSPI
*  -   see clock manager component for more details
*/
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
			g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_FORCIBLE);

/* Initialize pins
*  -   Init FlexCAN, LPSPI and GPIO pins
*  -   See PinSettings component for more info
*/
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
}



void Enable_InterruptSerial(uint8_t vector_number)
{
S32_NVIC->ISER[(uint32_t)(vector_number) >> 5U] = (uint32_t)(1U << ((uint32_t)(vector_number) & (uint32_t)0x1FU));
S32_NVIC->ICPR[(uint32_t)(vector_number) >> 5U] = (uint32_t)(1U << ((uint32_t)(vector_number) & (uint32_t)0x1FU));
S32_NVIC->IP[(uint32_t)(vector_number)] = 7;
}


void PORT_init (void)
{
//UART1
PCC->PCCn[PCC_PORTC_INDEX ]|=PCC_PCCn_CGC_MASK; /* Enable clock for PORTC */
PORTC->PCR[8]|=PORT_PCR_MUX(2);	/* Port C6: MUX = ALT2, UART1 TX */
PORTC->PCR[9]|=PORT_PCR_MUX(2);   /* Port C7: MUX = ALT2, UART1 RX */
PORTD->PCR[1] = PORT_PCR_MUX(1);
PTD-> PSOR |= 1<<1;
}


void delay(volatile int cycles)
{
/* Delay function - do nothing for a number of cycles */
while(cycles--);
}




int main(void)
	{

/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT();                 /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization.                    ***/

/* Do the initializations required for this application */
BoardInit();
PORT_init();


// UART Initializations
    	LPUART1_init();
    	Enable_InterruptSerial(LPUART1_RxTx_IRQn);
    	LPUART1_transmit_string("Running LPUART1 \r\n");


// CAN Initialization
CAN_Init(INST_CAN_PAL1, &can_pal1_Config0);
CAN_Init(INST_CAN_PAL2, &can_pal2_Config0);

/* Set information about the data to be received
*  - Extended message ID
*  - Bit rate switch enabled to use a different bitrate for the data segment
*  - Flexible data rate enabled
*  - Use zeros for FD padding
*/

can_buff_config_t buffcfg =  {
							.enableFD = true,
							.enableBRS = true,
							.fdPadding = 0U,
							.idType = CAN_MSG_ID_EXT,
							.isRemote = false
							};


/* Set information about the data to be send
 *  - Standard message ID
 *  - Bit rate switch enabled to use a different bitrate for the data segment
 *  - Flexible data rate enabled
 *  - Use zeros for FD padding
 */
can_buff_config_t buffcfg1 =  {
							.enableFD = false,
							.enableBRS = false,
							.fdPadding = 0U,
							.idType = CAN_MSG_ID_STD,
							.isRemote = false
						     };


/* Configure can_pal1 TX buffer with index TX_MAILBOX */
CAN_ConfigTxBuff(INST_CAN_PAL1, TX_MAILBOX_1,   &buffcfg);
CAN_ConfigTxBuff(INST_CAN_PAL1, TX_MAILBOX_2,   &buffcfg);
CAN_ConfigTxBuff(INST_CAN_PAL1, TX_MAILBOX_3,   &buffcfg);
CAN_ConfigTxBuff(INST_CAN_PAL1, TX_MAILBOX_4,   &buffcfg);
CAN_ConfigTxBuff(INST_CAN_PAL1, TX_MAILBOX_5,   &buffcfg);

/* Configure can_pal1 RX buffer with index RX_MAILBOX */
CAN_ConfigRxBuff(INST_CAN_PAL1, RX_MAILBOX_1, &buffcfg, RX_MSG_ID_1);
CAN_ConfigRxBuff(INST_CAN_PAL1, RX_MAILBOX_2, &buffcfg, RX_MSG_ID_2);
CAN_ConfigRxBuff(INST_CAN_PAL1, RX_MAILBOX_3, &buffcfg, RX_MSG_ID_3);
CAN_ConfigRxBuff(INST_CAN_PAL1, RX_MAILBOX_4, &buffcfg, RX_MSG_ID_4);
CAN_ConfigRxBuff(INST_CAN_PAL1, RX_MAILBOX_5, &buffcfg, RX_MSG_ID_5);

/* Configure can_pal2 TX buffer with index TX_MAILBOX */
CAN_ConfigTxBuff(INST_CAN_PAL2, TX_MAILBOX_11,   &buffcfg1);
CAN_ConfigTxBuff(INST_CAN_PAL2, TX_MAILBOX_12,   &buffcfg1);
CAN_ConfigTxBuff(INST_CAN_PAL2, TX_MAILBOX_13,   &buffcfg1);
CAN_ConfigTxBuff(INST_CAN_PAL2, TX_MAILBOX_14,   &buffcfg1);
CAN_ConfigTxBuff(INST_CAN_PAL2, TX_MAILBOX_15,   &buffcfg1);


/* Define Ext send buffer */
               can_message_t ExtTxMsg1 = {
                           	  .cs = 0U,
                           	  .id = TX_MSG_ID_1,
.data[0] = 00,.data[1] = 00,.data[2] = 00,.data[3] = 00,.data[4] = 00,.data[5] = 00,.data[6] = 00,.data[7] = 00,
                                 .length = 8U
                           	          };


				can_message_t ExtTxMsg2 = {
                           	  .cs = 0U,
                           	  .id = TX_MSG_ID_2,
.data[0] = 00,.data[1] = 00,.data[2] = 00,.data[3] = 00,.data[4] = 00,.data[5] = 00,.data[6] = 00,.data[7] = 00,
                                 .length = 8U
                           	          };


				can_message_t ExtTxMsg3 = {
								  .cs = 0U,
								  .id = TX_MSG_ID_3,
.data[0] = 00,.data[1] = 00,.data[2] = 00,.data[3] = 00,.data[4] = 00,.data[5] = 00,.data[6] = 00,.data[7] = 00,
								  .length = 8U
										  };


				can_message_t ExtTxMsg4 = {
								  .cs = 0U,
								  .id = TX_MSG_ID_4,
.data[0] = 00,.data[1] = 00,.data[2] = 00,.data[3] = 00,.data[4] = 00,.data[5] = 00,.data[6] = 00,.data[7] = 00,
								  .length = 8U
										  };

				can_message_t ExtTxMsg5 = {
								  .cs = 0U,
								  .id = TX_MSG_ID_5,
.data[0] = 00,.data[1] = 00,.data[2] = 00,.data[3] = 00,.data[4] = 00,.data[5] = 00,.data[6] = 00,.data[7] = 00,
								  .length = 8U
										  };






while (1)
{


/* Define Ext Receive buffer */
		can_message_t ExtRxMsg1;
		can_message_t ExtRxMsg2;
		can_message_t ExtRxMsg3;
		can_message_t ExtRxMsg4;
		can_message_t ExtRxMsg5;

delay (675000);

//CAN_Send(INST_CAN_PAL1, TX_MAILBOX_1, &ExtTxMsg1);
if (CAN_Send(INST_CAN_PAL1, TX_MAILBOX_1, &ExtTxMsg1) == STATUS_SUCCESS)
{
	//delay(100000);
	LPUART1_transmit_string("\r\n  0x18900140 Transmitted \r\n");
	//CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_1, &ExtRxMsg1);
}


//CAN_Send(INST_CAN_PAL1, TX_MAILBOX_2, &ExtTxMsg2);
if (CAN_Send(INST_CAN_PAL1, TX_MAILBOX_2, &ExtTxMsg2) == STATUS_SUCCESS)
{
	//delay(100000);
	LPUART1_transmit_string("\r\n  0x18910140 Transmitted \r\n");
	//CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_2, &ExtRxMsg2);
}


//CAN_Send(INST_CAN_PAL1, TX_MAILBOX_3, &ExtTxMsg3);
if (CAN_Send(INST_CAN_PAL1, TX_MAILBOX_3, &ExtTxMsg3) == STATUS_SUCCESS)
{
	//delay(100000);
	LPUART1_transmit_string("\r\n  0x18920140 Transmitted \r\n");
	//CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_3, &ExtRxMsg3);
}


//CAN_Send(INST_CAN_PAL1, TX_MAILBOX_4, &ExtTxMsg4);
if (CAN_Send(INST_CAN_PAL1, TX_MAILBOX_4, &ExtTxMsg4) == STATUS_SUCCESS)
{
	//delay(100000);
	LPUART1_transmit_string("\r\n  0x18940140 Transmitted \r\n");
	//CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_4, &ExtRxMsg4);
}


//CAN_Send(INST_CAN_PAL1, TX_MAILBOX_5, &ExtTxMsg5);
if (CAN_Send(INST_CAN_PAL1, TX_MAILBOX_5, &ExtTxMsg5) == STATUS_SUCCESS)
{
	//delay(100000);
	LPUART1_transmit_string("\r\n  0x18980140 Transmitted \r\n");
	//CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_5, &ExtRxMsg5) ;
}



///* Define Receive buffer */
//can_message_t ExtRxMsg1;
//	while(1)
//	{
//		if(CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_1, &ExtRxMsg1) == STATUS_SUCCESS)
//		{
//			if(ExtRxMsg1.id == 0x18904001)
//			{
//			LPUART1_transmit_string("\r\n  0x18904001 Received \r\n");
//			break;
//			}
//		}
//	}
//
//can_message_t ExtRxMsg2;
//can_message_t ExtRxMsg3;
//can_message_t ExtRxMsg4;
//can_message_t ExtRxMsg5;
//delay (675000);

CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_1, &ExtRxMsg1);
CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_2, &ExtRxMsg2);
CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_3, &ExtRxMsg3);
CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_4, &ExtRxMsg4);
CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_5, &ExtRxMsg5);



//while (CAN_Receive(INST_CAN_PAL1, RX_MAILBOX_1, &ExtRxMsg1) == STATUS_BUSY);
//while (CAN_GetTransferStatus(INST_CAN_PAL1,RX_MAILBOX_1) == STATUS_BUSY);
//while (CAN_GetTransferStatus(INST_CAN_PAL1,RX_MAILBOX_2) == STATUS_BUSY);
//while (CAN_GetTransferStatus(INST_CAN_PAL1,RX_MAILBOX_3) == STATUS_BUSY);
//while (CAN_GetTransferStatus(INST_CAN_PAL1,RX_MAILBOX_4) == STATUS_BUSY);
//while (CAN_GetTransferStatus(INST_CAN_PAL1,RX_MAILBOX_5) == STATUS_BUSY);


// Set Up of the Message Payload of Std Msg

can_message_t StdTxMsg11 = {
            	  .cs = 0U,
            	  .id = TX_MSG_ID_11,
             .data[0] = ExtRxMsg1.data[0],
		     .data[1] = ExtRxMsg1.data[1],
			 .data[2] = ExtRxMsg1.data[2],
			 .data[3] = ExtRxMsg1.data[3],
			 .data[4] = ExtRxMsg1.data[4],
			 .data[5] = ExtRxMsg1.data[5],
			 .data[6] = ExtRxMsg1.data[6],
			 .data[7] = ExtRxMsg1.data[7],
              .length = 8U
            	        };

can_message_t StdTxMsg12 = {
            	  .cs = 0U,
            	  .id = TX_MSG_ID_12,
             .data[0] = ExtRxMsg2.data[0],
		     .data[1] = ExtRxMsg2.data[1],
			 .data[2] = ExtRxMsg2.data[2],
			 .data[3] = ExtRxMsg2.data[3],
			 .data[4] = ExtRxMsg2.data[4],
			 .data[5] = ExtRxMsg2.data[5],
			 .data[6] = ExtRxMsg2.data[6],
			 .data[7] = ExtRxMsg2.data[7],
              .length = 8U
            	        };

can_message_t StdTxMsg13 = {
            	  .cs = 0U,
            	  .id = TX_MSG_ID_13,
             .data[0] = ExtRxMsg3.data[0],
		     .data[1] = ExtRxMsg3.data[1],
			 .data[2] = ExtRxMsg3.data[2],
			 .data[3] = ExtRxMsg3.data[3],
			 .data[4] = ExtRxMsg3.data[4],
			 .data[5] = ExtRxMsg3.data[5],
			 .data[6] = ExtRxMsg3.data[6],
			 .data[7] = ExtRxMsg3.data[7],
              .length = 8U
            	        };

can_message_t StdTxMsg14 = {
            	  .cs = 0U,
            	  .id = TX_MSG_ID_14,
             .data[0] = ExtRxMsg4.data[0],
		     .data[1] = ExtRxMsg4.data[1],
			 .data[2] = ExtRxMsg4.data[2],
			 .data[3] = ExtRxMsg4.data[3],
			 .data[4] = ExtRxMsg4.data[4],
			 .data[5] = ExtRxMsg4.data[5],
			 .data[6] = ExtRxMsg4.data[6],
			 .data[7] = ExtRxMsg4.data[7],
              .length = 8U
            	        };

can_message_t StdTxMsg15 = {
            	  .cs = 0U,
            	  .id = TX_MSG_ID_15,
             .data[0] = ExtRxMsg5.data[0],
		     .data[1] = ExtRxMsg5.data[1],
			 .data[2] = ExtRxMsg5.data[2],
			 .data[3] = ExtRxMsg5.data[3],
			 .data[4] = ExtRxMsg5.data[4],
			 .data[5] = ExtRxMsg5.data[5],
			 .data[6] = ExtRxMsg5.data[6],
			 .data[7] = ExtRxMsg5.data[7],
              .length = 8U
            	        };


//  Switch cases for sending Std Data ID based on the reception of Ext Data ID

		switch (ExtRxMsg1.id)
		{
		case 0x18904001: //CAN

			CAN_Send (INST_CAN_PAL2,TX_MAILBOX_11, &StdTxMsg11);
			LPUART1_transmit_string("\r\n  0x310 Transmitted \r\n");

			break;

		default:
			break;

		}


		switch (ExtRxMsg2.id)
		{
				case 0x18914001: //CAN

					CAN_Send (INST_CAN_PAL2,TX_MAILBOX_12, &StdTxMsg12);
					LPUART1_transmit_string("\r\n  0x311 Transmitted \r\n");

					break;

				default:
					break;
				}


		switch (ExtRxMsg3.id)
		{
				case 0x18924001: //CAN

					CAN_Send (INST_CAN_PAL2,TX_MAILBOX_13, &StdTxMsg13);
					LPUART1_transmit_string("\r\n  0x312 Transmitted \r\n");

					break;

				default:
					break;
				}

		switch (ExtRxMsg4.id)
		{
				case 0x18944001: //CAN

					CAN_Send (INST_CAN_PAL2,TX_MAILBOX_14, &StdTxMsg14);
					LPUART1_transmit_string("\r\n  0x314 Transmitted \r\n");

					break;

				default:
					break;
				}

		switch (ExtRxMsg5.id)
		{
				case 0x1844001: //CAN

					CAN_Send (INST_CAN_PAL2,TX_MAILBOX_15, &StdTxMsg15);
					LPUART1_transmit_string("\r\n  0x318 Transmitted \r\n");

					break;

				default:
					break;
				}


}

/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;) {
    if(exit_code != 0) {
      break;
    }
  }
  return exit_code;
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.1 [05.21]
**     for the Freescale S32K series of microcontrollers.
**
** ###################################################################
*/

 

 

And i am attching the Settings of my CAN 0 and CAN 1 

 

And please notice that i coudln't able to successfully receive the extended CAN ID after i was successfully able to transmit the Ext CAN data. Can some one give me different perspective 

0 Kudos
1 Solution
1,090 Views
akhilranga
Contributor IV

Hi Peter. 

Thanks for the tip. I just changed the MB's of the Rx. And the code worked perfectly fine. 

Earlier I was assuming that Tx and Rx should have different MB. But i didn't knew that the each CAN instance the MB should be different. 

View solution in original post

0 Kudos
2 Replies
1,103 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

seems you are using same MB for TX and RX functionality on the same FlexCAN module. Rather use another set of MBs just for RX. Same one can be used but you must config it for RX after each Send is finished. I do not think it is ideal.
Also Receive function just enable MB interrupt and you should wait or check the receive is complete. I can recommend to add Callback function which is called from MB interrupt, so you know a time message is moved into the MB and updated RX buffer can be read.

BR, Petr

0 Kudos
1,091 Views
akhilranga
Contributor IV

Hi Peter. 

Thanks for the tip. I just changed the MB's of the Rx. And the code worked perfectly fine. 

Earlier I was assuming that Tx and Rx should have different MB. But i didn't knew that the each CAN instance the MB should be different. 

0 Kudos