I am trying to communicate using can protocol but its not working

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

I am trying to communicate using can protocol but its not working

1,922 Views
prasadrao
Contributor I

Hello All,

I am trying to communicate  bus master using  can protocol but communication not happening.

I am new to this concepts.

I run the freescale example related to CAN protocol but its not working.

please explain with some working code below points

How to communicate using CAN protocol

How to Transmit messages

How to Receive messages

Please do the needful

0 Kudos
18 Replies

1,421 Views
daweiyou
NXP Employee
NXP Employee

Hi Prasad:

Do you use Tower board to evaluate?

Or yourself board?

Dawei You(尤大为)

0 Kudos

1,421 Views
prasadrao
Contributor I

Hi Dawei,

I am using tower board for evaluate.

if it's required then will send the code.

Thanks and Regards

Prasad

0 Kudos

1,421 Views
daweiyou
NXP Employee
NXP Employee

Hi Prasad:

For tower board, PLS check TWR-SER J15 configuration;

The lable says as below, there is some misunderstanding

Shunt across 1&2: CAN TRX in Sleep mode

Shunt across 3&4: CAN_S Grounded through elevator

Shunt across 5&6: Connect CANRX to C_RXD

Shunt across 7&8: Connect CANTX to C_TXD

Shunt across 9&10: Add 120 R between CANL and CANH

Default Jumper Setting for J7, Open

Actually ,for TJA1050T, 1&2 should shunt to make TJA1050T enter into high speed mode.

Dawei You(尤大为)

0 Kudos

1,421 Views
prasadrao
Contributor I

Hi Dawei,

Ok i will check and i will mail you.

Thanks and Regards

Prasad

0 Kudos

1,421 Views
prasadrao
Contributor I

Hi Dawei,

One thing actually i am using FLEXCAN and MQX for communication.

With out Flexcan and mqx it's working fine(simple c code).

But i need to develop with MQX.

i modified freescale can example but still im having the problem.

i waiting for ur replay.

Regards

Prasad

0 Kudos

1,421 Views
daweiyou
NXP Employee
NXP Employee

Hi:

I ever test MQX can example, which could work.

Once you send the CAN frame, you could measure the TX and RX pin to make sure the pin wave occurs.

And CAN bus should have signal also, but is diffierential signal.

Dawei You(尤大为)

0 Kudos

1,421 Views
prasadrao
Contributor I

Hi,

Can send my current code.

Regards

prasad

0 Kudos

1,421 Views
prasadrao
Contributor I

Last one  week i am working but not resolving the problem

0 Kudos

1,421 Views
prasadrao
Contributor I

Any update on this...

0 Kudos

1,421 Views
daweiyou
NXP Employee
NXP Employee

Hi Prasad:

I use original example located in Freescale_MQX_4_1\mqx\examples\can\flexcan; the original code is OK,

Now you use Tower board to communicate with which client? Another tower board? Make sure they have different node ID

Set as below:

// Set NODE to 1 or 2 depending on which endpoint you are running. See notes in test.c

#define NODE 1

When I debug this CAN code, really some error occurs ever before:

Here are some clues:

(1) the TWR-SER J15 configuration, connect S pin to GND

(2) ever one USB cable make everything awful.

I could measure the CANTX signal when transmitting, but CANH/CANL signal is poor

After I change one cable, all are OK, I guess the previous USB cable have poor power supply capability.

So if you make sure all Hardware configuration is OK, and code is OK, you have to measure the TX and RX signal to see whether the CAN TX signal has passed to TJA1050T.

Dawei You(尤大为)

0 Kudos

1,421 Views
prasadrao
Contributor I

Hi Dawei,

I am using K70  and communicating with busmaster using can protocol.

Busmaster keep on sending  massages  using CAN protocol i have to receive the message and send to k70.

if u need code i will send it now.

i am eagerly waiting for the replay.

Thanks and Regards

Prasad

0 Kudos

1,421 Views
daweiyou
NXP Employee
NXP Employee

Yes, PLS post you sample code.

I could help check.

Dawei You(尤大为)

0 Kudos

1,421 Views
prasadrao
Contributor I

/* Task template list */

TASK_TEMPLATE_STRUCT MQX_template_list[] = {

   { MAIN_TASK, Main_Task, 1000L, 8L, "Main task", MQX_AUTO_START_TASK},

   /* { TX_TASK, Tx_Task, 1000L, 7L, "TX task", 0, 0, 0},*/

    { RX_TASK, Rx_Task, 1000L, 7L, "RX task", 0, 0, 0},

   { 0L, 0L, 0L, 0L, 0L, 0L }

};

