cancel
Showing results for 
Search instead for 
Did you mean: 

I am unable to configure CAN in STM32L476VG Discovery board. I wanted to develop an application which sends CAN message and receives CAN message. I enabled 2 interrupts CAN1_TX_Interrupt, CAN1_RX0_Interrupt. I selected Normal Mode only .

RahulR
Associate II

When debugging I found that MX_CAN1_Init() is not getting initialized, inside this code HAL_CAN_Init() is blocked by a while loop.

That blocking iteration is as follows

/* Get tick */

 tickstart = HAL_GetTick();

 /* Check Sleep mode leave acknowledge */

 while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)

 {

  if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)

  {

   /* Update error code */

   hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;

   /* Change CAN state */

   hcan->State = HAL_CAN_STATE_ERROR;

   return HAL_ERROR;

  }

 }

I generated the start configuration using:

STM32CubeIDE

Version: 1.4.0

Build: 7511_20200720_0928 (UTC)

Firmwere STM32Cube FW_L4 V1.16.0

Can anyone please tell me the issue??

Code I used:

/* USER CODE BEGIN PV */

CAN_FilterTypeDef sFilterConfig;

CAN_TxHeaderTypeDef TxMessage;

CAN_RxHeaderTypeDef RxMessage;

uint8_t TxData[8];

uint8_t RxData[8];

uint32_t TxMailbox;

/* USER CODE END PV */

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

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_CAN1_Init();

 /* USER CODE BEGIN 2 */

  sFilterConfig.FilterBank = 0;

   sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

   sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

   sFilterConfig.FilterIdHigh = 0x0000;

   sFilterConfig.FilterIdLow = 0x0000;

   sFilterConfig.FilterMaskIdHigh = 0x0000;

   sFilterConfig.FilterMaskIdLow = 0x0000;

   sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;

   sFilterConfig.FilterActivation = ENABLE;

   sFilterConfig.SlaveStartFilterBank = 14;

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

   {

  test1 = 1;

   Error_Handler();

   }

   if((HAL_CAN_Start(&hcan1)) != HAL_OK)

   {

   test2 = 10;

  Error_Handler();

   }

   if((HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING | CAN_IT_TX_MAILBOX_EMPTY)) != HAL_OK)

   {

   test3 = 100;

  Error_Handler();

   }

   TxMessage.StdId = 0x321;

   TxMessage.ExtId = 0x01;

   TxMessage.RTR  = CAN_RTR_DATA;

   TxMessage.IDE  = CAN_ID_STD;

   TxMessage.DLC  = 8;

   TxMessage.TransmitGlobalTime = DISABLE;

   TxData[0] = 1;

   TxData[1] = 2;

   TxData[2] = 3;

   TxData[3] = 4;

   TxData[4] = 5;

   TxData[5] = 6;

   TxData[6] = 7;

   TxData[7] = 8;

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 HAL_CAN_AddTxMessage(&hcan1, &TxMessage, TxData, &TxMailbox);

 HAL_Delay(500);

 TxData[7] = TxData[7] + 1;

 }

 void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)

 {

 HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);

 }

 void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)

 {

 HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RxMessage, RxData);

 HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_8);

 }

 /* USER CODE END 3 */

}

2 REPLIES 2
Imen.D
ST Employee

Hello @RahulR​ ,

Welcome to the STM32 Community 😊

I recommend you to refer and check the configuration of CAN example, compare the code/functions with yours and make updates according to your needs:

STM32Cube_FW_L4_V1.16.0\Projects\STM32L476G-EVAL\Examples\CAN\CAN_Networking

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

Thank for the reply @Imen DAHMEN

Now HAL_CAN_Init() is working fine. When doing the debug it seems ok.

Now HAL_CAN_Start() and HAL_CAN_ActivateNotification() are not working. HAL_CAN_Start() function is trapped inside the following "wait the acknowledge " loop

 while ((hcan->Instance->MSR & CAN_MSR_INAK) != 0U)

  {

   /* Check for the Timeout */

   if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)

   {

    /* Update error code */

    hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;

    /* Change CAN state */

    hcan->State = HAL_CAN_STATE_ERROR;

    return HAL_ERROR;

   }

  }

It returns the state other than (state == HAL_CAN_STATE_READY) || (state == HAL_CAN_STATE_LISTENING) ) , HAL_CAN_ActivateNotification() also getting failed.

What may be the issue? Please help me.