Skip to content

Commit 100a045

Browse files
remove asynchronous interface under busio.SPI
1 parent ea344c3 commit 100a045

File tree

3 files changed

+0
-215
lines changed

3 files changed

+0
-215
lines changed

ports/atmel-samd/common-hal/busio/SPI.c

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -338,62 +338,6 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou
338338
return status >= 0; // Status is number of chars read or an error code < 0.
339339
}
340340

341-
void common_hal_busio_spi_transfer_async_start(busio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len) {
342-
if (len == 0) {
343-
return;
344-
}
345-
if (self->running_dma.failure != 1) {
346-
mp_raise_RuntimeError(MP_ERROR_TEXT("Async SPI transfer in progress on this bus, keep awaiting."));
347-
}
348-
Sercom* sercom = self->spi_desc.dev.prvt;
349-
self->running_dma = shared_dma_transfer_start(sercom, data_out, &sercom->SPI.DATA.reg, &sercom->SPI.DATA.reg, data_in, len, 0);
350-
351-
// There is an issue where if an unexpected SPI transfer is received before the user calls "end" for the in-progress, expected
352-
// transfer, the SERCOM has an error and gets confused. This can be detected from INTFLAG.ERROR. I think the code in
353-
// ports/atmel-samd/peripherals/samd/dma.c at line 277 (as of this commit; it's the part that reads s->SPI.INTFLAG.bit.RXC and
354-
// s->SPI.DATA.reg) is supposed to fix this, but experimentation seems to show that it does not in fact fix anything. Anyways, if
355-
// the ERROR bit is set, let's just reset the peripheral and then setup the transfer again -- that seems to work.
356-
if (hri_sercomspi_get_INTFLAG_ERROR_bit(sercom)) {
357-
shared_dma_transfer_close(self->running_dma);
358-
359-
// disable the sercom
360-
spi_m_sync_disable(&self->spi_desc);
361-
hri_sercomspi_wait_for_sync(sercom, SERCOM_SPI_SYNCBUSY_MASK);
362-
363-
// save configurations
364-
hri_sercomspi_ctrla_reg_t ctrla_saved_val = hri_sercomspi_get_CTRLA_reg(sercom, -1); // -1 mask is all ones: save all bits
365-
hri_sercomspi_ctrlb_reg_t ctrlb_saved_val = hri_sercomspi_get_CTRLB_reg(sercom, -1); // -1 mask is all ones: save all bits
366-
hri_sercomspi_baud_reg_t baud_saved_val = hri_sercomspi_get_BAUD_reg(sercom, -1); // -1 mask is all ones: save all bits
367-
// reset
368-
hri_sercomspi_set_CTRLA_SWRST_bit(sercom);
369-
hri_sercomspi_wait_for_sync(sercom, SERCOM_SPI_SYNCBUSY_MASK);
370-
// re-write configurations
371-
hri_sercomspi_write_CTRLA_reg(sercom, ctrla_saved_val);
372-
hri_sercomspi_write_CTRLB_reg(sercom, ctrlb_saved_val);
373-
hri_sercomspi_write_BAUD_reg (sercom, baud_saved_val);
374-
hri_sercomspi_wait_for_sync(sercom, SERCOM_SPI_SYNCBUSY_MASK);
375-
376-
// re-enable the sercom
377-
spi_m_sync_enable(&self->spi_desc);
378-
hri_sercomspi_wait_for_sync(sercom, SERCOM_SPI_SYNCBUSY_MASK);
379-
380-
self->running_dma = shared_dma_transfer_start(sercom, data_out, &sercom->SPI.DATA.reg, &sercom->SPI.DATA.reg, data_in, len, 0);
381-
}
382-
}
383-
384-
bool common_hal_busio_spi_transfer_async_check(busio_spi_obj_t *self) {
385-
return self->running_dma.failure == 1 || shared_dma_transfer_finished(self->running_dma);
386-
}
387-
388-
int common_hal_busio_spi_transfer_async_end(busio_spi_obj_t *self) {
389-
if (self->running_dma.failure == 1) {
390-
return 0;
391-
}
392-
int res = shared_dma_transfer_close(self->running_dma);
393-
self->running_dma.failure = 1;
394-
return res;
395-
}
396-
397341
uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) {
398342
return samd_peripherals_spi_baud_reg_value_to_baudrate(hri_sercomspi_read_BAUD_reg(self->spi_desc.dev.prvt));
399343
}

shared-bindings/busio/SPI.c

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -469,145 +469,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_get_frequency_obj, busio_spi_obj_get_frequen
469469
MP_PROPERTY_GETTER(busio_spi_frequency_obj,
470470
(mp_obj_t)&busio_spi_get_frequency_obj);
471471

