cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 CAN HAL API Driver - Rx Interrupt Not Working (CAN_IT_RX_FIFO0_MSG_PENDING)

RFinc.1
Associate

Hi,

I am having some trouble with the CAN peripheral on the STM32F103C8Tx. I am using the HAL API's (via STM32CubeMX) to configure the device. I am using the device in loopback mode to test.

Up to now, I can successfully get the CAN peripheral to work in polling mode. However, I cannot for the life of me get the interrupt to fire for CAN_IT_RX_FIFO0_MSG_PENDING. Please see my code below, which I believe covers the correct setup procedure. I have also made sure to active the interrupt in the CubeMX setup.

//Initialise CAN

static void MX_CAN_Init(void)
{
 
  /* USER CODE BEGIN CAN_Init 0 */
 
  /* USER CODE END CAN_Init 0 */
 
  /* USER CODE BEGIN CAN_Init 1 */
 
  /* USER CODE END CAN_Init 1 */
  hcan.Instance = CAN1;
  hcan.Init.Prescaler = 4;
  hcan.Init.Mode = CAN_MODE_LOOPBACK;
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_15TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_2TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = DISABLE;
  hcan.Init.AutoWakeUp = DISABLE;
  hcan.Init.AutoRetransmission = ENABLE;
  hcan.Init.ReceiveFifoLocked = DISABLE;
  hcan.Init.TransmitFifoPriority = DISABLE;
  if (HAL_CAN_Init(&hcan) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN CAN_Init 2 */
 
  /* USER CODE END CAN_Init 2 */
 
}

//Initialise filter

  sf.FilterMaskIdHigh = 0x0000;
  sf.FilterMaskIdLow = 0x0000;
  sf.FilterFIFOAssignment = CAN_FILTER_FIFO0;
  sf.FilterBank = 0;
  sf.FilterMode = CAN_FILTERMODE_IDMASK;
  sf.FilterScale = CAN_FILTERSCALE_32BIT;
  sf.FilterActivation = CAN_FILTER_ENABLE;
 
  if(HAL_CAN_ConfigFilter(&hcan, &sf) != HAL_OK)
  {
	  Error_Handler();
  }

//Start CAN and activate interrupt

  if(HAL_CAN_Start(&hcan) != HAL_OK)
  {
	  Error_Handler();
  }
 
  if (HAL_CAN_ActivateNotification(&hcan, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  {
	  Error_Handler();
  }

//Transmit CAN data once every second (timer driven)

void CAN_Tx (void)
{
	CAN_TxHeaderTypeDef TxHeader;
 
	uint32_t TxMailbox;
 
	uint8_t canMessage[8] = {0x02, 0x01, 0x0C, 0x55, 0x55, 0x55, 0x55, 0x55};
 
	TxHeader.DLC = 8;
	TxHeader.StdId = 0x7DF;
	TxHeader.IDE = CAN_ID_STD;
	TxHeader.RTR = CAN_RTR_DATA;
 
	if (HAL_CAN_AddTxMessage(&hcan, &TxHeader, canMessage, &TxMailbox) != HAL_OK)
	{
		Error_Handler();
	}
 
	while(HAL_CAN_IsTxMessagePending(&hcan, TxMailbox));
 
}

at this point I have the following call back function which resides in /* USER CODE BEGIN 4 */ ...

void HAL_CAN_RxFIFO0MsgPendingCallback (CAN_HandleTypeDef *hcan)
{
	HAL_UART_Transmit(&huart2, (uint8_t *)"CAN_Received_OK\r\n", 17U, 100U);
}

No matter what i try, I cannot get the callback function to be called.

I would be very grateful if someone could point me in the right direction,

Thanks in advance,

Richard.

0 REPLIES 0