Merge pull request #3612 from gamblor21/bus_device

Moving Adafruit_CircuitPython_BusDevice to core
This commit is contained in:
Scott Shawcroft 2020-12-02 13:23:02 -08:00 committed by GitHub
commit 1130b80e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 885 additions and 14 deletions

View File

@ -498,7 +498,8 @@ msgstr ""
msgid "Buffer must be a multiple of 512 bytes"
msgstr ""
#: shared-bindings/bitbangio/I2C.c shared-bindings/busio/I2C.c
#: shared-bindings/bitbangio/I2C.c shared-bindings/busdevice/I2CDevice.c
#: shared-bindings/busio/I2C.c
msgid "Buffer must be at least length 1"
msgstr ""
@ -1295,6 +1296,11 @@ msgstr ""
msgid "No DMA channel found"
msgstr ""
#: shared-module/busdevice/I2CDevice.c
#, c-format
msgid "No I2C device at address: %x"
msgstr ""
#: ports/esp32s2/common-hal/busio/SPI.c ports/mimxrt10xx/common-hal/busio/SPI.c
#: ports/stm/common-hal/busio/SPI.c
msgid "No MISO Pin"

View File

@ -19,9 +19,9 @@ SUPEROPT_GC = 0
CFLAGS_INLINE_LIMIT = 60
CIRCUITPY_GAMEPAD = 1
CIRCUITPY_BUSDEVICE = 1
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD
#FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_ADXL34x

View File

@ -15,6 +15,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
CFLAGS_INLINE_LIMIT = 60
SUPEROPT_GC = 0

View File

@ -21,7 +21,6 @@ SUPEROPT_GC = 0
CFLAGS_INLINE_LIMIT = 55
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_HID
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH

View File

@ -25,7 +25,6 @@ CFLAGS_INLINE_LIMIT = 50
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH

View File

@ -26,7 +26,6 @@ SUPEROPT_GC = 0
CFLAGS_INLINE_LIMIT = 55
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

View File

@ -18,6 +18,7 @@ CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 1
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ROTARYIO = 0

View File

@ -16,6 +16,7 @@ CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
CFLAGS_INLINE_LIMIT = 60
SUPEROPT_GC = 0

View File

@ -20,7 +20,6 @@ CIRCUITPY_GAMEPAD = 0
CFLAGS_INLINE_LIMIT = 50
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

View File

@ -21,11 +21,11 @@ CIRCUITPY_SAMD = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_USB_HID = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_BUSDEVICE = 1
CFLAGS_INLINE_LIMIT = 35
# Make more room.
SUPEROPT_GC = 0
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM69

View File

@ -22,11 +22,11 @@ CIRCUITPY_SAMD = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_USB_HID = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_BUSDEVICE = 1
CFLAGS_INLINE_LIMIT = 35
# Make more room.
SUPEROPT_GC = 0
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM9x

View File

@ -17,6 +17,7 @@ CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
# supersized, not ultra-supersized
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
CFLAGS_INLINE_LIMIT = 60
SUPEROPT_GC = 0

View File

@ -27,7 +27,6 @@ CFLAGS_INLINE_LIMIT = 55
SUPEROPT_GC = 0
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

View File

@ -15,6 +15,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
CFLAGS_INLINE_LIMIT = 60
SUPEROPT_GC = 0

View File

@ -21,7 +21,6 @@ CIRCUITPY_GAMEPAD = 0
CIRCUITPY_RGBMATRIX = 0
CIRCUITPY_PS2IO = 0
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Register
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

View File

@ -21,7 +21,6 @@ CIRCUITPY_GAMEPAD = 0
CIRCUITPY_RGBMATRIX = 0
CIRCUITPY_PS2IO = 0
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Register
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

View File

@ -13,5 +13,4 @@ LONGINT_IMPL = MPZ
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_USTACK = 1
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

View File

@ -15,6 +15,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
CFLAGS_INLINE_LIMIT = 60

View File

@ -16,6 +16,7 @@ CIRCUITPY_COUNTIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
CFLAGS_INLINE_LIMIT = 60
SUPEROPT_GC = 0

View File

@ -21,6 +21,6 @@ CIRCUITPY_PULSEIO=0
CIRCUITPY_ROTARYIO=0
CIRCUITPY_TOUCHIO_USE_NATIVE=0
CIRCUITPY_TOUCHIO=0
CIRCUITPY_BUSDEVICE=1
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice

View File

