2016-11-03 18:50:59 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Scott Shawcroft
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This file contains all of the Python API definitions for the
|
2017-04-10 16:32:19 -04:00
|
|
|
// busio.I2C class.
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2016-11-29 18:49:50 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/busio/I2C.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "shared-bindings/util.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-20 14:24:05 -04:00
|
|
|
#include "lib/utils/buffer_helper.h"
|
2017-02-19 10:11:33 -05:00
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
#include "py/runtime.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| class I2C:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Two wire serial protocol"""
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-09-25 14:07:22 -04:00
|
|
|
//| def __init__(self, scl: microcontroller.Pin, sda: microcontroller.Pin, *, frequency: int = 100000, timeout: int = 255) -> None:
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| """I2C is a two-wire protocol for communicating between devices. At the
|
|
|
|
//| physical level it consists of 2 wires: SCL and SDA, the clock and data
|
|
|
|
//| lines respectively.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| .. seealso:: Using this class directly requires careful lock management.
|
2021-06-26 04:50:28 -04:00
|
|
|
//| Instead, use :class:`~adafruit_bus_device.I2CDevice` to
|
2020-04-29 15:20:05 -04:00
|
|
|
//| manage locks.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-04-29 15:20:05 -04:00
|
|
|
//| .. seealso:: Using this class to directly read registers requires manual
|
|
|
|
//| bit unpacking. Instead, use an existing driver or make one with
|
|
|
|
//| :ref:`Register <register-module-reference>` data descriptors.
|
|
|
|
//|
|
|
|
|
//| :param ~microcontroller.Pin scl: The clock pin
|
|
|
|
//| :param ~microcontroller.Pin sda: The data pin
|
|
|
|
//| :param int frequency: The clock frequency in Hertz
|
2021-06-26 04:50:28 -04:00
|
|
|
//| :param int timeout: The maximum clock stretching timeut - (used only for
|
|
|
|
//| :class:`bitbangio.I2C`; ignored for :class:`busio.I2C`)
|
2020-04-29 15:20:05 -04:00
|
|
|
//|
|
|
|
|
//| .. note:: On the nRF52840, only one I2C object may be created,
|
|
|
|
//| except on the Circuit Playground Bluefruit, which allows two,
|
|
|
|
//| one for the onboard accelerometer, and one for offboard use."""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2019-01-14 20:26:36 -05:00
|
|
|
STATIC mp_obj_t busio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2017-04-10 16:32:19 -04:00
|
|
|
busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t);
|
|
|
|
self->base.type = &busio_i2c_type;
|
2018-05-13 21:54:44 -04:00
|
|
|
enum { ARG_scl, ARG_sda, ARG_frequency, ARG_timeout };
|
2016-11-29 18:49:50 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
2020-09-25 14:07:22 -04:00
|
|
|
{ MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
|
2018-05-13 21:54:44 -04:00
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} },
|
2016-11-29 18:49:50 -05:00
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-02-28 23:32:24 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
const mcu_pin_obj_t *scl = validate_obj_is_free_pin(args[ARG_scl].u_obj);
|
|
|
|
const mcu_pin_obj_t *sda = validate_obj_is_free_pin(args[ARG_sda].u_obj);
|
2020-02-28 23:32:24 -05:00
|
|
|
|
2018-05-13 21:54:44 -04:00
|
|
|
common_hal_busio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int, args[ARG_timeout].u_int);
|
2016-11-29 18:49:50 -05:00
|
|
|
return (mp_obj_t)self;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def deinit(self) -> None:
|
2020-04-29 15:45:19 -04:00
|
|
|
//| """Releases control of the underlying hardware so other classes can use it."""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_obj_deinit(mp_obj_t self_in) {
|
2017-06-07 17:39:12 -04:00
|
|
|
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_busio_i2c_deinit(self);
|
|
|
|
return mp_const_none;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_deinit_obj, busio_i2c_obj_deinit);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(busio_i2c_obj_t *self) {
|
|
|
|
if (common_hal_busio_i2c_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __enter__(self) -> I2C:
|
2020-04-29 15:45:19 -04:00
|
|
|
//| """No-op used in Context Managers."""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2017-02-19 10:11:33 -05:00
|
|
|
// Provided by context manager helper.
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __exit__(self) -> None:
|
2020-04-29 15:45:19 -04:00
|
|
|
//| """Automatically deinitializes the hardware on context exit. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
2016-11-03 18:50:59 -04:00
|
|
|
(void)n_args;
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_busio_i2c_deinit(args[0]);
|
2016-11-03 18:50:59 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_i2c___exit___obj, 4, 4, busio_i2c_obj___exit__);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
static void check_lock(busio_i2c_obj_t *self) {
|
2021-03-15 09:57:36 -04:00
|
|
|
asm ("");
|
2017-04-10 16:32:19 -04:00
|
|
|
if (!common_hal_busio_i2c_has_lock(self)) {
|
2019-03-24 20:41:52 -04:00
|
|
|
mp_raise_RuntimeError(translate("Function requires lock"));
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| def scan(self) -> List[int]:
|
2020-07-23 14:22:41 -04:00
|
|
|
//| """Scan all I2C addresses between 0x08 and 0x77 inclusive and return a
|
|
|
|
//| list of those that respond.
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :return: List of device ids on the I2C bus
|
|
|
|
//| :rtype: list"""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_scan(mp_obj_t self_in) {
|
2017-06-07 17:39:12 -04:00
|
|
|
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-07 17:39:12 -04:00
|
|
|
check_lock(self);
|
|
|
|
mp_obj_t list = mp_obj_new_list(0, NULL);
|
|
|
|
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
|
|
|
|
for (int addr = 0x08; addr < 0x78; ++addr) {
|
|
|
|
bool success = common_hal_busio_i2c_probe(self, addr);
|
|
|
|
if (success) {
|
|
|
|
mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_scan_obj, busio_i2c_scan);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def try_lock(self) -> bool:
|
2020-04-29 15:45:19 -04:00
|
|
|
//| """Attempts to grab the I2C lock. Returns True on success.
|
2020-04-29 15:20:05 -04:00
|
|
|
//|
|
2020-04-29 15:45:19 -04:00
|
|
|
//| :return: True when lock has been grabbed
|
|
|
|
//| :rtype: bool"""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_obj_try_lock(mp_obj_t self_in) {
|
2017-10-02 20:49:40 -04:00
|
|
|
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-10-02 20:49:40 -04:00
|
|
|
return mp_obj_new_bool(common_hal_busio_i2c_try_lock(self));
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock);
|
2016-11-30 18:08:34 -05:00
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def unlock(self) -> None:
|
2020-04-29 15:45:19 -04:00
|
|
|
//| """Releases the I2C lock."""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_obj_unlock(mp_obj_t self_in) {
|
2017-10-02 20:49:40 -04:00
|
|
|
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-10-02 20:49:40 -04:00
|
|
|
common_hal_busio_i2c_unlock(self);
|
2016-11-30 18:08:34 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock);
|
2016-11-30 18:08:34 -05:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| def readfrom_into(self, address: int, buffer: WriteableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
|
2020-07-23 14:22:41 -04:00
|
|
|
//| """Read into ``buffer`` from the device selected by ``address``.
|
|
|
|
//| The number of bytes read will be the length of ``buffer``.
|
|
|
|
//| At least one byte must be read.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
|
|
|
|
//| as if ``buffer[start:end]``. This will not cause an allocation like
|
|
|
|
//| ``buf[start:end]`` will so it saves memory.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :param int address: 7-bit device address
|
2020-08-03 00:35:43 -04:00
|
|
|
//| :param ~_typing.WriteableBuffer buffer: buffer to write into
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :param int start: Index to start writing at
|
|
|
|
//| :param int end: Index to write up to but not include. Defaults to ``len(buffer)``"""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2019-07-26 03:18:29 -04:00
|
|
|
// Shared arg parsing for readfrom_into and writeto_then_readfrom.
|
|
|
|
STATIC void readfrom(busio_i2c_obj_t *self, mp_int_t address, mp_obj_t buffer, int32_t start, mp_int_t end) {
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
|
|
|
|
|
2019-08-23 15:13:11 -04:00
|
|
|
size_t length = bufinfo.len;
|
2019-07-26 03:18:29 -04:00
|
|
|
normalize_buffer_bounds(&start, end, &length);
|
|
|
|
if (length == 0) {
|
|
|
|
mp_raise_ValueError(translate("Buffer must be at least length 1"));
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t status = common_hal_busio_i2c_read(self, address, ((uint8_t *)bufinfo.buf) + start, length);
|
2019-07-26 03:18:29 -04:00
|
|
|
if (status != 0) {
|
|
|
|
mp_raise_OSError(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2016-11-28 21:35:19 -05:00
|
|
|
enum { ARG_address, ARG_buffer, ARG_start, ARG_end };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
|
|
|
|
};
|
2017-04-10 16:32:19 -04:00
|
|
|
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2016-11-30 18:08:34 -05:00
|
|
|
check_lock(self);
|
2016-11-28 21:35:19 -05:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2019-07-26 03:18:29 -04:00
|
|
|
readfrom(self, args[ARG_address].u_int, args[ARG_buffer].u_obj, args[ARG_start].u_int,
|
2021-03-15 09:57:36 -04:00
|
|
|
args[ARG_end].u_int);
|
2016-11-03 18:50:59 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_readfrom_into_obj, 3, busio_i2c_readfrom_into);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2021-09-17 09:17:39 -04:00
|
|
|
//| def writeto(self, address: int, buffer: ReadableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
|
2020-07-23 14:22:41 -04:00
|
|
|
//| """Write the bytes from ``buffer`` to the device selected by ``address`` and
|
|
|
|
//| then transmit a stop bit.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
|
|
|
|
//| as if ``buffer[start:end]``. This will not cause an allocation like
|
|
|
|
//| ``buffer[start:end]`` will so it saves memory.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| Writing a buffer or slice of length zero is permitted, as it can be used
|
2020-07-23 14:22:41 -04:00
|
|
|
//| to poll for the existence of a device.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :param int address: 7-bit device address
|
2021-06-26 04:50:28 -04:00
|
|
|
//| :param ~_typing.ReadableBuffer buffer: buffer containing the bytes to write
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :param int start: Index to start writing from
|
|
|
|
//| :param int end: Index to read up to but not include. Defaults to ``len(buffer)``"""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2019-07-26 03:18:29 -04:00
|
|
|
// Shared arg parsing for writeto and writeto_then_readfrom.
|
|
|
|
STATIC void writeto(busio_i2c_obj_t *self, mp_int_t address, mp_obj_t buffer, int32_t start, mp_int_t end, bool stop) {
|
|
|
|
// get the buffer to write the data from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
|
|
|
|
|
2019-08-23 15:13:11 -04:00
|
|
|
size_t length = bufinfo.len;
|
2019-07-26 03:18:29 -04:00
|
|
|
normalize_buffer_bounds(&start, end, &length);
|
|
|
|
|
|
|
|
// do the transfer
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t status = common_hal_busio_i2c_write(self, address, ((uint8_t *)bufinfo.buf) + start,
|
|
|
|
length, stop);
|
2019-07-26 03:18:29 -04:00
|
|
|
if (status != 0) {
|
|
|
|
mp_raise_OSError(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2020-07-23 14:06:14 -04:00
|
|
|
enum { ARG_address, ARG_buffer, ARG_start, ARG_end };
|
2016-11-03 18:50:59 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
2016-11-28 21:35:19 -05:00
|
|
|
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
2017-04-10 16:32:19 -04:00
|
|
|
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2016-11-30 18:08:34 -05:00
|
|
|
check_lock(self);
|
2016-11-03 18:50:59 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2019-07-26 03:18:29 -04:00
|
|
|
writeto(self, args[ARG_address].u_int, args[ARG_buffer].u_obj, args[ARG_start].u_int,
|
2021-03-15 09:57:36 -04:00
|
|
|
args[ARG_end].u_int, true);
|
2019-07-26 03:18:29 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_writeto_obj, 1, busio_i2c_writeto);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| def writeto_then_readfrom(self, address: int, out_buffer: ReadableBuffer, in_buffer: WriteableBuffer, *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
|
2020-07-23 14:22:41 -04:00
|
|
|
//| """Write the bytes from ``out_buffer`` to the device selected by ``address``, generate no stop
|
|
|
|
//| bit, generate a repeated start and read into ``in_buffer``. ``out_buffer`` and
|
|
|
|
//| ``in_buffer`` can be the same buffer because they are used sequentially.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2021-06-26 04:50:28 -04:00
|
|
|
//| If ``start`` or ``end`` is provided, then the corresponding buffer will be sliced
|
2020-07-23 14:22:41 -04:00
|
|
|
//| as if ``buffer[start:end]``. This will not cause an allocation like ``buf[start:end]``
|
|
|
|
//| will so it saves memory.
|
2020-04-25 15:07:58 -04:00
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :param int address: 7-bit device address
|
2021-06-26 04:50:28 -04:00
|
|
|
//| :param ~_typing.ReadableBuffer out_buffer: buffer containing the bytes to write
|
2020-08-03 00:35:43 -04:00
|
|
|
//| :param ~_typing.WriteableBuffer in_buffer: buffer to write into
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :param int out_start: Index to start writing from
|
|
|
|
//| :param int out_end: Index to read up to but not include. Defaults to ``len(buffer)``
|
|
|
|
//| :param int in_start: Index to start writing at
|
|
|
|
//| :param int in_end: Index to write up to but not include. Defaults to ``len(buffer)``"""
|
|
|
|
//| ...
|
2020-04-29 15:55:06 -04:00
|
|
|
//|
|
2019-07-26 03:18:29 -04:00
|
|
|
STATIC mp_obj_t busio_i2c_writeto_then_readfrom(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_address, ARG_out_buffer, ARG_in_buffer, ARG_out_start, ARG_out_end, ARG_in_start, ARG_in_end };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_out_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_in_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_out_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_out_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
|
|
|
|
{ MP_QSTR_in_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_in_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
|
|
|
|
};
|
|
|
|
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
check_for_deinit(self);
|
|
|
|
check_lock(self);
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2017-04-20 14:24:05 -04:00
|
|
|
|
2019-07-26 03:18:29 -04:00
|
|
|
writeto(self, args[ARG_address].u_int, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int,
|
2021-03-15 09:57:36 -04:00
|
|
|
args[ARG_out_end].u_int, false);
|
2019-07-26 03:18:29 -04:00
|
|
|
readfrom(self, args[ARG_address].u_int, args[ARG_in_buffer].u_obj, args[ARG_in_start].u_int,
|
2021-03-15 09:57:36 -04:00
|
|
|
args[ARG_in_end].u_int);
|
2016-11-28 21:35:19 -05:00
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-07-26 03:18:29 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_writeto_then_readfrom_obj, 3, busio_i2c_writeto_then_readfrom);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC const mp_rom_map_elem_t busio_i2c_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_i2c_deinit_obj) },
|
2017-02-19 10:11:33 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_i2c___exit___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&busio_i2c_scan_obj) },
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&busio_i2c_try_lock_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&busio_i2c_unlock_obj) },
|
2016-11-30 18:08:34 -05:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&busio_i2c_readfrom_into_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&busio_i2c_writeto_obj) },
|
2019-07-26 03:18:29 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeto_then_readfrom), MP_ROM_PTR(&busio_i2c_writeto_then_readfrom_obj) },
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(busio_i2c_locals_dict, busio_i2c_locals_dict_table);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
const mp_obj_type_t busio_i2c_type = {
|
2021-03-15 09:57:36 -04:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_I2C,
|
|
|
|
.make_new = busio_i2c_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t *)&busio_i2c_locals_dict,
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|