#if PSP_MQX_CPU_IS_KINETIS

void MY_FLEXCAN_ISR

(

   /* [IN] FlexCAN base address */

   pointer can_ptr

)

{

   volatile CAN_MemMapPtr        can_reg_ptr;

   vuint_32                               tmp_reg;

   vuint_32                               temp;

     

   can_reg_ptr = (CAN_MemMapPtr)can_ptr;

   /* get the interrupt flag */

   tmp_reg = (can_reg_ptr->IFLAG1 & CAN_IMASK1_BUFLM_MASK);

   // check Tx/Rx interrupt flag and clear the interrupt

   if(tmp_reg){

      /* clear the interrupt and unlock message buffer */

      /* Start CR# 1751 */

      _lwevent_set(&event, tmp_reg);

      //can_reg_ptr->IFLAG1 |= tmp_reg;

      can_reg_ptr->IFLAG1 = tmp_reg;

      /* End CR# 1751 */

      temp = can_reg_ptr->TIMER;

   }/* Endif */

   // Clear all other interrupts in ERRSTAT register (Error, Busoff, Wakeup)

   tmp_reg = can_reg_ptr->ESR1;

   if(tmp_reg & FLEXCAN_ALL_INT){

      /* Start CR# 1751 */

      can_reg_ptr->ESR1 |= (tmp_reg & FLEXCAN_ALL_INT);

      /* End CR# 1751 */

   } /* Endif */

     

   return;

}

#else

void MY_FLEXCAN_ISR

(

   /* [IN] FlexCAN base address */

   pointer can_ptr

)

{

   volatile FLEXCAN_REG_STRUCT_PTR        can_reg_ptr;

   vuint_32                               tmp_reg;

   vuint_32                               temp;

     

   can_reg_ptr = (FLEXCAN_REG_STRUCT_PTR)can_ptr;

   /* get the interrupt flag */

   tmp_reg = (can_reg_ptr->IFLAG & FLEXCAN_IMASK_VALUE);

   // check Tx/Rx interrupt flag and clear the interrupt

   if(tmp_reg){

      /* clear the interrupt and unlock message buffer */

      /* Start CR# 1751 */

      _lwevent_set(&event, tmp_reg);

     // can_reg_ptr->IFLAG |= tmp_reg;

      can_reg_ptr->IFLAG1 = tmp_reg;

      /* End CR# 1751 */

      temp = can_reg_ptr->TIMER;

   }/* Endif */

   // Clear all other interrupts in ERRSTAT register (Error, Busoff, Wakeup)

   tmp_reg = can_reg_ptr->ERRSTAT;

   if(tmp_reg & FLEXCAN_ALL_INT){

      /* Start CR# 1751 */

      can_reg_ptr->ERRSTAT |= (tmp_reg & FLEXCAN_ALL_INT);

      /* End CR# 1751 */

   } /* Endif */

     

   return;

}

#endif

/*TASK*-----------------------------------------------------------

*

* Task Name : Main_Task

* Comments :

*

*

*END*-----------------------------------------------------------*/

void Main_Task(uint_32 parameter)

