This UART communication library provides a reliable way to exchange data between two STM32 microcontrollers using UART. The library utilizes a custom protocol that includes:
- Start byte: Marks the beginning of the frame.
- Stop byte: Marks the end of the frame.
- Checksum: Ensures data integrity.
- Variable-length data: Supports sending frames of different sizes.
- ACK/NACK: Mechanism to confirm successful reception or request retransmission in case of errors.
- Start/Stop Bytes: Each frame starts with a special byte and ends with another, clearly marking the frame's boundaries.
- Checksum: Verifies the integrity of transmitted data.
- Variable-length Data: Supports different data sizes in the frames.
- ACK/NACK: After receiving a frame, the receiver sends back an ACK (acknowledgment) or a NACK (negative acknowledgment).
UART_Protocol_Transmit_ByteByByte
: Sends a data frame byte by byte.UART_Protocol_Receive_IT
: Receives data asynchronously via UART interrupts.- Checksum Calculation: Used to validate the data integrity.
Before using the library, initialize the UART handle (e.g., huart2
) in your main program.
You can send data using the UART_Protocol_Transmit_ByteByByte
function:
uint8_t data[] = {0x01, 0x02, 0x03}; // Example data to send
UART_Protocol_Transmit_ByteByByte(&uartProtocolHandle, data, sizeof(data));
To receive data, use the HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
function inside the UART interrupt handler. The received data is handled asynchronously.
Example:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart2 ){
UART_Protocol_Receive_IT(&uartProtocol);
HAL_UART_Receive_IT(uartProtocol.huart, &uartProtocol.rxByte, 1);
}
}
After receiving the data, check if it's ready using the isDataReady
flag:
if (uartProtocolHandle.isDataReady) {
// Process the received data
uartProtocolHandle.isDataReady = 0; // Reset the flag after processing
}