求求各位帮帮忙,在下新人,用HAL库在can初始化的时候,调试发现在
while ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET) {
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;
}
}
这里最后报错,好几天了一直弄不好,各位帮忙看一下[mw_shl_code=applescript,true]初始化参数如下,时钟42MHZ
void CAN_Init(CAN_TypeDef *CANx, FilterUnit unit[], uint16_t num)
{
CAN_HandleTypeDef *phandle;
if(CANx == CAN1)
{
phandle = &CAN1Handle;
}
else
{
phandle = &CAN2Handle;
}
phandle->Instance = CANx;
phandle->Init.Prescaler = 3; //APB1总线时钟为42MHz,3分频后为14MHz
phandle->Init.Mode = CAN_MODE_NORMAL;
phandle->Init.SyncJumpWidth = CAN_SJW_4TQ;
phandle->Init.TimeSeg1 = CAN_BS1_7TQ;
phandle->Init.TimeSeg2 = CAN_BS2_6TQ; //CAN波特率为 14MHz / (7 + 6 + 1) = 1MHz
phandle->Init.TimeTriggeredMode = DISABLE;
phandle->Init.AutoBusOff = DISABLE;
phandle->Init.AutoWakeUp = DISABLE;
phandle->Init.AutoRetransmission = DISABLE;
phandle->Init.ReceiveFifoLocked = DISABLE;
phandle->Init.TransmitFifoPriority = DISABLE;
if(HAL_CAN_Init(phandle)!=HAL_OK)
{
while(1);//停在这个地方
}
CAN_FilterInit(CANx, unit, num);
HAL_CAN_ActivateNotification(phandle, CAN_IT_RX_FIFO0_MSG_PENDING
| CAN_IT_RX_FIFO1_MSG_PENDING );
HAL_CAN_Start(phandle);
}
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
if(hcan->Instance == CAN1)
{
__HAL_RCC_CAN1_CLK_ENABLE();
CAN1_TX_PORT_CLK_ENABLE();
CAN1_RX_PORT_CLK_ENABLE();
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
GPIO_InitStruct.Pin = CAN1_TX_PIN;
HAL_GPIO_Init(CAN1_TX_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
GPIO_InitStruct.Pin = CAN1_RX_PIN;
HAL_GPIO_Init(CAN1_RX_PORT, &GPIO_InitStruct);
HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 1, 0);
HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);
}
else if(hcan->Instance == CAN2)
{
__HAL_RCC_CAN2_CLK_ENABLE();
CAN2_TX_PORT_CLK_ENABLE();
CAN2_RX_PORT_CLK_ENABLE();
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
GPIO_InitStruct.Pin = CAN2_TX_PIN;
HAL_GPIO_Init(CAN2_TX_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = GPIO_AF9_CAN2;
GPIO_InitStruct.Pin = CAN2_RX_PIN;
HAL_GPIO_Init(CAN2_RX_PORT, &GPIO_InitStruct);
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 0);
HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
}
}
void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
{
if(hcan->Instance==CAN1)
{
__HAL_RCC_CAN1_FORCE_RESET();
__HAL_RCC_CAN1_RELEASE_RESET();
HAL_GPIO_DeInit(CAN1_TX_PORT, CAN1_TX_PIN);
HAL_GPIO_DeInit(CAN1_RX_PORT, CAN1_RX_PIN);
}
else if(hcan->Instance == CAN2)
{
__HAL_RCC_CAN2_FORCE_RESET();
__HAL_RCC_CAN2_RELEASE_RESET();
HAL_GPIO_DeInit(CAN2_TX_PORT, CAN2_TX_PIN);
HAL_GPIO_DeInit(CAN2_RX_PORT, CAN2_RX_PIN);
}
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>