Skip to main content
ALAN4
Associate II
June 29, 2020
Solved

The problem in CAN Receive using Interrupt. As code hangs in (HAL_CAN_RxFifo0MsgPendingCallback) function in STM32F103C8T6.

  • June 29, 2020
  • 15 replies
  • 10862 views

Hi,

I am working on CAN on STM32F103C8T6.

I am using a Callback Function of (HAL_CAN_RxFifo0MsgPendingCallback) but soon after receiving message code hangs in Callback Function even though no data sent afterwards it.

I am not getting the reason. Because this function works fine on CORTEX-M4 pocessor.

However, Transmit_Interrupt is working fine. But receive interrupt is hanging.

0693W000001rfS1QAI.png

Here, is the code Snippet.

Code is getting stuck in the receiver callback function (HAL_CAN_RxFifo0MsgPendingCallback).

/* INTERRUPT HANG IN RECEIVER ISR FUNCTION */

void HAL_CAN_RxFifo0MsgPendingCallback (CAN_HandleTypeDef *hcan) {

CAN_RxHeaderTypeDef RxHeader;

uint8_t rcvd_msg[5];

char msg[50];

if(HAL_CAN_GetRxMessage((CAN_HandleTypeDef*)&hcan,CAN_RX_FIFO0,&RxHeader,rcvd_msg) != HAL_OK)

{

Error_Handler();

}

sprintf(msg,"Message Received : %s\n",rcvd_msg);

HAL_UART_Transmit(&huart1,(uint8_t*)msg,strlen(msg),10);

}

Guys, please help.

Thanks,

Alankrit

This topic has been closed for replies.
Best answer by SKacp.1

Hi,

Please, see my example. I tested on the STM32F103VBT6. My example work in NORMAL and LOOPBACK mode. I have also connected LCD for diagnostic. HCLK = 72 MHZ, APB1 PCLK1 = 36 MHz

15 replies

SKacp.1
Associate
June 29, 2020

Hi,

I propose to increase table rcvd_msg from 5 to 8 elements.

Tesla DeLorean
Guru
June 29, 2020

Perhaps 9, and insure the data is properly NUL terminated to use string functions against it, and content is ASCII​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
ALAN4
ALAN4Author
Associate II
June 29, 2020

Sure, going to try.

ALAN4
ALAN4Author
Associate II
June 29, 2020

0693W000001rgKSQAY.png

0693W000001rgLBQAY.png

Tilen MAJERLE
ST Employee
June 29, 2020

uint8_t rcvd_msg[10] = {0} might help to clear the buffer from very beginning. You are using it as string afterwards, so if there is no NULL termination, it may go forever and crash the sprintf and system afterwards.

ALAN4
ALAN4Author
Associate II
June 29, 2020

Sure, trying it.

SKacp.1
Associate
June 29, 2020

Hi,

Please show me Your CAN settings.

I propose change in Your function void HAL_CAN_RxFifo0MsgPendingCallback (CAN_HandleTypeDef *hcan). Use sprintf and HAL_UART_transmit in the external functions and call it from the main. Maybe You have problem with the stack.

ALAN4
ALAN4Author
Associate II
June 29, 2020

Sure, going to share the images and my source code.

ALAN4
ALAN4Author
Associate II
June 29, 2020

There are 2 interesting things happening :

#1 : Without enabling CAN_Transmit_IT , HAL_CAN_TxMailbox0CompleteCallback() function gets called.

#2 : And then, code gets stuck in the (HAL_CAN_RxFifo0MsgPendingCallback) function even if I am not transmitting in callback function and clearing the buffer.

I am pretty sure, it is not a programming bug. There may be 2 areas of bug :

  1. Wrong CAN_settings.
  2. May be some issue with library.

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_CAN_Init();
 MX_USART1_UART_Init();
 /* USER CODE BEGIN 2 */
 
 CAN_Filter_Config();
 
	if(HAL_CAN_ActivateNotification(&hcan,CAN_IT_TX_MAILBOX_EMPTY | CAN_IT_RX_FIFO0_MSG_PENDING |CAN_IT_BUSOFF) != HAL_OK)
	{
		Error_Handler();
	}
 
 
 if (HAL_CAN_Start(&hcan) != HAL_OK) {
	 Error_Handler();
 }
 
 CAN_TX();
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
	 if (rx_flag > 0) {
		 rx_flag = 0;
		 HAL_UART_Transmit(&huart1,(uint8_t*)msg,strlen(msg),10);
		 HAL_UART_Transmit(&huart1,(uint8_t*)msg,sprintf(msg,"HI\n"),10); 
 /* NOT WORKING*/
	 }
 }
 
======================================================================
 
/* USER CODE BEGIN 4 */
 
void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) {
	HAL_UART_Transmit(&huart1,(uint8_t*)buffer,sprintf(buffer,"MESSAGE SENT : MAILBOX_0...\n"),10);
}
 
void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan) {
	HAL_UART_Transmit(&huart1,(uint8_t*)buffer,sprintf(buffer,"MESSAGE SENT : MAILBOX_1...\n"),10);
}
 
void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan) {
	HAL_UART_Transmit(&huart1,(uint8_t*)buffer,sprintf(buffer,"MESSAGE SENT : MAILBOX_2...\n"),10);
}
 
/* INTERRUPT HANG IN RECEIVER ISR FUNCTION */
void HAL_CAN_RxFifo0MsgPendingCallback (CAN_HandleTypeDef *hcan) {
 
	CAN_RxHeaderTypeDef RxHeader;
	uint8_t rcvd_msg[10];
 
/* INTERRUPT HANGING */
 
//	if (HAL_CAN_GetRxMessage((CAN_HandleTypeDef*)&hcan,CAN_RX_FIFO0,&RxHeader,rcvd_msg) != HAL_OK) {
//			Error_Handler();
//		}
 HAL_CAN_GetRxMessage((CAN_HandleTypeDef*)&hcan,CAN_RX_FIFO0,&RxHeader,rcvd_msg);
 	sprintf(msg, "FUNCTION_RETURN_VALUE : %s\n", rcvd_msg);
 	memset(rcvd_msg, 0, sizeof(rcvd_msg));
 
 	rx_flag = 1; /* Raise the flag */
 
 	/* Control never gets into the while loop as there is incessant CAN_Receive_Interrupts */
 
}
 
/* USER CODE END 4 */

0693W000001ri7VQAQ.png0693W000001ri7QQAQ.png

I am attaching my source code in attachment. Please go through it.

Please give your valuable feedback.

Regards,

Alankrit

SKacp.1
SKacp.1Best answer
Associate
June 29, 2020

Hi,

Please, see my example. I tested on the STM32F103VBT6. My example work in NORMAL and LOOPBACK mode. I have also connected LCD for diagnostic. HCLK = 72 MHZ, APB1 PCLK1 = 36 MHz

ALAN4
ALAN4Author
Associate II
June 29, 2020

Thanks man,

These settings worked. I appreciate for your time and effort you put to help me out.

Regards,

Alankrit