cancel
Showing results for 
Search instead for 
Did you mean: 

CAN bus is not receiving any message

rm239955
Associate II
Posted on December 17, 2014 at 11:52

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 : 11/12/2014 15:46:18

* 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);

static void CAN2_RX0_IRQHandler(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 */


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 ;

GPIO_Initpins.Speed = GPIO_SPEED_LOW;

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.ExtId = 0x13375000;

TxMess.DLC = 0x1;

TxMess.Data[0] = 0xAA;

TxMess.IDE = CAN_ID_STD ;

TxMess.RTR = CAN_RTR_DATA;

hcan2.pTxMsg = &TxMess;


//HAL_CAN_Transmit(&hcan2,50);


CanRxMsgTypeDef rmess;


rmess.FIFONumber = CAN_FIFO0;

rmess.FMI = 14;

rmess.IDE = CAN_ID_STD;

hcan2.pRxMsg = &rmess;



HAL_CAN_Receive_IT(&hcan2,CAN_FIFO0);




while(1)

{

HAL_CAN_Receive(&hcan2,CAN_FIFO0,0);

HAL_Delay(50);

}

/* 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 = 2;

hcan1.Init.Mode = CAN_MODE_NORMAL;

hcan1.Init.SJW = CAN_SJW_1TQ;

hcan1.Init.BS1 = CAN_BS1_5TQ;

hcan1.Init.BS2 = CAN_BS2_2TQ;

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);




}


/* 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);



CAN_FilterConfTypeDef sFilterConfig;

sFilterConfig.FilterActivation = ENABLE;

sFilterConfig.FilterNumber = 14; 

sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; 

sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT; 

sFilterConfig.FilterIdHigh = 0x00; 

sFilterConfig.FilterIdLow = 0x00; 

sFilterConfig.FilterMaskIdHigh = 0x00; 

sFilterConfig.FilterMaskIdLow = 0x00; 

sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; 

HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig);


}


/** 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();

__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****/

#filter #bxcan #can #stm32 #code #stm32f2 #example
1 REPLY 1
mario239955_st
Associate II
Posted on April 01, 2015 at 17:26

Hi,

I can use the board STM32f429i-Discovery and I would like to communicate the CAN1 and CAN.

The bus CAN1 transmits but the bus CAN2 does not receive anything.

The code is here:

/* CAN_Tx init function */

void CAN1_Init(CAN_HandleTypeDef *canTxHandlePtr){

/*##-1- Configure the CAN1 peripheral #######################################*/

/* Can1 configured as follow:

     -

*/

canTxHandlePtr->Instance = CAN1;

canTxHandlePtr->Init.Prescaler = 16;

canTxHandlePtr->Init.Mode = CAN_MODE_LOOPBACK;

canTxHandlePtr->Init.SJW = CAN_SJW_1TQ;

canTxHandlePtr->Init.BS1 = CAN_BS1_8TQ;

canTxHandlePtr->Init.BS2 = CAN_BS2_1TQ;

canTxHandlePtr->Init.TTCM = DISABLE;

canTxHandlePtr->Init.ABOM = ENABLE;

canTxHandlePtr->Init.AWUM = ENABLE;

canTxHandlePtr->Init.NART = DISABLE;

canTxHandlePtr->Init.RFLM = DISABLE;

canTxHandlePtr->Init.TXFP = ENABLE;

if(HAL_CAN_Init(canTxHandlePtr) != HAL_OK)

{

BSP_LED_Toggle(LED4);

/* Transmition Error */

Error_Handler();

}else{

BSP_LED_Toggle(LED3);

}

CAN_FilterConfTypeDef  sFilterConfig;

/*##-2- Configure the CAN Filter ###########################################*/

sFilterConfig.FilterNumber = 14;

sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;

sFilterConfig.FilterIdHigh = 0x0000;

sFilterConfig.FilterIdLow = 0x0000;

sFilterConfig.FilterMaskIdHigh = 0x0000;

sFilterConfig.FilterMaskIdLow = 0x0000;

sFilterConfig.FilterFIFOAssignment = CAN_FIFO0;

sFilterConfig.FilterActivation = ENABLE;

sFilterConfig.BankNumber = 14;

if(HAL_CAN_ConfigFilter(canTxHandlePtr, &sFilterConfig) != HAL_OK)

{

BSP_LED_Toggle(LED4);

/* Transmition Error */

Error_Handler();

}else{

BSP_LED_Toggle(LED3);

}

}

/* CAN_Rx init function */

void CAN2_Init(CAN_HandleTypeDef *canRxHandlePtr){

HAL_CAN_DeInit(canRxHandlePtr);

canRxHandlePtr->Instance = CANRx;

canRxHandlePtr->Init.Prescaler = 16;

canRxHandlePtr->Init.Mode = CAN_MODE_LOOPBACK;

canRxHandlePtr->Init.SJW = CAN_SJW_1TQ;

canRxHandlePtr->Init.BS1 = CAN_BS1_8TQ;

canRxHandlePtr->Init.BS2 = CAN_BS2_1TQ;

canRxHandlePtr->Init.TTCM = DISABLE;

canRxHandlePtr->Init.ABOM = ENABLE;

canRxHandlePtr->Init.AWUM = ENABLE;

canRxHandlePtr->Init.NART = DISABLE;

canRxHandlePtr->Init.RFLM = DISABLE;

canRxHandlePtr->Init.TXFP = ENABLE;

if(HAL_CAN_Init(canRxHandlePtr) != HAL_OK)

{

BSP_LED_Toggle(LED4);

/* Transmition Error */

Error_Handler();

}else{

BSP_LED_Toggle(LED3);

}

}

/** Pinout Configuration

 */

void GPIO_Init(){

/* GPIO Ports Clock Enable */

CANRx_GPIO_CLK_ENABLE();

CANTx_GPIO_CLK_ENABLE();

}

int main(void)

{

HAL_Init();

BSP_LED_Init(LED3);

BSP_LED_Init(LED4);

BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);

SystemClock_Config();

/* Initialize all configured peripherals */

GPIO_Init();

CAN1_Init(&canTxHandle);

CAN2_Init(&canRxHandle);

/* Infinite loop */

BSP_LED_Off(LED3);

BSP_LED_Off(LED4);

/*##-3- Start the Transmission process #####################################*/

canTxHandle.pTxMsg = &TxMessage;

canTxHandle.pTxMsg->ExtId = 0x04214006;

canTxHandle.pTxMsg->RTR = CAN_RTR_DATA;

canTxHandle.pTxMsg->IDE = CAN_ID_EXT;

canTxHandle.pTxMsg->DLC = 0x8;

canTxHandle.pTxMsg->Data[0] = 0xaa; /* 100 km/h for the bits corresponding to ''speed'' */

canTxHandle.pTxMsg->Data[1] = 0xaa;

for(;;){

while (HAL_CAN_GetState(&canTxHandle) != HAL_CAN_STATE_READY) {

BSP_LED_On(LED4);

//osDelay(500);

HAL_Delay(500);

BSP_LED_Off(LED4);

}

if (HAL_CAN_GetState(&canTxHandle) == HAL_CAN_STATE_READY) {

if(HAL_CAN_Transmit(&canTxHandle,50) == HAL_OK){

BSP_LED_On(LED3);

//osDelay(500);

HAL_Delay(500);

BSP_LED_Off(LED3);

}

}

RxMessage.FIFONumber = CAN_FIFO1;

RxMessage.FMI = 14;

RxMessage.ExtId = 0x04214006;

RxMessage.DLC = 8;

RxMessage.RTR = CAN_RTR_DATA;

RxMessage.IDE = CAN_ID_EXT;

canRxHandle.pRxMsg = &RxMessage;

if(HAL_CAN_Receive(&canRxHandle,CAN_FIFO1,100) == HAL_OK)

{

BSP_LED_Toggle(LED4);

HAL_Delay(500);

BSP_LED_Toggle(LED4);

/* Transmition Error */

Error_Handler();

}

}

/* We should never get here as control is now taken by the scheduler */

while (1)

{}

}

What is the problem!?!? Can you help me!!! :(