cancel
Showing results for 
Search instead for 
Did you mean: 

Starting CAN Bus on STM32F302K8

Rey
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

5 REPLIES 5
SofLit
ST Employee

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?

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Rey
Associate II

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

}

 

Could you please share your schematics? 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Rey
Associate II

I am not sure why do you need the schematic as there is not much on the board. I am referring to the coding problem. I have three of the Identical device connected in the bus, one acts as a master and two are slave. I would like to emphasize that 120R resistor is not visible on schematic diagram but is placed on the bus termination.

Rey_0-1732784850765.png

 

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.