{ /* Body */

   _task_id     created_task;

   uint_32 result;

   _int_install_unexpected_isr();

  

   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 = 250;

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

   data_len_code = 1;

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

 

   /* Select message format */

   format = FLEXCAN_STANDARD;

  

   /* Select mailbox number */

   //RX_mailbox_num = 0; just for checking purpose.

   RX_mailbox_num = 1;

   TX_mailbox_num = 0;

   RX_remote_mailbox_num = 2;

   TX_remote_mailbox_num = 3;

/*#if NODE==1

   RX_identifier = 0x123;//0x123;

   TX_identifier = 0x321;

   RX_remote_identifier = 0x0F0;

   TX_remote_identifier = 0x00F;

#else

   RX_identifier = 0x123;

   TX_identifier = 0x321;

   RX_remote_identifier = 0x00F;

   TX_remote_identifier = 0x0F0;

#endif   */

  RX_identifier = 0x123;

  TX_identifier = 0x321;

  RX_remote_identifier = 0x00F;

  TX_remote_identifier = 0x0F0;

  

   /* We use default settings */

   bit_timing0 = bit_timing1 = 0;

  

   /* Select operation mode */

   flexcan_mode = FLEXCAN_NORMAL_MODE;

   //  flexcan_mode = FLEXCAN_LOOPBK_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, 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, RX_TASK, 0);

   if (created_task == MQX_NULL_TASK_ID)

   {

      printf("\nRx task: task creation failed.");

   }

   /*created_task = _task_create(0, 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

   while(1){}

} /* EndBody */

void Rx_Task(uint_32 parameter)

{/* Body */  

   //uchar   dptr[8];

   uchar   dptr[8] = {0, 0, 0, 0, 0, 0, 0, 0};

   uint_32 result = -1;

   uint_32 DLC = 0;

   uint_32 ID = 0;

  result = FLEXCAN_Initialize_mailbox( CAN_DEVICE, RX_mailbox_num, RX_identifier,

                                       8, FLEXCAN_RX, format,

                                        interrupt);

   printf("\nFLEXCAN rx remote mailbox initialization. result: 0x%lx", result);

   /* 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);*/

   }

if (_lwevent_wait_ticks(&event, 1 << RX_mailbox_num, FALSE, 5) != 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

  {

    printf("\nReceived data: ");

    for (result = 0; result < DLC; result++) printf ("0x%x ", dptr[result]);

     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);

  }

  

   while(1)

   {           

  

      if (_lwevent_wait_ticks(&event, 1 << RX_mailbox_num, FALSE, 5) != MQX_OK) {

         printf("\nEvent Wait failed");

      }

     _time_delay(5);

  

      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)

      {

         if(result != FLEXCAN_OK)

        {

           printf("\nReceived error. Error Code: 0x%lx\n", result);

            switch(result)

            {

              case FLEXCAN_MESSAGE_OVERWRITTEN:

                  printf("\nMessage overwritten\n");

                  break;

              case FLEXCAN_INVALID_ADDRESS:

                  printf("\Wrong device Number\n");

                  break;

              case FLEXCAN_INVALID_MAILBOX:

                  printf("\Wrong message box number\n");

                  break;

              case FLEXCAN_NO_MESSAGE:

                  printf("\Mail box is empty\n");

                  break;

              case FLEXCAN_MESSAGE_FORMAT_UNKNOWN:

                  printf("\Wrong msg format\n");

                  break;

            }

        }

         printf("\nReceived error. Error Code: 0x%lx", result);

      }

      else

      {

         printf("\nReceived data: ");

         for (result = 0; result < DLC; result++) printf ("0x%x ", dptr[result]);

         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);

    

   } 

  

   result = FLEXCAN_Initialize_mailbox( CAN_DEVICE, RX_remote_mailbox_num, RX_remote_identifier,

  8, FLEXCAN_TX, format,

  interrupt);

  printf("\nFLEXCAN rx remote mailbox initialization. result: 0x%lx", result);

 

} /* EndBody */

0 Kudos

1,421 Views
daweiyou
NXP Employee
NXP Employee

I think if you can’t enter into ISR, the issue maybe not caused by software.

Dawei You(尤大为)

0 Kudos

1,421 Views
prasadrao
Contributor I

Hi Dawei,

if (_lwevent_wait_ticks(&event, 1 << RX_mailbox_num, FALSE, 0) != MQX_OK) {

         printf("\nEvent Wait failed");

      }

please find the bold one(0) if zero replace with five then its not allowing me inside the code,its stopping there only.(I debug and i checked).

Thanks and Regards

Prasad

0 Kudos

1,421 Views
prasadrao
Contributor I

Yes u are right i am not entering into the ISR.

0 Kudos

1,421 Views
daweiyou
NXP Employee
NXP Employee

Hi Prasad:

Find some info from MQX release notes, for you ref:

5.12 FlexCan driver issues

Several issues are identified during the development of the FlexCAN driver:

On TWR-K70F120M board, the TX/RX signals are not routed to the elevator by default and the FlexCAN example does not work. To enable the FlexCAN operation, solder the zero-ohm resistors, R22 and R23, on TWR-K70F120M board.

The 10-kbit baudrate doesn‟t generally work. FlexCAN detects bit0 errors in its own transmitted messages.

Dawei You(尤大为)

0 Kudos

1,421 Views
daweiyou
NXP Employee
NXP Employee

Hi Prasad:

I think your code have no critical issue,

I want to know, could you enter into ISR when busmaster send data with 250kbps?

BTW, still some minor adjust needed:

After you call below code, you could lock mail box only when below call return MQX_OK, for LWEVENT_WAIT_TIMEOUT returned value, you shouldn’t continue;

if (_lwevent_wait_ticks(&event, 1 << RX_mailbox_num, FALSE, 5) != MQX_OK) {

printf("\nEvent Wait failed");

}

timedelay(5);

result = FLEXCAN_Lock_mailbox (CAN_DEVICE, RX_mailbox_num);

if(result != FLEXCAN_OK)

{

printf("\nLock mailbox failed. Error Code: 0x%lx", result);

}

Dawei You(尤大为)

0 Kudos