Skip to main content
KMew
Senior III
September 27, 2022
Question

CAN Communication: Messages Not Received w/ No Filter

  • September 27, 2022
  • 0 replies
  • 1039 views

Hello,

I am attempting to communicate between my STM32H7B3I-EVAL and a Vector CANalyzer (VN1630A).

I previously reached out to figure out why my CAN messages from the CANalyzer are not being received by the STM32 MCU. There was a belief that it was the filters causing the issue, so I did not initialize the filters and tried to see if any message was being received. There are still no messages being received, but I can send them from the STM32 to the CANalyzer without issue.

I would like to find out why my code is not working, but I have some specific questions to try and assist my thought process:

  • If the FDCAN_FilterTypeDef is a defined variable but not initialized, it will not filter, correct?
  • Is the function correct for receiving data from the RXBUFFER?

if(HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_BUFFER0, &RxHeader, RxData) != HAL_OK){

Error_Handler();

}

Setup Picture:0693W00000SvpupQAB.jpg 

#include "main.h"
 
/** @addtogroup STM32H7xx_HAL_Examples
 * @{
 */
 
/** @addtogroup Templates
 * @{
 */
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
FDCAN_HandleTypeDef hfdcan1;
FDCAN_FilterTypeDef sFilterConfig;
FDCAN_TxHeaderTypeDef TxHeader;
FDCAN_RxHeaderTypeDef RxHeader;
uint8_t TxData0[] = {0x10, 0x32, 0x54, 0x76, 0x98, 0x00, 0x11, 0x22};
uint8_t TxData1[] = {0x00, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
uint8_t TxData2[] = {0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00};
uint8_t TxData3[] = {0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89};
uint8_t RxData[8];
__IO uint8_t UserButtonClickEvent = RESET;
 
/* Private function prototypes -----------------------------------------------*/
static void MPU_Config(void);
static void SystemClock_Config(void);
static void Error_Handler(void);
static void CPU_CACHE_Enable(void);
static void CAN_Init(void);
static void CAN_RxStdFilter_Init(void);
static void CAN_RxExtFilter_Init(void);
static void CAN_TxFilter_Init(void);
static uint32_t BufferCmp8b(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
/*
static DTS_HandleTypeDef DtsHandle;
static int32_t Temperature;
*/
 
/* Private functions ---------------------------------------------------------*/
 
/**
 * @brief Main program
 * @param None
 * @retval None
 */
int main(void)
{
 /* Configure the MPU attributes */
 MPU_Config();
 
 /* Enable the CPU Cache */
 CPU_CACHE_Enable();
 
 /* STM32H7xx HAL library initialization:
 - Configure the Systick to generate an interrupt each 1 msec
 - Set NVIC Group Priority to 4
 - Low Level Initialization
 */
 HAL_Init();
 
 /* Configure the system clock to 280 MHz */
 SystemClock_Config();
 
 /* Configure LED1 and LED2 */
 BSP_LED_Init(LED1);
 BSP_LED_Init(LED2);
 BSP_LED_Init(LED3);
 
 /* Configure Tamper push-button in interrupt mode */
 BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_EXTI);
 
// CAN Initialization & Configuration
 CAN_Init();
 //CAN_RxStdFilter_Init();
 //CAN_RxExtFilter_Init();
 //CAN_TxFilter_Init();
 
 if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
 {
 /* Filter configuration Error */
 Error_Handler();
 }
 
 /* Start the FDCAN module */
 HAL_FDCAN_Start(&hfdcan1);
 
 if (HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
 {
 /* Notification Error */
 Error_Handler();
 }
 
 /* Send Tx buffer message */
 HAL_FDCAN_EnableTxBufferRequest(&hfdcan1, FDCAN_TX_BUFFER0);
 
 
 while(1)
 {
	 for (uint16_t i = 0; i < 0xFFFF; i++)
			 {
 
		 	 	 uint16_t TxData1[] = {i, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
 
				 //Add message to Tx FIFO
 
					TxHeader.Identifier = 0x111;
					TxHeader.IdType = FDCAN_STANDARD_ID;
					TxHeader.TxFrameType = FDCAN_DATA_FRAME;
					TxHeader.DataLength = FDCAN_DLC_BYTES_8;
					TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
					TxHeader.BitRateSwitch = FDCAN_BRS_ON;
					TxHeader.FDFormat = FDCAN_FD_CAN;
					TxHeader.TxEventFifoControl = FDCAN_STORE_TX_EVENTS;
					TxHeader.MessageMarker = 0x52;
					HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData1);
					//HAL_Delay(100);
 
					uint16_t TxData2[] = {i, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
 
					TxHeader.Identifier = 0x135;
					TxHeader.IdType = FDCAN_STANDARD_ID;
					TxHeader.TxFrameType = FDCAN_DATA_FRAME;
					TxHeader.DataLength = FDCAN_DLC_BYTES_8;
					TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
					TxHeader.BitRateSwitch = FDCAN_BRS_ON;
					TxHeader.FDFormat = FDCAN_FD_CAN;
					TxHeader.TxEventFifoControl = FDCAN_STORE_TX_EVENTS;
					TxHeader.MessageMarker = 0x52;
					HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &TxHeader, TxData2);
					//HAL_Delay(100);
 
 
					if(HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_BUFFER0, &RxHeader, RxData) != HAL_OK){
						Error_Handler();
					}
 
					if(RxHeader.Identifier == 0x123)
						BSP_LED_Toggle(LED3);
 
			 }
 
 
 }
static void CAN_Init(){
	/* Initializes the FDCAN peripheral in loopback mode */
	 hfdcan1.Instance = FDCAN1;
	 hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
	 hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
	 hfdcan1.Init.AutoRetransmission = ENABLE;
	 hfdcan1.Init.TransmitPause = DISABLE;
	 hfdcan1.Init.ProtocolException = DISABLE;
	 /* Bit time configuration:
	 ************************
	 Bit time parameter | Nominal | Data
	 ---------------------------|--------------|----------------
	 fdcan_ker_ck | 20 MHz | 20 MHz
	 Time_quantum (tq) | 50 ns | 50 ns
	 Synchronization_segment | 1 tq | 1 tq
	 Propagation_segment | 23 tq | 23 tq
	 Phase_segment_1 | 8 tq | 8 tq
	 Phase_segment_2 | 8 tq | 8 tq
	 Synchronization_Jump_width | 8 tq | 8 tq
	 Bit_length | 40 tq = 2 �s | 40 tq = 2 �s
	 Bit_rate | 0.5 MBit/s | 0.5 MBit/s
	 */
	 hfdcan1.Init.NominalPrescaler = 0x1; /* tq = NominalPrescaler x (1/fdcan_ker_ck) */
	 hfdcan1.Init.NominalSyncJumpWidth = 0x8;
	 hfdcan1.Init.NominalTimeSeg1 = 0x1F; /* NominalTimeSeg1 = Propagation_segment + Phase_segment_1 */
	 hfdcan1.Init.NominalTimeSeg2 = 0x8;
	 hfdcan1.Init.DataPrescaler = 0x1;
	 hfdcan1.Init.DataSyncJumpWidth = 0x8;
	 hfdcan1.Init.DataTimeSeg1 = 0x1F; /* DataTimeSeg1 = Propagation_segment + Phase_segment_1 */
	 hfdcan1.Init.DataTimeSeg2 = 0x8;
	 hfdcan1.Init.MessageRAMOffset = 0;
	 hfdcan1.Init.StdFiltersNbr = 1;
	 hfdcan1.Init.ExtFiltersNbr = 1;
	 hfdcan1.Init.RxFifo0ElmtsNbr = 1;
	 hfdcan1.Init.RxFifo0ElmtSize = FDCAN_DATA_BYTES_8;
	 hfdcan1.Init.RxFifo1ElmtsNbr = 2;
	 hfdcan1.Init.RxFifo1ElmtSize = FDCAN_DATA_BYTES_8;
	 hfdcan1.Init.RxBuffersNbr = 1;
	 hfdcan1.Init.RxBufferSize = FDCAN_DATA_BYTES_8;
	 hfdcan1.Init.TxEventsNbr = 2;
	 hfdcan1.Init.TxBuffersNbr = 1;
	 hfdcan1.Init.TxFifoQueueElmtsNbr = 2;
	 hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
	 hfdcan1.Init.TxElmtSize = FDCAN_DATA_BYTES_8;
	 HAL_FDCAN_Init(&hfdcan1);
}

This topic has been closed for replies.