AnsweredAssumed Answered

STM32F429 is not receiving the CAN Message

Question asked by karbasi.ghasem on Nov 3, 2016
Latest reply on Nov 8, 2016 by ST Renegade
I am using STM32F429 Microcontroller and need to implement CAN Bus Communication between CAN1 and Car ECU. I am not able to receive any message in Silent Mode Or Other Mode.I am using sn65hvd230 (i am use other ic such as PCA82c250,mc2551 But they did not result) CAN transreceiver in the microcontroller.The Problem isnt 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               : 02/05/2016 14:58:40
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2016 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;


/* USER CODE BEGIN PV */


/* USER CODE END PV */
volatile uint32_t datarx[10];
uint8_t TransmitMailbox = 0;
uint32_t uwCounter = 0;


/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN1_Init(void);


/* USER CODE BEGIN PFP */


/* USER CODE END PFP */


/* USER CODE BEGIN 0 */


/* USER CODE END 0 */


static CanTxMsgTypeDef TxMessage;
    static CanRxMsgTypeDef RxMessage;
    
void CAN_filter_init(void)
{
     CAN_FilterConfTypeDef  sFilterConfig;




    /*##-1- Configure the CAN peripheral #######################################*/
    hcan1.pTxMsg = &TxMessage;
    hcan1.pRxMsg = &RxMessage;
       /*##-2- Configure the CAN Filter ###########################################*/
    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 = CAN_FILTER_FIFO0;
    sFilterConfig.FilterActivation = ENABLE;
    sFilterConfig.BankNumber =0;
        if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK) {
        /* Filter configuration Error */
       
    }


    if (HAL_CAN_WakeUp(&hcan1) != HAL_OK) {
        
    }
}




/*CAN1 Receving function */
void CAN_Rx(void)
{
     if (__HAL_CAN_MSG_PENDING(&hcan1, CAN_FILTER_FIFO0)) 
          {
            HAL_CAN_Receive(&hcan1,CAN_FILTER_FIFO0, 3000);
                 datarx[0]=hcan1.pRxMsg->StdId ;                    
                datarx[1] =hcan1.pRxMsg->DLC;
                 datarx[2]= hcan1.pRxMsg->RTR;
                datarx[3]=hcan1.pRxMsg->Data[0] ;
                datarx[4]=hcan1.pRxMsg->Data[1] ;
                datarx[5]=hcan1.pRxMsg->Data[2] ;
                datarx[6]= hcan1.pRxMsg->Data[3];
                 datarx[7]= hcan1.pRxMsg->Data[4] ;
                 datarx[8]= hcan1.pRxMsg->Data[5];
            }


}




void NISO15765_TransmitExtendedStart()
{
   
    hcan1.pTxMsg->StdId = 0x741;
    hcan1.pTxMsg->RTR = CAN_RTR_DATA;
    hcan1.pTxMsg->IDE = CAN_ID_STD;
    hcan1.pTxMsg->DLC = 8;
    hcan1.pTxMsg->Data[0] = 0x02;
    hcan1.pTxMsg->Data[1] = 0x10;
    hcan1.pTxMsg->Data[2] = 0x90;
    hcan1.pTxMsg->Data[3] = 0xFF;
    hcan1.pTxMsg->Data[4] = 0xFF;
    hcan1.pTxMsg->Data[5] = 0xFF;
    hcan1.pTxMsg->Data[6] = 0xFF;
    hcan1.pTxMsg->Data[7] = 0xFF;
    HAL_CAN_Transmit(&hcan1, 3000);


}




int main(void)
{  
  /* 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();
    CAN_filter_init();
  while (1)
  { 
    NISO15765_TransmitExtendedStart();
    HAL_Delay(100); 
    CAN_Rx();


  }
  /* USER CODE END 3 */


}


/** System Clock Configuration
*/
void SystemClock_Config(void)
{


  RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;


  __PWR_CLK_ENABLE();


  
   __HAL_RCC_CAN1_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  


  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 10;
  RCC_OscInitStruct.PLL.PLLN = 210;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);


  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
 


}


/* CAN1 init function */
void MX_CAN1_Init(void)
{


  hcan1.Instance = CAN1;


  hcan1.Init.Prescaler = 7;//56;//Prescaler (for Time Quantum)
  hcan1.Init.Mode = CAN_MODE_SILENT;//Operating Mode
  hcan1.Init.SJW = CAN_SJW_1TQ;//ReSynchronization Jump Width
  hcan1.Init.BS1 = CAN_BS1_7TQ;//Time Quanta in Bit Segment 1
  hcan1.Init.BS2 = CAN_BS2_4TQ;//Time Quanta in Bit Segment 2
  hcan1.Init.TTCM = DISABLE;//Time Triggered Communication Mode
  hcan1.Init.ABOM = DISABLE;//Automatic Bus-Off Management
  hcan1.Init.AWUM = DISABLE;//Automatic Wake-Up Mode
  hcan1.Init.NART = DISABLE;//No-Automatic Retransmission
  hcan1.Init.RFLM = DISABLE;//Receive Fifo Locked Mode
  hcan1.Init.TXFP = ENABLE;//Transmit Fifo Priority
  HAL_CAN_Init(&hcan1);
  


}


/** Pinout Configuration
*/
void MX_GPIO_Init(void)
{


  /* GPIO Ports Clock Enable */
  __GPIOA_CLK_ENABLE();
  __GPIOB_CLK_ENABLE();
  __GPIOC_CLK_ENABLE();
  __GPIOD_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****/



void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{


  GPIO_InitTypeDef GPIO_InitStruct;
  if(hcan->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspInit 0 */


  /* USER CODE END CAN1_MspInit 0 */
    /* Peripheral clock enable */
    __CAN1_CLK_ENABLE();
  
    /**CAN1 GPIO Configuration    
    PA11     ------> CAN1_RX
    PA12     ------> CAN1_TX 
    */
     GPIO_InitStruct.Pin =  GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
     
    GPIO_InitStruct.Pin =  GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;//GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


  /* System interrupt init*/
    HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0xFF, 0);
    HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);
    HAL_NVIC_SetPriority(CAN1_TX_IRQn, 0xFF, 0);
    HAL_NVIC_EnableIRQ(CAN1_TX_IRQn);
  /* USER CODE BEGIN CAN1_MspInit 1 */


  /* USER CODE END CAN1_MspInit 1 */
  }


}

and i sure prescaler and ts1 and ts2 is currect.
thank you very much.

Outcomes