DM: added write_readinto to bitbangio
This commit is contained in:
parent
271c4bead6
commit
a7f72eb2d8
@ -236,6 +236,38 @@ STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_readinto_obj, 2, 2, bitbangio_spi_readinto);
|
||||
|
||||
//| .. method:: SPI.write_readinto(buf_in, buf_out)
|
||||
//|
|
||||
//| Write out the data in ``buffer_out`` while simultaneously reading data into ``buffer_in``.
|
||||
STATIC mp_obj_t bitbangio_spi_write_readinto(size_t n_args, const mp_obj_t *args) {
|
||||
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
||||
raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self));
|
||||
|
||||
mp_buffer_info_t bufinfoin;
|
||||
mp_get_buffer_raise(args[2], &bufinfoin, MP_BUFFER_WRITE);
|
||||
|
||||
if (bufinfoin.len == 0) {
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_buffer_info_t bufinfoout;
|
||||
mp_get_buffer_raise(args[1], &bufinfoout, MP_BUFFER_READ);
|
||||
|
||||
if (bufinfoout.len != bufinfoin.len) {
|
||||
mp_raise_ValueError("buffers must be of equal length");
|
||||
}
|
||||
|
||||
bool ok = shared_module_bitbangio_spi_transfer(self,
|
||||
((uint8_t*)bufinfoout.buf),
|
||||
((uint8_t*)bufinfoin.buf),
|
||||
bufinfoin.len);
|
||||
if (!ok) {
|
||||
mp_raise_OSError(MP_EIO);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_spi_write_readinto_obj, 3, 3, bitbangio_spi_write_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(&default___enter___obj) },
|
||||
@ -247,6 +279,7 @@ STATIC const mp_rom_map_elem_t bitbangio_spi_locals_dict_table[] = {
|
||||
|
||||
{ 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) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&bitbangio_spi_write_readinto_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(bitbangio_spi_locals_dict, bitbangio_spi_locals_dict_table);
|
||||
|
||||
|
@ -56,4 +56,7 @@ extern bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const u
|
||||
// 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);
|
||||
|
||||
// Transfer out len bytes while reading len bytes
|
||||
extern bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, const uint8_t *dout, uint8_t *din, size_t len);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_SPI_H
|
||||
|
@ -238,3 +238,69 @@ bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data,
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// transfer
|
||||
bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, const uint8_t *dout, uint8_t *din, size_t len) {
|
||||
if (len > 0 && (!self->has_mosi || !self->has_miso) ) {
|
||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
||||
"Cannot transfer without MOSI and MISO pins."));
|
||||
}
|
||||
uint32_t delay_half = self->delay_half;
|
||||
|
||||
// only MSB transfer is implemented
|
||||
|
||||
// If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured
|
||||
// delay_half is equal to this value, then the software SPI implementation
|
||||
// will run as fast as possible, limited only by CPU speed and GPIO time.
|
||||
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
|
||||
if (delay_half <= MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
uint8_t data_out = dout[i];
|
||||
uint8_t data_in = 0;
|
||||
for (int j = 0; j < 8; ++j, data_out <<= 1) {
|
||||
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
|
||||
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
|
||||
data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso);
|
||||
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
|
||||
}
|
||||
din[i] = data_in;
|
||||
|
||||
if (dest != NULL) {
|
||||
dest[i] = data_in;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
uint8_t data_out = dout[i];
|
||||
uint8_t data_in = 0;
|
||||
for (int j = 0; j < 8; ++j, data_out <<= 1) {
|
||||
common_hal_digitalio_digitalinout_set_value(&self->mosi, (data_out >> 7) & 1);
|
||||
if (self->phase == 0) {
|
||||
common_hal_mcu_delay_us(delay_half);
|
||||
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
|
||||
} else {
|
||||
common_hal_digitalio_digitalinout_set_value(&self->clock, 1 - self->polarity);
|
||||
common_hal_mcu_delay_us(delay_half);
|
||||
}
|
||||
data_in = (data_in << 1) | common_hal_digitalio_digitalinout_get_value(&self->miso);
|
||||
if (self->phase == 0) {
|
||||
common_hal_mcu_delay_us(delay_half);
|
||||
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
|
||||
} else {
|
||||
common_hal_digitalio_digitalinout_set_value(&self->clock, self->polarity);
|
||||
common_hal_mcu_delay_us(delay_half);
|
||||
}
|
||||
}
|
||||
din[i] = data_in;
|
||||
|
||||
// Some ports need a regular callback, but probably we don't need
|
||||
// to do this every byte, or even at all.
|
||||
#ifdef MICROPY_EVENT_POLL_HOOK
|
||||
MICROPY_EVENT_POLL_HOOK;
|
||||
#endif
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user