STM32 understanding how UART data is transmitted
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:
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