Skip to main content
Explorer
November 27, 2024
Solved

Starting CAN Bus on STM32F302K8

  • November 27, 2024
  • 1 reply
  • 1609 views

Hi,

I am working on STM32F302K8 and would like to use it's CAN Bus to communicate between devices both as master and slave It is my first time using STM32CubeIDE and the HAL library and implementing CAN Bus on my work. I did use some samples scattered around the internet but I couldn't get the CAN bus working not even finishing the initialization.  I have attached my sample program with bare minimum just USART to check the sequence of the init() function and couple of GPIO outputs for the LEDs.

    This topic has been closed for replies.
    Best answer by mƎALLEm

    There is an issue in your HW:

    VIO pin of the transceiver is connected to 5V.

    Going to the datasheet: 

    SofLit_0-1732785517451.png

    VIHmin = 0.7 x 5V = 3.5V < VDD.

    You need to connect transceiver VIO pin to VDD (3.3V) not to 5V.

    Please don't forget the terminating resistors of 120ohms on CAN_H and CAN_L.

    1 reply

    Technical Moderator
    November 27, 2024

    Hello @Rey and welcome to the community,

    Before going ahead with analysis, could you please tell about your CAN hardware configuration?

    1- How are you connecting the two devices? are you using CAN transceivers?

    2- Are you adopting this exact structure?: SofLit_0-1729078448084.png

    transceivers+ terminating resistors.

    If you are using transceiver, could you please provide its part number?

     

    ReyAuthor
    Explorer
    November 28, 2024

    Thanks for the response. It is the same structure as the picture you have provided. Aside from the confusing part where CAN TX and RX must be connected to the transceiver's TX and RX which should not be confused with a USART connection. Everything is connected properly. I am using a NXP CAN transceiver the part number is TJA1442ATK mounted to a DIP8 adapter.
    I also have a oscilloscope to probe the TX and RX line and the CAN_L and CAN_H line but there is no signal to be found.

    In terms of coding, my problem is when it calls the HAL_CAN_Start(&hcan) it trigger an Error. I have added a few line shown below to know where it stops as the Error_Handler() goes on a empty while(1) loop.

     
    if(HAL_CAN_Start(&hcan) != HAL_OK)
    
    {
    
    char strbuf[20] = "CAN NOT STARTED... ";
    
    HAL_UART_Transmit(&huart1, (uint8_t *)strbuf, strlen(strbuf), HAL_MAX_DELAY);
    
    CAN_ErrorState();
    
    Error_Handler();
    
    }
    
    

     

    Technical Moderator
    November 28, 2024

    Could you please share your schematics?