cancel
Showing results for 
Search instead for 
Did you mean: 

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

ALAN4
Associate II

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

15 REPLIES 15

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.

Sure, trying it.

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

ALAN4
Associate II

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

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
Associate II

Thanks man,

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

Regards,

Alankrit