cancel
Showing results for 
Search instead for 
Did you mean: 

I couldnot make STM32F103RC CANBUS work

mehmet.karakaya
Associate III
Posted on February 24, 2017 at 17:37

hello dear forum,

I copied following code from std peripheral library

however I couldnot make it work

what I am doing wrong ?

thank you

---------------

-

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

----------------

  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

--------------------------------------

CAN_Initialize(void)

{

  CAN_InitTypeDef        CAN_InitStructure;

  CAN_FilterInitTypeDef  CAN_FilterInitStructure;

  /* CAN register init */

  CAN_DeInit(CAN1);

  CAN_StructInit(&CAN_InitStructure);

  /* CAN cell init */

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

  CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;

  CAN_InitStructure.CAN_Prescaler=1;

  CAN_Init(CAN1, &CAN_InitStructure);

  /* CAN filter init */

  CAN_FilterInitStructure.CAN_FilterNumber=1;

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

  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;

  CAN_FilterInit(&CAN_FilterInitStructure);

  /* CAN FIFO0 message pending interrupt enable */

  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

  return ;

}

---------------------------

void CAN_TXmit(void){

// 

I call this function 10 times every second

  CanTxMsg TxMessage;

/* transmit 1 message */

  TxMessage.StdId=0x00;

  TxMessage.ExtId=0x1234;

  TxMessage.IDE=CAN_ID_EXT;

  TxMessage.RTR=CAN_RTR_DATA;

  TxMessage.DLC=2;

  TxMessage.Data[0]=0xDE;

  TxMessage.Data[1]=0xCA;

  CAN_Transmit(CAN1, &TxMessage);

}

-----------------------

void USB_LP_CAN1_RX0_IRQHandler(void)

{

    i++;  

this variable never increase - i.e. it never enters interrupt handler

  CanRxMsg RxMessage;

  CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);

}

#can #stm32f103
3 REPLIES 3
Posted on February 24, 2017 at 19:09

>>

what I am doing wrong ?

I'm not interested in playing these 'guess what's wrong games', post complete and compilable code, where the context, connectivity, and intent are clearly apparent.

STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\CAN\LoopBack\main.c

Has the correct part been selected and defined for the compiler/project?

Does the IRQ handler get linked properly for the part, and match the vector table entries?

Are you using a .CPP file?

Does the chip support CAN?

Do you see an error or status returned?

Does the FIFO status indicate is has some content?

Is the problem in code you have chosen not to provide?

I've posted working examples for the F2 and F4, should be very similar, review those.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on February 25, 2017 at 14:49

 ,

 ,

thanks for your kind answer Clive

I added all the CAN related code in the above post

1 ) I searched MCU forum however I find only HAL examples ,

can you provide the link to your working examples for STD library which you mentioned above

2) my CAN_Init function returns 0 which means ,

♯ define CANINITFAILED  ,  ,  ,  ,  ,  ,  ,(uint8_t)0x00

is it becouse I have no physical CAN transceiver connected to the MCU ?

cannot I watch TX line with osciloscope without a tranceiver connected ?

thanks

Posted on February 25, 2017 at 17:47

In loop-back mode you shouldn't need anything on the pins.

Digging through the forum search I pulled a couple

https://community.st.com/0D70X000006SHfTSAW

 

https://community.st.com/0D70X000006SUMiSAO

 

Not familiar with your board/chip

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..