@ -24,7 +24,7 @@ CIRCUITPY_TOUCHIO=0
CIRCUITPY_USB_MIDI=0
CIRCUITPY_RTC=0
CIRCUITPY_COUNTIO=0
CIRCUITPY_BUSDEVICE=1
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

View File

@ -18,6 +18,7 @@ CIRCUITPY_AUDIOIO = 0
CIRCUITPY_BITBANGIO = 0
CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_BOARD = 0
CIRCUITPY_BUSDEVICE = 0
CIRCUITPY_BUSIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_DISPLAYIO = 0

View File

@ -25,6 +25,7 @@ CIRCUITPY_RTC = 1
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_WATCHDOG = 1
CIRCUITPY_BUSDEVICE = 0
# Enable micropython.native
#CIRCUITPY_ENABLE_MPY_NATIVE = 1

View File

@ -136,6 +136,9 @@ endif
ifeq ($(CIRCUITPY_BOARD),1)
SRC_PATTERNS += board/%
endif
ifeq ($(CIRCUITPY_BUSDEVICE),1)
SRC_PATTERNS += adafruit_bus_device/%
endif
ifeq ($(CIRCUITPY_BUSIO),1)
SRC_PATTERNS += busio/% bitbangio/OneWire.%
endif
@ -437,6 +440,9 @@ SRC_SHARED_MODULE_ALL = \
bitbangio/SPI.c \
bitbangio/__init__.c \
board/__init__.c \
adafruit_bus_device/__init__.c \
adafruit_bus_device/I2CDevice.c \
adafruit_bus_device/SPIDevice.c \
busio/OneWire.c \
canio/Match.c \
canio/Message.c \

View File

@ -336,6 +336,13 @@ extern const struct _mp_obj_module_t board_module;
#define BOARD_UART_ROOT_POINTER
#endif
#if CIRCUITPY_BUSDEVICE
extern const struct _mp_obj_module_t adafruit_bus_device_module;
#define BUSDEVICE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_adafruit_bus_device), (mp_obj_t)&adafruit_bus_device_module },
#else
#define BUSDEVICE_MODULE
#endif
#if CIRCUITPY_BUSIO
extern const struct _mp_obj_module_t busio_module;
#define BUSIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module },
@ -792,6 +799,7 @@ extern const struct _mp_obj_module_t wifi_module;
BITBANGIO_MODULE \
BLEIO_MODULE \
BOARD_MODULE \
BUSDEVICE_MODULE \
BUSIO_MODULE \
CAMERA_MODULE \
CANIO_MODULE \

View File

@ -96,6 +96,9 @@ CFLAGS += -DCIRCUITPY_BLEIO=$(CIRCUITPY_BLEIO)
CIRCUITPY_BOARD ?= 1
CFLAGS += -DCIRCUITPY_BOARD=$(CIRCUITPY_BOARD)
CIRCUITPY_BUSDEVICE ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BUSDEVICE=$(CIRCUITPY_BUSDEVICE)
CIRCUITPY_BUILTINS_POW3 ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_BUILTINS_POW3=$(CIRCUITPY_BUILTINS_POW3)

View File

