cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 understanding how UART data is transmitted

LPetr.1
Senior

Hello. At the moment I am learning about STM32 and UART. I have configured UART4 and I am sending various data. I have connect logic analyzer on RX and TX pins are I am monitoring the signal.

I send modbus write holding register message via the UART with the following function:

The data I am sending:

0x01 0x06 0x0004 0x1234 0x7CC5

uint8_t MODBUS_write_holding(uint8_t slave_id,uint16_t start_address,uint16_t value){
 
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 1); // ENABLE THE TRANSCEIVER
 
 
 
	uint8_t message_buffer[20];
 
	message_buffer[0] = slave_id;
 
	message_buffer[1] = WRITE_SINGLE_HOLDING;
 
	message_buffer[2] =  ((uint16_t)start_address >> 8) & 0xFF;
 
	message_buffer[3] = ((uint16_t)start_address >> 0) & 0xFF;
 
	message_buffer[4] =  ((uint16_t)value >> 8) & 0xFF;
 
	message_buffer[5] = ((uint16_t)value >> 0) & 0xFF;
 
	uint16_t crc = MODBUS_CRC16_v3(message_buffer,6);
 
	message_buffer[6] = ((uint16_t)crc >> 0) & 0xFF;
 
	message_buffer[7] = ((uint16_t)crc >> 8) & 0xFF;
 
/*
 
	for(int i = 0; i <8;i++){
 
 printf("message_buffer[%i]=%02x \n",i,message_buffer[i]);
 
	}
 
	printf("CRC calculated = %02x \n",crc);
 
*/
 
	HAL_UART_Transmit(&huart4, message_buffer, 8, 10);
 
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, 0); // DISABLE THE TRANSCEIVER
 
	return 0;
 
}

 I call the fucntion in my main.c:

MODBUS_write_holding(1,4,0x1234);

When I monitor the signal on the signal analyzer, I see the following:

0693W00000QNMQGQA5.png 

I have a silly question:

From what I understand, by default, the data in STM32 UART is being transmitted LSB first. However, what I am struggling to understand is why do I need to read bits in reverse, for example:

If I read bits from left to right I get the following:

1000 0000 +stop bit

0110 0000 +stop bit

etc....

While in reality I am sending:

0000 0001 +stop bit

0000 0110 +stop bit

1 ACCEPTED SOLUTION

Accepted Solutions

The 1 bit is the LSB of your data byte and it is sent first on the line in temporal order.

hth

KnarfB

View solution in original post

8 REPLIES 8
PHolt.1
Senior II

The data coming out of a micro is intended for an inverting transceiver chip.

The LSB comes out first, after the start bit. After data, 1 or more stop bits.

Thanks for a quick response. Could you clarify a little bit? In my project, the UART RX and TX are connected to MODBUS transceiver chip. Does that have any effect?

Can you clarify what you mean about LSB comes out first after the start bit?

KnarfB
Principal III

Serial line transmission is traditionally LSB first. See 8052 UART chip and before. Bit order can be reversed in many (all?) STM32 chips.

hth

KnarfB

Hello. I understand that is LSB first but I dont fully understand why the bit order is reversed. If I send 1 byte 0x01 over the UART and monitor it on the serial analyzer and read it left to right, I will read it as 0x80. If I read it from right to left I can now read it as 0x01. That part I am confused about :(

0693W00000QNMewQAH.png

I think I understand it now.. I totally misunderstood how data is transmitted over LSB.. If I look at the 8 bit value 0000 0001, the 1 is MSB and the very left zero is LSB. Since data is sent LSB first, the 0 gets sent first over the serial and the 1 is sent last since it is MSB

The 1 bit is the LSB of your data byte and it is sent first on the line in temporal order.

hth

KnarfB

You struggle to understand it, because we all learned that numbers are written with the least significant digit at the right, i.e. they are effectively written most-significant-digit-first. In other words, MSB-first seems "natural" to us.

JW

Yes that is exactly what happened. Now I understand. Thank you a lot