472-
#if CIRCUITPY_SAMD
473-
474-
//| import sys
475-
//| def async_transfer_start(
476-
//| self,
477-
//| out_buffer: ReadableBuffer,
478-
//| in_buffer: WriteableBuffer,
479-
//| *,
480-
//| out_start: int = 0,
481-
//| out_end: int = sys.maxsize,
482-
//| in_start: int = 0,
483-
//| in_end: int = sys.maxsize
484-
//| ) -> None:
485-
//| """Write out the data in ``out_buffer`` while simultaneously reading data into ``in_buffer``.
486-
//| The SPI object must be locked. Note: this method returns immediately, and the data will not
487-
//| actually be transferred until some time has passed. Use `async_transfer_finished` and
488-
//| `async_transfer_end` to check on the status of the transfer and close out its resources.
489-
//|
490-
//| If ``out_start`` or ``out_end`` is provided, then the buffer will be sliced
491-
//| as if ``out_buffer[out_start:out_end]`` were passed, but without copying the data.
492-
//| The number of bytes written will be the length of ``out_buffer[out_start:out_end]``.
493-
//|
494-
//| If ``in_start`` or ``in_end`` is provided, then the input buffer will be sliced
495-
//| as if ``in_buffer[in_start:in_end]`` were passed,
496-
//| The number of bytes read will be the length of ``out_buffer[in_start:in_end]``.
497-
//|
498-
//| The lengths of the slices defined by ``out_buffer[out_start:out_end]``
499-
//| and ``in_buffer[in_start:in_end]`` must be equal.
500-
//| If buffer slice lengths are both 0, nothing happens.
501-
//|
502-
//| Note: This method is currently only available on atmel-samd` ports of CircuitPython.
503-
//|
504-
//| :param ReadableBuffer out_buffer: write out bytes from this buffer
505-
//| :param WriteableBuffer in_buffer: read bytes into this buffer
506-
//| :param int out_start: beginning of ``out_buffer`` slice
507-
//| :param int out_end: end of ``out_buffer`` slice; if not specified, use ``len(out_buffer)``
508-
//| :param int in_start: beginning of ``in_buffer`` slice
509-
//| :param int in_end: end of ``in_buffer slice``; if not specified, use ``len(in_buffer)``
510-
//| """
511-
//| ...
512-
513-
STATIC mp_obj_t busio_spi_start_async_transfer(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
514-
enum { ARG_out_buffer, ARG_in_buffer, ARG_out_start, ARG_out_end, ARG_in_start, ARG_in_end };
515-
static const mp_arg_t allowed_args[] = {
516-
{ MP_QSTR_out_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
517-
{ MP_QSTR_in_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
518-
{ MP_QSTR_out_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
519-
{ MP_QSTR_out_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
520-
{ MP_QSTR_in_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
521-
{ MP_QSTR_in_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
522-
};
523-
busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
524-
check_for_deinit(self);
525-
check_lock(self);
526-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
527-
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
528-
529-
mp_buffer_info_t buf_out_info;
530-
mp_get_buffer_raise(args[ARG_out_buffer].u_obj, &buf_out_info, MP_BUFFER_READ);
531-
int out_stride_in_bytes = mp_binary_get_size('@', buf_out_info.typecode, NULL);
532-
int32_t out_start = args[ARG_out_start].u_int;
533-
size_t out_length = buf_out_info.len / out_stride_in_bytes;
534-
normalize_buffer_bounds(&out_start, args[ARG_out_end].u_int, &out_length);
535-
536-
mp_buffer_info_t buf_in_info;
537-
mp_get_buffer_raise(args[ARG_in_buffer].u_obj, &buf_in_info, MP_BUFFER_WRITE);
538-
int in_stride_in_bytes = mp_binary_get_size('@', buf_in_info.typecode, NULL);
539-
int32_t in_start = args[ARG_in_start].u_int;
540-
size_t in_length = buf_in_info.len / in_stride_in_bytes;
541-
normalize_buffer_bounds(&in_start, args[ARG_in_end].u_int, &in_length);
542-
543-
// Treat start and length in terms of bytes from now on.
544-
out_start *= out_stride_in_bytes;
545-
out_length *= out_stride_in_bytes;
546-
in_start *= in_stride_in_bytes;
547-
in_length *= in_stride_in_bytes;
548-
549-
if (out_length != in_length) {
550-
mp_raise_ValueError(MP_ERROR_TEXT("buffer slices must be of equal length"));
551-
}
552-
553-
common_hal_busio_spi_transfer_async_start(self,
554-
((uint8_t *)buf_out_info.buf) + out_start,
555-
((uint8_t *)buf_in_info.buf) + in_start,
556-
out_length);
557-
return mp_const_none;
558-
}
559-
MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_start_transfer_obj, 1, busio_spi_start_async_transfer);
560-
561-
//| import sys
562-
//| def async_transfer_finished(
563-
//| self
564-
//| ) -> None:
565-
//| """Check whether or not the last async transfer started on this SPI object has finished. If
566-
//| no transfer was started, this method behaves as though the most recent transfer has finished
567-
//| and returns `True`. Otherwise, it returns `False`.
568-
//|
569-
//| Note: This method is currently only available on atmel-samd` ports of CircuitPython.
570-
//| """
571-
//| ...
572-
STATIC mp_obj_t busio_spi_obj_check_async_transfer(mp_obj_t self_in) {
573-
busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
574-
check_for_deinit(self);
575-
return common_hal_busio_spi_transfer_async_check(self) ? mp_const_true : mp_const_false;
576-
}
577-
MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_check_transfer_obj, busio_spi_obj_check_async_transfer);
578-
579-
//| import sys
580-
//| def async_transfer_end(
581-
//| self
582-
//| ) -> None:
583-
//| """Return the status code with which the last async transfer on this SPI object completed. This
584-
//| method MUST be called for all transfers, regardless of user interest in status code. The resources
585-
//| for the transfer will be left open until this method is called. Once this method is called, the
586-
//| peripheral resets and is ready for another transfer. The return code of this method also resets to
587-
//| its pre-transfer state: repeated calls to this method may produce different codes.
588-
//|
589-
//| Return code 0: No transfer has occured, either because `start_async_transfer` was never called, or because
590-
//| it was called with zero-length buffers.
591-
//| Return code -1: The transfer failed because no DMA channels are available.
592-
//| Return code -2: The transfer executed, but the DMA controller indicates that either some data is
593-
//| untransferred, that a software issue prevented the data transfer from completing, or that some other error
594-
//| has occured within the DMA controller.
595-
//| Return code -3: An unaligned buffer was passed to the QSPI peripheral, which prevents the DMA controller from
596-
//| appropriately chunking the transfer.
597-
//| Return code n>0: A transfer of `n` bytes in each direction has succeeded.
598-
//|
599-
//| Note: This method is currently only available on atmel-samd` ports of CircuitPython.
600-
//| """
601-
//| ...
602-
STATIC mp_obj_t busio_spi_obj_end_async_transfer(mp_obj_t self_in) {
603-
busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
604-
check_for_deinit(self);
605-
return MP_OBJ_NEW_SMALL_INT(common_hal_busio_spi_transfer_async_end(self));
606-
}
607-
MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_end_transfer_obj, busio_spi_obj_end_async_transfer);
608-
609-
#endif // CIRCUITPY_SAMD
610-
611472
#endif // CIRCUITPY_BUSIO_SPI
612473

613474

@@ -626,13 +487,6 @@ STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table[] = {
626487
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&busio_spi_write_readinto_obj) },
627488
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&busio_spi_frequency_obj) }
628489

