cancel
Showing results for 
Search instead for 
Did you mean: 

CAN doesn't works (STM32F767ZI)

Uvess.1
Associate II

Hi everyone,

I use STM32F767ZI nucleo board and i'd like to use CAN to send data.

To begin ijust want to see the data frame sent on Can_Tx on my oscilloscope.

For this i just connect the Can_rx to the Can_tx of my nucleo board on PD0 and PD1 to see what happens.

ONce done i'llconnect two nucleo to send data between them.

On this way, i configured my board as below :

THen i generate the project,

and configure the can Transmission:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>
&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f7xx_hal_can.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 hcan1;
CAN_FilterTypeDef sFilterConfig;
CAN_TxHeaderTypeDef   TxHeader;
CAN_RxHeaderTypeDef   RxHeader;
uint8_t               TxData[8];
uint8_t               RxData[8];
uint32_t              TxMailBox;
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN1_Init(void);
void CAN_Config(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* 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_CAN1_Init();
  /* USER CODE BEGIN 2 */
  CAN_Config();
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	  HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailBox); //envoie message CAN
	  HAL_Delay(500);
    /* 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};
 
  /** 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_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief CAN1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_CAN1_Init(void)
{
 
  /* USER CODE BEGIN CAN1_Init 0 */
 
  /* 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_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 = DISABLE;
  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 */
 
  /* USER CODE END CAN1_Init 2 */
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_14|GPIO_PIN_7, GPIO_PIN_RESET);
 
  /*Configure GPIO pins : PB0 PB14 PB7 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_14|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
}
 
/* USER CODE BEGIN 4 */
void CAN_Config(void)
{
  CAN_FilterTypeDef  sFilterConfig;
 
  sFilterConfig.FilterBank = 0;
  sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
  sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
  sFilterConfig.FilterIdHigh = 0x0000;//filtre id
  sFilterConfig.FilterIdLow = 0x0000;//filtre id
  sFilterConfig.FilterMaskIdHigh = 0x0000;//pas de masque
  sFilterConfig.FilterMaskIdLow = 0x0000;//pas de masque
  sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
  sFilterConfig.FilterActivation = ENABLE;
  sFilterConfig.SlaveStartFilterBank = 14;
 
  HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig);
  HAL_CAN_Start(&hcan1);
  HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING /*| CAN_IT_TX_MAILBOX_EMPTY*/);
 
  /* Paramétrage de la trame à envoyer */
  TxHeader.StdId = 0x321; 						/*identifiant standard (11 bits --> 0x7FF)
  	  	  	  	  	  	  	  	  	  	  	  	  Détermine l'adresse du périphérique auquelle la trame est destiné
  	  	  	  	  	  	  	  	  	  	  	  	  Si plusieurs périphériques sur le bus comprennent cette adresse dans leur filtre, ils recevront tous la trame */
  TxHeader.ExtId = 0x01; 						//identifiant etendu (29 bits --> 0x1FFFFFFF) Adresse etendu non utilisée
  TxHeader.RTR = CAN_RTR_DATA; 					//Précise que la trame contient des données
  TxHeader.IDE = CAN_ID_STD; 					//Précise que la trame est de type Standard
  TxHeader.DLC =8; 								//8 bits de données
  TxHeader.TransmitGlobalTime = DISABLE;	 	//écrit le temps de transmission de la trame dans les données en Data[6] et Data[7]
  TxData[0] = 1;
  TxData[1] = 2;
  TxData[2] = 3;
  TxData[3] = 4;								/* 8 bits de données */
  TxData[4] = 5;
  TxData[5] = 6;
  TxData[6] = 7;
  TxData[7] = 8;
  HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
}
 
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	UNUSED(hcan);
	HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &RxHeader, RxData);
	HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
	HAL_Delay(100);
}
/* 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 can add his own implementation to report the HAL error return state */
 
  /* 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 can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

However when i check the CAN_Tx on the oscilloscope, nothing is send

0693W000003Bp10QAC.jpg

PS: i've been looking different topics on the internet but nothing solved my problem.

Thanks for help,

11 REPLIES 11
Jack Peacock_2
Senior III

CAn requires a minimum of two nodes to work. You also need a twisted-pair cable with termination resistors at each end. Connecting the TX and RX pins is NOT a CAN hardware loopback.

If you check the CAN status registers you will see TX errors and a bus disconnect in effect, which is why you don't see anything on the scope.

Jack Peacock

Hi Jack,

Thx for your reply,

I'm a beginner in computing and in stm32 so all isn't an evidence.

So if i understand i've to connect directly my nucleos like that ?

Thanks in advance,

Uvess

Uvess.1
Associate II

0693W000003BqbNQAS.pngHere is the manip i want to verify

Nikita91
Lead II

Your wiring to use CAN without drivers is wrong.

Look at: https://www.mikrocontroller.net/attachment/28831/siemens_AP2921.pdf

0693W000003Bs3DQAS.png

The CAN is not an UART!

HI Nikita,

Ok, thx for your help, I'm gonna to work on it...

But I've a problem:

The goal of my project is to send data in broadcast.

In fact, today we have this system:

0693W000003BvpUQAS.png

that we want to replace by this:

0693W000003BvjcQAC.png

TO begin, i just want to generate CAN frame and verify the reception. Next, i'd like to include the broadcast module.

Thx for your help,

Uvess

Uvess.1
Associate II

HI Nikita,

Ok, thx for your help, I'm gonna to work on it...

But I've a problem:

The goal of my project is to send data in broadcast.

In fact, today we have this system:

0693W000003BvpUQAS.png

that we want to replace by this:

0693W000003BvjcQAC.png

TO begin, i just want to generate CAN frame and verify the reception. Next, i'd like to include the broadcast module.

Thx for your help,

Uvess

Nikita91
Lead II

The CAN bus is a broadcast type of bus. This means that all nodes can “hear�? all transmissions.

If one node receive a correct message it will ACK this message.

If the message is not acked, probably the sender will retry.

The CAN bus timing is very tight.

For my information, what is the "broadcast module" you use ?

Uvess.1
Associate II

Ok, thx for your explanations. It helps me a lot ...

Of course the can bus timing is worth around 1Mb/s in my case.

I'm going to test The Siemens CAN application with one data line.

This is a NEW module, which is not yet available on the market but it supports can application and so tight timming ^^

I come back to present my results...

Thx a lot Nikita.

Uvess.1
Associate II

Hi everyone,

Some BAD news -_-,

I directly tested the wireless experiment :

0693W000003BvjcQAC.png

PS: I connect a GND between the 2 MCU.

But nothing happens... no CAN frames sent on Can1_Tx on the scope.

I'm still waiting an order to test the SIEMENS CAN application without transceiver.

See you,

Uvess