CAN Initialization Error in HAL using STM32F072CBT6

-1

I'm trying to use CAN in Interupt Mode on STM32F072CBT6.

Here my Code:

HAL_CAN_ConfigFilter(&hcan, &FilterConfig);
HAL_CAN_Start(&hcan);
HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING);

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {

    memset(Rx_Data, 0, 8);
if (HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &Rx_Header, Rx_Data)
        == HAL_OK) {


        CAN_Received_Data = 1;

    }


}

The Interupt and the Callback are called if the MCU receives a message via CAN but when I debug HAL_CAN_GetRxMessage it allways returns HAL_CAN_ERROR_NOT_INITIALIZED

The Interrupt calls then into a loop and Program doesnt nothing else.

If i use this Code (and with disabled Interupt) in polling Mode in Main loop it works and I get my data

if (HAL_CAN_GetRxMessage(&hcan, CAN_RX_FIFO0, &Rx_Header, Rx_Data)
    == HAL_OK) {


    ....

}

This exact copy of code works fine for me on STM32F107.

Here is my Init for CAN:

  static void MX_CAN_Init(void)
{

  /* USER CODE BEGIN CAN_Init 0 */

  /* USER CODE END CAN_Init 0 */

  /* USER CODE BEGIN CAN_Init 1 */

  /* USER CODE END CAN_Init 1 */
  hcan.Instance = CAN;
  hcan.Init.Prescaler = 8;
  hcan.Init.Mode = CAN_MODE_NORMAL;
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_9TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = DISABLE;
  hcan.Init.AutoWakeUp = ENABLE;
  hcan.Init.AutoRetransmission = ENABLE;
  hcan.Init.ReceiveFifoLocked = DISABLE;
  hcan.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN_Init 2 */

  /* USER CODE END CAN_Init 2 */

}

Here is my FilterConfig:

FilterConfig.FilterBank = 0;
FilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0;
FilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
FilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
FilterConfig.FilterActivation = CAN_FILTER_ENABLE;

    //Filter from700-7FF
    filter_ID = 0x00000700;
    filter_MASK = 0x1FFFF700;
    FilterConfig.FilterIdHigh = ((filter_ID << 5) | (filter_ID >> (32 - 5)))
            & 0xFFFF; // STID[10:0] & EXTID[17:13]
    FilterConfig.FilterIdLow = (filter_ID >> (11 - 3)) & 0xFFF8;
    FilterConfig.FilterMaskIdHigh = ((filter_MASK << 5)
            | (filter_MASK >> (32 - 5))) & 0xFFFF;
    FilterConfig.FilterMaskIdLow = (filter_MASK >> (11 - 3)) & 0xFFF8;

Send Messages works well.

EDIT / SOLUTION:

Wrong Pointer.

    void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {

    memset(Rx_Data, 0, 8);
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &Rx_Header, Rx_Data)
        == HAL_OK) {


        CAN_Received_Data = 1;

    }


}
c
embedded
stm32
can-bus
hal
asked on Stack Overflow Sep 29, 2020 by vt1111 • edited Sep 29, 2020 by vt1111

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0