cancel
Showing results for 
Search instead for 
Did you mean: 

STM32MP157C-DK2 - FDCAN not transmitting.

AComa.170
Associate II

Hello,

I'm using a ST32MP157C-DK2 board to develop an application running on CM4 that basically handles a CAN Standard communication.

I routed the FDCAN1 signals on PA11 and PA12 and I am trying to send out some random data with no result...

Here is my code:

static void MX_FDCAN1_Init(void)
{
 
  /* USER CODE BEGIN FDCAN1_Init 0 */
 
  /* USER CODE END FDCAN1_Init 0 */
 
  /* USER CODE BEGIN FDCAN1_Init 1 */
 
  /* USER CODE END FDCAN1_Init 1 */
  hfdcan1.Instance = FDCAN1;
  hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
  hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
  hfdcan1.Init.AutoRetransmission = DISABLE;
  hfdcan1.Init.TransmitPause = DISABLE;
  hfdcan1.Init.ProtocolException = DISABLE;
  hfdcan1.Init.NominalPrescaler = 1;
  hfdcan1.Init.NominalSyncJumpWidth = 1;
  hfdcan1.Init.NominalTimeSeg1 = 2;
  hfdcan1.Init.NominalTimeSeg2 = 2;
  hfdcan1.Init.DataPrescaler = 1;
  hfdcan1.Init.DataSyncJumpWidth = 1;
  hfdcan1.Init.DataTimeSeg1 = 1;
  hfdcan1.Init.DataTimeSeg2 = 1;
  hfdcan1.Init.MessageRAMOffset = 0;
  hfdcan1.Init.StdFiltersNbr = 0;
  hfdcan1.Init.ExtFiltersNbr = 0;
  hfdcan1.Init.RxFifo0ElmtsNbr = 1;
  hfdcan1.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
  hfdcan1.Init.RxFifo1ElmtsNbr = 0;
  hfdcan1.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;
  hfdcan1.Init.RxBuffersNbr = 0;
  hfdcan1.Init.RxBufferSize = FDCAN_DATA_BYTES_8;
  hfdcan1.Init.TxEventsNbr = 1;
  hfdcan1.Init.TxBuffersNbr = 8;
  hfdcan1.Init.TxFifoQueueElmtsNbr = 8;
  hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
  hfdcan1.Init.TxElmtSize = FDCAN_DATA_BYTES_8;
  if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN FDCAN1_Init 2 */
 
  /* USER CODE END FDCAN1_Init 2 */
}
 
int main(void)
{
  /* USER CODE BEGIN 1 */
   uint32_t Tickstart;
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  if(IS_ENGINEERING_BOOT_MODE())
  {
    /* Configure the system clock */
    SystemClock_Config();
  }
  
  if(IS_ENGINEERING_BOOT_MODE())
  {
    /* Configure the peripherals common clocks */
    PeriphCommonClock_Config();
  }
 
  /* IPCC initialisation */
   MX_IPCC_Init();
  /* OpenAmp initialisation ---------------------------------*/
  MX_OPENAMP_Init(RPMSG_REMOTE, NULL);
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_FDCAN1_Init();
  /* USER CODE BEGIN 2 */
 
   /*##-2 Start FDCAN controller (continuous listening CAN bus) ##############*/
   if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
   {
     Error_Handler();
   }
 
   /*##-3 Transmit messages ##################################################*/
   /* Add message to Tx FIFO */
   TxHeader.Identifier = 0x444;
   TxHeader.IdType = FDCAN_STANDARD_ID;
   TxHeader.TxFrameType = FDCAN_DATA_FRAME;
   TxHeader.DataLength = FDCAN_DLC_BYTES_8;
   TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
   TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
   TxHeader.FDFormat = FDCAN_CLASSIC_CAN;
   TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
   TxHeader.MessageMarker = 0;
   if (HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData0) != HAL_OK)
   {
     Error_Handler();
   }
 
 
   /* Get tick */
   Tickstart = HAL_GetTick();
 
   /* Check transmission occurred before timeout */
   while(HAL_FDCAN_IsTxBufferMessagePending(&hfdcan1, FDCAN_TX_BUFFER0 ) != 0)
   {
     if((HAL_GetTick() - Tickstart) > TX_FAST_TIMEOUT)
     {
       Error_Handler();
       break;
     }
   }
 
   /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
   while (1)
   {
   
       HAL_Delay(100);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
   }
  /* USER CODE END 3 */
}
void HAL_FDCAN_MspInit(FDCAN_HandleTypeDef* hfdcan)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(hfdcan->Instance==FDCAN1)
  {
  /* USER CODE BEGIN FDCAN1_MspInit 0 */
 
  /* USER CODE END FDCAN1_MspInit 0 */
  if(IS_ENGINEERING_BOOT_MODE())
  {
  /** Initializes the peripherals clock 
  */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
    PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_HSE;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }
 
  }
 
    /* Peripheral clock enable */
    __HAL_RCC_FDCAN_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
 
    /**FDCAN1 GPIO Configuration    
    PA11     ------> FDCAN1_RX
    PA12     ------> FDCAN1_TX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  /* USER CODE BEGIN FDCAN1_MspInit 1 */
 
  /* USER CODE END FDCAN1_MspInit 1 */
  }
 
}

Any idea? What am I doing wrong?

Thank you!

Andrea

7 REPLIES 7
Olivier GALLIEN
ST Employee

Hi @AComa.170​ 

Are you in Engineering boot mode or Production mode.

If second case did you properly manage assignement of FDCAN in Device Tree ?

Also I notice a long thread about FDCAN here in case it contains hint for you :

https://community.st.com/s/question/0D50X0000C9fliSSQQ/stm32mp1-fdcan-issues

Hope it help

Olivier

Olivier GALLIEN
In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Hi @Community member​,

I'm in production mode... Am I supposed to assign FDCAN in Device Tree even if I'm using it on Cortex-M4? If yes, is there a guide about this?

Thank you so much!

Andrea

Hi @AComa.170​ 

Yes, you must have some Device Tree related declaration, as shared resources (e.g. clocks) are handled by Linux.

Please have a look to https://wiki.st.com/stm32mpu/wiki/How_to_assign_an_internal_peripheral_to_a_runtime_context

and https://wiki.st.com/stm32mpu/wiki/How_to_configure_system_resources#System_resource_management_on_the_Cortex-M4_context

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Hi @PatrickF​ ,

thank you for the informations, now thigs are getting clearer.

I switched to Engineering Mode, but still no data on FDCAN output port...

Andrea

Please check first SystemClock_Config() and PeriphCommonClock_Config().

I see you are using HSE for FDCAN, so check if HSE is started. Similarly, check correct PLL and all clocks related settings.

is the FDCAN correctly initialized in MX_FDCAN1_Init() ?

Then, you need to debug step by step to see what is blocking. I'm not specialist of FDCAN, so I cannot check if your functions usage are ok.

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
AComa.170
Associate II

Hello everyone,

I checked what @PatrickF​ has suggested, everyting seems to be ok.

I found out that the only way to see something on the TX pin is setting the peripheral on EXTERNAL_LOOPBACK_MODE...

Obviously, in this way I'm not able to receive anything from external devices...

Is there something in particular that may cause this condition?

Najib Kassab
Associate III

0693W00000Y9jXsQAJ.jpg@AComa.170​ 

@PatrickF​ 

Hi guys, I have the same issue here, I'm trying to use fdcan in m4, and i upload an new device tree which is generated by stm32cudeide, However nothing to seem to work, even though it's working in engineering mode.

what do you think is the problem?

I notice the clock is not enabled when I'm looking to clock summary