cancel
Showing results for 
Search instead for 
Did you mean: 

CAN loopback very weird problem

randyao12345
Associate II
Posted on October 10, 2014 at 19:25

Hi ST forums,

I wrote a piece of code to try out CAN_TX and CAN_RX to loopback the data into from PD1 (CAN1_TX) into PD0 (CAN1_RX). It seems to be working (I get back what I sent). Then I realized even if I unplug the jumper from PD0 to PD1 it is still receiving CAN data, (Receive LED on, data streaming in).

Is TxMessage.Data connected to RxMessage.Data? Please take a look:

&sharpinclude ''stm32f4xx_gpio.h''

&sharpinclude ''stm32f4xx_rcc.h''

&sharpinclude ''stm32f4xx_can.h''

&sharpinclude ''misc.h''

&sharpinclude ''semihosting.h''

GPIO_InitTypeDef  GPIO_InitStructure;

CAN_InitTypeDef        CAN_InitStructure;

CAN_FilterInitTypeDef  CAN_FilterInitStructure;

NVIC_InitTypeDef  NVIC_InitStructure;

CanTxMsg TxMessage;

CanRxMsg RxMessage;

void CAN_Config(void);

void NVIC_Config(void);

void CAN_Config(void)

{

  /* Enable GPIO clock */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

  /* Connect CAN pins to AF9 */

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);

  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);

  /* Configure CAN RX and TX pins */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_Init(GPIOD, &GPIO_InitStructure);

  //CAN1

  /* CAN register init */

  CAN_DeInit(CAN1);

  /* CAN cell init */

  CAN_DBGFreeze(CAN1, DISABLE);

  CAN_InitStructure.CAN_TTCM = DISABLE;

  CAN_InitStructure.CAN_ABOM = DISABLE;

  CAN_InitStructure.CAN_AWUM = DISABLE;

  CAN_InitStructure.CAN_NART = DISABLE;

  CAN_InitStructure.CAN_RFLM = DISABLE;

  CAN_InitStructure.CAN_TXFP = DISABLE;

  CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;

  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

  CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;

  CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;

  CAN_InitStructure.CAN_Prescaler = 200; //CAN baud rate 10kbits

  CAN_Init(CAN1, &CAN_InitStructure);

  /* CAN filter init */

  CAN_FilterInitStructure.CAN_FilterNumber = 0;

  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;

  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;

  CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;

  CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;

  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;

  CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;

  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;

  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;

  CAN_FilterInit(&CAN_FilterInitStructure);

  /* Transmit Structure preparation */

  TxMessage.StdId = 0x321;

  TxMessage.ExtId = 0x00;

  TxMessage.RTR = CAN_RTR_DATA;

  TxMessage.IDE = CAN_ID_STD;

  TxMessage.DLC = 1;

  TxMessage.Data[0] = 0x55;

  /* Enable FIFO 0 message pending Interrupt */

  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

}

void NVIC_Config(void)

{

  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

void Init_RxMes(CanRxMsg *RxMessage)

{

  uint8_t i = 0;

  RxMessage->StdId = 0x00;

  RxMessage->ExtId = 0x00;

  RxMessage->IDE = CAN_ID_STD;

  RxMessage->DLC = 8;

  RxMessage->FMI = 0;

  for (i = 0;i<8;i++)

  {

    RxMessage->Data[i] = 0x00;

  }

}

int main(void)

{

  CAN_Config();

  NVIC_Config();

  Init_RxMes(&RxMessage);

  /* Infinite loop */

  while (1)

  {

    CAN_Transmit(CAN1, &TxMessage);

    printf(''\nData Received (0): %X'',RxMessage.Data[0]);

//  printf(''\nData Received (1): %d'',RxMessage.Data[1]);

//  printf(''\nData Received (2): %d'',RxMessage.Data[2]);

//  printf(''\nData Received (3): %d'',RxMessage.Data[3]);

//  printf(''\nData Received (4): %d'',RxMessage.Data[4]);

//  printf(''\nData Received (5): %d'',RxMessage.Data[5]);

//  printf(''\nData Received (6): %d'',RxMessage.Data[6]);

//  printf(''\nData Received (7): %d'',RxMessage.Data[7]);

  }

}

void CAN1_RX0_IRQHandler(void)

{

  CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);

  GPIOD->BSRRL = GPIO_Pin_12;

}

#!weird
4 REPLIES 4
Posted on October 10, 2014 at 19:53

Loop back works internally, shorting the pins externally won't provide ''loopback'' in normal mode.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
randyao12345
Associate II
Posted on October 10, 2014 at 20:03

Oh, thanks Clive.

But if I wish to loopback CAN1_TX to CAN2_RX, is that possible?

Posted on October 10, 2014 at 21:22

But if I wish to loopback CAN1_TX to CAN2_RX, is that possible?

It would surprise me, this isn't a USART, it's a bus with specific signalling requirements.

You'd want transceivers at both ends, and an Rx device to sink the data

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Mark Edwards
Associate II
Posted on October 11, 2014 at 22:44

This will work as a hardware loopback

https://my.st.com/public/STe2ecommunities/ImageLibrary/CAN.png