cancel
Showing results for 
Search instead for 
Did you mean: 

StdPeriph Driver canBUS (STM32f4) and HAL CAN (STM32f0) not working (SOLVED)

dracog71
Associate II

Hi everyone!

I'm in a hurry, I have a canbus network working with stm32F4 without problems working in STD Periph drivers, and even I have 2 sTM32F0 working HAL Can with out problems (I'm gonna left the 2 config), when I try to conect both networks, the CAN_H and CAN_L goes high, I can see in osciloscoe, the signal, when I connect the F0 to F4 network this happens, I cannot see whats wrong, please some advice?

this is the STM32F407 CANbus config with STD periph Drivers, I get those values from this web page from (http://www.bittiming.can-wiki.info) with a sample point of 87.5% Bit Rate = 125 and Clock rate of 42Mhz based in configuration

 NVIC_IRQChannel = CAN1_RX0_IRQn;                
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE)
CAN_FilterInitStructure.CAN_FilterNumber         = 13;   
 
CAN_DeInit(CANBUSx);   
 
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_13tq; 
CAN_InitStructure.CAN_BS2  = CAN_BS2_2tq;    
CAN_InitStructure.CAN_Prescaler = 21;       
		 
CAN_Init(CANBUSx, &CAN_InitStructure);
 
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO;
CAN_FilterInitStructure.CAN_FilterMode           = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale          = CAN_FilterScale_16bit;
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_FIFO_ID;
CAN_FilterInitStructure.CAN_FilterActivation     = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);	
	
NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 255;
NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//FIFO  
CAN_ITConfig(CANBUSx, CAN_IT_FMP0, ENABLE);
CAN_ITConfig(CANBUSx, CAN_IT_FMP1, ENABLE);
CAN_ITConfig(CANBUSx, CAN_IT_FF0, ENABLE);
CAN_ITConfig(CANBUSx, CAN_IT_FF1, ENABLE);
CAN_ITConfig(CANBUSx, CAN_IT_FOV0, ENABLE);
CAN_ITConfig(CANBUSx, CAN_IT_FOV1, ENABLE);	

And this is the configuracion of STM32F042 based in HAL drivers with a sample point of 87.5% Bit Rate = 125 and Clock rate of 48Mhz based in configuration

CAN_FilterConfTypeDef  sFilterConfig;
 static CanTxMsgTypeDef        TxMessage;
 static CanRxMsgTypeDef        RxMessage;
 
 hcan.Instance = CAN;
 
 hcan.Init.Mode = CAN_MODE_NORMAL;
 hcan.Init.SJW = CAN_SJW_1TQ;
 hcan.Init.BS1 = CAN_BS1_13TQ;
 hcan.Init.BS2 = CAN_BS2_2TQ;
 hcan.Init.Prescaler = 24;
 hcan.Init.TTCM = DISABLE;
 hcan.Init.ABOM = ENABLE;
 hcan.Init.AWUM = ENABLE;
 hcan.Init.NART = DISABLE;
 hcan.Init.RFLM = DISABLE;
 hcan.Init.TXFP = DISABLE;
 if (HAL_CAN_Init(&hcan) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 hcan.pTxMsg = &TxMessage;
 hcan.pRxMsg = &RxMessage;
 
   /*##-2- Configure the CAN Filter ###########################################*/
 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(&hcan, &sFilterConfig) != HAL_OK)
 {
 /* Filter configuration Error */
 Error_Handler();
 }
 
 hcan.pTxMsg->StdId = 0x321;
 hcan.pTxMsg->ExtId = 0x00;
 hcan.pTxMsg->RTR = CAN_RTR_DATA;
 hcan.pTxMsg->IDE = CAN_ID_STD;
 hcan.pTxMsg->DLC = 8;
 hcan.pTxMsg->Data[0] = 0x40;
 hcan.pTxMsg->Data[1] = 0x41;
 hcan.pTxMsg->Data[2] = 0x42;
 hcan.pTxMsg->Data[3] = 0x43;
 hcan.pTxMsg->Data[4] = 0x44;
 hcan.pTxMsg->Data[5] = 0x45;
 hcan.pTxMsg->Data[6] = 0x46;
 hcan.pTxMsg->Data[7] = 0x47;  

Both networks are working with interruptions, working well canbus F4 network without problems and canbus F0 network without problems, I dont know if this ir enough to detect a problem, if you need my MCU initialization I can upload it, or even any part of my code, I need to communicate those two, I CANNOT change to hal in F4 (Because that MCU are in a product already) and with F0 I need to use HAL (Because of the project).

Any help would be appreciated.

2 REPLIES 2
dracog71
Associate II

Ok! I was able to get to the error, just in case someone has the same problem. in STM32F407 canbus initialization:

// From
CAN_FilterInitStructure.CAN_FilterNumber         = 13;
//To
CAN_FilterInitStructure.CAN_FilterNumber         = 0;  
//From
CAN_FilterInitStructure.CAN_FilterScale          = CAN_FilterScale_16bit;
//To
CAN_FilterInitStructure.CAN_FilterScale          = CAN_FilterScale_32bit;

I'v got this parameter twice assigned to " different parameters:

CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;

Changing that, everything was working!

SGUIG.1
Associate II

hello, i am facing the same problem rn! is it possible for u to send me the code after working? it would be really great if u did. thank u