Skip to content

File syn_soft_i2c.h

FileList > drivers > syn_soft_i2c.h

Go to the source code of this file

Software I2C (bit-banging) driver. More...

  • #include "../common/syn_defs.h"
  • #include <stdbool.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_SoftI2C
Software I2C instance.

Public Functions

Type Name
void syn_soft_i2c_init (SYN_SoftI2C * i2c, SYN_GPIO_Pin scl, SYN_GPIO_Pin sda, uint32_t delay_loops)
Initialize the soft I2C pins.
uint8_t syn_soft_i2c_read (const SYN_SoftI2C * i2c, bool ack)
Read a byte from the I2C bus.
void syn_soft_i2c_start (const SYN_SoftI2C * i2c)
Generate an I2C START condition.
void syn_soft_i2c_stop (const SYN_SoftI2C * i2c)
Generate an I2C STOP condition.
bool syn_soft_i2c_write (const SYN_SoftI2C * i2c, uint8_t data)
Write a byte to the I2C bus.
bool syn_soft_i2c_write_read (SYN_SoftI2C * i2c, uint8_t dev_addr, const uint8_t * tx_data, size_t tx_len, uint8_t * rx_data, size_t rx_len)
Perform a write-then-read I2C transaction.

Detailed Description

Implements I2C master functionality using any two GPIO pins. Relies on syn_port_gpio.h for pin manipulation. Uses a simple NOP loop for clock stretching/delays.

Public Functions Documentation

function syn_soft_i2c_init

Initialize the soft I2C pins.

void syn_soft_i2c_init (
    SYN_SoftI2C * i2c,
    SYN_GPIO_Pin scl,
    SYN_GPIO_Pin sda,
    uint32_t delay_loops
) 

Parameters:

  • i2c Pointer to I2C instance.
  • scl SCL pin.
  • sda SDA pin.
  • delay_loops Number of iterations for a half-clock delay.

function syn_soft_i2c_read

Read a byte from the I2C bus.

uint8_t syn_soft_i2c_read (
    const SYN_SoftI2C * i2c,
    bool ack
) 

Parameters:

  • i2c Pointer to I2C instance.
  • ack true to ACK the byte, false to NACK (end of read).

Returns:

The byte read.


function syn_soft_i2c_start

Generate an I2C START condition.

void syn_soft_i2c_start (
    const SYN_SoftI2C * i2c
) 

Parameters:

  • i2c Pointer to I2C instance.

function syn_soft_i2c_stop

Generate an I2C STOP condition.

void syn_soft_i2c_stop (
    const SYN_SoftI2C * i2c
) 

Parameters:

  • i2c Pointer to I2C instance.

function syn_soft_i2c_write

Write a byte to the I2C bus.

bool syn_soft_i2c_write (
    const SYN_SoftI2C * i2c,
    uint8_t data
) 

Parameters:

  • i2c Pointer to I2C instance.
  • data Byte to write.

Returns:

true if the slave ACKed, false if NACKed.


function syn_soft_i2c_write_read

Perform a write-then-read I2C transaction.

bool syn_soft_i2c_write_read (
    SYN_SoftI2C * i2c,
    uint8_t dev_addr,
    const uint8_t * tx_data,
    size_t tx_len,
    uint8_t * rx_data,
    size_t rx_len
) 

Sends a START, writes the device address + tx_data, then issues a repeated START, reads rx_len bytes into rx_data, and sends a STOP. This covers the common "write register address, read data" pattern.

Parameters:

  • i2c I2C instance.
  • dev_addr 7-bit device address (will be left-shifted internally).
  • tx_data Data to write (e.g., register address). Can be NULL if tx_len is 0.
  • tx_len Number of bytes to write.
  • rx_data Buffer to read into. Can be NULL if rx_len is 0.
  • rx_len Number of bytes to read.

Returns:

true if all bytes were ACKed, false on NACK.



The documentation for this class was generated from the following file src/syntropic/drivers/syn_soft_i2c.h