1 #include <twr_sc16is740.h> 3 #define _TWR_SC16IS740_CRYSTCAL_FREQ (13560000UL) 4 #define _TWR_SC16IS740_FIFO_SIZE 64 5 #define _TWR_SC16IS740_REG_RHR 0x00 6 #define _TWR_SC16IS740_REG_THR 0x00 7 #define _TWR_SC16IS740_REG_IER 0x01 << 3 8 #define _TWR_SC16IS740_REG_FCR 0x02 << 3 9 #define _TWR_SC16IS740_REG_LCR 0x03 << 3 10 #define _TWR_SC16IS740_REG_MCR 0x04 << 3 11 #define _TWR_SC16IS740_BIT_FIFO_ENABLE 0x01 12 #define _TWR_SC16IS740_REG_SPR 0x07 << 3 13 #define _TWR_SC16IS740_REG_TXLVL 0x08 << 3 14 #define _TWR_SC16IS740_REG_RXLVL 0x09 << 3 15 #define _TWR_SC16IS740_REG_IOCONTROL 0x0E << 3 16 #define _TWR_SC16IS740_BIT_UART_SOFTWARE_RESET 0x08 17 #define _TWR_SC16IS740_LCR_SPECIAL_REGISTER 0x80 18 #define _TWR_SC16IS740_SPECIAL_REG_DLL 0x00 << 3 19 #define _TWR_SC16IS740_SPECIAL_REG_DLH 0x01 << 3 20 #define _TWR_SC16IS740_LCR_SPECIAL_ENHANCED_REGISTER 0xBF 21 #define _TWR_SC16IS740_ENHANCED_REG_EFR 0x02 << 3 25 memset(
self, 0,
sizeof(*
self));
27 self->_i2c_channel = i2c_channel;
28 self->_i2c_address = i2c_address;
33 if (!
twr_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, _TWR_SC16IS740_REG_LCR, _TWR_SC16IS740_LCR_SPECIAL_REGISTER))
49 if (!
twr_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, _TWR_SC16IS740_REG_LCR, _TWR_SC16IS740_LCR_SPECIAL_ENHANCED_REGISTER))
83 register_fcr = fifo | _TWR_SC16IS740_BIT_FIFO_ENABLE;
97 *spaces_available = value;
104 size_t spaces_available;
106 if (length > _TWR_SC16IS740_FIFO_SIZE)
116 if (spaces_available < length)
152 size_t read_length = 0;
171 transfer.
buffer = buffer + read_length;
172 transfer.
length = length - read_length;
174 if (transfer.
length > available)
176 transfer.
length = available;
189 read_length += transfer.
length;
191 if (read_length == length)
213 if (!
twr_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, _TWR_SC16IS740_REG_LCR, _TWR_SC16IS740_LCR_SPECIAL_REGISTER))
218 if (!
twr_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, _TWR_SC16IS740_SPECIAL_REG_DLL, baudrate))
bool twr_sc16is740_get_spaces_available(twr_sc16is740_t *self, size_t *spaces_available)
Get TX FIXO space available.
bool twr_i2c_memory_write(twr_i2c_channel_t channel, const twr_i2c_memory_transfer_t *transfer)
Memory write to I2C channel.
uint32_t memory_address
8-bit I2C memory address (it can be extended to 16-bit format if OR-ed with TWR_I2C_MEMORY_ADDRESS_16...
bool twr_sc16is740_set_baudrate(twr_sc16is740_t *self, twr_sc16is740_baudrate_t baudrate)
Set baudrate.
twr_sc16is740_fifo_t
Fifo type.
bool twr_i2c_memory_read(twr_i2c_channel_t channel, const twr_i2c_memory_transfer_t *transfer)
Memory read from I2C channel.
void * buffer
Pointer to buffer which is being written or read.
bool twr_sc16is740_reset_fifo(twr_sc16is740_t *self, twr_sc16is740_fifo_t fifo)
Reset FIFO.
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
I2C memory transfer parameters.
bool twr_sc16is740_init(twr_sc16is740_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
SC16IS740 instance.
uint64_t twr_tick_t
Timestamp data type.
bool twr_i2c_memory_write_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data)
Memory write 1 byte to I2C channel.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
twr_sc16is740_baudrate_t
Baudrates.
twr_i2c_channel_t
I2C channels.
size_t length
Length of buffer which is being written or read.
bool twr_i2c_memory_read_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t *data)
Memory read 1 byte from I2C channel.
size_t twr_sc16is740_write(twr_sc16is740_t *self, uint8_t *buffer, size_t length)
Write.
#define TWR_TICK_INFINITY
Maximum timestamp value.
bool twr_sc16is740_available(twr_sc16is740_t *self, size_t *available)
Get RX FIXO available data.
I2C communication speed is 400 kHz.
uint8_t device_address
7-bit I2C device address
size_t twr_sc16is740_read(twr_sc16is740_t *self, uint8_t *buffer, size_t length, twr_tick_t timeout)
Read.