cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Protocol Receiver isn't working

Piyush_Gupta
Associate II
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* 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 */
CAN_TxHeaderTypeDef pHeader1; //declare a specific header for message transmittions
CAN_RxHeaderTypeDef pRxHeader1; //declare header for message reception
uint32_t TxMailbox1; //mail box number
CAN_FilterTypeDef sFilterConfig1; //declare CAN filter structure
 uint8_t stmp1[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // To send the flow control frame code
 
//uint8_t stmp2[8] = {0, 0, 54, 0, 34, 0, 0, 0}; // To send the flow control frame code
uint8_t receiveData1[8];  // receive every message in this array
int16_t rxCounter1 = 0;   // message number for multi-frame data received
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan1;
 
/* USER CODE BEGIN PV */
uint16_t CAN_transmit = 0,CAN_prevtransmit = 0, res=5;
int count;
volatile uint32_t tick5;
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_CAN1_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint16_t time_diff;
/* 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 */
//	HAL_TIM_Base_Start_IT(&htim2);
	
  pHeader1.DLC = 8; // give message size of 8 byte
  pHeader1.IDE = CAN_ID_STD; // set identifier to standard
  pHeader1.RTR = CAN_RTR_DATA; //set data type to remote transmission request?
  pHeader1.StdId = 0x5; //define a standard identifier, used for message identification by filters (switch this for the other microcontroller)
 
 //filter one (stack light blink)
   sFilterConfig1.FilterBank = 0;
  sFilterConfig1.FilterMode = CAN_FILTERMODE_IDMASK;
 
  sFilterConfig1.FilterFIFOAssignment = CAN_FILTER_FIFO0; //set fifo assignment
  sFilterConfig1.FilterIdHigh = 0; //the ID that the filter looks for (switch this for the other microcontroller)
  sFilterConfig1.FilterIdLow = 0; 
  sFilterConfig1.FilterMaskIdHigh = 0;
  sFilterConfig1.FilterMaskIdLow = 0;
  sFilterConfig1.FilterScale = CAN_FILTERSCALE_32BIT; //set filter scale
  sFilterConfig1.FilterActivation = ENABLE;
 
  sFilterConfig1.SlaveStartFilterBank = 14;
 
 
  res = HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig1); //configure CAN filter
 
 
 
  HAL_CAN_Start(&hcan1); //start CAN1
	
	
	
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
//		stmp1[4] = 34;
//	stmp1[7] = count;
					if (  HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0) > 0 ) {
					HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &pRxHeader1, receiveData1);                     //receiver testing_piyush
				}
//			time_diff = HAL_GetTick()-tick5;		
//		if( time_diff > 200){			
//							
//HAL_CAN_AddTxMessage(&hcan1, &pHeader1, stmp1, &TxMailbox1);
 
//						HAL_Delay(200);
					count++;
//					if(CAN_transmit != CAN_prevtransmit){	
//			    HAL_CAN_AddTxMessage(&hcan1, &pHeader1, stmp1, &TxMailbox1);
//					CAN_prevtransmit = CAN_transmit;
//					}
				 tick5= HAL_GetTick();
//} 
    /* 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};
 
  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
 
  /** 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_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 15;
  RCC_OscInitStruct.PLL.PLLN = 192;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  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_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != 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 = 10;
  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 Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
 
}
 
/* USER CODE BEGIN 4 */
 
//void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
 
//	if(htim == &htim2){
//	//	 HAL_CAN_AddTxMessage(&hcan1, &pHeader1, stmp1, &TxMailbox1);
//	CAN_transmit++;
//	}
 
//}
 
/* 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 */
  __disable_irq();
  while (1)
  {
  }
  /* 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,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

Above snippet STM32f767ZGT6

Hello, I'm using STM32f767ZGT6 as a receiver and STM32F446VE as a transmitter for CAN communication.

When if transmit data from 1st microcontroller to 2nd microcontroller, Everything is working properly data is begin received to 2nd microcontroller without any issue. But, when when I try to transmit from 2nd microcontroller to the 1st. The HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0) is always less than zero.

1st microcontroller isn't receiving any data.

Both Microcontroller have same filter parameters and baud rate and different address.. I can see Transmitted data on oscilloscope but microcontroller isn't receiving.

What's the issue? Can any one help...

Thank you.

1 REPLY 1
Piyush_Gupta
Associate II

the same code work with other stm32f767Zx microcontroller but not with Stm32f676ZGT6, I have tried multiple microcontroller of same model... but still no progress.