Problem using HAL_UART_Transmit_IT
I'm using the HAL_UART_Transmit_IT procedure on a STM32G431 for the LPUART on a 115200 baud communication to a Putty terminal. The MCU is also running ST Motor code.
I have some simple 3 character commands that either set variables in the code or read back variables. The communication is fairly robust when the motor is not running but is very bad when it is running.
The interface code reads a single character at a time in the callback HAL_UART_RxCpltCallback and puts it in a Rxbuffer. If the character is invalid the buffer is cleared. If a CR is received the command is processed and data is transmitted back to Putty using the HAL_UART_Transmit_IT procedure. I can break on the code just prior to transmitting the message and the data is correct but when the HAL_UART_Transmit_IT is executed the data coming back is very corrupted.
This is the callback:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint8_t Rx_Char;
uint8_t tmp;
tmp = LPUART1_rxBuffer[0]; // uart receives 1 byte
rx_buffer[Rx_Byte] = tmp;
Rx_Char = tmp;
Rx_Byte++; // # of accumulated bytes received for current command
if ( (Rx_Char == ESC) || (Rx_Byte > Nbytes)) // clear command state machine immediately
{
Rx_Byte = 0;
}
else if (Rx_Char == CR) // go process command
{
Rx_Byte = 0;
processUserCmnd();
}
else if ( Rx_Char == 0x7F ) // handle backspace
{
Rx_Byte--;
Rx_Byte--;
rx_buffer[Rx_Byte] = 0;
}
HAL_UART_Receive_IT( &hlpuart1, LPUART1_rxBuffer, 1 ); // ready next byte
}The processUserCommand decodes the command in the rxBuffer and executes the command. As example, after decoding 'SPD' it will send back the motor speed.
case 7: /* SPD, read speed */
/* speed in #SPEED_UNIT's, _001HZ */
speed = MC_GetMecSpeedAverageMotor1();
sendSPEED(speed);
break;void sendSPEED(int16_t spd)
{
// send speed in units of 0.001Hz mechanical
if( spd > 32767 ) spd = 32767;
// HZ_001 from ST is 0.01Hz
spd = (spd << 3) + (spd << 1); // 10x
char msg[15];
sprintf(msg, "speed %5u \n", spd); // convert int16_t to ascii, legal itoa
char v = CR;
strncat(msg, &v, 1);
HAL_UART_Transmit_IT(&hlpuart1, (uint8_t *) msg, 14);
// --- done
}What will happen when at break at line 13 above the value of 'msg' is okay but executing HAL_UART_Transmit_IT, the data received by Putty is bad. Again, this is much worse when the code is running the motor and also, I tried 9600 baud and that was bad as well.
I know I must be missing something basic... ? Please offer some advice,