629-
#if CIRCUITPY_SAMD
630-
,
631-
{ MP_ROM_QSTR(MP_QSTR_async_transfer_start), MP_ROM_PTR(&busio_spi_start_transfer_obj) },
632-
{ MP_ROM_QSTR(MP_QSTR_async_transfer_finished), MP_ROM_PTR(&busio_spi_check_transfer_obj) },
633-
{ MP_ROM_QSTR(MP_QSTR_async_transfer_end), MP_ROM_PTR(&busio_spi_end_transfer_obj) }
634-
#endif // CIRCUITPY_SAMD
635-
636490
#endif // CIRCUITPY_BUSIO_SPI
637491
};
638492
STATIC MP_DEFINE_CONST_DICT(busio_spi_locals_dict, busio_spi_locals_dict_table);

shared-bindings/busio/SPI.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,6 @@ extern bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size
5757
// Reads and write len bytes simultaneously.
5858
extern bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len);
5959

60-
#if CIRCUITPY_SAMD
61-
62-
// Initiates a transfer that reads and write len bytes simultaneously
63-
extern void common_hal_busio_spi_transfer_async_start(busio_spi_obj_t *self, const uint8_t *data_out, uint8_t *data_in, size_t len);
64-
65-
// Reads the state of the in-progress transfer
66-
extern bool common_hal_busio_spi_transfer_async_check(busio_spi_obj_t *self);
67-
68-
// Cleans up the completed transfer and returns any error code produced by the transfer
69-
extern int common_hal_busio_spi_transfer_async_end(busio_spi_obj_t *self);
70-
71-
#endif // CIRCUITPY_SAMD
72-
7360
// Return actual SPI bus frequency.
7461
uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self);
7562

0 commit comments

Comments
 (0)