STM32F1xx CAN2接收未被调用的中断

时间:2016-03-30 14:05:51

标签: c interrupt stm32 can-bus

我已成功与两个STM32F105vC(有两个CAN)的CAN1建立CAN通信,我可以发送和接收CAN帧。

但是当我改变我的代码以使用CAN2时,它可以工作,我的意思是它确认收到的CAN帧,但接收中断根本没有被调用

我认为这是过滤器配置,我多次更改了BankNumber(尝试了0,1,12,20),没有运气。

以下是我的配置:

Initilizing:

init_HAL_CAN(CAN2);

if(HAL_CAN_Receive_IT(canHandle, CAN_FIFO0) != HAL_OK)
{
    /* Reception Error */
    Error_Handler();
}

接收回拨:

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* CanHandle)
{
    LED_DBG_TOGGLE();

    // other 
    // stuff 
    // here
    //...

    /* Receive */
    if(HAL_CAN_Receive_IT(CanHandle, CAN_FIFO0) != HAL_OK)
    {
        /* Reception Error */
        Error_Handler();
    }
}

初始化功能:

void init_HAL_CAN(CAN_TypeDef* _CANInstance)
{
    CAN_FilterConfTypeDef sFilterConfig;
    static CanTxMsgTypeDef TxMessage;
    static CanRxMsgTypeDef RxMessage;

    /*##-1- Configure the CAN peripheral #######################################*/
    canHandle->Instance = _CANInstance;
    canHandle->pTxMsg = &TxMessage;
    canHandle->pRxMsg = &RxMessage;
    canHandle->Init.TTCM = DISABLE; //Non time trigger communication mode //
    canHandle->Init.ABOM = DISABLE;  //The software automatically Bus-off management     //
    canHandle->Init.AWUM = DISABLE; //Sleep mode wake by software (clear CAN-> MCR SLEEP) (automatic wake-up mode)//
    canHandle->Init.NART = DISABLE;  //Disable automatic transfer message (non-automatic retransmission mode)//
    canHandle->Init.RFLM = DISABLE; //The message is not locked, the new cover the old // 
    canHandle->Init.TXFP = DISABLE; // Priority is determined by the message identifier //

    canHandle->Init.Mode = CAN_MODE_NORMAL;
    canHandle->Init.SJW = CAN_SJW_1TQ;
    canHandle->Init.BS1 = CAN_BS1_2TQ;
    canHandle->Init.BS2 = CAN_BS2_1TQ;
    canHandle->Init.Prescaler = 7;

    if(HAL_CAN_Init(canHandle) != HAL_OK)
    {
        /* Initialization Error */
        Error_Handler();
        return false;
    }

    /*##-2- Configure the CAN Filter ###########################################*/
    CAN_FilterConfTypeDef sFilterConfig;
    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 = 20;

    if (HAL_CAN_ConfigFilter(canHandle, &sFilterConfig) != HAL_OK)
    {
        /* Filter configuration Error */
        Error_Handler();
        return false;
    }

}

IRQHandler(放在stm32f1xx_it.c中):

void CAN2_RX0_IRQHandler(void)
{
    HAL_CAN_IRQHandler(canHandle);
}

MspInit函数(放在stm32f1xx_hal_msp.c中):

void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
{
    GPIO_InitTypeDef   GPIO_InitStruct;

    /*##-1- Enable peripherals and GPIO Clocks #################################*/
    /* CAN1 Periph clock enable */
    CANx1_CLK_ENABLE(); // Need to enable CAN1 clock too.
    CANx2_CLK_ENABLE();
    /* Enable GPIO clock ****************************************/
    CANx2_GPIO_CLK_ENABLE();
    /* CAN2 needs no remapping *******/

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

    HAL_GPIO_Init(CANx2_TX_GPIO_PORT, &GPIO_InitStruct);

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

    HAL_GPIO_Init(CANx2_RX_GPIO_PORT, &GPIO_InitStruct);

    /*##-3- Configure the NVIC #################################################*/
    /* NVIC configuration for CAN2 Reception complete interrupt */

    HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);

}

我应该再次提到,以下代码与 CAN1 完美配合(当然,将CAN2更改为CAN1等)。

很明显CAN控制器正在工作,因为另一块板正在传输CAN帧,接收到确认(并且没有任何传输错误或超时),这里唯一的问题是< strong>接收中断,为什么?

2 个答案:

答案 0 :(得分:3)

我有类似的问题,参考手册有什么帮助:

CAN2SB [5:0]:CAN2启动存储区

这些位由软件置位和清除。它们定义了CAN2的起始库 接口(从机),范围为0到27。

注意:当CAN2SB [5:0] = 28d时,可以使用CAN1的所有过滤器。

当CAN2SB [5:0]设置为0时,不会为CAN1分配滤波器 sFilterConfig.BankNumber = 20;

请改变   sFilterConfig.BankNumber = 20;

sFilterConfig.BankNumber = 0x2d;

答案 1 :(得分:0)

您必须通过hcan1执行所有过滤器的设置。即使对于Can2。

  

在连接线中   设备,偏移量0x200到31C的寄存器仅出现在CAN1中。

...并且偏移量0x200开始过滤器设置...