Skip to main content
Explorer II
April 9, 2024
Solved

NUCLEO-F412ZG CAN Communication, No RX and No tx

  • April 9, 2024
  • 2 replies
  • 1708 views

I am using CAN1 pins to Connect to the driver board (SN65HVD230).

 

JDOG_3-1712698313675.png

 

0405241626~2.jpg

All Voltages are good.

I am using 2 PCAN dongles with 120 Ohm terminations at both ends of the BUS with the Nucleo CAN shown above in the middle of the bus. 

I ported the code from the following project;

JDOG_0-1712696721291.png

 


>TX Mailbox used: 0x1
t
>TX Mailbox used: 0x2
s
>MCR: 0x30
Can State: 0x2
Interrupts: 0x1007F, msr flags: 0xC00 , tsr flags: 0x52000000, rf0r flags: 0x0, rf1r flags: 0x0, esr flags: 0x0

The Tx message is not getting out.

 

Also I have a PCAN message being transmitted on one PCAN dongle and the other PCAN Dongle is receiving the message fine.

JDOG_1-1712698012460.pngJDOG_2-1712698114551.png

 

 

The Nucleo pcb does not see the message at all.

I am using the default clock setup for the NUCLEO board

 

 

 

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 

/** Configure the main internal regulator output voltage

*/

__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 

/** Initializes the RCC Oscillators according to the specified parameters

* in the RCC_OscInitTypeDef structure.

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 8;

RCC_OscInitStruct.PLL.PLLN = 384;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

RCC_OscInitStruct.PLL.PLLQ = 8;

RCC_OscInitStruct.PLL.PLLR = 2;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

 

/** Initializes the CPU, AHB and APB buses 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_3) != HAL_OK)

{

Error_Handler();

}

}

 

The CAN setup for 250 K

/**

* @brief CAN1 Initialization Function

* None

* @retval None

*/

static void MX_CAN1_Init(void)

{

 

/* USER CODE BEGIN CAN1_Init 0 */

HAL_StatusTypeDef HAL_error;

CAN_FilterTypeDef sFilterConfigCAN1;

 

/* USER CODE END CAN1_Init 0 */

 

/* USER CODE BEGIN CAN1_Init 1 */

 

/* USER CODE END CAN1_Init 1 */

hcan1.Instance = CAN1;

hcan1.Init.Prescaler = 12;

hcan1.Init.Mode = CAN_MODE_SILENT; //CAN_MODE_NORMAL;

hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;

hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;

hcan1.Init.TimeTriggeredMode = DISABLE;

hcan1.Init.AutoBusOff = DISABLE;

hcan1.Init.AutoWakeUp = ENABLE;

hcan1.Init.AutoRetransmission = DISABLE;

hcan1.Init.ReceiveFifoLocked = DISABLE;

hcan1.Init.TransmitFifoPriority = DISABLE;

if (HAL_CAN_Init(&hcan1) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN CAN1_Init 2 */

 

sFilterConfigCAN1.FilterBank = 0;

sFilterConfigCAN1.FilterMode = CAN_FILTERMODE_IDMASK;

sFilterConfigCAN1.FilterScale = CAN_FILTERSCALE_32BIT;

sFilterConfigCAN1.FilterIdHigh = 0x0000;

sFilterConfigCAN1.FilterIdLow = 0x0000;

sFilterConfigCAN1.FilterMaskIdHigh = 0x0000;

sFilterConfigCAN1.FilterMaskIdLow = 0x0000;

sFilterConfigCAN1.FilterFIFOAssignment = CAN_RX_FIFO0;

sFilterConfigCAN1.FilterActivation = ENABLE;

sFilterConfigCAN1.SlaveStartFilterBank = 14;

 

HAL_error = HAL_CAN_ConfigFilter(&hcan1, &sFilterConfigCAN1);

 

if (HAL_error != HAL_OK)

{

Error_Handler();

}

 

 

 

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

HAL_error = HAL_CAN_Start(&hcan1);

 

if (HAL_error != HAL_OK)

{

Error_Handler();

}

 

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

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_FULL);

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING);

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_FULL);

 

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_OVERRUN);

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_TX_MAILBOX_EMPTY);

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_OVERRUN);

 

 

HAL_error = HAL_CAN_ActivateNotification(&hcan1, CAN_IT_WAKEUP);

 

 

if (HAL_error != HAL_OK)

{

Error_Handler();

}

/* USER CODE END CAN1_Init 2 */

 

}

 

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    1- Silent mode?

     

    hcan1.Init.Mode = CAN_MODE_SILENT; //CAN_MODE_NORMAL;

     

    2- You need to check your GPIOs: are you sure about the correct GPIOs connected to the transceiver (correct alternate functions)?

    3- Are you sure about your bitrate?

    4- You need to validate your tranceiver module:

    Are you seeing frames on CAN_TX and CAN_RX? If not, you have an issue with your module. Are you sure the transceiver is power supplied? check the voltage on its pins.

    PS: in next time please use </> button to insert your code.

    Thanks.

    2 replies

    mƎALLEmAnswer
    Technical Moderator
    April 9, 2024

    Hello,

    1- Silent mode?

     

    hcan1.Init.Mode = CAN_MODE_SILENT; //CAN_MODE_NORMAL;

     

    2- You need to check your GPIOs: are you sure about the correct GPIOs connected to the transceiver (correct alternate functions)?

    3- Are you sure about your bitrate?

    4- You need to validate your tranceiver module:

    Are you seeing frames on CAN_TX and CAN_RX? If not, you have an issue with your module. Are you sure the transceiver is power supplied? check the voltage on its pins.

    PS: in next time please use </> button to insert your code.

    Thanks.

    JDOGAuthor
    Explorer II
    April 10, 2024

    0410241400~2.jpg

    I changed to different transceiver and fixed the Port Pin selection on the CAN_TX and CAN_RX. I had Pin 0 and Pin 1 configured to the alternate selection but it was on port G instead of port D. I also had the CAN_H and CAN_L swapped. I am now working great. Thanks SoftLit and Karl for your help.

    Graduate II
    April 10, 2024

    How do you know if one of the Activate CAN RX notification section returns HAL_ERROR but gets set to HAL_OK towards the end?

    Did you enable the NVIC? What is the APB1 clock frequency?