cancel
Showing results for 
Search instead for 
Did you mean: 

CAN bus does not work

kqian
Associate II

Hello,

I am using the STM32F7 in my project, which uses the CAN1. I create the project from cubemx and connect it to a STM32F4 evaluation board to test. I can see the Can signal via the oscillations cope after the transceiver, but no interrupt can be triggered. The main code is below, and anyone can help me?

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_CAN1_Init();

  while (1)

 {

}

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 HAL_PWR_EnableBkUpAccess();

 /** Configure the main internal regulator output voltage 

 */

 __HAL_RCC_PWR_CLK_ENABLE();

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

 /** Initializes the CPU, AHB and APB busses clocks 

 */

 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 = 24;

 RCC_OscInitStruct.PLL.PLLN = 96;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

 RCC_OscInitStruct.PLL.PLLQ = 9;

RCC_OscInitStruct.PLL.PLLR = 7;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 /** Activate the Over-Drive mode 

 */

 if (HAL_PWREx_EnableOverDrive() != HAL_OK)

 {

  Error_Handler();

 }

 /** Initializes the CPU, AHB and APB busses 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_DIV2;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)

 {

  Error_Handler();

 }

}

static void MX_CAN1_Init(void)

{

 CAN_FilterTypeDef sFilterConfig;

 /*##-1- Configure the CAN peripheral #######################################*/

 hcan1.Instance = CAN1;

 hcan1.Init.TimeTriggeredMode = DISABLE;

 hcan1.Init.AutoBusOff = ENABLE;

 hcan1.Init.AutoWakeUp = DISABLE;

 hcan1.Init.AutoRetransmission = ENABLE;

 hcan1.Init.ReceiveFifoLocked = DISABLE;

 hcan1.Init.TransmitFifoPriority = DISABLE;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_6TQ;

 hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;

 hcan1.Init.Prescaler = 6;

 if (HAL_CAN_Init(&hcan1) != HAL_OK)

{

  Error_Handler();

 }

 /*##-2- Configure the CAN Filter ###########################################*/

 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)

 {

  /* Filter configuration Error */

  Error_Handler();

 }

 /*##-3- Start the CAN peripheral ###########################################*/

 if (HAL_CAN_Start(&hcan1) != HAL_OK)

 {

  /* Start Error */

  Error_Handler();

 }

 /*##-4- Activate CAN RX notification #######################################*/

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

 {

  /* Notification Error */

  Error_Handler();

 }

 /*##-5- Configure Transmission process #####################################*/

 TxHeader.StdId = 0x321;

 TxHeader.ExtId = 0x01;

 TxHeader.RTR = CAN_RTR_DATA;

 TxHeader.IDE = CAN_ID_STD;

 TxHeader.DLC = 2;

 TxHeader.TransmitGlobalTime = DISABLE;

}

/**

 * @brief GPIO Initialization Function

 * @param None

 * @retval None

 */

static void MX_GPIO_Init(void)

{

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOC_CLK_ENABLE();

 __HAL_RCC_GPIOH_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOD_CLK_ENABLE();

}

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)

{

 /* Get RX message */

 if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, RxData) != HAL_OK)

 {

  /* Reception Error */

  Error_Handler();

 }

 /* Display LEDx */

 if ((RxHeader.StdId == 0x321) && (RxHeader.IDE == CAN_ID_STD) && (RxHeader.DLC == 2))

 {

 // LED_Display(RxData[0]);

  ubKeyNumber = RxData[0];

 }

}

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)

{

 GPIO_InitTypeDef  GPIO_InitStruct;

 CANx_CLK_ENABLE();

 /* Enable GPIO clock ****************************************/

 CANx_GPIO_CLK_ENABLE();

 /*##-2- Configure peripheral GPIO ##########################################*/

 /* CAN1 TX GPIO pin configuration */

 GPIO_InitStruct.Pin = CANx_TX_PIN;

 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

 GPIO_InitStruct.Pull = GPIO_PULLUP;

 GPIO_InitStruct.Alternate = CANx_TX_AF;

 HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct);

 /* CAN1 RX GPIO pin configuration */

 GPIO_InitStruct.Pin = CANx_RX_PIN;

 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

 GPIO_InitStruct.Pull = GPIO_PULLUP;

 GPIO_InitStruct.Alternate = CANx_RX_AF;

 HAL_GPIO_Init(CANx_RX_GPIO_PORT, &GPIO_InitStruct);

 /*##-3- Configure the NVIC #################################################*/

 /* NVIC configuration for CAN1 Reception complete interrupt */

 HAL_NVIC_SetPriority(CANx_RX_IRQn, 1, 0);

 HAL_NVIC_EnableIRQ(CANx_RX_IRQn);

}

2 REPLIES 2
turboscrew
Senior III

I think the best way to begin debugging is to let the system run for a second or two, stop it and check the CAN_IER and CAN_ESR registers, especially the lec-field of the CAN_ESR.

turboscrew
Senior III

BTW, TxHeader.StdId and TxHeader.ExtId are mutually exclusive. TxHeader.IDE defines which one is used.

They are not standard part and extension part.