STM32F429 is not receiving the CAN Message

13,254

If CAN2 can send messages, the clock and master-slave configurations between CAN1 (master) and CAN2 (slave) are ok.

If there are receiving issues these should be related to the FIFO config or the acceptance filter config.

You configured the FIFO, but it looks like you did not configure the "CAN2StartBank". CAN1 and CAN2 share also the filter banks and by default the banks are divided in half, so the first half is used for CAN1 and the second half for CAN2.

From the reference manual chapter "CAN Filter Registers":

CAN filter master register (CAN_FMR)

Address offset: 0x200 Reset value: 0x2A1C 0E01 All bits of this

register are set and cleared by software. ... CAN2SB[5:0]: CAN2 start bank These bits are set and cleared by

software. They define the start bank for the CAN2 interface (Slave) in

the range 0 to 27. Note: When CAN2SB[5:0] = 28d, all the filters to

CAN1 can be used. When CAN2SB[5:0] is set to 0,no filters are assigned to CAN1.

You set the

CAN_Filters.BankNumber = 0;

but you should choose a bank from the second half with default settings or change the CAN2StartBank value to 0 if you don't need CAN1.

You also have to make sure, that the Filters an in initialisation mode during filter configuration.

CAN filter master register (CAN_FMR)

Bit 0 FINIT : Filter init mode Initialization mode for filter banks

0: Active filters mode.

1: Initialization mode for the filters

Sadly I have no hardware right now to test this by my self.

Share:
13,254
user2870154
Author by

user2870154

Updated on June 28, 2022

