Hi guys, I've got couple of questions about using half duplex UART. I want to interface STM32VL board with AX-12 servo, and I've got the transimt part working, but receive part isn't chooching as it should.
For the moment I'm using STM's HAL. As I've managed to gather from various examples, by default, the uart is in TX mode. In order to get it to RX, enabling RX with HAL is with HAL_HalfDuplex_EnableTransmitter().
So the function for reading data from servos goes like this, ignore the error handler, it isn't implemented yet
uint16_t AX12_read_data(uint8_t id){
uint8_t rxBuffer[7]={0};
uint8_t length=0;
uint8_t error=0;
uint16_t return_value;
HAL_HalfDuplex_EnableReceiver(&huart2);
HAL_UART_Receive(&huart2, (uint8_t*) rxBuffer, sizeof(rxBuffer), 200);
if (rxBuffer[0] == 0xFF && rxBuffer[1] == 0xFF){
}
else //Error_Handler();
length = rxBuffer[3]-2; //ax12 magic, see manual
error = rxBuffer[4];
if (error != 0){}
//Error_Handler();
else if (length == 0){}
//Error_Handler();
else {
if (length > 1){
return_value = (rxBuffer[6] << 8) + rxBuffer[5];
return return_value;
}
else {
return_value = rxBuffer[5];
return return_value;
}
}
HAL_HalfDuplex_EnableTransmitter(&huart2);
return 0;
}
This function is called right after transmitting some command.
Problem is the received data doesn't seem right, first is 255 which is right, but receiving some random numbers after that, usually zeroes.
Am I missing something obvious, and what exactly does timeout parameter in uart send and receive do?