I'm working on small project based on STM32 and a custom board. I run into a wall trying to setup a working telemetry from STM to transmitter (FS-I6X). I wonder if anybody can guide me to the proper solution. The board uses single wire for receiving and transmitting data (half-duplex).
So far i made a properly working receiver driving 4 servos, so receiving data via IBus is working fine, as well as parsing GPS data.
My code below:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
if(huart->Instance == USART1) {
if (ibus_read(&ibus_data_struct, ibus_channels, ibus_data[0]) == 0) {
uint32_t currTick = HAL_GetTick();
if (lastTelemetryTime + 10 <= currTick) {
ibus_send_telemetry(huart);
lastTelemetryTime = HAL_GetTick();
}
// serve servos
}
HAL_UART_Receive_IT(&huart1, ibus_data, ibus_data_size);
}
if(huart->Instance == USART3 || huart->Instance == USART4) {
// GPS stuff...
}
}
void ibus_send_telemetry(UART_HandleTypeDef *huart) {
uint8_t payload[1];
payload[0] = packet_type;
uint8_t header[3];
header[0] = 0x20; // Start byte
header[1] = 0x02; // Payload length
header[2] = packet_type; // Packet type
uint8_t checksum = ~(header[0] + header[1] + header[2] + payload[0]) & 0xFF;
uint8_t packet[sizeof(header) + sizeof(payload) + sizeof(checksum)];
memcpy(packet, header, sizeof(header));
memcpy(packet + sizeof(header), payload, sizeof(payload));
packet[sizeof(header) + sizeof(payload)] = checksum;
while(HAL_HalfDuplex_EnableTransmitter(huart) != HAL_OK);
HAL_UART_Transmit(huart, (uint8_t *) packet, sizeof(packet), 300);
HAL_HalfDuplex_EnableReceiver(huart);
// an act of desperation - packet_type is set to 0x00
// at the beginning and incrementing every loop to avoid wrong packet type and value
packet_type = packet_type + 0x01;
}
Any help would be appreciated!
I've tried googling proper solution, without success. Have no idea, why it isn't working.