cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to receive messages from USB CAN Analyzer to STM32F407 Discovery board.

rohitkumarkv07
Associate III

Hello,

I am trying to send the message from my USB-CAN Analyzer to my board STM32F407 discovery board, but I am unable to do that. 

 

HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rx_header, rx_data);



// Process received message

handleReceivedMessage(&rx_header, rx_data);

 

This is my function to read message.

 

 

void handleReceivedMessage(CAN_RxHeaderTypeDef *rx_header, uint8_t *rx_data)

{

// Your custom handling of received message

// Example: LED blinking based on received data

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_12); // Toggle LED

}

 

This is my handlereceivedMessage function.

 

Below is my message which I am sending from USB-CAN Analyzer.

 

Best Regards

Rohit Kumar

12 REPLIES 12

Hello,

I have tried this method but it didn't work on my STM32F407 discovery board. In my STM32F407 discovery board  I want to configure the reception using these. How can I do with these ?

HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0);

 

HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &rx_header, rx_data);

 

// Handle the received message

handleReceivedMessage(&rx_header, rx_data);

 

What is the return value of fifolevel in this ?

B.R

Rohit Kumar

rohitkumarkv07
Associate III

Hello,

I want to receive messages in stm32F407 board, from the USB CAN Analyzer in interrupt based. My FMPIE1 bit got updated and In LEC it shows stuff error, but RFOR bit still not get updated. I have attached my source code below . I have tried the github example code also but unable to do that.

 

 

#include "main.h"

#include "stm32f4xx_hal_can.h"
/* USER CODE BEGIN PM */


/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan1;

/* USER CODE BEGIN PV */

static CAN_RxHeaderTypeDef rx_header;
static uint8_t rx_data[8];

int datacheck = 0;
/* USER CODE END PV */

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



void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan1)
{
	HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO1, &rx_header, rx_data);

}
int main(void)
{

	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();

	/* Configure the system clock */
	SystemClock_Config();


	// Enable CAN RX FIFO Interrupt in NVIC
	HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0); // Set priority
	HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); // Enable interrupt
	/* USER CODE END 2 */

	/* Initialize all configured peripherals */
	MX_GPIO_Init();
	MX_CAN1_Init();




	while (1)
	{


	}
}



void SystemClock_Config(void)
{
	RCC_OscInitTypeDef RCC_OscInitStruct = {0};
	RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

	/** Configure the main internal regulator output voltage
	 */
	__HAL_RCC_PWR_CLK_ENABLE();
	__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

	/** Initializes the RCC Oscillators according to the specified parameters
	 * in the RCC_OscInitTypeDef structure.
	 */
	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 = 25;
	RCC_OscInitStruct.PLL.PLLN = 288;
	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
	RCC_OscInitStruct.PLL.PLLQ = 4;
	if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
	{
		Error_Handler();
	}

	/** Initializes the CPU, AHB and APB buses clocks
	 */
	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
			|RCC_CLOCKTYPE_PCLK1|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;

	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
	{
		Error_Handler();
	}
}


static void MX_CAN1_Init(void)
{

	/* USER CODE END CAN1_Init 1 */
	hcan1.Instance = CAN1;
	hcan1.Init.Prescaler = 3 ;
	hcan1.Init.Mode = CAN_MODE_NORMAL;
	hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
	hcan1.Init.TimeSeg1 = CAN_BS1_16TQ;
	hcan1.Init.TimeSeg2 = CAN_BS2_7TQ;
	hcan1.Init.TimeTriggeredMode = DISABLE;
	hcan1.Init.AutoBusOff = DISABLE;
	hcan1.Init.AutoWakeUp = DISABLE;
	hcan1.Init.AutoRetransmission = ENABLE;
	hcan1.Init.ReceiveFifoLocked = DISABLE;
	hcan1.Init.TransmitFifoPriority = DISABLE;
	if (HAL_CAN_Init(&hcan1) != HAL_OK)
	{
		Error_Handler();
	}
	/* USER CODE BEGIN CAN1_Init 2 */
	CAN_FilterTypeDef can_filter;
	can_filter.FilterIdHigh = 0x0000;
	can_filter.FilterIdLow = 0x0000;
	can_filter.FilterMaskIdHigh = 0x0000;
	can_filter.FilterMaskIdLow = 0x0000;
	can_filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
	can_filter.FilterBank = 0;
	can_filter.FilterMode = CAN_FILTERMODE_IDMASK;
	can_filter.FilterScale = CAN_FILTERSCALE_32BIT;
	can_filter.FilterActivation = CAN_FILTER_ENABLE;
	can_filter.SlaveStartFilterBank = 2;
	if (HAL_CAN_ConfigFilter(&hcan1, &can_filter) != HAL_OK)
	{
		Error_Handler();
	}
	HAL_CAN_Start(&hcan1);

	// Activate the notification
	HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING);
}


static void MX_GPIO_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	/* GPIO Ports Clock Enable */
	//__HAL_RCC_GPIOA_CLK_ENABLE();
	__HAL_RCC_GPIOD_CLK_ENABLE();

	/*Configure GPIO pin Output Level */
	HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin, GPIO_PIN_RESET);

	/*Configure GPIO pin : B1_Pin */
	GPIO_InitStruct.Pin = B1_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

	/*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin LD6_Pin */
	GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}



void Error_Handler(void)
{
	/* USER CODE BEGIN Error_Handler_Debug */
	/* User can add his own implementation to report the HAL error return state */
	__disable_irq();
	while (1)
	{
	}
	/* USER CODE END Error_Handler_Debug */
}

 

Hello,

Tips in CAN communication:

When you face an issue in normal mode start by using Loopback mode at least to validate your Software and filters config. Because it could be something related to your HW. You can refer to this example: https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_LoopBack/

Meanwhile, you still using a bad CAN config. You assigned FIFO0 to the filter but you activated FIFO1 interrupt.

can_filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
	// Activate the notification
	HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING)
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan1)
{
	HAL_CAN_GetRxMessage(hcan1, CAN_RX_FIFO1, &rx_header, rx_data);

}

PS: regarding your question about the usage of HAL_CAN_GetRxMessage(); refer also to the example I pointed out above (loopback mode) where it has used as follows:

  /*##-5- Start the Reception process ########################################*/
  if(HAL_CAN_GetRxFifoFillLevel(&CanHandle, CAN_RX_FIFO0) != 1)
  {
    /* Reception Missing */
    Error_Handler();
  }

  if(HAL_CAN_GetRxMessage(&CanHandle, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)
  {
    /* Reception Error */
    Error_Handler();
  }

In any case you need to use the examples from STM32CubeF4 package.

The two examples of the two modes are there:

https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_LoopBack

https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_Networking

They are working well. 

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.