硬汉嵌入式论坛

 找回密码
 立即注册
查看: 685|回复: 0
收起左侧

STM32F4使用HAL库双CAN通信出现错误Wait initialisation acknowledge解决办法

[复制链接]

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
111540
QQ
发表于 2023-10-24 08:29:50 | 显示全部楼层 |阅读模式



准备给V5和V6板子板载的双CAN移植CANopen协议栈,发现板载的CAN1和CAN2连接后,CAN的初始化都过不去,直接卡死在这个里面了:

[C] 纯文本查看 复制代码
  /* Request initialisation */
  SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);

  /* Get tick */
  tickstart = HAL_GetTick();

  /* Wait initialisation acknowledge */
  while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
  {
    if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
    {
      /* Update error code */
      hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;

      /* Change CAN state */
      hcan->State = HAL_CAN_STATE_ERROR;

      return HAL_ERROR;
    }
  }


经过不断测试,发现要提前将CAN1和CAN2的引脚状态配置好才可以正常初始化:


[C] 纯文本查看 复制代码
/*
*********************************************************************************************************
*	函 数 名: HAL_CAN_MspInit
*	功能说明: 配置CAN gpio
*	形    参: hcan
*	返 回 值: 无
*********************************************************************************************************
*/
void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
{
	GPIO_InitTypeDef   GPIO_InitStruct;

	/*##-1- Enable peripherals and GPIO Clocks #################################*/
	/* CAN1 Periph clock enable */
	__HAL_RCC_CAN1_CLK_ENABLE();
	__HAL_RCC_CAN2_CLK_ENABLE();
	/* Enable GPIO clock ****************************************/
	CAN1_TX_GPIO_CLK_ENABLE();
	CAN1_RX_GPIO_CLK_ENABLE();

	/*##-2- Configure peripheral GPIO ##########################################*/
	/* CAN1 TX GPIO pin configuration */
	GPIO_InitStruct.Pin = CAN1_TX_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Alternate =  CAN1_TX_AF;

	HAL_GPIO_Init(CAN1_TX_GPIO_PORT, &GPIO_InitStruct);

	/* CAN1 RX GPIO pin configuration */
	GPIO_InitStruct.Pin = CAN1_RX_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Alternate =  CAN1_RX_AF;

	HAL_GPIO_Init(CAN1_RX_GPIO_PORT, &GPIO_InitStruct);

	/*##-3- Configure the NVIC #################################################*/
	/* NVIC configuration for CAN1 Reception complete interrupt */
	HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 1, 0);
	HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
	
	HAL_NVIC_SetPriority(CAN1_TX_IRQn, 1, 0);
	HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);

	/*##-1- Enable peripherals and GPIO Clocks #################################*/
	/* CAN1 Periph clock enable */
	__HAL_RCC_CAN1_CLK_ENABLE();
	__HAL_RCC_CAN2_CLK_ENABLE();
	/* Enable GPIO clock ****************************************/
	CAN2_TX_GPIO_CLK_ENABLE();
	CAN2_RX_GPIO_CLK_ENABLE();

	/*##-2- Configure peripheral GPIO ##########################################*/
	/* CAN1 TX GPIO pin configuration */
	GPIO_InitStruct.Pin = CAN2_TX_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Alternate =  CAN2_TX_AF;

	HAL_GPIO_Init(CAN2_TX_GPIO_PORT, &GPIO_InitStruct);

	/* CAN1 RX GPIO pin configuration */
	GPIO_InitStruct.Pin = CAN2_RX_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Alternate =  CAN2_RX_AF;

	HAL_GPIO_Init(CAN2_RX_GPIO_PORT, &GPIO_InitStruct);

	/*##-3- Configure the NVIC #################################################*/
	/* NVIC configuration for CAN1 Reception complete interrupt */
	HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 1, 0);
	HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
	
	HAL_NVIC_SetPriority(CAN2_TX_IRQn, 1, 0);
	HAL_NVIC_EnableIRQ(CAN2_TX_IRQn);
}



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-11-1 07:19 , Processed in 0.686677 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表