cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F105 using only CAN2 module

taca_vi
Associate

I'm using a STM32F105 MCU on which I want to use only the CAN 2 Module to communicate. The pins for the CAN1 Module are used for other stuff. 

Currently I'm setting the init parameters for CAN1 and CAN2 but CAN1 is never startet. On the low level initialisation for CAN1, only the peripheral clock is activated but the GPIO's are not initialized.

Somehow it doesn't seem to work. 

If I'm using a protocol Stack for CANOpen bevor and then initalize the basic CAN protocol afterwards it seems to work. All the CAN1 and CAN2 Registers do have the same values in both cases (starting CAN directly and starting CAN after using the CANOpen stack). 

Do I really have to start the CAN1 Modul in order to use the CAN2 Modul ? Isn't it enough to just activate the peripheral Clock of CAN1 ?

Does anybody have a solution to start only the CAN2 Modul without having to set the GPIO's for the CAN1 modul?

Here's my CAN Initialisation:


void MX_CAN2_Init(CAN_HandleTypeDef* hcan1, CAN_HandleTypeDef* hcan2)
{
/* Inits CAN1 (CAN1 is not initalized but struct is needed for filter) */
hcan1->Instance = CAN1;
hcan1->Init.Prescaler = 24;
hcan1->Init.Mode = CAN_MODE_NORMAL;
hcan1->Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1->Init.TimeSeg1 = CAN_BS1_6TQ;
hcan1->Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1->Init.TimeTriggeredMode = DISABLE;
hcan1->Init.AutoBusOff = DISABLE;
hcan1->Init.AutoWakeUp = DISABLE;
hcan1->Init.AutoRetransmission = DISABLE;
hcan1->Init.ReceiveFifoLocked = DISABLE;
hcan1->Init.TransmitFifoPriority = DISABLE;

/* Inits CAN2*/
hcan2->Instance = CAN2;
hcan2->Init.Prescaler = 24;
hcan2->Init.Mode = CAN_MODE_NORMAL;
hcan2->Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan2->Init.TimeSeg1 = CAN_BS1_6TQ;
hcan2->Init.TimeSeg2 = CAN_BS2_1TQ;
hcan2->Init.TimeTriggeredMode = DISABLE;
hcan2->Init.AutoBusOff = DISABLE;
hcan2->Init.AutoWakeUp = DISABLE;
hcan2->Init.AutoRetransmission = DISABLE;
hcan2->Init.ReceiveFifoLocked = DISABLE;
hcan2->Init.TransmitFifoPriority = DISABLE;

/* intis CAN2*/
if (HAL_CAN_Init(hcan2) != HAL_OK)
{
f1a1_errHdlr__treatHalErr();
}

/* init pins, clock and enables interrupts*/
HAL_CAN_MspInit(hcan2);

HAL_NVIC_SetPriority(CAN2_RX0_IRQn, F1A1_HWACC_PRI__CAN2RX0, 0);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_SetPriority(CAN2_TX_IRQn, F1A1_HWACC_PRI__CAN2TX, 0);
HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
}

And here is my lowlevel initialisation:

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hcan->Instance==CAN2)
{
/* USER CODE BEGIN CAN2_MspInit 0 */

/* USER CODE END CAN2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_CAN1_CLK_ENABLE();
__HAL_RCC_CAN2_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();
/**CAN2 GPIO Configuration
PB5 ------> CAN2_RX
PB6 ------> CAN2_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
__HAL_AFIO_REMAP_CAN2_ENABLE();
}
}

 

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello,

If you are using only CAN2 instance why are you doing the init of CAN1 instance? What you need to do is enabling CAN1 RCC clock to work with CAN2. So remove all the code related to CAN1 init except __HAL_RCC_CAN1_CLK_ENABLE();

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
taca_vi
Associate

Maybe important, after the initalization I'm starting the CAN2 Modul with:

HAL_CAN_Start(&hcan2);

and some pictures of the CAN Registers:

 

taca_vi_1-1715421486790.pngtaca_vi_2-1715421499140.png

 

SofLit
ST Employee

Hello,

If you are using only CAN2 instance why are you doing the init of CAN1 instance? What you need to do is enabling CAN1 RCC clock to work with CAN2. So remove all the code related to CAN1 init except __HAL_RCC_CAN1_CLK_ENABLE();

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.