From 26229efe78f6c5f8598d6f9f1b2261c5a84d7a7b Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 30 Nov 2016 15:08:34 -0800 Subject: Add try_lock and unlock to I2C and SPI classes to make sure things are shared well between threads and underlying MicroPython (SPI Flash for example.) It is recommended to use the bus device classes to manage the locks and other transaction state. https://github.com/adafruit/Adafruit_MicroPython_BusDevice Fixed #58 Fixed #59 Fixed #60 --- shared-bindings/bitbangio/I2C.c | 133 ++++++++++++++++++++++++++++++++-------- shared-bindings/bitbangio/I2C.h | 6 +- shared-bindings/bitbangio/SPI.c | 123 ++++++++++++++++++++++++++----------- shared-bindings/bitbangio/SPI.h | 21 ++++--- 4 files changed, 214 insertions(+), 69 deletions(-) (limited to 'shared-bindings/bitbangio') diff --git a/shared-bindings/bitbangio/I2C.c b/shared-bindings/bitbangio/I2C.c index 9b2dc4f2c..cc77dc00e 100644 --- a/shared-bindings/bitbangio/I2C.c +++ b/shared-bindings/bitbangio/I2C.c @@ -36,7 +36,7 @@ //| :class:`I2C` --- Two wire serial protocol //| ------------------------------------------ //| -//| .. class:: I2C(scl, sda, \*, freq=400000) +//| .. class:: I2C(scl, sda, \* frequency=400000) //| //| 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 @@ -44,7 +44,7 @@ //| //| :param ~microcontroller.Pin scl: The clock pin //| :param ~microcontroller.Pin sda: The data pin -//| :param int freq: The clock frequency +//| :param int frequency: The clock frequency of the bus //| STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); @@ -52,11 +52,11 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, self->base.type = &bitbangio_i2c_type; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_scl, ARG_sda, ARG_freq }; + enum { ARG_scl, ARG_sda, ARG_frequency }; 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 }, - { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, + { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, }; 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); @@ -64,7 +64,7 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, assert_pin(args[ARG_sda].u_obj, false); const mcu_pin_obj_t* scl = MP_OBJ_TO_PTR(args[ARG_scl].u_obj); const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj); - shared_module_bitbangio_i2c_construct(self, scl, sda, args[ARG_freq].u_int); + shared_module_bitbangio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int); return (mp_obj_t)self; } @@ -99,6 +99,12 @@ STATIC mp_obj_t bitbangio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_i2c_obj___exit___obj, 4, 4, bitbangio_i2c_obj___exit__); +static void check_lock(bitbangio_i2c_obj_t *self) { + if (!shared_module_bitbangio_i2c_has_lock(self)) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Function requires I2C lock.")); + } +} + //| .. method:: I2C.scan() //| //| Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of @@ -107,6 +113,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_i2c_obj___exit___obj, 4, 4, //| STATIC mp_obj_t bitbangio_i2c_scan(mp_obj_t self_in) { bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + 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) { @@ -119,19 +126,97 @@ STATIC mp_obj_t bitbangio_i2c_scan(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_scan_obj, bitbangio_i2c_scan); -//| .. method:: I2C.writeto(address, buffer, stop=True) +//| .. method:: I2C.try_lock() +//| +//| Attempts to grab the I2C lock. Returns True on success. +//| +STATIC mp_obj_t bitbangio_i2c_obj_try_lock(mp_obj_t self_in) { + shared_module_bitbangio_i2c_try_lock(MP_OBJ_TO_PTR(self_in)); + return self_in; +} +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock); + +//| .. method:: I2C.unlock() +//| +//| Releases the I2C lock. +//| +STATIC mp_obj_t bitbangio_i2c_obj_unlock(mp_obj_t self_in) { + shared_module_bitbangio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_unlock_obj, bitbangio_i2c_obj_unlock); + +//| .. method:: I2C.readfrom_into(address, buffer, \*, start=0, end=len(buffer)) +//| +//| Read into ``buffer`` from the slave specified by ``address``. +//| The number of bytes read will be the length of ``buffer``. +//| +//| 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. +//| +//| :param int address: 7-bit device address +//| :param bytearray buffer: buffer to write into +//| :param int start: Index to start writing at +//| :param int end: Index to write up to but not include +//| +STATIC mp_obj_t bitbangio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + 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} }, + }; + bitbangio_i2c_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); + check_lock(self); + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE); + int32_t end = args[ARG_end].u_int; + if (end < 0) { + end += bufinfo.len; + } + uint32_t start = args[ARG_start].u_int; + uint32_t len = end - start; + if ((uint32_t) end < start) { + len = 0; + } else if (len > bufinfo.len) { + len = bufinfo.len; + } + shared_module_bitbangio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_i2c_readfrom_into_obj, 3, bitbangio_i2c_readfrom_into); + +//| .. method:: I2C.writeto(address, buffer, \*, start=0, end=len(buffer), stop=True) //| //| Write the bytes from ``buffer`` to the slave specified by ``address``. //| Transmits a stop bit if ``stop`` is set. //| +//| 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 int address: 7-bit device address +//| :param bytearray buffer: 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 +//| :param bool stop: If true, output an I2C stop condition after the +//| buffer is written +//| STATIC mp_obj_t bitbangio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_address, ARG_buffer, ARG_stop }; + enum { ARG_address, ARG_buffer, ARG_start, ARG_end, ARG_stop }; 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_stop, MP_ARG_BOOL, {.u_bool = true} }, + { 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_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, }; bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + 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); @@ -139,9 +224,21 @@ STATIC mp_obj_t bitbangio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, m mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); + int32_t end = args[ARG_end].u_int; + if (end < 0) { + end += bufinfo.len; + } + uint32_t start = args[ARG_start].u_int; + uint32_t len = end - start; + if ((uint32_t) end < start) { + len = 0; + } else if (len > bufinfo.len) { + len = bufinfo.len; + } + // do the transfer bool ok = shared_module_bitbangio_i2c_write(self, args[ARG_address].u_int, - bufinfo.buf, bufinfo.len, args[ARG_stop].u_bool); + ((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool); if (!ok) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error")); } @@ -149,27 +246,15 @@ STATIC mp_obj_t bitbangio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, m } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_i2c_writeto_obj, 1, bitbangio_i2c_writeto); -//| .. method:: I2C.readfrom_into(address, buffer) -//| -//| Read into ``buffer`` from the slave specified by ``address``. -//| The number of bytes read will be the length of `buf`. -//| -STATIC mp_obj_t bitbangio_i2c_readfrom_into(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_t buf_in) { - bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); - shared_module_bitbangio_i2c_read(self, mp_obj_get_int(addr_in), (uint8_t*)bufinfo.buf, bufinfo.len); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_3(bitbangio_i2c_readfrom_into_obj, bitbangio_i2c_readfrom_into); - STATIC const mp_rom_map_elem_t bitbangio_i2c_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bitbangio_i2c_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&bitbangio_i2c___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_i2c_obj___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&bitbangio_i2c_scan_obj) }, - // standard bus operations + { MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&bitbangio_i2c_try_lock_obj) }, + { MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&bitbangio_i2c_unlock_obj) }, + { MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&bitbangio_i2c_writeto_obj) }, { MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&bitbangio_i2c_readfrom_into_obj) }, }; diff --git a/shared-bindings/bitbangio/I2C.h b/shared-bindings/bitbangio/I2C.h index 71d379eb8..451fa4d94 100644 --- a/shared-bindings/bitbangio/I2C.h +++ b/shared-bindings/bitbangio/I2C.h @@ -39,10 +39,14 @@ extern const mp_obj_type_t bitbangio_i2c_type; extern void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self, const mcu_pin_obj_t * scl, const mcu_pin_obj_t * sda, - uint32_t freq); + uint32_t frequency); extern void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self); +extern bool shared_module_bitbangio_i2c_try_lock(bitbangio_i2c_obj_t *self); +extern bool shared_module_bitbangio_i2c_has_lock(bitbangio_i2c_obj_t *self); +extern void shared_module_bitbangio_i2c_unlock(bitbangio_i2c_obj_t *self); + // Probe the bus to see if a device acknowledges the given address. extern bool shared_module_bitbangio_i2c_probe(bitbangio_i2c_obj_t *self, uint8_t addr); diff --git a/shared-bindings/bitbangio/SPI.c b/shared-bindings/bitbangio/SPI.c index ea8c2f255..bec6fcdaa 100644 --- a/shared-bindings/bitbangio/SPI.c +++ b/shared-bindings/bitbangio/SPI.c @@ -47,14 +47,13 @@ //| select line. (This is common because multiple slaves can share the `!clock`, //| `!MOSI` and `!MISO` lines and therefore the hardware.) //| -//| .. class:: SPI(clock, MOSI, MISO, baudrate=1000000) +//| .. class:: SPI(clock, MOSI, MISO) //| //| Construct an SPI object on the given pins. //| //| :param ~microcontroller.Pin clock: the pin to use for the clock. //| :param ~microcontroller.Pin MOSI: the Master Out Slave In pin. //| :param ~microcontroller.Pin MISO: the Master In Slave Out pin. -//| :param int baudrate: is the SCK clock rate. //| // TODO(tannewt): Support LSB SPI. @@ -70,10 +69,6 @@ STATIC mp_obj_t bitbangio_spi_make_new(const mp_obj_type_t *type, size_t n_args, { 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} }, - { 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 = 400000} }, - { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} }, - { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, }; 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); @@ -83,7 +78,7 @@ STATIC mp_obj_t bitbangio_spi_make_new(const mp_obj_type_t *type, size_t n_args, const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(args[ARG_clock].u_obj); const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(args[ARG_MOSI].u_obj); const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(args[ARG_MISO].u_obj); - shared_module_bitbangio_spi_construct(self, clock, mosi, miso, args[ARG_baudrate].u_int); + shared_module_bitbangio_spi_construct(self, clock, mosi, miso); return (mp_obj_t)self; } @@ -118,59 +113,113 @@ STATIC mp_obj_t bitbangio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_obj___exit___obj, 4, 4, bitbangio_spi_obj___exit__); -//| .. method:: SPI.transfer(write_buffer=None, read_buffer=None, address=0) -//| -//| Write out ``write_buffer`` and then read into ``read_buffer``. They do -//| not need to be the same length. If either buffer is omitted then the -//| transfer skips the corresponding portion. -//| -//| ``address`` is taken for I2C compatibility but is ignored. + +static void check_lock(bitbangio_spi_obj_t *self) { + if (!shared_module_bitbangio_spi_has_lock(self)) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Function requires SPI lock.")); + } +} + +//| .. method:: SPI.configure(baudrate=100000) //| -//| When writing, data received is dropped. When reading, zeroes are written -//| out. +//| Configures the SPI bus. Only valid when locked. //| -STATIC mp_obj_t bitbangio_spi_transfer(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_write_buffer, ARG_read_buffer, ARG_address }; +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 }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_write_buffer, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL } }, - { MP_QSTR_read_buffer, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL } }, - { MP_QSTR_address, MP_ARG_INT, {.u_int = 0} }, + { 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} }, }; bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + 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); - // get the buffer to store data into - mp_buffer_info_t write_bufinfo; - if (!mp_get_buffer(args[ARG_write_buffer].u_obj, &write_bufinfo, MP_BUFFER_READ)) { - write_bufinfo.len = 0; + uint8_t polarity = args[ARG_polarity].u_int; + if (polarity != 0 && polarity != 1) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid polarity.")); } - - mp_buffer_info_t read_bufinfo; - if (!mp_get_buffer(args[ARG_read_buffer].u_obj, &read_bufinfo, MP_BUFFER_WRITE)) { - read_bufinfo.len = 0; + uint8_t phase = args[ARG_phase].u_int; + if (phase != 0 && phase != 1) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid phase.")); + } + uint8_t bits = args[ARG_bits].u_int; + if (bits != 8 && bits != 9) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid number of bits.")); } - if (write_bufinfo.len == 0 && read_bufinfo.len == 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "At least one buffer should be provided.")); + 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); + +//| .. method:: SPI.try_lock() +//| +//| Attempts to grab the SPI lock. Returns True on success. +//| +STATIC mp_obj_t bitbangio_spi_obj_try_lock(mp_obj_t self_in) { + shared_module_bitbangio_spi_try_lock(MP_OBJ_TO_PTR(self_in)); + return self_in; +} +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock); + +//| .. method:: SPI.unlock() +//| +//| Releases the SPI lock. +//| +STATIC mp_obj_t bitbangio_spi_obj_unlock(mp_obj_t self_in) { + shared_module_bitbangio_spi_unlock(MP_OBJ_TO_PTR(self_in)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock); + +//| .. method:: SPI.write(buf) +//| +//| Write the data contained in ``buf``. Requires the SPI being locked. +//| +STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) { + mp_buffer_info_t src; + mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ); + bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_lock(self); + bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len); + if (!ok) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI bus error")); } + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_spi_write_obj, bitbangio_spi_write); + - // do the transfer - bool ok = shared_module_bitbangio_spi_transfer(self, write_bufinfo.buf, - write_bufinfo.len, read_bufinfo.buf, read_bufinfo.len); +//| .. method:: SPI.readinto(buf) +//| +//| Read into the buffer specified by ``buf`` while writing zeroes. Requires the SPI being locked. +//| +STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); + check_lock(args[0]); + bool ok = shared_module_bitbangio_spi_read(args[0], bufinfo.buf, bufinfo.len); if (!ok) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error")); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI bus error")); } return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_transfer_obj, 2, bitbangio_spi_transfer); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_readinto_obj, 2, 2, bitbangio_spi_readinto); 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) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&bitbangio_spi___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&bitbangio_spi_obj___exit___obj) }, - { MP_ROM_QSTR(MP_QSTR_transfer), MP_ROM_PTR(&bitbangio_spi_transfer_obj) }, + { 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) }, }; STATIC MP_DEFINE_CONST_DICT(bitbangio_spi_locals_dict, bitbangio_spi_locals_dict_table); diff --git a/shared-bindings/bitbangio/SPI.h b/shared-bindings/bitbangio/SPI.h index a11df1f7b..7b851b30d 100644 --- a/shared-bindings/bitbangio/SPI.h +++ b/shared-bindings/bitbangio/SPI.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2016 Scott Shawcroft + * Copyright (c) 2016 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 @@ -38,14 +38,21 @@ extern const mp_obj_type_t bitbangio_spi_type; // Construct an underlying SPI object. extern void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self, const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, - const mcu_pin_obj_t * miso, uint32_t baudrate); + const mcu_pin_obj_t * miso); extern void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self); -// Write out write_buffer then read read_buffer. Returns true on success, false -// otherwise. -extern bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, - const uint8_t *write_buffer, size_t write_buffer_len, - uint8_t *read_buffer, size_t read_buffer_len); +extern void shared_module_bitbangio_spi_configure(bitbangio_spi_obj_t *self, + uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); + +extern bool shared_module_bitbangio_spi_try_lock(bitbangio_spi_obj_t *self); +extern bool shared_module_bitbangio_spi_has_lock(bitbangio_spi_obj_t *self); +extern void shared_module_bitbangio_spi_unlock(bitbangio_spi_obj_t *self); + +// Writes out the given data. +extern bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t *data, size_t len); + +// Reads in len bytes while outputting zeroes. +extern bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len); #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_SPI_H__ -- cgit v1.2.3