-
Notifications
You must be signed in to change notification settings - Fork 7
Serial IO
Hrisabh Yadav edited this page Nov 14, 2025
·
1 revision
Serial communication interfaces for UART, I2C, and SPI protocols.
typedef enum {
BAUD_RATE_4800, BAUD_RATE_9600, BAUD_RATE_14400,
BAUD_RATE_19200, BAUD_RATE_38400, BAUD_RATE_57600,
BAUD_RATE_115200, BAUD_RATE_128000, BAUD_RATE_256000
} UART_Baud_Rate_e;typedef enum {
UART2 // UART2 port
} UART_Port_e;typedef enum SPImode_s {
MODE0 = 0, // CPOL=0, CPHA=0
MODE1, // CPOL=0, CPHA=1
MODE2, // CPOL=1, CPHA=0
MODE3 // CPOL=1, CPHA=1
} SPImode_t;typedef enum SPIfirst_bit_s {
LSBFIRST = 0, // LSB first
MSBFIRST // MSB first
} SPIfirst_bit_t;-
void Uart_Init(UART_Port_e PORT, UART_Baud_Rate_e BAUD)- Initialize UART
-
uint8_t Uart_Read8(UART_Port_e PORT)- Read 8-bit value -
uint16_t Uart_Read16(UART_Port_e PORT)- Read 16-bit value -
uint32_t Uart_Read32(UART_Port_e PORT)- Read 32-bit value
-
void Uart_Write(UART_Port_e PORT, uint8_t data)- Write single byte -
void Uart_Write(UART_Port_e PORT, const char *str)- Write string -
void Uart_Write(UART_Port_e PORT, uint8_t *data, uint16_t length)- Write byte array
-
bool Uart_rxBytesWaiting(UART_Port_e PORT)- Check if data available -
bool Uart_txBytesFree(UART_Port_e PORT)- Check if space available
-
bool I2C_Read(uint8_t device_add, uint8_t reg, uint8_t &value)- Read single byte -
int16_t I2C_Read(uint8_t device_add, uint8_t reg, uint32_t length, uint8_t *buffer)- Read multiple bytes
-
bool I2C_Write(uint8_t device_add, uint8_t reg, uint8_t data)- Write single byte -
bool I2C_Write(uint8_t device_add, uint8_t reg, uint32_t length, uint8_t *data)- Write multiple bytes
-
void SPI_Init()- Initialize with default settings -
void SPI_Init(SPImode_t mode, uint16_t speed, SPIfirst_bit_t bit)- Initialize with custom settings
-
void SPI_Enable(void)- Enable SPI interface -
void SPI_Disable(void)- Disable SPI interface
-
uint8_t SPI_Read(uint8_t register_address)- Read single byte -
void SPI_Read(uint8_t register_address, int16_t length, uint8_t *buffer)- Read multiple bytes -
void SPI_Write(uint8_t register_address, uint8_t data)- Write single byte
// UART communication
Uart_Init(UART2, BAUD_RATE_115200);
Uart_Write(UART2, "Hello World\n");
if (Uart_rxBytesWaiting(UART2)) {
uint8_t data = Uart_Read8(UART2);
}
// I2C sensor communication
const uint8_t SENSOR_ADDR = 0x68;
uint8_t sensorID;
if (I2C_Read(SENSOR_ADDR, 0x75, sensorID)) {
Monitor_Print("Sensor ID: ", sensorID);
}
uint8_t configData = 0x01;
I2C_Write(SENSOR_ADDR, 0x6B, configData);
// SPI communication
SPI_Init(MODE0, 1000000, MSBFIRST);
SPI_Enable();
uint8_t deviceID = SPI_Read(0x0F);
SPI_Write(0x20, 0x8F);
SPI_Disable();MagisV2 © 2025 Drona Aviation | Licensed under GPL-3.0 | Report Issues