|
| 1 | +.. currentmodule:: machine |
| 2 | +.. _machine.I2CTarget: |
| 3 | + |
| 4 | +class I2CTarget -- a device over I2C |
| 5 | +==================================== |
| 6 | + |
| 7 | +An I2C target is a device which connects to an I2C bus and is controlled by an |
| 8 | +I2C controller. I2C targets can take many forms. The :class:`machine.I2CTarget` |
| 9 | +class implements an I2C target which allows reading and writing to a specific |
| 10 | +part of the target's internal memory. |
| 11 | + |
| 12 | +Example usage:: |
| 13 | + |
| 14 | + from machine import I2CTarget |
| 15 | + |
| 16 | + # Create the backing memory for the I2C target. |
| 17 | + mem = bytearray(8) |
| 18 | + |
| 19 | + # Create an I2C target. Depending on the port, extra parameters |
| 20 | + # may be required to select the peripheral and/or pins to use. |
| 21 | + i2c = I2CTargetMemory(addr=67, mem=mem) |
| 22 | + |
| 23 | + # At this point an I2C controller can read and write `mem`. |
| 24 | + ... |
| 25 | + |
| 26 | + # Deinitialise the I2C target. |
| 27 | + i2c.deinit() |
| 28 | + |
| 29 | +Constructors |
| 30 | +------------ |
| 31 | + |
| 32 | +.. class:: I2CTarget(id, addr, *, addrsize=7, mem=None, mem_addrsize=8, scl=None, sda=None) |
| 33 | + |
| 34 | + Construct and return a new I2CTarget object using the following parameters: |
| 35 | + |
| 36 | + - *id* identifies a particular I2C peripheral. Allowed values for |
| 37 | + depend on the particular port/board. |
| 38 | + - *addr* is the I2C address of the target. |
| 39 | + - *addrsize* is the number of bits in the I2C target address. |
| 40 | + - *mem* is an object with the buffer protocol that is writable. |
| 41 | + - *mem_addrsize* is the number of bits in the memory address. |
| 42 | + - *scl* should be a pin object specifying the pin to use for SCL. |
| 43 | + - *sda* should be a pin object specifying the pin to use for SDA. |
| 44 | + |
| 45 | + Note that some ports/boards will have default values of *scl* and *sda* |
| 46 | + that can be changed in this constructor. Others will have fixed values |
| 47 | + of *scl* and *sda* that cannot be changed. |
| 48 | + |
| 49 | +General Methods |
| 50 | +--------------- |
| 51 | + |
| 52 | +.. method:: I2CTarget.deinit() |
| 53 | + |
| 54 | + Deinitialise the I2C target. It will no longer respond to requests on the I2C |
| 55 | + bus after this method is called. |
| 56 | + |
| 57 | +.. method:: I2CTarget.readinto(buf) |
| 58 | + |
| 59 | + Read bytes into the given buffer. Returns the number of bytes read. |
| 60 | + |
| 61 | +.. method:: I2CTarget.write(buf) |
| 62 | + |
| 63 | + Write out the bytes from the buffer. Returns the number of bytes written. |
| 64 | + |
| 65 | +.. method:: I2CTarget.irq(handler=None, trigger=0, hard=False) |
| 66 | + |
| 67 | + Configure an IRQ handler. |
| 68 | + |
| 69 | +Constants |
| 70 | +--------- |
| 71 | + |
| 72 | +.. data:: I2CTarget.IRQ_ADDR_MATCH |
| 73 | + I2CTarget.IRQ_READ_REQ |
| 74 | + I2CTarget.IRQ_WRITE_REQ |
| 75 | + I2CTarget.IRQ_END |
| 76 | + I2CTarget.IRQ_STOP |
| 77 | + |
| 78 | + IRQ trigger sources. |
0 commit comments