Call to HAL_UART_Transmit() not sending the expected amount of data?
I have a strange issue here. I'm trying to do a short, blocking, UART transmission.
I have a 23 byte uint8_t array ( MSG ) that I construct before calling the HAL_UART_Transmit() function to send the data. (SLAVE_MSG_SIZE = 23, UART is a pointer to the UART4 object)
HAL_UART_Transmit(UART, &MSG, SLAVE_MSG_SIZE, 1);
The problem is that it never sends just 23 bytes, its sends more, sometimes less, but almost always more. Most I've seen is around 46 bytes, which is close to 2x, which may be a clue to what is going on.
(The receiver side can handle extra bytes at the end because it just doesn't look at them, but when the TX packet is too short, the 2 CRC bytes are absent and the CRC check fails.)
The strange thing is that if I manually set the "data size" parameter in the HAL call to 15 bytes or less, I get the expected, truncated data of the correct length. However, if the size argument is >15 bytes, the amount of transmitted data is always more than the expected value.
My code is the following:
void Slave_Transmit(struct bhg5_slave_message MSG_Content, UART_HandleTypeDef *UART){
uint8_t MSG[SLAVE_MSG_SIZE];
//build message packet
MSG[0] = 0xAA;
MSG[1] = MSG_Content.address;
MSG[2] = MSG_Content.node_address;
MSG[3] = MSG_Content.protocol_version;
MSG[4] = MSG_Content.message_type;
MSG[5] = (MSG_Content.payload1) & 0xFF; //low payload byte
MSG[6] = (MSG_Content.payload1 >> 8) & 0xFF; //...shift right and mask
MSG[7] = (MSG_Content.payload1 >> 16) & 0xFF; //...shift right and mask
MSG[8] = (MSG_Content.payload1 >> 24) & 0xFF; //high payload byte
MSG[9] = (MSG_Content.payload2) & 0xFF; //low payload byte
MSG[10] = (MSG_Content.payload2 >> 8) & 0xFF; //...shift right and mask
MSG[11] = (MSG_Content.payload2 >> 16) & 0xFF; //...shift right and mask
MSG[12] = (MSG_Content.payload2 >> 24) & 0xFF; //high payload byte
MSG[13] = (MSG_Content.payload3) & 0xFF; //low payload byte
MSG[14] = (MSG_Content.payload3 >> 8) & 0xFF; //...shift right and mask
MSG[15] = (MSG_Content.payload3 >> 16) & 0xFF; //...shift right and mask
MSG[16] = (MSG_Content.payload3 >> 24) & 0xFF; //high payload byte
MSG[17] = (MSG_Content.payload4) & 0xFF; //low payload byte
MSG[18] = (MSG_Content.payload4 >> 8) & 0xFF; //...shift right and mask
MSG[19] = (MSG_Content.payload4 >> 16) & 0xFF; //...shift right and mask
MSG[20] = (MSG_Content.payload4 >> 24) & 0xFF; //high payload byte
// compute CRC to place into last 2 bytes
uint32_t calculatedCRC = HAL_CRC_Calculate(&hcrc, MSG, 21);
calculatedCRC = calculatedCRC & 0xFFFF; // only lower 2 bytes will be used
MSG[21] = calculatedCRC & 0xFF; //low CRC byte
MSG[22] = (calculatedCRC >> 8) & 0xFF; //high CRC byte
HAL_UART_Transmit(UART, &MSG, SLAVE_MSG_SIZE, 1);// Sending in normal mode
//HAL_UART_Transmit(UART, &MSG, 16, 1);// Sending in normal mode
}The UART initialization create by HAL is the following:
static void MX_UART4_Init(void)
{
/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 230400;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 */
/* USER CODE END UART4_Init 2 */
}What could be going on here?
Any help appreciated.
