cancel
Showing results for 
Search instead for 
Did you mean: 

Issues with CANBUS on STM32F072: Bit Dominant Error

PF-JohnF
Associate

Hello,

I am currently working to get CAN transmission to work on a STM32F072B-DISCO. I am continuously getting a BIT_DOMINANT_ERROR in the LEC of the CAN_ESR when attempting to transmit.

 

I am using a TCAN1044 (https://www.ti.com/lit/ds/symlink/tcan1044v-q1.pdf) as the CAN transceiver with the following connections:

TCAN PIN | Connection
1 | PA12
2 | GND
3 | 5V
4 | PA11
5 | 3V
6 | CANL
7 | CANH
8 | GND

There is also a 120 ohm terminating resistor at both ends CAN Bus, and only one other device is connected, which is a PCAN-USB reader for viewing transmitted CAN messaged. The PCAN was tested previously on another device and works without issues.

Here is my code:

/* USER CODE BEGIN Header */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan;

/* USER CODE BEGIN PV */
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t egg[8] = {'5',0,0,0,0,0,0,0};
uint32_t TxMailbox;
CAN_TxHeaderTypeDef TxHeader;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_CAN_Init();
  /* USER CODE BEGIN 2 */
  HAL_CAN_Start(&hcan);

  TxHeader.StdId = 0x056;
  TxHeader.ExtId = 0x01;
  TxHeader.RTR = CAN_RTR_DATA;
  TxHeader.IDE = CAN_ID_STD;
  TxHeader.DLC = 2;
  TxHeader.TransmitGlobalTime = DISABLE;

  uint8_t sleepy;

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while(1){
	  HAL_Delay(100);
	  HAL_CAN_AddTxMessage(&hcan, &TxHeader, egg, &TxMailbox);
	  HAL_Delay(10);
	  sleepy = HAL_CAN_IsTxMessagePending(&hcan,TxMailbox);
  }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief CAN Initialization Function
  * @PAram None
  * @retval None
  */
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 = CAN;
  hcan.Init.Prescaler = 16;
  hcan.Init.Mode = CAN_MODE_NORMAL;
  hcan.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan.Init.TimeSeg1 = CAN_BS1_1TQ;
  hcan.Init.TimeSeg2 = CAN_BS2_1TQ;
  hcan.Init.TimeTriggeredMode = DISABLE;
  hcan.Init.AutoBusOff = ENABLE;
  hcan.Init.AutoWakeUp = DISABLE;
  hcan.Init.AutoRetransmission = DISABLE;
  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 */

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */

/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

When viewing PA12/CAN_TX pin on an oscilloscope, the voltage remains constant when running. 

When unplugging the other device, the BIT_DOMINANT_ERROR persists.

At this point, I'm not sure if it's a software or hardware issue. Would anybody be able to run this code on their own hardware that is known to work with CAN and verify if it is a hardware or software issue?

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello @PF-JohnF ,

At a first glance I have two remarks based on what you provide as source code:

1- It's not recommended to use HSI clock as source clock for CAN communication. As you are using STM32F072B-DISCO board, I recommend you to use The clock generated by STLink module through its MCO output and solder the solder bridge SB19 to connect MCO to PF0-OSC_IN of your MCU. You need to configure the RCC source clock in Bypass mode.

2- Your values of BS1 and BS2 are very low. You need to increase them as much as possible in order to let BS1 ~70 to 80% of the bit time. You need to decrease your CAN prescaler to match this requirement.

I've attached a zipped .ioc file for you reference.

- CAN bitrate = 1Mb/s

- SysClock = 48MHz

- External source clock = 8MHz

- CAN prescaler = 3

- BS1 = 12

- BS2 = 3

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

4 REPLIES 4
SofLit
ST Employee

Hello @PF-JohnF ,

At a first glance I have two remarks based on what you provide as source code:

1- It's not recommended to use HSI clock as source clock for CAN communication. As you are using STM32F072B-DISCO board, I recommend you to use The clock generated by STLink module through its MCO output and solder the solder bridge SB19 to connect MCO to PF0-OSC_IN of your MCU. You need to configure the RCC source clock in Bypass mode.

2- Your values of BS1 and BS2 are very low. You need to increase them as much as possible in order to let BS1 ~70 to 80% of the bit time. You need to decrease your CAN prescaler to match this requirement.

I've attached a zipped .ioc file for you reference.

- CAN bitrate = 1Mb/s

- SysClock = 48MHz

- External source clock = 8MHz

- CAN prescaler = 3

- BS1 = 12

- BS2 = 3

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
PranavGurav
Associate II

Hi @SofLit  I am also facing the same issue i tried modifying the clock source to HSE_BYPASS on STM32F407 discovery board. can you give me more details about how to solve this issue

 

PranavGurav
Associate II

Hi @PF-JohnF  Were you able to solve the issue if Yes can you help me as i am also facing same condition. Thank you in advance

Depending on the port/pins you are using, you may need to connect some solder bridges on the back of the board. On the STM32F072 board, I had to solder SB20 and SB23 bridges to connect PA11 and PA12.

There may also be a solder bridge necessary to connect HSE to the microcontroller as well.