Comments

  • user2870154
    user2870154 almost 2 years

    I am using STM32F429 Microcontroller and need to implement CAN Bus Communication between CAN2 and PCAN View.I am able to transmit the message from CAN2 but I am not able to receive any message.I am using TJA1041A CAN transreceiver in the microcontroller.The Problem is that during debugging my CAN bus are properly initalized but it doesn't go to the receive command although I have initalized FIFO0.Herewith I am attching the program for further reference.I have used STM32 HAL Cube for programming.

    /**
      ******************************************************************************
      * File Name          : main.c
      * Date               : 05/12/2014 09:43:55
      * Description        : Main program body
      ******************************************************************************
      *
      * COPYRIGHT(c) 2014 STMicroelectronics
      *
      * Redistribution and use in source and binary forms, with or without modification,
      * are permitted provided that the following conditions are met:
      *   1. Redistributions of source code must retain the above copyright notice,
      *      this list of conditions and the following disclaimer.
      *   2. Redistributions in binary form must reproduce the above copyright notice,
      *      this list of conditions and the following disclaimer in the documentation
      *      and/or other materials provided with the distribution.
      *   3. Neither the name of STMicroelectronics nor the names of its contributors
      *      may be used to endorse or promote products derived from this software
      *      without specific prior written permission.
      *
      * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
      * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
      * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
      * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
      * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      *
      ******************************************************************************
      */
    
    /* Includes ------------------------------------------------------------------*/
    #include "stm32f4xx_hal.h"
    
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    /* Private variables ---------------------------------------------------------*/
    CAN_HandleTypeDef hcan1;
    CAN_HandleTypeDef hcan2;
    
    /* USER CODE BEGIN PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_CAN1_Init(void);
    static void MX_CAN2_Init(void);
    
    /* USER CODE BEGIN PFP */
    
    /* USER CODE END PFP */
    
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    int main(void)
    {
    
      /* USER CODE BEGIN 1 */
    
      /* USER CODE END 1 */
    
      /* MCU Configuration----------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_CAN1_Init();
      MX_CAN2_Init();
    
      /* USER CODE BEGIN 2 */
     __GPIOD_CLK_ENABLE();
    
     GPIO_InitTypeDef  GPIO_Initpins;
     
     GPIO_Initpins.Mode = GPIO_MODE_OUTPUT_PP ;
     GPIO_Initpins.Pin = GPIO_PIN_5|GPIO_PIN_7;
     GPIO_Initpins.Pull = GPIO_NOPULL  ;
     HAL_GPIO_Init(GPIOD, &GPIO_Initpins);
     
     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5|GPIO_PIN_7, GPIO_PIN_SET);
      /* USER CODE END 2 */
    
      /* USER CODE BEGIN 3 */
      /* Infinite loop */
      CanTxMsgTypeDef TxMess;
      
      TxMess.StdId = 0x123;
      TxMess.DLC = 0x0;
      TxMess.Data[0] = 0x12;
      TxMess.IDE = 0;
      TxMess.RTR = 0;
     
        hcan2.pTxMsg = &TxMess;
        HAL_CAN_Transmit(&hcan2,50);
        HAL_Delay(500);
      
     /* USER CODE END 2 */
    
      
    
     CanRxMsgTypeDef RMess;
     
      RMess.FIFONumber = CAN_FIFO1;
      RMess.FMI = 14;
      RMess.StdId = 0x541;
      RMess.DLC = 0;
      RMess.RTR = 0;
      RMess.IDE = CAN_ID_STD;
      hcan2.pRxMsg = &RMess;
      
      HAL_CAN_Receive_IT(&hcan2,CAN_FIFO1);
      /* USER CODE BEGIN 3 */
      /* Infinite loop */
      while (1)
      {
      
       HAL_CAN_Receive(&hcan2,CAN_FIFO1,0);
       
      }
      /* USER CODE END 3 */
    
    }
    
    /** System Clock Configuration
    */
    void SystemClock_Config(void)
    {
    
      RCC_OscInitTypeDef RCC_OscInitStruct;
      RCC_ClkInitTypeDef RCC_ClkInitStruct;
    
      __PWR_CLK_ENABLE();
    
      __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
    
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
      RCC_OscInitStruct.HSEState = RCC_HSE_ON;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
      HAL_RCC_OscConfig(&RCC_OscInitStruct);
    
      RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
      RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
      RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
      RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
      RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
      HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
    
    }
    
    /* CAN1 init function */
    void MX_CAN1_Init(void)
    {
    
      hcan1.Instance = CAN1;
      hcan1.Init.Prescaler = 16;
      hcan1.Init.Mode = CAN_MODE_NORMAL;
      hcan1.Init.SJW = CAN_SJW_1TQ;
      hcan1.Init.BS1 = CAN_BS1_1TQ;
      hcan1.Init.BS2 = CAN_BS2_1TQ;
      hcan1.Init.TTCM = DISABLE;
      hcan1.Init.ABOM = DISABLE;
      hcan1.Init.AWUM = DISABLE;
      hcan1.Init.NART = DISABLE;
      hcan1.Init.RFLM = DISABLE;
      hcan1.Init.TXFP = DISABLE;
      HAL_CAN_Init(&hcan1);
    
      /*CAN_FilterConfTypeDef CAN_Filters;
      
         CAN_Filters.BankNumber = 0;
         CAN_Filters.FilterActivation = ENABLE;
         CAN_Filters.FilterFIFOAssignment = CAN_FILTER_FIFO0 ;
         CAN_Filters.FilterIdHigh = 0x00;
         CAN_Filters.FilterIdLow = 0x00;
         CAN_Filters.FilterMaskIdHigh = 0x00;
         CAN_Filters.FilterMaskIdLow = 0x00;
         CAN_Filters.FilterMode = CAN_FILTERMODE_IDMASK;
         CAN_Filters.FilterNumber = 0;
         CAN_Filters.FilterScale = CAN_FILTERSCALE_32BIT;
       
         HAL_CAN_ConfigFilter(&hcan1, &CAN_Filters);*/ 
    
    }
    
    /* CAN2 init function */
    void MX_CAN2_Init(void)
    {
      hcan2.Instance = CAN2;
      hcan2.Init.Prescaler = 2;
      hcan2.Init.Mode = CAN_MODE_NORMAL;
      hcan2.Init.SJW = CAN_SJW_1TQ;
      hcan2.Init.BS1 = CAN_BS1_5TQ;
      hcan2.Init.BS2 = CAN_BS2_2TQ;
      hcan2.Init.TTCM = DISABLE;
      hcan2.Init.ABOM = DISABLE;
      hcan2.Init.AWUM = DISABLE;
      hcan2.Init.NART = DISABLE;
      hcan2.Init.RFLM = DISABLE;
      hcan2.Init.TXFP = DISABLE;
      HAL_CAN_Init(&hcan2);
      
     }
    
    /** Configure pins as 
            * Analog 
            * Input 
            * Output
            * EVENT_OUT
            * EXTI
    */
    void MX_GPIO_Init(void)
    {
    
      /* GPIO Ports Clock Enable */
      __GPIOH_CLK_ENABLE();
      __GPIOB_CLK_ENABLE();
      __GPIOA_CLK_ENABLE();
    
    }
    
    /* USER CODE BEGIN 4 */
    
    /* USER CODE END 4 */
    
    #ifdef USE_FULL_ASSERT
    
    /**
       * @brief Reports the name of the source file and the source line number
       * where the assert_param error has occurred.
       * @param file: pointer to the source file name
       * @param line: assert_param error line source number
       * @retval None
       */
    void assert_failed(uint8_t* file, uint32_t line)
    {
      /* USER CODE BEGIN 6 */
      /* User can add his own implementation to report the file name and line number,
        ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
      /* USER CODE END 6 */
    
    }
    
    #endif
    
    /**
      * @}
      */ 
    
    /**
      * @}
    */ 
    
    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

    Thanks