@ -0,0 +1,259 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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
// busio.I2C class.
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/adafruit_bus_device/I2CDevice.h"
#include "shared-bindings/util.h"
#include "shared-module/adafruit_bus_device/I2CDevice.h"
#include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
//| class I2CDevice:
//| """I2C Device Manager"""
//|
//| def __init__(self, i2c: busio.I2C, device_address: int, probe: bool = True) -> None:
//|
//| """Represents a single I2C device and manages locking the bus and the device
//| address.
//| :param ~busio.I2C i2c: The I2C bus the device is on
//| :param int device_address: The 7 bit device address
//| :param bool probe: Probe for the device upon object creation, default is true
//|
//| Example::
//|
//| import busio
//| from board import *
//| from adafruit_bus_device.i2c_device import I2CDevice
//| with busio.I2C(SCL, SDA) as i2c:
//| device = I2CDevice(i2c, 0x70)
//| bytes_read = bytearray(4)
//| with device:
//| device.readinto(bytes_read)
//| # A second transaction
//| with device:
//| device.write(bytes_read)"""
//| ...
//|
STATIC mp_obj_t adafruit_bus_device_i2cdevice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
adafruit_bus_device_i2cdevice_obj_t *self = m_new_obj(adafruit_bus_device_i2cdevice_obj_t);
self->base.type = &adafruit_bus_device_i2cdevice_type;
enum { ARG_i2c, ARG_device_address, ARG_probe };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_i2c, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_device_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_probe, MP_ARG_BOOL, {.u_bool = true} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
busio_i2c_obj_t* i2c = args[ARG_i2c].u_obj;
common_hal_adafruit_bus_device_i2cdevice_construct(MP_OBJ_TO_PTR(self), i2c, args[ARG_device_address].u_int);
if (args[ARG_probe].u_bool == true) {
common_hal_adafruit_bus_device_i2cdevice_probe_for_device(self);
}
return (mp_obj_t)self;
}
//| def __enter__(self) -> I2CDevice:
//| """Context manager entry to lock bus."""
//| ...
//|
STATIC mp_obj_t adafruit_bus_device_i2cdevice_obj___enter__(mp_obj_t self_in) {
adafruit_bus_device_i2cdevice_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_adafruit_bus_device_i2cdevice_lock(self);
return self;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adafruit_bus_device_i2cdevice___enter___obj, adafruit_bus_device_i2cdevice_obj___enter__);
//| def __exit__(self) -> None:
//| """Automatically unlocks the bus on exit."""
//| ...
//|
STATIC mp_obj_t adafruit_bus_device_i2cdevice_obj___exit__(size_t n_args, const mp_obj_t *args) {
common_hal_adafruit_bus_device_i2cdevice_unlock(MP_OBJ_TO_PTR(args[0]));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_i2cdevice___exit___obj, 4, 4, adafruit_bus_device_i2cdevice_obj___exit__);
//| def readinto(self, buf: WriteableBuffer, *, start: int = 0, end: int = 0) -> None:
//| """Read into ``buf`` from the device. The number of bytes read will be the
//| length of ``buf``.
//| If ``start`` or ``end`` is provided, then the buffer will be sliced
//| as if ``buf[start:end]``. This will not cause an allocation like
//| ``buf[start:end]`` will so it saves memory.
//| :param bytearray buf: buffer to write into
//| :param int start: Index to start writing at
//| :param int end: Index to write up to but not include; if None, use ``len(buf)``"""
//| ...
//|
STATIC void readinto(adafruit_bus_device_i2cdevice_obj_t *self, 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);
size_t length = bufinfo.len;
normalize_buffer_bounds(&start, end, &length);
if (length == 0) {
mp_raise_ValueError(translate("Buffer must be at least length 1"));
}
uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
if (status != 0) {
mp_raise_OSError(status);
}
}
STATIC mp_obj_t adafruit_bus_device_i2cdevice_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ 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} },
};
adafruit_bus_device_i2cdevice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
readinto(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_readinto_obj, 2, adafruit_bus_device_i2cdevice_readinto);
//| def write(self, buf: ReadableBuffer, *, start: int = 0, end: int = 0) -> None:
//| """Write the bytes from ``buffer`` to the device, then transmit a stop bit.
//| 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.
//| :param bytearray buf: buffer containing the bytes to write
//| :param int start: Index to start writing from
//| :param int end: Index to read up to but not include; if None, use ``len(buf)``
//| """
//| ...
//|
STATIC void write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t start, mp_int_t end) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
size_t length = bufinfo.len;
normalize_buffer_bounds(&start, end, &length);
if (length == 0) {
mp_raise_ValueError(translate("Buffer must be at least length 1"));
}
uint8_t status = common_hal_adafruit_bus_device_i2cdevice_write(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
if (status != 0) {
mp_raise_OSError(status);
}
}
STATIC mp_obj_t adafruit_bus_device_i2cdevice_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ 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} },
};
adafruit_bus_device_i2cdevice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
write(self, args[ARG_buffer].u_obj, args[ARG_start].u_int, args[ARG_end].u_int);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_write_obj, 2, adafruit_bus_device_i2cdevice_write);
//| def write_then_readinto(self, out_buffer: WriteableBuffer, in_buffer: ReadableBuffer, *, out_start: int = 0, out_end: int = 0, in_start: int = 0, in_end: int = 0) -> None:
//| """Write the bytes from ``out_buffer`` to the device, then immediately
//| reads into ``in_buffer`` from the device. The number of bytes read
//| will be the length of ``in_buffer``.
//| If ``out_start`` or ``out_end`` is provided, then the output buffer
//| will be sliced as if ``out_buffer[out_start:out_end]``. This will
//| not cause an allocation like ``buffer[out_start:out_end]`` will so
//| it saves memory.
//| If ``in_start`` or ``in_end`` is provided, then the input buffer
//| will be sliced as if ``in_buffer[in_start:in_end]``. This will not
//| cause an allocation like ``in_buffer[in_start:in_end]`` will so
//| it saves memory.
//| :param bytearray out_buffer: buffer containing the bytes to write
//| :param bytearray in_buffer: buffer containing the bytes to read into
//| :param int out_start: Index to start writing from
//| :param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
//| :param int in_start: Index to start writing at
//| :param int in_end: Index to write up to but not include; if None, use ``len(in_buffer)``
//| """
//| ...
//|
STATIC mp_obj_t adafruit_bus_device_i2cdevice_write_then_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { 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_out_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_in_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ 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} },
};
adafruit_bus_device_i2cdevice_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
write(self, args[ARG_out_buffer].u_obj, args[ARG_out_start].u_int, args[ARG_out_end].u_int);
readinto(self, args[ARG_in_buffer].u_obj, args[ARG_in_start].u_int, args[ARG_in_end].u_int);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(adafruit_bus_device_i2cdevice_write_then_readinto_obj, 3, adafruit_bus_device_i2cdevice_write_then_readinto);
STATIC const mp_rom_map_elem_t adafruit_bus_device_i2cdevice_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&adafruit_bus_device_i2cdevice___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adafruit_bus_device_i2cdevice___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&adafruit_bus_device_i2cdevice_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&adafruit_bus_device_i2cdevice_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_then_readinto), MP_ROM_PTR(&adafruit_bus_device_i2cdevice_write_then_readinto_obj) },
};
STATIC MP_DEFINE_CONST_DICT(adafruit_bus_device_i2cdevice_locals_dict, adafruit_bus_device_i2cdevice_locals_dict_table);
const mp_obj_type_t adafruit_bus_device_i2cdevice_type = {
{ &mp_type_type },
.name = MP_QSTR_I2CDevice,
.make_new = adafruit_bus_device_i2cdevice_make_new,
.locals_dict = (mp_obj_dict_t*)&adafruit_bus_device_i2cdevice_locals_dict,
};

