|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Mark Komus |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +// This file contains all of the Python API definitions for the |
| 28 | +// busio.SPI class. |
| 29 | + |
| 30 | +#include "shared-bindings/microcontroller/Pin.h" |
| 31 | +#include "shared-bindings/busdevice/SPIDevice.h" |
| 32 | +#include "shared-bindings/util.h" |
| 33 | +#include "shared-module/busdevice/SPIDevice.h" |
| 34 | +#include "common-hal/digitalio/DigitalInOut.h" |
| 35 | +#include "shared-bindings/digitalio/DigitalInOut.h" |
| 36 | + |
| 37 | + |
| 38 | +#include "lib/utils/buffer_helper.h" |
| 39 | +#include "lib/utils/context_manager_helpers.h" |
| 40 | +#include "py/runtime.h" |
| 41 | +#include "supervisor/shared/translate.h" |
| 42 | + |
| 43 | + |
| 44 | +//| class SPIDevice: |
| 45 | +//| """ |
| 46 | +//| Represents a single SPI device and manages locking the bus and the device |
| 47 | +//| address. |
| 48 | +//| :param ~busio.SPI spi: The SPI bus the device is on |
| 49 | +//| :param int device_address: The 7 bit device address |
| 50 | +//| :param bool probe: Probe for the device upon object creation, default is true |
| 51 | +//| .. note:: This class is **NOT** built into CircuitPython. See |
| 52 | +//| :ref:`here for install instructions <bus_device_installation>`. |
| 53 | +//| Example: |
| 54 | +//| .. code-block:: python |
| 55 | +//| import busio |
| 56 | +//| from board import * |
| 57 | +//| from adafruit_bus_device.spi_device import SPIDevice |
| 58 | +//| with busio.SPI(SCL, SDA) as spi: |
| 59 | +//| device = SPIDevice(spi, 0x70) |
| 60 | +//| bytes_read = bytearray(4) |
| 61 | +//| with device: |
| 62 | +//| device.readinto(bytes_read) |
| 63 | +//| # A second transaction |
| 64 | +//| with device: |
| 65 | +//| device.write(bytes_read)""" |
| 66 | +//| ... |
| 67 | +//| |
| 68 | +STATIC mp_obj_t busdevice_spidevice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 69 | + busdevice_spidevice_obj_t *self = m_new_obj(busdevice_spidevice_obj_t); |
| 70 | + self->base.type = &busdevice_spidevice_type; |
| 71 | + enum { ARG_spi, ARG_chip_select, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks }; |
| 72 | + static const mp_arg_t allowed_args[] = { |
| 73 | + { MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 74 | + { MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 75 | + { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} }, |
| 76 | + { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 77 | + { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 78 | + { MP_QSTR_extra_clocks, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 79 | + }; |
| 80 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 81 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 82 | + |
| 83 | + busio_spi_obj_t* spi = args[ARG_spi].u_obj; |
| 84 | + |
| 85 | + common_hal_busdevice_spidevice_construct(MP_OBJ_TO_PTR(self), spi, args[ARG_chip_select].u_obj, args[ARG_baudrate].u_int, args[ARG_polarity].u_int, |
| 86 | + args[ARG_phase].u_int, args[ARG_extra_clocks].u_int); |
| 87 | + |
| 88 | + if (args[ARG_chip_select].u_obj != MP_OBJ_NULL) { |
| 89 | + digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(MP_OBJ_TO_PTR(args[ARG_chip_select].u_obj), |
| 90 | + true, DRIVE_MODE_PUSH_PULL); |
| 91 | + if (result == DIGITALINOUT_INPUT_ONLY) { |
| 92 | + mp_raise_NotImplementedError(translate("Pin is input only")); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return (mp_obj_t)self; |
| 97 | +} |
| 98 | + |
| 99 | +STATIC mp_obj_t busdevice_spidevice_obj___enter__(mp_obj_t self_in) { |
| 100 | + busdevice_spidevice_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 101 | + common_hal_busdevice_spidevice_enter(self); |
| 102 | + return self; |
| 103 | +} |
| 104 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(busdevice_spidevice___enter___obj, busdevice_spidevice_obj___enter__); |
| 105 | + |
| 106 | +STATIC mp_obj_t busdevice_spidevice_obj___exit__(size_t n_args, const mp_obj_t *args) { |
| 107 | + common_hal_busdevice_spidevice_exit(MP_OBJ_TO_PTR(args[0])); |
| 108 | + return mp_const_none; |
| 109 | +} |
| 110 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busdevice_spidevice___exit___obj, 4, 4, busdevice_spidevice_obj___exit__); |
| 111 | + |
| 112 | +STATIC const mp_rom_map_elem_t busdevice_spidevice_locals_dict_table[] = { |
| 113 | + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&busdevice_spidevice___enter___obj) }, |
| 114 | + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busdevice_spidevice___exit___obj) }, |
| 115 | +}; |
| 116 | + |
| 117 | +STATIC MP_DEFINE_CONST_DICT(busdevice_spidevice_locals_dict, busdevice_spidevice_locals_dict_table); |
| 118 | + |
| 119 | +const mp_obj_type_t busdevice_spidevice_type = { |
| 120 | + { &mp_type_type }, |
| 121 | + .name = MP_QSTR_SPIDevice, |
| 122 | + .make_new = busdevice_spidevice_make_new, |
| 123 | + .locals_dict = (mp_obj_dict_t*)&busdevice_spidevice_locals_dict, |
| 124 | +}; |
0 commit comments