pyb.I2C
Classes
Construct an I2C object on the given bus. |
Module Contents
- class pyb.I2C.I2C(bus, mode, baudrate=328125, *, prescaler=-1, polarity=1, phase=0, bits=8, firstbit=MSB, ti=False, crc=None)
Construct an I2C object on the given bus.
buscan be 1 or 2, ‘X’ or ‘Y’. With no additional parameters, the I2C object is created but not initialised (it has the settings from the last initialisation of the bus, if any). If extra arguments are given, the bus is initialised. Seeinitfor parameters of initialisation.The physical pins of the I2C buses on Pyboards V1.0 and V1.1 are:
I2C(1)is on the X position:(SCL, SDA) = (X9, X10) = (PB6, PB7)I2C(2)is on the Y position:(SCL, SDA) = (Y9, Y10) = (PB10, PB11)
On the Pyboard Lite:
I2C(1)is on the X position:(SCL, SDA) = (X9, X10) = (PB6, PB7)I2C(3)is on the Y position:(SCL, SDA) = (Y9, Y10) = (PA8, PB8)
Calling the constructor with ‘X’ or ‘Y’ enables portability between Pyboard types.
- init(mode, *, addr=18, baudrate=400000, gencall=False, dma=False) None
Initialise the I2C bus with the given parameters:
modemust be eitherI2C.CONTROLLERorI2C.PERIPHERALaddris the 7-bit address (only sensible for a peripheral)baudrateis the SCL clock rate (only sensible for a controller)gencallis whether to support general call modedmais whether to allow the use of DMA for the I2C transfers (note that DMA transfers have more precise timing but currently do not handle bus errors properly)
The actual clock frequency may be lower than the requested frequency. This is dependent on the platform hardware. The actual rate may be determined by printing the I2C object.
- is_ready(addr) Incomplete
Check if an I2C device responds to the given address. Only valid when in controller mode.
- mem_read(data, addr, memaddr, *, timeout=5000, addr_size=8) Incomplete
Read from the memory of an I2C device:
datacan be an integer (number of bytes to read) or a buffer to read intoaddris the I2C device addressmemaddris the memory location within the I2C devicetimeoutis the timeout in milliseconds to wait for the readaddr_sizeselects width of memaddr: 8 or 16 bits
Returns the read data. This is only valid in controller mode.
- mem_write(data, addr, memaddr, *, timeout=5000, addr_size=8) None
Write to the memory of an I2C device:
datacan be an integer or a buffer to write fromaddris the I2C device addressmemaddris the memory location within the I2C devicetimeoutis the timeout in milliseconds to wait for the writeaddr_sizeselects width of memaddr: 8 or 16 bits
Returns
None. This is only valid in controller mode.
- recv(recv, addr=0, *, timeout=5000) bytes
Receive data on the bus:
recvcan be an integer, which is the number of bytes to receive, or a mutable buffer, which will be filled with received bytesaddris the address to receive from (only required in controller mode)timeoutis the timeout in milliseconds to wait for the receive
Return value: if
recvis an integer then a new buffer of the bytes received, otherwise the same buffer that was passed in torecv.
- scan() List
Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond. Only valid when in controller mode.
- send(send, addr=0, *, timeout=5000) None
Send data on the bus:
sendis the data to send (an integer to send, or a buffer object)addris the address to send to (only required in controller mode)timeoutis the timeout in milliseconds to wait for the send
Return value:
None.
- CONTROLLER: Incomplete
for initialising the bus to controller mode
- PERIPHERAL: Incomplete
for initialising the bus to peripheral mode