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
|
|
|
|
// bitbangio.SPI class.
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "shared-bindings/bitbangio/SPI.h"
|
2016-11-29 18:49:50 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "shared-bindings/util.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2018-05-09 15:12:42 -04:00
|
|
|
#include "lib/utils/buffer_helper.h"
|
2017-02-19 10:11:33 -05:00
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
2017-02-24 09:13:07 -05:00
|
|
|
#include "py/mperrno.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"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| class SPI:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """A 3-4 wire serial protocol
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| SPI is a serial protocol that has exclusive pins for data in and out of the
|
2020-06-14 15:28:11 -04:00
|
|
|
//| main device. It is typically faster than :py:class:`~bitbangio.I2C` because a
|
|
|
|
//| separate pin is used to select a device rather than a transmitted
|
2020-05-01 18:23:27 -04:00
|
|
|
//| address. This class only manages three of the four SPI lines: `!clock`,
|
2020-06-14 15:28:11 -04:00
|
|
|
//| `!MOSI`, `!MISO`. Its up to the client to manage the appropriate
|
|
|
|
//| select line, often abbreviated `!CS` or `!SS`. (This is common because
|
|
|
|
//| multiple secondaries can share the `!clock`, `!MOSI` and `!MISO` lines
|
|
|
|
//| and therefore the hardware.)"""
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| def __init__(self, clock: microcontroller.Pin, MOSI: Optional[microcontroller.Pin] = None, MISO: Optional[microcontroller.Pin] = None) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Construct an SPI object on the given pins.
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-06-14 15:28:11 -04:00
|
|
|
//| .. seealso:: Using this class directly requires careful lock management.
|
|
|
|
//| Instead, use :class:`~adafruit_bus_device.spi_device.SPIDevice` to
|
|
|
|
//| manage locks.
|
|
|
|
//|
|
|
|
|
//| .. 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.
|
|
|
|
//|
|
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param ~microcontroller.Pin clock: the pin to use for the clock.
|
2020-06-14 15:28:11 -04:00
|
|
|
//| :param ~microcontroller.Pin MOSI: the Main Out Selected In pin.
|
|
|
|
//| :param ~microcontroller.Pin MISO: the Main In Selected Out pin."""
|
2020-05-01 18:23:27 -04:00
|
|
|
//| ...
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
|
|
|
|
|
|
|
// TODO(tannewt): Support LSB SPI.
|
2019-01-14 20:26:36 -05:00
|
|
|
STATIC mp_obj_t bitbangio_spi_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2016-11-03 18:50:59 -04:00
|
|
|
enum { ARG_clock, ARG_MOSI, ARG_MISO, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2021-03-15 09:57:36 -04:00
|
|
|
{ MP_QSTR_clock, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_MOSI, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_MISO, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2016-11-03 18:50:59 -04: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 *clock = validate_obj_is_free_pin(args[ARG_clock].u_obj);
|
|
|
|
const mcu_pin_obj_t *mosi = validate_obj_is_free_pin_or_none(args[ARG_MOSI].u_obj);
|
|
|
|
const mcu_pin_obj_t *miso = validate_obj_is_free_pin_or_none(args[ARG_MISO].u_obj);
|
2019-01-14 20:26:36 -05:00
|
|
|
|
|
|
|
bitbangio_spi_obj_t *self = m_new_obj(bitbangio_spi_obj_t);
|
|
|
|
self->base.type = &bitbangio_spi_type;
|
2016-11-30 18:08:34 -05:00
|
|
|
shared_module_bitbangio_spi_construct(self, clock, mosi, miso);
|
2016-11-03 18:50:59 -04:00
|
|
|
return (mp_obj_t)self;
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def deinit(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Turn off the SPI bus."""
|
|
|
|
//| ...
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t bitbangio_spi_obj_deinit(mp_obj_t self_in) {
|
|
|
|
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
shared_module_bitbangio_spi_deinit(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_deinit_obj, bitbangio_spi_obj_deinit);
|
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(bitbangio_spi_obj_t *self) {
|
|
|
|
if (shared_module_bitbangio_spi_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __enter__(self) -> SPI:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """No-op used by Context Managers."""
|
|
|
|
//| ...
|
2016-11-03 18:50:59 -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-05-01 18:23:27 -04:00
|
|
|
//| """Automatically deinitializes the hardware when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t bitbangio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args;
|
|
|
|
shared_module_bitbangio_spi_deinit(args[0]);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_obj___exit___obj, 4, 4, bitbangio_spi_obj___exit__);
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
|
|
|
|
static void check_lock(bitbangio_spi_obj_t *self) {
|
|
|
|
if (!shared_module_bitbangio_spi_has_lock(self)) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("Function requires lock"));
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 12:37:03 -04:00
|
|
|
//| def configure(self, *, baudrate: int = 100000, polarity: int = 0, phase: int = 0, bits: int = 8) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Configures the SPI bus. Only valid when locked.
|
2016-11-03 18:50:59 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param int baudrate: the clock rate in Hertz
|
|
|
|
//| :param int polarity: the base state of the clock line (0 or 1)
|
|
|
|
//| :param int phase: the edge of the clock that data is captured. First (0)
|
|
|
|
//| or second (1). Rising or falling depends on clock polarity.
|
|
|
|
//| :param int bits: the number of bits per word"""
|
|
|
|
//| ...
|
2017-03-07 06:58:57 -05:00
|
|
|
//|
|
2016-11-30 18:08:34 -05:00
|
|
|
STATIC mp_obj_t bitbangio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits };
|
2016-11-03 18:50:59 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2016-11-30 18:08:34 -05:00
|
|
|
{ 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_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
|
|
|
bitbangio_spi_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);
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
uint8_t polarity = args[ARG_polarity].u_int;
|
|
|
|
if (polarity != 0 && polarity != 1) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("Invalid polarity"));
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
uint8_t phase = args[ARG_phase].u_int;
|
|
|
|
if (phase != 0 && phase != 1) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("Invalid phase"));
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
uint8_t bits = args[ARG_bits].u_int;
|
|
|
|
if (bits != 8 && bits != 9) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("Invalid number of bits"));
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
shared_module_bitbangio_spi_configure(self, args[ARG_baudrate].u_int, polarity, phase, bits);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_configure_obj, 1, bitbangio_spi_configure);
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def try_lock(self) -> bool:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Attempts to grab the SPI lock. Returns True on success.
|
2016-11-30 18:08:34 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :return: True when lock has been grabbed
|
|
|
|
//| :rtype: bool"""
|
|
|
|
//| ...
|
2017-03-07 06:58:57 -05:00
|
|
|
//|
|
2016-11-30 18:08:34 -05:00
|
|
|
STATIC mp_obj_t bitbangio_spi_obj_try_lock(mp_obj_t self_in) {
|
2017-10-02 20:49:40 -04:00
|
|
|
bitbangio_spi_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(shared_module_bitbangio_spi_try_lock(self));
|
2016-11-30 18:08:34 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock);
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def unlock(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Releases the SPI lock."""
|
|
|
|
//| ...
|
2016-11-30 18:08:34 -05:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t bitbangio_spi_obj_unlock(mp_obj_t self_in) {
|
2017-10-02 20:49:40 -04:00
|
|
|
bitbangio_spi_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
|
|
|
shared_module_bitbangio_spi_unlock(self);
|
2016-11-30 18:08:34 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock);
|
|
|
|
|
2020-07-02 17:47:52 -04:00
|
|
|
//| def write(self, buf: ReadableBuffer) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Write the data contained in ``buf``. Requires the SPI being locked.
|
|
|
|
//| If the buffer is empty, nothing happens."""
|
|
|
|
//| ...
|
2016-11-30 18:08:34 -05:00
|
|
|
//|
|
2017-04-20 14:24:05 -04:00
|
|
|
// TODO(tannewt): Add support for start and end kwargs.
|
2016-11-30 18:08:34 -05:00
|
|
|
STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) {
|
2017-10-02 20:49:40 -04:00
|
|
|
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2016-11-30 18:08:34 -05:00
|
|
|
mp_buffer_info_t src;
|
|
|
|
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
|
2018-01-02 21:25:41 -05:00
|
|
|
if (src.len == 0) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
check_lock(self);
|
|
|
|
bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len);
|
|
|
|
if (!ok) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2016-11-30 18:08:34 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_spi_write_obj, bitbangio_spi_write);
|
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2021-03-26 13:06:25 -04:00
|
|
|
//| def readinto(self, buffer: WriteableBuffer, *, start: int = 0, end: Optional[int] = None, write_value: int = 0) -> None:
|
|
|
|
//| """Read into ``buffer`` while writing ``write_value`` for each byte read.
|
|
|
|
//| The SPI object must be locked.
|
|
|
|
//| If the number of bytes to read is 0, nothing happens.
|
|
|
|
//|
|
|
|
|
//| :param bytearray buffer: Read data into this buffer
|
|
|
|
//| :param int start: Start of the slice of ``buffer`` to read into: ``buffer[start:end]``
|
|
|
|
//| :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``
|
|
|
|
//| :param int write_value: Value to write while reading."""
|
2020-05-01 18:23:27 -04:00
|
|
|
//| ...
|
2016-11-30 18:08:34 -05:00
|
|
|
//|
|
2021-03-26 13:06:25 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_buffer, ARG_start, ARG_end, ARG_write_value };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ 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} },
|
|
|
|
{ MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
};
|
|
|
|
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2021-03-26 13:06:25 -04:00
|
|
|
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);
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
mp_buffer_info_t bufinfo;
|
2021-03-26 13:06:25 -04:00
|
|
|
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
|
|
|
|
int32_t start = args[ARG_start].u_int;
|
|
|
|
size_t length = bufinfo.len;
|
|
|
|
normalize_buffer_bounds(&start, args[ARG_end].u_int, &length);
|
|
|
|
|
|
|
|
if (length == 0) {
|
2018-01-02 21:25:41 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2021-03-26 13:06:25 -04:00
|
|
|
|
2021-03-26 16:38:20 -04:00
|
|
|
bool ok = shared_module_bitbangio_spi_read(self, ((uint8_t *)bufinfo.buf) + start, length, args[ARG_write_value].u_int);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (!ok) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2021-03-26 13:06:25 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_readinto_obj, 2, bitbangio_spi_readinto);
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2021-03-26 13:06:25 -04:00
|
|
|
//| def write_readinto(self, buffer_out: ReadableBuffer, buffer_in: ReadableBuffer, *, out_start: int = 0, out_end: Optional[int] = None, in_start: int = 0, in_end: Optional[int] = None) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Write out the data in ``buffer_out`` while simultaneously reading data into ``buffer_in``.
|
2021-03-26 13:06:25 -04:00
|
|
|
//| The SPI object must be locked.
|
2020-05-01 18:23:27 -04:00
|
|
|
//| The lengths of the slices defined by ``buffer_out[out_start:out_end]`` and ``buffer_in[in_start:in_end]``
|
|
|
|
//| must be equal.
|
|
|
|
//| If buffer slice lengths are both 0, nothing happens.
|
2018-05-09 15:12:42 -04:00
|
|
|
//|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| :param ~_typing.ReadableBuffer buffer_out: Write out the data in this buffer
|
|
|
|
//| :param ~_typing.WriteableBuffer buffer_in: Read data into this buffer
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param int out_start: Start of the slice of buffer_out to write out: ``buffer_out[out_start:out_end]``
|
|
|
|
//| :param int out_end: End of the slice; this index is not included. Defaults to ``len(buffer_out)``
|
|
|
|
//| :param int in_start: Start of the slice of ``buffer_in`` to read into: ``buffer_in[in_start:in_end]``
|
|
|
|
//| :param int in_end: End of the slice; this index is not included. Defaults to ``len(buffer_in)``"""
|
|
|
|
//| ...
|
2018-05-09 15:12:42 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t bitbangio_spi_write_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_buffer_out, ARG_buffer_in, ARG_out_start, ARG_out_end, ARG_in_start, ARG_in_end };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_buffer_out, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_buffer_in, 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} },
|
|
|
|
};
|
|
|
|
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2021-03-26 13:06:25 -04:00
|
|
|
check_lock(self);
|
2018-01-30 17:18:57 -05:00
|
|
|
|
2018-05-09 15:12:42 -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);
|
2018-01-30 17:18:57 -05:00
|
|
|
|
2018-05-09 15:12:42 -04:00
|
|
|
mp_buffer_info_t buf_out_info;
|
|
|
|
mp_get_buffer_raise(args[ARG_buffer_out].u_obj, &buf_out_info, MP_BUFFER_READ);
|
|
|
|
int32_t out_start = args[ARG_out_start].u_int;
|
2019-08-27 15:49:46 -04:00
|
|
|
size_t out_length = buf_out_info.len;
|
2018-05-09 15:12:42 -04:00
|
|
|
normalize_buffer_bounds(&out_start, args[ARG_out_end].u_int, &out_length);
|
2018-01-30 17:18:57 -05:00
|
|
|
|
2018-05-09 15:12:42 -04:00
|
|
|
mp_buffer_info_t buf_in_info;
|
|
|
|
mp_get_buffer_raise(args[ARG_buffer_in].u_obj, &buf_in_info, MP_BUFFER_WRITE);
|
|
|
|
int32_t in_start = args[ARG_in_start].u_int;
|
2019-08-27 15:49:46 -04:00
|
|
|
size_t in_length = buf_in_info.len;
|
2018-05-09 15:12:42 -04:00
|
|
|
normalize_buffer_bounds(&in_start, args[ARG_in_end].u_int, &in_length);
|
2018-01-30 17:18:57 -05:00
|
|
|
|
2018-05-09 15:12:42 -04:00
|
|
|
if (out_length != in_length) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("buffer slices must be of equal length"));
|
2018-05-09 15:12:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (out_length == 0) {
|
|
|
|
return mp_const_none;
|
2018-02-05 10:44:29 -05:00
|
|
|
}
|
2018-01-30 17:18:57 -05:00
|
|
|
|
|
|
|
bool ok = shared_module_bitbangio_spi_transfer(self,
|
2021-03-15 09:57:36 -04:00
|
|
|
((uint8_t *)buf_out_info.buf) + out_start,
|
|
|
|
((uint8_t *)buf_in_info.buf) + in_start,
|
|
|
|
out_length);
|
2018-01-30 17:18:57 -05:00
|
|
|
if (!ok) {
|
|
|
|
mp_raise_OSError(MP_EIO);
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2018-05-09 15:12:42 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_write_readinto_obj, 2, bitbangio_spi_write_readinto);
|
2018-01-30 17:18:57 -05:00
|
|
|
|
2016-11-03 18:50:59 -04:00
|
|
|
STATIC const mp_rom_map_elem_t bitbangio_spi_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bitbangio_spi_deinit_obj) },
|
2017-02-19 10:11:33 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
2016-11-03 18:50:59 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_spi_obj___exit___obj) },
|
|
|
|
|
2016-11-30 18:08:34 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&bitbangio_spi_configure_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&bitbangio_spi_try_lock_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&bitbangio_spi_unlock_obj) },
|
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&bitbangio_spi_readinto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&bitbangio_spi_write_obj) },
|
2018-01-30 17:45:04 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&bitbangio_spi_write_readinto_obj) },
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(bitbangio_spi_locals_dict, bitbangio_spi_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t bitbangio_spi_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_SPI,
|
|
|
|
.make_new = bitbangio_spi_make_new,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&bitbangio_spi_locals_dict,
|
2016-11-03 18:50:59 -04:00
|
|
|
};
|