View File

@ -0,0 +1,53 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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.
*/
// Machine is the HAL for low-level, hardware accelerated functions. It is not
// meant to simplify APIs, its only meant to unify them so that other modules
// do not require port specific logic.
//
// This file includes externs for all functions a port should implement to
// support the machine module.
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_I2CDEVICE_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_I2CDEVICE_H
#include "py/obj.h"
#include "shared-module/adafruit_bus_device/I2CDevice.h"
//#include "shared-bindings/busio/I2C.h"
// Type object used in Python. Should be shared between ports.
extern const mp_obj_type_t adafruit_bus_device_i2cdevice_type;
// Initializes the hardware peripheral.
extern void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address);
extern uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
extern uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
extern void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self);
extern void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_I2CDEVICE_H

View File

@ -0,0 +1,128 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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.
*/
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/adafruit_bus_device/SPIDevice.h"
#include "shared-bindings/util.h"
#include "shared-module/adafruit_bus_device/SPIDevice.h"
#include "common-hal/digitalio/DigitalInOut.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
//| class SPIDevice:
//| """SPI Device Manager"""
//|
//| def __init__(self, spi: busio.SPI, chip_select: microcontroller.Pin, *, baudrate: int = 100000, polarity: int = 0, phase: int = 0, extra_clocks : int = 0) -> None:
//|
//| """
//| Represents a single SPI device and manages locking the bus and the device address.
//| :param ~busio.SPI spi: The SPI bus the device is on
//| :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the DigitalInOut API.
//| :param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.)
//|
//| Example::
//|
//| import busio
//| import digitalio
//| from board import *
//| from adafruit_bus_device.spi_device import SPIDevice
//| with busio.SPI(SCK, MOSI, MISO) as spi_bus:
//| cs = digitalio.DigitalInOut(D10)
//| device = SPIDevice(spi_bus, cs)
//| bytes_read = bytearray(4)
//| # The object assigned to spi in the with statements below
//| # is the original spi_bus object. We are using the busio.SPI
//| # operations busio.SPI.readinto() and busio.SPI.write().
//| with device as spi:
//| spi.readinto(bytes_read)
//| # A second transaction
//| with device as spi:
//| spi.write(bytes_read)"""
//| ...
//|
STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
adafruit_bus_device_spidevice_obj_t *self = m_new_obj(adafruit_bus_device_spidevice_obj_t);
self->base.type = &adafruit_bus_device_spidevice_type;
enum { ARG_spi, ARG_chip_select, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_extra_clocks, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
busio_spi_obj_t* spi = args[ARG_spi].u_obj;
common_hal_adafruit_bus_device_spidevice_construct(MP_OBJ_TO_PTR(self), spi, args[ARG_chip_select].u_obj, args[ARG_baudrate].u_int, args[ARG_polarity].u_int,
args[ARG_phase].u_int, args[ARG_extra_clocks].u_int);
if (args[ARG_chip_select].u_obj != MP_OBJ_NULL) {
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(MP_OBJ_TO_PTR(args[ARG_chip_select].u_obj),
true, DRIVE_MODE_PUSH_PULL);
if (result == DIGITALINOUT_INPUT_ONLY) {
mp_raise_NotImplementedError(translate("Pin is input only"));
}
}
return (mp_obj_t)self;
}
STATIC mp_obj_t adafruit_bus_device_spidevice_obj___enter__(mp_obj_t self_in) {
adafruit_bus_device_spidevice_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_adafruit_bus_device_spidevice_enter(self);
return self->spi;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(adafruit_bus_device_spidevice___enter___obj, adafruit_bus_device_spidevice_obj___enter__);
STATIC mp_obj_t adafruit_bus_device_spidevice_obj___exit__(size_t n_args, const mp_obj_t *args) {
common_hal_adafruit_bus_device_spidevice_exit(MP_OBJ_TO_PTR(args[0]));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_spidevice___exit___obj, 4, 4, adafruit_bus_device_spidevice_obj___exit__);
STATIC const mp_rom_map_elem_t adafruit_bus_device_spidevice_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&adafruit_bus_device_spidevice___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adafruit_bus_device_spidevice___exit___obj) },
};
STATIC MP_DEFINE_CONST_DICT(adafruit_bus_device_spidevice_locals_dict, adafruit_bus_device_spidevice_locals_dict_table);
const mp_obj_type_t adafruit_bus_device_spidevice_type = {
{ &mp_type_type },
.name = MP_QSTR_SPIDevice,
.make_new = adafruit_bus_device_spidevice_make_new,
.locals_dict = (mp_obj_dict_t*)&adafruit_bus_device_spidevice_locals_dict,
};

