Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C Repeated Start Conditions #7

Open
yiziliuxian opened this issue Oct 31, 2024 · 2 comments
Open

I2C Repeated Start Conditions #7

yiziliuxian opened this issue Oct 31, 2024 · 2 comments

Comments

@yiziliuxian
Copy link

yiziliuxian commented Oct 31, 2024

I am trying use this library with some TI chip. Could you please explain how to use i2c.read(reg,length) to do Pointer Byte Set Followed by a Repeat Start and Word (Two Byte) Read as showing in the following figure? Now I can only use i2c.read and i2c.write seprately.

image

@i2cy
Copy link
Owner

i2cy commented Oct 31, 2024

A typical I2C read situation that resembles reading from mpu6050 chip:

image

The Pointer Byter from Master byte you mentioned resembles the Reg Address byte shown above, and they were both followed by a Repeat Start signal from the master device (CH347). And the whole process may break down to two seperated steps: write 1 byte and read couple of bytes (2 bytes in your specific situation).

To perform a Repeat Start and Word Read (that your document called Repeat Start and Word Read) using this libraray, the code may look like this:

from ch347api import I2CDevice as i2c

dev = i2c(addr=0x48)  # in case of A0=0, A1=0, replace with actual 7-bits address of yours
pointer = 0xff  # replace with your pointer byte
response = dev.read(reg=pointer, length=2)  # Repeat Start and Word Read

I hope this would help :)

@yiziliuxian
Copy link
Author

yiziliuxian commented Nov 4, 2024

I tired you code but I found it doesn't work. I am currently using the following code:

i2c = I2CDevice(addr=0x48, clock_freq_level=I2CClockFreq.f_100K)
i2c.write(reg=0x01)
d = i2c.read(reg=None, length=2)

It seems i2c.read only generates the pattern for addr + register_addr and read two bytes as shown in the following code. It is different from the pattern you showed: addr(write) +register_addr + addr(read) and read two bytes.

    def i2c_read(self, addr: (int, bytes), read_length: int,
                 register_addr: (int, bytes) = None) -> Tuple[bool, bytes]:
        """
        read byte(s) data from i2c bus with register address using 7-bits of device address
        :param addr: 7-bits of device address
        :type addr: :obj:`int`, :obj:`bytes`
        :param read_length: length of data to read from i2c bus
        :type read_length: int
        :param register_addr: Optional[int, bytes], one byte or several bytes of address of register,
        this method will automatically convert it into bytes with byte order 'big' unsigned if data type is int,
        e.g. 0x00f1f2 -> b'\xf1\xf2'
        :type register_addr: :obj:`int`, :obj:`bytes` (optional)
        :return: Tuple[operation_status bool, feedback bytes]
        :rtype: (bool, bytes)
        """
        # convert address
        if register_addr is None:
            register_addr = b""
            # convert address with reading signal
            addr = convert_i2c_address(addr, read=True)

        else:
            register_addr = convert_int_to_bytes(register_addr)
            # convert address with writing signal
            addr = convert_i2c_address(addr, read=False)

        # assemble payload
        payload = addr + register_addr

        # send and receive data from i2c bus
        status, feedback = self.__i2c_read_write_raw(payload, read_len=read_length)

I will show you the waveform I captured from the ouput of CH347 later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants