cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042 CAN Example code

jbrela
Associate
Posted on June 22, 2014 at 17:32

Hello

Does anyone have working initialization code for STM32F042?

I wrote this one bellow,  but it doesn't work and I dont know why?

CAN_TX line is low (measured by means of oscilloscope).

I have got simillar working code for stm32f103c6t6.

void CAN_init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);

//CAN TX

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  //CAN RX

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable CAN1 RX0 interrupt IRQ channel */

  NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  CAN_InitTypeDef CAN_InitStructure;

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

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

  CAN_Init(CAN1, &CAN_InitStructure);

  /* CAN filter init */

  CAN_FilterInitTypeDef CAN_FilterInitStructure;

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

  /* transmit 1 message */

  CanTxMsg TxMessage;

  TxMessage.StdId = 0x7ff;

  //TxMessage.ExtId = 0x7f7f;

  TxMessage.IDE = CAN_ID_STD;

  TxMessage.RTR = CAN_RTR_DATA;

  TxMessage.DLC = 0;

  TxMessage.Data[0] = 0xDE;

  TxMessage.Data[1] = 0xCA;

  CAN_Transmit(CAN1, &TxMessage);

}

Can anyone help?

Best regards

Janusz Brela
3 REPLIES 3
Posted on June 22, 2014 at 22:23

Would you need to use GPIO_PinAFConfig() to configure the specific peripheral functionality?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 22, 2014 at 22:30

STM32F0xx_StdPeriph_Lib_V1.3.1\Projects\STM32F0xx_StdPeriph_Examples\CAN\CAN_Networking\main.c

http://www.st.com/web/en/catalog/tools/PF257884

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jbrela
Associate
Posted on June 23, 2014 at 14:03

I'll check it.

Thanks a lot, I could not find good examples o ST web site.

Best Regards

Janusz Brela