Commit Graph

31 Commits

Author SHA1 Message Date
Michael Buesch
cef678b2db extmod/machine_i2c: Fix buffer overrun if 'addrsize' is bigger than 32.
The memory operation functions read_mem() and write_mem() create a
temporary buffer on the local C stack for the address bytes with the size
of 4 bytes.  This buffer is filled in a loop from the user supplied address
and address length.  If the user supplied 'addrsize' is bigger than 32, the
local buffer is overrun.

Fix this by raising an exception for invalid 'addrsize' values.

Signed-off-by: Michael Buesch <m@bues.ch>
2020-08-27 12:39:11 +10:00
Jim Mussared
def76fe4d9 all: Use MP_ERROR_TEXT for all error messages. 2020-04-05 15:02:06 +10:00
Damien George
69661f3343 all: Reformat C and Python source code with tools/codeformat.py.
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-28 10:33:03 +11:00
Damien George
b10d0664be extmod/machine_i2c: Add i2c.writevto() that can write a vector of bufs.
For example: i2c.writevto(addr, (buf1, buf2)).  This allows to efficiently
(wrt memory) write data composed of separate buffers, such as a command
followed by a large amount of data.
2019-05-20 15:04:29 +10:00
Damien George
8bcb552d97 extmod/machine_i2c: Remove need for temporary memory in writemem() call. 2019-05-20 15:04:29 +10:00
Damien George
606ea2b10f extmod/machine_i2c: Change C-level API to allow split I2C transactions.
API is:

    int transfer(
        mp_obj_base_t *obj,
        uint16_t addr,
        size_t n,
        mp_machine_i2c_buf_t *bufs,
        unsigned int flags
    )
2019-05-20 15:04:29 +10:00
Damien George
aa735dc6a4 extmod: Fix to support compiling with object representation D. 2018-07-08 23:15:44 +10:00
Damien George
71c9cfb028 all: Convert remaining "mp_uint_t n_args" to "size_t n_args".
This is to have consistency across the whole repository.
2017-08-30 10:59:58 +10:00
Damien George
5d2279bec1 extmod/machine_i2c: Add hook to constructor to call port-specific code.
If MICROPY_PY_MACHINE_I2C_MAKE_NEW is defined then it is called when an
I2C object is constructed with an id which is not -1.
2016-11-24 00:12:51 +11:00
Damien George
8b74048d2a extmod/machine_i2c: Expose soft I2C obj and readfrom/writeto funcs.
For external use by ports if needed.
2016-11-24 00:11:45 +11:00
Damien George
4c905616f0 extmod/machine_i2c: Remove trivial function wrappers. 2016-11-23 17:05:38 +11:00
Damien George
37333cb00a extmod/machine_i2c: Add 'stop' argument to i2c readfrom/writeto meths. 2016-11-23 17:05:38 +11:00
Damien George
0bc99b4836 extmod/machine_i2c: Make i2c.write[to] methods return num of ACKs recvd. 2016-11-23 17:05:38 +11:00
Damien George
07e83573c8 extmod/machine_i2c: Add 'nack' argument to i2c.readinto. 2016-11-23 17:05:38 +11:00
Damien George
ced240e72a extmod/machine_i2c: Make C-level functions return -errno on I2C error. 2016-11-23 17:05:38 +11:00
Damien George
946f8dd46f extmod/machine_i2c: Remove unneeded i2c_write_mem/i2c_read_mem funcs. 2016-11-23 17:05:38 +11:00
Damien George
96c3911a0a extmod/machine_i2c: Rewrite mem xfer funcs in terms of C-level protocol. 2016-11-23 17:05:37 +11:00
Damien George
c81247f1ab extmod/machine_i2c: Rewrite i2c.scan in terms of C-level protocol. 2016-11-23 17:05:37 +11:00
Damien George
bc4ea69795 extmod/machine_i2c: Add argument to C funcs to control stop generation. 2016-11-23 17:05:37 +11:00
Damien George
b983cfaf41 extmod/machine_i2c: Add a C-level I2C-protocol, refactoring soft I2C. 2016-11-23 17:05:37 +11:00
Radomir Dopieralski
e81a5353cb extmod/machine_i2c: Release SDA on bus error 2016-11-17 12:43:13 +11:00
Radomir Dopieralski
9a82b67f39 extmod/machine_i2c: Raise an error when clock stretching times out 2016-11-17 12:43:13 +11:00
Radomir Dopieralski
702928915c extmod/machine_i2c: Make the clock stretching timeout configurable 2016-11-17 12:43:12 +11:00
Radomir Dopieralski
eaef6b5324 extmod/machine_i2c: Use writes not reads in i2c.scan().
As per discussion in #2449, using write requests instead of read requests
for I2C.scan() seems to support a larger number of devices, especially
ones that are write-only.  Even a read-only I2C device has to implement
writes in order to be able to receive the address of the register to read.
2016-10-11 15:30:46 +11:00
Radomir Dopieralski
219245e10f extmod/machine_i2c: Add support for the addrsize parameter in mem xfers.
The memory read/write I2C functions now take an optional keyword-only
parameter that specifies the number of bits in the memory address.
Only mem-addrs that are a multiple of 8-bits are supported (otherwise
the behaviour is undefined).

Due to the integer type used for the address, for values larger than 32
bits, only 32 bits of address will be sent, and the rest will be padded
with 0s. Right now no exception is raised when that happens. For values
smaller than 8, no address is sent. Also no exception then.

Tested with a VL6180 sensor, which has 16-bit register addresses.

Due to code refactoring, this patch reduces stmhal and esp8266 builds
by about 50 bytes.
2016-09-28 14:45:29 +10:00
Radomir Dopieralski
ec078af985 extmod/machine_i2c: Add clock stretching support.
When the clock is too fast for the i2c slave, it can temporarily hold
down the scl line to signal to the master that it needs to wait. The
master should check the scl line when it is releasing it after
transmitting data, and wait for it to be released.

This change has been tested with a logic analyzer and an i2c slace
implemented on an atmega328p using its twi peripheral, clocked at 8Mhz.
Without the change, the i2c communication works up to aboy 150kHz
frequency, and above that results in the slave stuck in an unresponsive
state. With this change, communication has been tested to work up to
400kHz.
2016-09-22 14:10:02 +10:00
Damien George
4b37e775ea extmod/machine_i2c: Redo mp_hal_pin macros to use open_drain and od_low.
mp_hal_pin_config_od is renamed mp_hal_pin_open_drain, and mp_hal_pin_low
is mp_hal_pin_od_low.
2016-05-26 17:06:40 +01:00
Damien George
624738ca64 extmod/machine_i2c: Allow mp_hal_pin_obj_t to be any type, not a ptr. 2016-04-22 09:56:02 +01:00
Damien George
eec8a94f04 extmod/machine_i2c: Implement I2C memory reading/writing. 2016-04-12 15:52:17 +01:00
Damien George
9314b2df4f extmod/machine_i2c: Fix I2C reading by sending ack/nack at end of byte. 2016-04-12 15:46:13 +01:00
Damien George
d083712224 extmod: Add generic machine.I2C class, with bit-bang I2C.
Should work on any machine that provides the correct pin functions.
2016-04-12 14:06:54 +01:00