Skip to main content
Associate III
July 23, 2024
Solved

Switching from FDCAN mode to UART mode

  • July 23, 2024
  • 9 replies
  • 2718 views

Hi,

 i am using STM32H753z  i try to switching FDCAN GPIO is PD0, PD1  to UART tx and rx (same pins)
now i changed FDCAN to UART tx side  is fine but in Rx side  i face issue ( no data is receiving to receive buffer).

 

 

#define CAN_AFR 0x99
#define UART_AFR 0x88
#define CHANGING_PORT 0xFFFFFFFA


void switch_UART_TO_CAN(void)
{
	if(HAL_UART_DeInit(&huart4) != HAL_OK)
	{
		Error_Handler();
	}
	GPIOD->AFR[0] = CAN_AFR;
	GPIOD->MODER=CHANGING_PORT;
	MX_FDCAN1_Init(&hfdcan1);


}

void switch_CAN_TO_UART(void)
{
	if(HAL_FDCAN_DeInit(&hfdcan1)!= HAL_OK)
	{
		Error_Handler();
	}
	GPIOD->MODER=CHANGING_PORT;
	GPIOD->AFR[0] = UART_AFR;
	HAL_UART_Init(&huart4);
	MX_UART4_Init();
}

 

 

 

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

It's a bad idea to share the same pins between UART and FDCAN. Externally, the HW is not the same: CAN transceiver + a RS232 driver or STlink virtual comport.

So need to separate the pins.

9 replies

mƎALLEm
ST Technical Moderator
July 24, 2024

Hello,

You share USART4 and FDCAN1 pins, this supposes that FDCAN1 is seeing USART HW on its pins where you are switching to it! which is not recommended. You need to have free pins for FDCAN.

Which board are you using? Could you please share your schematics?

Also you have two UART4 init call which I couldn't understand:

	HAL_UART_Init(&huart4);
	MX_UART4_Init();

 

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Naresh_Author
Associate III
July 24, 2024
Method 1:
 void switch_UART_TO_CAN(void)
{
	if(HAL_UART_DeInit(&huart4) != HAL_OK)
	{
		Error_Handler();
	}
	GPIOD->AFR[0] = CAN_AFR;
	GPIOD->MODER=CHANGING_PORT;
	MX_FDCAN1_Init(&hfdcan1);


}
//
void switch_CAN_TO_UART(void)
{
	if(HAL_FDCAN_DeInit(&hfdcan1)!= HAL_OK)
	{
		Error_Handler();
	}
	GPIOD->MODER=CHANGING_PORT;

	GPIOD->AFR[0] = UART_AFR;
	MX_UART4_Init();
}

Method 2:
void Switch_CAN_to_UART(void)
{
	// Deinitialize FDCAN
	if (HAL_FDCAN_DeInit(&hfdcan1) != HAL_OK)
	{
		Error_Handler();
	}
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
	HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
	// Initialize UART
	MX_UART4_Init();
}

void Switch_UART_to_CAN(void)
{
	// Deinitialize UART
	if (HAL_UART_DeInit(&huart4) != HAL_OK)
	{
		Error_Handler();
	}

	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF9_FDCAN1;
	HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
	// Initialize CAN
	MX_FDCAN1_Init();
}

i am transfer is happened in UART4 but in receive side HAL_UART_Receive()   thought time out error, issue

  

mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
July 24, 2024

It's a bad idea to share the same pins between UART and FDCAN. Externally, the HW is not the same: CAN transceiver + a RS232 driver or STlink virtual comport.

So need to separate the pins.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
LCE
Principal II
July 24, 2024

It's a little bit sad that nobody explains why this is not working...

The probable cause is a problem not related to the STM32:

When connecting 2 low impedance / push-pull outputs, one output "shorts" the other one.

This might only work with open-drain / open-collector outputs.

 

mƎALLEm
ST Technical Moderator
July 24, 2024

I think there is no need for this statement:


@LCE wrote:

It's a little bit sad that nobody explains why this is not working...

 

It's not a probable cause but it's definitely not related to the STM32.


@LCE wrote:

The probable cause is a problem not related to the STM32:

When connecting 2 low impedance / push-pull outputs, one output "shorts" the other one.

This might only work with open-drain / open-collector outputs.

 

Moreover, even in the inverse case (no output shorts), there will be an issue with CAN_Tx & UART_Rx / CAN_Rx & UART_Tx  sharing the same pins because the external transceiver/driver will force its output at a level that will disturb the current peripheral not using the driver/transceiver.

Conclusion: in both cases it's a bad idea to share the pins as stated above. So need to MUX them by an external HW.

 

 

 

 

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