High Speed Serial

Post here first, or if you can't find a relevant section!
DrBanana
Posts: 29
Joined: Thu Jun 10, 2021 3:02 pm

High Speed Serial

Post by DrBanana »

I'm required to use 2.5M speed of Serial and when I use logic analyzer to check its output. It doesn't seem like its working at all. Whats the max speed of Serial STM32F103RE can handle?




Using following to set clock, having 8Mhz crystal.

Code: Select all

extern "C" void SystemClock_Config(void) {

	RCC_OscInitTypeDef RCC_OscInitStruct = {};
	RCC_ClkInitTypeDef RCC_ClkInitStruct = {};

	/** Initializes the RCC Oscillators according to the specified parameters
	* in the RCC_OscInitTypeDef structure.
	*/
	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
	RCC_OscInitStruct.HSEState = RCC_HSE_ON;
	RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
	RCC_OscInitStruct.HSIState = RCC_HSI_OFF;//changed FROM ON
	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
	RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
	if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
	{
		Error_Handler();
	}

	/** Initializes the CPU, AHB and APB buses clocks
	*/
	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
		| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
	{
		Error_Handler();
	}
}
ag123
Posts: 1898
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

Re: High Speed Serial

Post by ag123 »

'highest' speed serial is usb (CDC) serial with a maximum bandwidth of 12 Mbps (1.5 MB/s), and that is multiplexed by the number of usb devices you have (e.g. keyboard, mice, hubs (internal ones count too) with each having a 1 ms slice in turn polled by the *host* (that's how usb works)
what is more common is 1.5 Mbps observed with usb (cdc) serial

for anything more than that, you'd need to try things with stm32f405 etc with an external usb2.0 high speed ulpi transducer 480 Mbps, not directly supported out of stm32duino
DrBanana
Posts: 29
Joined: Thu Jun 10, 2021 3:02 pm

Re: High Speed Serial

Post by DrBanana »

When using

Code: Select all

Serial3.begin(2500000, SERIAL_8N1);
it writes in 4385 baud rate

Seems like some sort of overflow

Code: Select all

Serial3.begin(1500000, SERIAL_8N1);
1.5M working good

Code: Select all

Serial3.begin(2000000, SERIAL_8N1);
2.0M is having some issues of randomly writing correct values and wrong values
DrBanana
Posts: 29
Joined: Thu Jun 10, 2021 3:02 pm

Re: High Speed Serial

Post by DrBanana »

ag123 wrote: Sat Dec 28, 2024 4:44 am for anything more than that, you'd need to try things with stm32f405 etc with an external usb2.0 high speed ulpi transducer 480 Mbps, not directly supported out of stm32duino
I need it for rs485
fpiSTM
Posts: 1944
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 108
Location: Le Mans
Contact:

Re: High Speed Serial

Post by fpiSTM »

Based on datasheet:
The USART1 interface is able to communicate at speeds of up to 4.5 Mbit/s. The other
available interfaces communicate at up to 2.25 Mbit/s.
So Serial3 can't be used at 2.5Mbit/s.
Moreover It is "up to" and I guess it depends of the implementation, DMA usage and input clock have to be as fast as possible.
ag123
Posts: 1898
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

Re: High Speed Serial

Post by ag123 »

DrBanana wrote: Sat Dec 28, 2024 5:20 am
ag123 wrote: Sat Dec 28, 2024 4:44 am for anything more than that, you'd need to try things with stm32f405 etc with an external usb2.0 high speed ulpi transducer 480 Mbps, not directly supported out of stm32duino
I need it for rs485
try DMA like @fpiSTM suggested, that won't be in the 'standard' Serial() implementation.
it'd take reviewing the ref manual (e.g. rm0008)
https://www.st.com/resource/en/referenc ... ronics.pdf
and maybe HAL etc.
if you want to use Serial(), you can try updating the h/w registers directly e.g. baud rates etc
but that chances are that interrupts based Serial() can't catch up at high enough speeds.
DrBanana
Posts: 29
Joined: Thu Jun 10, 2021 3:02 pm

Re: High Speed Serial

Post by DrBanana »

I tried STM32F405RG, which suppose to have 5.25 Mbit/s, it also output random crap on serial.

Using following to make it run on 168Mhz

Code: Select all

extern "C" void SystemClock_Config(void) {

	RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
	RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };

	/** Configure the main internal regulator output voltage
	*/
	__HAL_RCC_PWR_CLK_ENABLE();
	__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

	/** Initializes the RCC Oscillators according to the specified parameters
	* in the RCC_OscInitTypeDef structure.
	*/
	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
	RCC_OscInitStruct.HSEState = RCC_HSE_ON;
	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
	RCC_OscInitStruct.PLL.PLLM = 8;
	RCC_OscInitStruct.PLL.PLLN = 336;
	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
	RCC_OscInitStruct.PLL.PLLQ = 4;
	if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
	{
		Error_Handler();
	}

	/** Initializes the CPU, AHB and APB buses clocks
	*/
	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
		| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
	{
		Error_Handler();
	}
}
ag123
Posts: 1898
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

Re: High Speed Serial

Post by ag123 »

uart is sensitive to baud rate differences, both the receiving and transmitting end
i'd suggest try at lower rates first hand. And do remember to change the remote receive/transmitting baud rates accordingly.
if baud rates mismatch, you simply get 'random crap'
ag123
Posts: 1898
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

Re: High Speed Serial

Post by ag123 »

I'm thinking one of those ways to get pretty high serial speeds on RS485 / RS422 is to 'speak' NRZI on the RS485 network.
https://en.wikipedia.org/wiki/Non-return-to-zero

that's actually how usb works

Unfortunately, encoding and decoding nrzi, the decoding requires some clever circuits with D flip flops, XOR gates and possibly PLL for clock recovery.
fpga? expensive maybe
I'm not too sure if it is after all possible to wire USB directly into RS485 ! (curious and interesting thought)
doing nrzi may likely mean extra chips to handle it or some proprietary comms chips. notably manchester encoding (10 base-T), 4b5b (100 base-T) etc
and those would be ethernet, I'd think chips and modules for those are plenty if you decided to use ethernet.

Another way though is to literally use usb (Full speed), you get 12 Mbps, but one of your device needs to play the usb-host.
And that can be done completely within the stm32 family of mcus, e.g. stm32f405 probably can do usb host
the effect of all that multiplexing means that the 12 Mbps is divided by the number of downstream devices that is connected including hubs.

But I'd guess usb is an interesting alternative vs rs485, the cables can probably be pretty long if you use good shielded cables.
DrBanana
Posts: 29
Joined: Thu Jun 10, 2021 3:02 pm

Re: High Speed Serial

Post by DrBanana »

Yes thats exactly what I want at the end. I need to read some data from a sensor thats using RS485 NRZ with 2.5Mhz rate. I cant change baud rate because its hard coded in sensor.

Is there any chip I can use that handles all this ?
Post Reply

Return to “General discussion”