cancel
Showing results for 
Search instead for 
Did you mean: 

need help designing a car CAN sniffer using STM32 446RE and MCP2551

Apakp.1
Associate II

i have written a simple program that should send "NO MESSAGE!" to the terminal if there are no CAN messages or "A NEW MESSAGE ARRIVED!" and then the CAN message itself through UART2. My idea is that since all car sensors are connected to the same CAN bus then i should be able to read all the messages live if i set all the CAN filter bits to 0 , i don't know if that's what i must do or if i just have to send requests and wait for responses.

what i expected to see were thousands of CAN messages as soon as i turn on the engine , instead what i got was the "NO MESSAGE" over and over again and since so many things could be wrong i have taken pictures of every hardware part to ask you guys where i must have made a mistake

here are the relevant parts of the code:

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_USART2_UART_Init();
  MX_CAN1_Init();
  /* USER CODE BEGIN 2 */
  CAN1_Config();
  if(HAL_CAN_Start(&hcan1) != HAL_OK)
    {
  	  Error_Handler();
    }
  CAN1_Rx();
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

then CAN1_CONFIG() is supposed to set the filters to none:

void CAN1_Config(void)
{
  CAN_FilterTypeDef CAN1_CONF;
 
  CAN1_CONF.FilterActivation = ENABLE;
  CAN1_CONF.FilterBank = 0;
  CAN1_CONF.FilterFIFOAssignment = CAN_RX_FIFO0;
  CAN1_CONF.FilterIdHigh = 0X0000;
  CAN1_CONF.FilterIdLow = 0X0000;
  CAN1_CONF.FilterMaskIdHigh = 0X0000;
  CAN1_CONF.FilterMaskIdLow = 0X0000;
  CAN1_CONF.FilterMode = CAN_FILTERMODE_IDMASK;
  CAN1_CONF.FilterScale = CAN_FILTERSCALE_32BIT;
 
  if(HAL_CAN_ConfigFilter(&hcan1, &CAN1_CONF) != HAL_OK)
  {
	  Error_Handler();
  }
 
}

then there is CAN1 init which I set using an online calculator based on the clock tree who's picture i will attach to the end of this post

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 = 5;
  hcan1.Init.Mode = CAN_MODE_SILENT;
  hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
  hcan1.Init.TimeSeg1 = CAN_BS1_15TQ;
  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 */
 
}

finally this is the the RX function:

void CAN1_Rx(void)
{
	uint8_t CAN_Rx_Msg[5];
	CAN_RxHeaderTypeDef CAN1_RxHeader;
    while(1)
    {
    	if(! HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0))
    	{
    		HAL_UART_Transmit(&huart2, (unsigned char *)"NO MESSEGE!\n", strlen("NO MESSEGE!\n"), HAL_MAX_DELAY);
    		HAL_Delay(1500);
    	}
 
    	else
    	{
    		HAL_UART_Transmit(&huart2, (unsigned char *)"MESSEGE ARRIVED!\n", strlen("MESSEGE ARRIVED!\n"), HAL_MAX_DELAY);
    		HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &CAN1_RxHeader, CAN_Rx_Msg);
    		HAL_UART_Transmit(&huart2, (unsigned char *)CAN_Rx_Msg, sizeof(CAN_Rx_Msg), HAL_MAX_DELAY);
    	}
    }
 
}

this is the software part , i don't think there is anything wrong with it , the problem is that stm32 LOOPBACK mode is internally connected and even though i can send and receive can messages successfully using loopback i cannot be sure if the mcp2551 ic is working properly. now i will show you pictures of my car's OBD port(i think it supports CAN since it has CANL and CANH wires) also i will attack pictures of the two types of wires i have.not sure which one i should use one is a telephone wire(I haven't used them yet) the other is the ordinary jumper wire type:0693W000008yzJLQAY.jpg0693W000008yzJ1QAI.jpg0693W000008yzIwQAI.png0693W000008yzImQAI.png0693W000008yzHQQAY.jpg0693W000008yzIcQAI.jpg0693W000008yzISQAY.jpg0693W000008yzIDQAY.jpg0693W000008yzI3QAI.jpg:

2 REPLIES 2

Do you have the bit/baud rate correct?

Would suggest scoping the signals on the connector and being sure there is a signal present.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

i have read that there are two standard baud rates supported, this clock setting is based on the 500 kbit/sec. i don't know which baud rate is supported by my car's ECU so maybe i should change the init settings after some time. i have a logic analyzer but it only has female connectors i don't know if it supports analog signals , will try to connect it somehow and see.