cancel
Showing results for 
Search instead for 
Did you mean: 

I have a CAN transceiver circuit with the STM32L496 discovery board. How do i program the hardware to receive and transmit VEHICLE data?

JGood
Associate II

The hardware of the circuit is similar to the draft below. Having trouble with programming to the vehicle. Need assistance programming to the board's specific pinout.0690X00000ApuTQQAZ.png

3 REPLIES 3
JoniS
Senior

So are trying to sniff data your vehicle is sending to the control units?

Can you show your current code? And point out what is working and what is not, so we can help you.

From where did you tap into the canbus? ​

What is the speed of the bus?​

Is it Can A or B bus in first place? (atleast Mercedes uses others aswell)

Have you verified that you need 120ohm termination resistor? (it's not always needed depending on the bus, and might even corrupt the data based on my experience)

JGood
Associate II

So essentially use i want to sniff the data from my vehicle is sending.

As a start i just want to be able to send and receive CAN messages, then I want to use that to ocnnect to the vehicle and receive the data and transmit it to be viewed on the PC.

Yes I've verified the resistor using the MCU. I followed this PDF for using CUBEMX for STM32L4 MCU's for CAN messaging. The main code isn't recognising any of the variables or filters I added in after generating the code

(e.g. CAN_FilterConfTypeDef sFilterConfig;)

Here's the code that generated with the add ins described by the PDF attached (next comment as didnt fit)

Let me know if I need to explain any more of it - the bold parts arent being recognised.

JGood
Associate II

/* USER CODE BEGIN Header */

/**

 ******************************************************************************

 * @file      : main.c

 * @brief     : Main program body

 ******************************************************************************

 * @attention

 *

 * <h2><center>&copy; Copyright (c) 2019 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 "stm32l4xx_hal_can.h"

/* Private variables ---------------------------------------------------------*/

CAN_HandleTypeDef hcan1;

/* USER CODE BEGIN PV */

CAN_FilterConfTypeDef sFilterConfig;

CanTxMsgTypeDef TxMessage;

CanRxMsgTypeDef RxMessage;

/* 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 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void) {

/* USER CODE BEGIN 1 */

sFilterConfig.FilterNumber = 0;

sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

sFilterConfig.FilterIdHigh = 0x0000;

sFilterConfig.FilterIdLow = 0x0000;

sFilterConfig.FilterMaskIdHigh = 0x0000;

sFilterConfig.FilterMaskIdLow = 0x0000;

sFilterConfig.FilterFIFOAssignment = 0;

sFilterConfig.FilterActivation = ENABLE;

sFilterConfig.BankNumber = 0;

HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig);

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

/* Configure the system clock */

SystemClock_Config();

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_CAN1_Init();

/* USER CODE BEGIN 2 */

TxMessage.StdId = 0x123;

TxMessage.RTR = CAN_RTR_DATA;

TxMessage.IDE = CAN_ID_STD;

TxMessage.DLC = 8;

TxMessage.Data[0] = 0x09;

TxMessage.Data[1] = 0x10;

TxMessage.Data[2] = 0x2A;

TxMessage.Data[3] = 0x3B;

TxMessage.Data[4] = 0x4C;

TxMessage.Data[5] = 0x5D;

TxMessage.Data[6] = 0x6E;

TxMessage.Data[7] = 0x7F;

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1) {

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

TxMessage.Data[0]++;

HAL_CAN_Transmit(&hcan1, 10);

HAL_CAN_Receive(&hcan1, CAN_FIFO0, 100);

}

/* 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 LSE Drive Capability

*/

HAL_PWR_EnableBkUpAccess();

__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

/** Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE| RCC_OSCILLATORTYPE_MSI;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.MSIState = RCC_MSI_ON;

RCC_OscInitStruct.MSICalibrationValue = 0;

RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11;

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_MSI;

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_2) != HAL_OK) {

Error_Handler();

}

/** Configure the main internal regulator output voltage

*/

if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1)

!= HAL_OK) {

Error_Handler();

}

/** Enable MSI Auto calibration

*/

HAL_RCCEx_EnableMSIPLLMode();

}

/**

 * @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_LOOPBACK;

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_GPIOB_CLK_ENABLE()

;

__HAL_RCC_GPIOH_CLK_ENABLE()

;

__HAL_RCC_GPIOC_CLK_ENABLE()

;

}

/* 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 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(char *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****/