2024-11-27 04:11 AM
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.
Solved! Go to Solution.
2024-11-28 01:20 AM - edited 2024-11-28 01:22 AM
There is an issue in your HW:
VIO pin of the transceiver is connected to 5V.
Going to the datasheet:
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.
2024-11-27 04:46 AM - edited 2024-11-27 05:15 AM
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?:
transceivers+ terminating resistors.
If you are using transceiver, could you please provide its part number?
2024-11-27 11:43 PM
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();
}
2024-11-28 12:26 AM
Could you please share your schematics?
2024-11-28 01:11 AM
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.
2024-11-28 01:20 AM - edited 2024-11-28 01:22 AM
There is an issue in your HW:
VIO pin of the transceiver is connected to 5V.
Going to the datasheet:
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.