View File

@ -0,0 +1,50 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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.
*/
// Machine is the HAL for low-level, hardware accelerated functions. It is not
// meant to simplify APIs, its only meant to unify them so that other modules
// do not require port specific logic.
//
// This file includes externs for all functions a port should implement to
// support the machine module.
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_SPIDEVICE_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_SPIDEVICE_H
#include "py/obj.h"
#include "shared-module/adafruit_bus_device/SPIDevice.h"
// Type object used in Python. Should be shared between ports.
extern const mp_obj_type_t adafruit_bus_device_spidevice_type;
// Initializes the hardware peripheral.
extern void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks);
extern void common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self);
extern void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_SPIDEVICE_H

View File

@ -0,0 +1,78 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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.
*/
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "py/objproperty.h"
#include "shared-bindings/adafruit_bus_device/__init__.h"
#include "shared-bindings/adafruit_bus_device/I2CDevice.h"
#include "shared-bindings/adafruit_bus_device/SPIDevice.h"
STATIC const mp_rom_map_elem_t adafruit_bus_device_i2c_device_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_i2c_device) },
{ MP_ROM_QSTR(MP_QSTR_I2CDevice), MP_ROM_PTR(&adafruit_bus_device_i2cdevice_type) },
};
STATIC MP_DEFINE_CONST_DICT(adafruit_bus_device_i2c_device_globals, adafruit_bus_device_i2c_device_globals_table);
const mp_obj_module_t adafruit_bus_device_i2c_device_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&adafruit_bus_device_i2c_device_globals,
};
STATIC const mp_rom_map_elem_t adafruit_bus_device_spi_device_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_spi_device) },
{ MP_ROM_QSTR(MP_QSTR_SPIDevice), MP_ROM_PTR(&adafruit_bus_device_spidevice_type) },
};
STATIC MP_DEFINE_CONST_DICT(adafruit_bus_device_spi_device_globals, adafruit_bus_device_spi_device_globals_table);
const mp_obj_module_t adafruit_bus_device_spi_device_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&adafruit_bus_device_spi_device_globals,
};
//| """Hardware accelerated external bus access
//|
//| The I2CDevice and SPIDevice helper classes make managing transaction state on a bus easy.
//| For example, they manage locking the bus to prevent other concurrent access. For SPI
//| devices, it manages the chip select and protocol changes such as mode. For I2C, it
//| manages the device address."""
//|
STATIC const mp_rom_map_elem_t adafruit_bus_device_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adafruit_bus_device) },
{ MP_ROM_QSTR(MP_QSTR_i2c_device), MP_ROM_PTR(&adafruit_bus_device_i2c_device_module) },
{ MP_ROM_QSTR(MP_QSTR_spi_device), MP_ROM_PTR(&adafruit_bus_device_spi_device_module) },
};
STATIC MP_DEFINE_CONST_DICT(adafruit_bus_device_module_globals, adafruit_bus_device_module_globals_table);
const mp_obj_module_t adafruit_bus_device_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&adafruit_bus_device_module_globals,
};

