Skip to content

Commit cd3c95b

Browse files
update docs and names for consistency and completeness
1 parent d059736 commit cd3c95b

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

ports/atmel-samd/common-hal/spitarget/SPITarget.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ void common_hal_spitarget_spi_target_construct(spitarget_spi_target_obj_t *self,
154154
claim_pin(ss);
155155

156156
self->running_dma.failure = 1; // not started
157+
self->mosi_packet = NULL;
158+
self->miso_packet = NULL;
157159

158160
spi_m_sync_enable(&self->spi_desc);
159161
}
@@ -185,6 +187,10 @@ void common_hal_spitarget_spi_target_transfer_start(spitarget_spi_target_obj_t *
185187
if (self->running_dma.failure != 1) {
186188
mp_raise_RuntimeError(MP_ERROR_TEXT("Async SPI transfer in progress on this bus, keep awaiting."));
187189
}
190+
191+
self->mosi_packet = mosi_packet;
192+
self->miso_packet = miso_packet;
193+
188194
Sercom* sercom = self->spi_desc.dev.prvt;
189195
self->running_dma = shared_dma_transfer_start(sercom, miso_packet, &sercom->SPI.DATA.reg, &sercom->SPI.DATA.reg, mosi_packet, len, 0);
190196

@@ -231,5 +237,9 @@ int common_hal_spitarget_spi_target_transfer_close(spitarget_spi_target_obj_t *s
231237
}
232238
int res = shared_dma_transfer_close(self->running_dma);
233239
self->running_dma.failure = 1;
240+
241+
self->mosi_packet = NULL;
242+
self->miso_packet = NULL;
243+
234244
return res;
235245
}

ports/atmel-samd/common-hal/spitarget/SPITarget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ typedef struct {
1515
uint8_t MISO_pin;
1616
uint8_t SS_pin;
1717

18+
uint8_t* mosi_packet;
19+
uint8_t* miso_packet;
20+
1821
dma_descr_t running_dma;
1922
} spitarget_spi_target_obj_t;
2023

shared-bindings/spitarget/SPITarget.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(spitarget_spi_target___exit___obj, 4,
8585
//| If a packet has already been queued for this SPI bus but has not yet been transferred, an error will be raised.
8686
//|
8787
//| :param bytearray miso_packet: Packet data to be sent from secondary to main on next request."""
88+
//| :param bytearray mosi_packet: Packet to be filled with data from main on next request."""
8889
//|
8990
STATIC mp_obj_t spitarget_spi_target_load_packet(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
9091
mp_check_self(mp_obj_is_type(pos_args[0], &spitarget_spi_target_type));
@@ -115,14 +116,14 @@ STATIC mp_obj_t spitarget_spi_target_load_packet(size_t n_args, const mp_obj_t *
115116
}
116117
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(spitarget_spi_target_load_packet_obj, 1, spitarget_spi_target_load_packet);
117118

118-
//| def try_transfer(self, *, timeout: float = -1) -> bool:
119+
//| def wait_transfer(self, *, timeout: float = -1) -> bool:
119120
//| """Wait for an SPI transfer from the main device.
120121
//|
121122
//| :param float timeout: Timeout in seconds. Zero means wait forever, a negative value means check once
122123
//| :return: True if the transfer is complete, or False if no response received before the timeout
123124
//| :rtype: ~bool"""
124125
//|
125-
STATIC mp_obj_t spitarget_spi_target_try_transfer(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
126+
STATIC mp_obj_t spitarget_spi_target_wait_transfer(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
126127
mp_check_self(mp_obj_is_type(pos_args[0], &spitarget_spi_target_type));
127128
spitarget_spi_target_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
128129
if (common_hal_spitarget_spi_target_deinited(self)) {
@@ -160,14 +161,14 @@ STATIC mp_obj_t spitarget_spi_target_try_transfer(size_t n_args, const mp_obj_t
160161

161162
return mp_const_false;
162163
}
163-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(spitarget_spi_target_try_transfer_obj, 1, spitarget_spi_target_try_transfer);
164+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(spitarget_spi_target_wait_transfer_obj, 1, spitarget_spi_target_wait_transfer);
164165

165166
STATIC const mp_rom_map_elem_t spitarget_spi_target_locals_dict_table[] = {
166167
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&spitarget_spi_target_deinit_obj) },
167168
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
168169
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&spitarget_spi_target___exit___obj) },
169170
{ MP_ROM_QSTR(MP_QSTR_load_packet), MP_ROM_PTR(&spitarget_spi_target_load_packet_obj) },
170-
{ MP_ROM_QSTR(MP_QSTR_try_transfer), MP_ROM_PTR(&spitarget_spi_target_try_transfer_obj) },
171+
{ MP_ROM_QSTR(MP_QSTR_wait_transfer), MP_ROM_PTR(&spitarget_spi_target_wait_transfer_obj) },
171172

172173
};
173174

shared-bindings/spitarget/__init__.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,49 @@
1111
//| """Serial Peripheral Interface protocol target
1212
//|
1313
//| The `spitarget` module contains classes to support an SPI target.
14+
//|
15+
//| Example that emulates an SPI analog-to-digital converter::
16+
//|
17+
//| import board
18+
//| import analogio
19+
//| from spitarget import SPITarget
20+
//|
21+
//| ain0 = analogio.AnalogIn(board.A0)
22+
//| ain1 = analogio.AnalogIn(board.A1)
23+
//| selected_channel = ain0
24+
//|
25+
//| def map_adc_channel(index):
26+
//| return ain0 if (index == 0) else ain1
27+
//|
28+
//| mosi_buffer = bytearray(2)
29+
//| miso_buffer = bytearray(2)
30+
//| with SPITarget(sck=board.D12, mosi=board.D13, miso=board.D11, ss=board.D10) as device:
31+
//| while True:
32+
//| # Convert analog signal to array of bytes
33+
//| reading = selected_channel.value
34+
//| miso_buffer[0] = (reading >> 8) & 0xFF
35+
//| miso_buffer[1] = (reading) & 0xFF
36+
//| # Send array of bytes over SPI to main
37+
//| device.load_packet(mosi_buffer, miso_buffer)
38+
//| device.wait_transfer(0)
39+
//| # Handle command from main, which sets the ADC channel
40+
//| selected_channel = map_adc_channel((mosi_buffer[0] << 8) | mosi_buffer[1])
41+
//|
42+
//| Communicating with the ADC emulator from the REPL of an attached CircuitPython board might look like ::
43+
//|
44+
//| >>> import busio
45+
//| >>> spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
46+
//| >>> spi.try_lock()
47+
//| True
48+
//| >>> spi.write(bytearray([0, 0])) # ADC command: read from A0
49+
//| >>> adc_result = bytearray(2)
50+
//| >>> spi.readinto(adc_result)
51+
//| >>> adc_result
52+
//| bytearray(b'\xc4\x16')
53+
//| >>> spi.unlock()
54+
//|
55+
//| """
56+
1457

1558

1659
STATIC const mp_rom_map_elem_t spitarget_module_globals_table[] = {

0 commit comments

Comments
 (0)