cancel
Showing results for 
Search instead for 
Did you mean: 

''CAN'' between STM32f103RB and STM32f407VG

MehranMsba
Associate II
Posted on March 21, 2015 at 16:01

I set this configuration to communicate between f103 and f4

stm32f407:

GPIO_InitTypeDef GPIO_InitStruct;
CAN_FilterConfTypeDef sFilterConfig;
prtCan.Instance = CAN1;
prtCan.pTxMsg = &TxMessage;
prtCan.pRxMsg = &RxMessage;
__CAN1_CLK_ENABLE();
__GPIOD_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* CAN1 RX GPIO pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1; 
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
prtCan.Init.TTCM = DISABLE;
prtCan.Init.ABOM = DISABLE;
prtCan.Init.AWUM = DISABLE;
prtCan.Init.NART = DISABLE;
prtCan.Init.RFLM = DISABLE;
prtCan.Init.TXFP = DISABLE;
prtCan.Init.Mode = CAN_MODE_NORMAL;
prtCan.Init.SJW = CAN_SJW_1TQ;
prtCan.Init.BS1 = CAN_BS1_6TQ;
prtCan.Init.BS2 = CAN_BS2_3TQ;
prtCan.Init.Prescaler = 42;
if(HAL_CAN_Init(&prtCan) != HAL_OK)
{
/* Initialization Error */ 
}
sFilterConfig.FilterNumber = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = 0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if(HAL_CAN_ConfigFilter(&prtCan, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
}
prtCan.pTxMsg->StdId = 1;
prtCan.pTxMsg->ExtId = 0x00;
prtCan.pTxMsg->RTR = CAN_RTR_DATA;
prtCan.pTxMsg->IDE = CAN_ID_STD;
prtCan.pTxMsg->DLC = 8;

stm32f103:

CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the CAN global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel =USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); 
/* Configure CAN pin: RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_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_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
CAN_InitStructure.CAN_Prescaler = 36;
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=CAN_Filter_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);

I can not send data from 407 to LEC bits of 103 is 1 or 2 in debugging. Whats the problem? Is this configuration true? Thanks.
1 REPLY 1
MehranMsba
Associate II
Posted on March 23, 2015 at 11:30

It's solved now. I change HAL_GetTick and create new function for timeout handling.

I use GCD for getting best clock prescaler between these cores.

6MHz is best CAN clock for this communication.

I used 75% sample point then BS1 is 8 and BS2 is 3 and with SJW=1.

I hope it's useful for others.