UART API
The UART API provides serial communication for debugging, firmware updates, and Bluetooth module communication.
System configuration
Available modules: 4 UART modules (UART1-UART4)
Baud rate presets:
UART_BAUD_9600 - 9600 baud (I-Bus communication)
UART_BAUD_115200 - 115200 baud (debugging, firmware updates)
Parity options:
UART_PARITY_NONE - No parity
UART_PARITY_EVEN - Even parity (I-Bus uses this)
UART_PARITY_ODD - Odd parity (bootloader uses this)
Data structures
UART_t
UART module instance.
Hardware register pointer
Initialization
UARTInit
Initializes a UART module.
UART_t UARTInit(
uint8_t uartModule,
uint8_t rxPriority,
uint8_t txPriority,
uint8_t baudRate,
uint8_t parity,
uint8_t rxPin,
uint8_t txPin
);
RX interrupt priority (0-7)
TX interrupt priority (0-7)
Baud rate divisor (use UART_BAUD_* constants)
Parity mode (UART_PARITY_*)
Initialized UART instance
Example - I-Bus UART:
UART_t ibusUart = UARTInit(
SYSTEM_UART_IBUS_MODULE, // UART1
0x06, // RX priority
0x04, // TX priority
UART_BAUD_9600, // 9600 baud
UART_PARITY_EVEN, // Even parity
SYSTEM_UART_IBUS_RX_PIN, // RX pin
SYSTEM_UART_IBUS_TX_PIN // TX pin
);
Example - Debug UART:
UART_t debugUart = UARTInit(
SYSTEM_UART_DEBUG_MODULE, // UART2
0x01, // RX priority
0x01, // TX priority
UART_BAUD_115200, // 115200 baud
UART_PARITY_NONE, // No parity
SYSTEM_UART_DEBUG_RX_PIN, // RX pin
SYSTEM_UART_DEBUG_TX_PIN // TX pin
);
Module management
UARTAddModuleHandler
Registers a UART instance for interrupt handling.
void UARTAddModuleHandler(UART_t *uart);
UARTGetModuleHandler
Retrieves UART instance by module number.
UART_t * UARTGetModuleHandler(uint8_t moduleIndex);
Pointer to UART instance, or NULL if not registered
UARTDestroy
Disables and destroys a UART module.
void UARTDestroy(uint8_t moduleIndex);
Transmit functions
UARTSendChar
Sends a single character.
void UARTSendChar(UART_t *uart, uint8_t data);
Example:
UARTSendChar(&debugUart, 'A');
UARTSendString
Sends a null-terminated string.
void UARTSendString(UART_t *uart, char *string);
Null-terminated string to send
Example:
UARTSendString(&debugUart, "BlueBus v1.0\r\n");
UARTSendData
Sends binary data.
void UARTSendData(
UART_t *uart,
uint8_t *data,
uint16_t length
);
Example:
uint8_t packet[] = {0x68, 0x05, 0x18, 0x38, 0x00, 0x00, 0x4D};
UARTSendData(&ibusUart, packet, 7);
Receive functions
Reading from RX queue
Received data is stored in the rxQueue buffer. Use CharQueue functions to read:
// Check if data available
if (uart.rxQueue.size > 0) {
// Read one character
uint8_t data = CharQueueNext(&uart.rxQueue);
}
UARTRXQueueReset
Clears the receive queue.
void UARTRXQueueReset(UART_t *uart);
Example:
UARTRXQueueReset(&debugUart);
Error handling
Error flags
#define UART_ERR_GERR 0x1 // General error
#define UART_ERR_OERR 0x2 // Overrun error
#define UART_ERR_FERR 0x4 // Framing error
#define UART_ERR_PERR 0x8 // Parity error
UARTReportErrors
Logs any UART errors.
void UARTReportErrors(UART_t *uart);
Example:
if (uart.rxError != 0) {
UARTReportErrors(&uart);
}
Usage patterns
I-Bus communication
// Initialize UART for I-Bus (9600 baud, even parity)
UART_t ibusUart = UARTInit(
SYSTEM_UART_IBUS_MODULE,
0x06, 0x04,
UART_BAUD_9600,
UART_PARITY_EVEN,
SYSTEM_UART_IBUS_RX_PIN,
SYSTEM_UART_IBUS_TX_PIN
);
// Send I-Bus message
uint8_t message[] = {0x68, 0x05, 0x18, 0x38, 0x00, 0x00, 0x4D};
UARTSendData(&ibusUart, message, sizeof(message));
Debug output
// Initialize debug UART (115200 baud, no parity)
UART_t debugUart = UARTInit(
SYSTEM_UART_DEBUG_MODULE,
0x01, 0x01,
UART_BAUD_115200,
UART_PARITY_NONE,
SYSTEM_UART_DEBUG_RX_PIN,
SYSTEM_UART_DEBUG_TX_PIN
);
// Send debug message
UARTSendString(&debugUart, "System initialized\r\n");
Bluetooth module communication
// Initialize UART for BM83 Bluetooth module
UART_t btUart = UARTInit(
SYSTEM_UART_BT_MODULE,
0x05, 0x05,
UART_BAUD_115200,
UART_PARITY_NONE,
SYSTEM_UART_BT_RX_PIN,
SYSTEM_UART_BT_TX_PIN
);
// Send command to Bluetooth module
uint8_t cmd[] = {0xAA, 0x00, 0x03, 0x00, 0x01};
UARTSendData(&btUart, cmd, sizeof(cmd));
Constants
#define UART_MODULES_COUNT 4 // Number of UART modules
#define UART_BAUD_115200 34 // 115200 baud divisor
#define UART_BAUD_9600 103 // 9600 baud divisor
See also