View File

@ -0,0 +1,32 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE___INIT___H
// Nothing now.
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE___INIT___H

View File

@ -0,0 +1,79 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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.
*/
#include "shared-bindings/adafruit_bus_device/I2CDevice.h"
#include "shared-bindings/busio/I2C.h"
#include "py/mperrno.h"
#include "py/nlr.h"
#include "py/runtime.h"
#include "lib/utils/interrupt_char.h"
void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address) {
self->i2c = i2c;
self->device_address = device_address;
}
void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self) {
bool success = common_hal_busio_i2c_try_lock(self->i2c);
while (!success) {
RUN_BACKGROUND_TASKS;
if (mp_hal_is_interrupted()) {
break;
}
success = common_hal_busio_i2c_try_lock(self->i2c);
}
}
void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self) {
common_hal_busio_i2c_unlock(self->i2c);
}
uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
return common_hal_busio_i2c_read(self->i2c, self->device_address, buffer, length);
}
uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
return common_hal_busio_i2c_write(self->i2c, self->device_address, buffer, length, true);
}
void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self) {
common_hal_adafruit_bus_device_i2cdevice_lock(self);
mp_buffer_info_t bufinfo;
mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(self, (uint8_t*)bufinfo.buf, 1);
if (status != 0) {
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
}
common_hal_adafruit_bus_device_i2cdevice_unlock(self);
}

View File

@ -0,0 +1,39 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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.
*/
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_I2CDEVICE_H
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_I2CDEVICE_H
#include "py/obj.h"
#include "common-hal/busio/I2C.h"
typedef struct {
mp_obj_base_t base;
busio_i2c_obj_t *i2c;
uint8_t device_address;
} adafruit_bus_device_i2cdevice_obj_t;
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_I2CDEVICE_H

View File

@ -0,0 +1,85 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Mark Komus
*
* 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.
*/
#include "shared-bindings/adafruit_bus_device/SPIDevice.h"
#include "shared-bindings/busio/SPI.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "py/mperrno.h"
#include "py/nlr.h"
#include "py/runtime.h"
void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks) {
self->spi = spi;
self->baudrate = baudrate;
self->polarity = polarity;
self->phase = phase;
self->extra_clocks = extra_clocks;
self->chip_select = cs;
}
void common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self) {
bool success = false;
while (!success) {
success = common_hal_busio_spi_try_lock(self->spi);
RUN_BACKGROUND_TASKS;
mp_handle_pending();
}
common_hal_busio_spi_configure(self->spi, self->baudrate, self->polarity, self->phase, 8);
if (self->chip_select != MP_OBJ_NULL) {
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), false);
}
}
void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self) {
if (self->chip_select != MP_OBJ_NULL) {
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), true);
}
if (self->extra_clocks > 0) {
mp_buffer_info_t bufinfo;
mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
((uint8_t*)bufinfo.buf)[0] = 0xFF;
uint8_t clocks = self->extra_clocks / 8;
if ((self->extra_clocks % 8) != 0)
clocks += 1;
while (clocks > 0) {
if (!common_hal_busio_spi_write(self->spi, ((uint8_t*)bufinfo.buf), 1)) {
mp_raise_OSError(MP_EIO);
}
clocks--;
}
}
common_hal_busio_spi_unlock(self->spi);
}

View File

@ -0,0 +1,44 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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.
*/
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H
#include "py/obj.h"
#include "common-hal/busio/SPI.h"
#include "common-hal/digitalio/DigitalInOut.h"
typedef struct {
mp_obj_base_t base;
busio_spi_obj_t *spi;
uint32_t baudrate;
uint8_t polarity;
uint8_t phase;
uint8_t extra_clocks;
digitalio_digitalinout_obj_t *chip_select;
} adafruit_bus_device_spidevice_obj_t;
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H