cancel
Showing results for 
Search instead for 
Did you mean: 

CAN-Bus no callback/received frame; FDCAN STM32G474

Hello,

when I try to receive a CAN-Frame it is not received by the Microcontroller, it is within the range, I have no idea what the issue is.

Thanks to everybody in advance, Best Regards, Seppel

0693W00000bhUdqQAE.png0693W00000bhUe5QAE.png 

0693W00000bhUeFQAU.png 

void FDCAN_Config(FDCAN_HandleTypeDef *pHfdcan)
{
 
    pHandleToFdCan = pHfdcan;
 
  /* Configure Rx filter */
  sFilterConfig.IdType = FDCAN_STANDARD_ID;
  sFilterConfig.FilterIndex = 0;
  sFilterConfig.FilterType = FDCAN_FILTER_RANGE;
  sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
  sFilterConfig.FilterID1 = 0x700;
  sFilterConfig.FilterID2 = 0x7FF;
  if (HAL_FDCAN_ConfigFilter(pHandleToFdCan, &sFilterConfig) != HAL_OK)
  {
    Error_Handler();
  }
 
  /* Configure global filter:
     Filter all remote frames with STD and EXT ID
     Reject non matching frames with STD ID and EXT ID */
  if (HAL_FDCAN_ConfigGlobalFilter(pHandleToFdCan, FDCAN_REJECT, FDCAN_REJECT, FDCAN_REJECT, FDCAN_REJECT) != HAL_OK)
  {
    Error_Handler();
  }
 
 
 
  /* Start the FDCAN module */
  if (HAL_FDCAN_Start(pHandleToFdCan) != HAL_OK)
  {
    Error_Handler();
  }
 
  if (HAL_FDCAN_ActivateNotification(pHandleToFdCan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
  {
    Error_Handler();
  }
 
}

0693W00000bhUf3QAE.png 

P.S. TX works perfectly fine, as you can see it Ack's the 0x701 Frame that was received.

22 REPLIES 22
AndrewST
Associate II

Can you show the "when I try to receive a CAN-Frame" part of your code? What does your callback function look like?

Hello,

as far as I'm aware the Function "HAL_FDCAN_RxFifo0Callback" needs to be implemented, which I did, I set a breakpoint, but it is never involked. "HAL_FDCAN_IRQHandler" is also never invoked.

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *phfdcan, uint32_t RxFifo0ITs)
{
  if((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET)
  {
    /* Retrieve Rx messages from RX FIFO0 */
    if (HAL_FDCAN_GetRxMessage(phfdcan, FDCAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
    {
    Error_Handler();
    }
    else
    {
      // HAL_Ok
      if ((RxHeader.DataLength == CanMsgLen_Pressure)&&(RxHeader.Identifier == CanMsgId_Control))
      {
            canSendData = RxData[0U];
      }
 
    }
 
  }
}

Best Regards, Seppel

AndrewST
Associate II

That looks about right.

How about your receive buffers configuration?

0693W00000bhVJhQAM.png

Hello,

there is no "Receive Buffer Configuration" in the IDE. How did you find it for the STM32G474?

I think there are 3 RX Buffers for the controller if I remember right, but I have no idea how to configure these and I have not found examples wher it is done.

Best Regards, Seppel

P.S. Nothing about RX-Buffers in the generated fdcan.c file.

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    fdcan.c
  * @brief   This file provides code for the configuration
  *          of the FDCAN instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "fdcan.h"
 
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
FDCAN_HandleTypeDef hfdcan2;
 
/* FDCAN2 init function */
void MX_FDCAN2_Init(void)
{
 
  /* USER CODE BEGIN FDCAN2_Init 0 */
 
  /* USER CODE END FDCAN2_Init 0 */
 
  /* USER CODE BEGIN FDCAN2_Init 1 */
 
  /* USER CODE END FDCAN2_Init 1 */
  hfdcan2.Instance = FDCAN2;
  hfdcan2.Init.ClockDivider = FDCAN_CLOCK_DIV1;
  hfdcan2.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
  hfdcan2.Init.Mode = FDCAN_MODE_NORMAL;
  hfdcan2.Init.AutoRetransmission = DISABLE;
  hfdcan2.Init.TransmitPause = DISABLE;
  hfdcan2.Init.ProtocolException = DISABLE;
  hfdcan2.Init.NominalPrescaler = 4;
  hfdcan2.Init.NominalSyncJumpWidth = 1;
  hfdcan2.Init.NominalTimeSeg1 = 59;
  hfdcan2.Init.NominalTimeSeg2 = 20;
  hfdcan2.Init.DataPrescaler = 8;
  hfdcan2.Init.DataSyncJumpWidth = 1;
  hfdcan2.Init.DataTimeSeg1 = 29;
  hfdcan2.Init.DataTimeSeg2 = 10;
  hfdcan2.Init.StdFiltersNbr = 0;
  hfdcan2.Init.ExtFiltersNbr = 0;
  hfdcan2.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
  if (HAL_FDCAN_Init(&hfdcan2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN FDCAN2_Init 2 */
 
  /* USER CODE END FDCAN2_Init 2 */
 
}
 
void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* fdcanHandle)
{
 
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(fdcanHandle->Instance==FDCAN2)
  {
  /* USER CODE BEGIN FDCAN2_MspInit 0 */
 
  /* USER CODE END FDCAN2_MspInit 0 */
 
  /** Initializes the peripherals clocks
  */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
    PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_PCLK1;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }
 
    /* FDCAN2 clock enable */
    __HAL_RCC_FDCAN_CLK_ENABLE();
 
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**FDCAN2 GPIO Configuration
    PB5     ------> FDCAN2_RX
    PB6     ------> FDCAN2_TX
    */
    GPIO_InitStruct.Pin = RXD_CanTransceiver_Pin|TXD_CanTransceiver_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    __HAL_SYSCFG_FASTMODEPLUS_ENABLE(SYSCFG_FASTMODEPLUS_PB6);
 
    /* FDCAN2 interrupt Init */
    HAL_NVIC_SetPriority(FDCAN2_IT0_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(FDCAN2_IT0_IRQn);
    HAL_NVIC_SetPriority(FDCAN2_IT1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(FDCAN2_IT1_IRQn);
  /* USER CODE BEGIN FDCAN2_MspInit 1 */
 
  /* USER CODE END FDCAN2_MspInit 1 */
  }
}
 
void HAL_FDCAN_MspDeInit(FDCAN_HandleTypeDef* fdcanHandle)
{
 
  if(fdcanHandle->Instance==FDCAN2)
  {
  /* USER CODE BEGIN FDCAN2_MspDeInit 0 */
 
  /* USER CODE END FDCAN2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_FDCAN_CLK_DISABLE();
 
    /**FDCAN2 GPIO Configuration
    PB5     ------> FDCAN2_RX
    PB6     ------> FDCAN2_TX
    */
    HAL_GPIO_DeInit(GPIOB, RXD_CanTransceiver_Pin|TXD_CanTransceiver_Pin);
 
    /* FDCAN2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(FDCAN2_IT0_IRQn);
    HAL_NVIC_DisableIRQ(FDCAN2_IT1_IRQn);
  /* USER CODE BEGIN FDCAN2_MspDeInit 1 */
 
  /* USER CODE END FDCAN2_MspDeInit 1 */
  }
}
 
/* USER CODE BEGIN 1 */
 
/* USER CODE END 1 */

There is a presentation, but no examples and explaination on how that relates to the HAL-Functions/API : STM32G4-Peripheral-Flexible_Datarate_Controller_Area_Network_FDCAN.pdf

Interesting,...

0693W00000bhVOmQAM.png0693W00000bhVPpQAM.png

If I remember correctly, this is only if you want to use callback(s) other than the default one already provided. You can leave this to 0, it will not prevent the callback function to be called when an RX IT is triggered.

The screenshot I took is for an F series MPU, but as far as FDCAN is concerned, this should be very similar to the Gs.

AndrewST
Associate II

Can you copy-paste the FDCAN_InitTypeDef structure declaration that was generated?