4
4
//
5
5
// SPDX-License-Identifier: MIT
6
6
7
+ //TODO: busio.WIZNET_PIO_SPI class.
7
8
// This file contains all of the Python API definitions for the
8
- // busio.SPI class.
9
+ // busio.WIZNET_PIO_SPI class.
9
10
10
11
#include <string.h>
11
12
20
21
#include "py/objproperty.h"
21
22
#include "py/runtime.h"
22
23
23
- #include <stdio.h>
24
-
25
-
26
- //| class SPI:
27
- //| """A 3-4 wire serial protocol
28
- //|
29
- //| SPI is a serial protocol that has exclusive pins for data in and out of the
30
- //| main device. It is typically faster than :py:class:`~bitbangio.I2C` because a
31
- //| separate pin is used to select a device rather than a transmitted
32
- //| address. This class only manages three of the four SPI lines: `!clock`,
33
- //| `!MOSI`, `!MISO`. It is up to the client to manage the appropriate
34
- //| select line, often abbreviated `!CS` or `!SS`. (This is common because
35
- //| multiple secondaries can share the `!clock`, `!MOSI` and `!MISO` lines
36
- //| and therefore the hardware.)
37
- //|
38
- //| .. raw:: html
39
- //|
40
- //| <p>
41
- //| <details>
42
- //| <summary>Available on these boards</summary>
43
- //| <ul>
44
- //| {% for board in support_matrix_reverse["busio.SPI"] %}
45
- //| <li> {{ board }}
46
- //| {% endfor %}
47
- //| </ul>
48
- //| </details>
49
- //| </p>
50
- //|
51
- //| .. seealso:: This class acts as an SPI main (controller).
52
- //| To act as an SPI secondary (target), use `spitarget.SPITarget`.
53
- //| """
54
- //|
55
- //| def __init__(
56
- //| self,
57
- //| clock: microcontroller.Pin,
58
- //| MOSI: Optional[microcontroller.Pin] = None,
59
- //| MISO: Optional[microcontroller.Pin] = None,
60
- //| half_duplex: bool = False,
61
- //| ) -> None:
62
- //| """Construct an SPI object on the given pins.
63
- //|
64
- //| .. note:: The SPI peripherals allocated in order of desirability, if possible,
65
- //| such as highest speed and not shared use first. For instance, on the nRF52840,
66
- //| there is a single 32MHz SPI peripheral, and multiple 8MHz peripherals,
67
- //| some of which may also be used for I2C. The 32MHz SPI peripheral is returned
68
- //| first, then the exclusive 8MHz SPI peripheral, and finally the shared 8MHz
69
- //| peripherals.
70
- //|
71
- //| .. seealso:: Using this class directly requires careful lock management.
72
- //| Instead, use :class:`~adafruit_bus_device.SPIDevice` to
73
- //| manage locks.
74
- //|
75
- //| .. seealso:: Using this class to directly read registers requires manual
76
- //| bit unpacking. Instead, use an existing driver or make one with
77
- //| :ref:`Register <register-module-reference>` data descriptors.
78
- //|
79
- //| :param ~microcontroller.Pin clock: the pin to use for the clock.
80
- //| :param ~microcontroller.Pin MOSI: the Main Out Selected In pin.
81
- //| :param ~microcontroller.Pin MISO: the Main In Selected Out pin.
82
- //| :param bool half_duplex: True when MOSI is used for bidirectional data. False when SPI is full-duplex or simplex.
83
- //|
84
- //| **Limitations:** ``half_duplex`` is available only on STM; other chips do not have the hardware support.
85
- //| """
86
- //| ...
87
- //|
88
-
89
- // TODO(tannewt): Support LSB SPI.
24
+
25
+ //TODO: class WIZNET_PIO_SPI
26
+
27
+
90
28
static mp_obj_t wiznet_pio_spi_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
91
29
#if CIRCUITPY_WIZNET_PIO_SPI
92
30
wiznet_pio_spi_obj_t * self = mp_obj_malloc (wiznet_pio_spi_obj_t , & busio_wiznet_pio_spi_type );
@@ -116,29 +54,19 @@ static mp_obj_t wiznet_pio_spi_make_new(const mp_obj_type_t *type, size_t n_args
116
54
}
117
55
118
56
#if CIRCUITPY_WIZNET_PIO_SPI
119
- //| def deinit(self) -> None:
120
- //| """Turn off the SPI bus."""
121
- //| ...
122
- //|
57
+
58
+ // TODO: def deinit
59
+
123
60
static mp_obj_t wiznet_pio_spi_obj_deinit (mp_obj_t self_in ) {
124
61
wiznet_pio_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
125
62
common_hal_wiznet_pio_spi_deinit (self );
126
63
return mp_const_none ;
127
64
}
128
65
MP_DEFINE_CONST_FUN_OBJ_1 (wiznet_pio_spi_deinit_obj , wiznet_pio_spi_obj_deinit );
129
66
130
- //| def __enter__(self) -> SPI:
131
- //| """No-op used by Context Managers.
132
- //| Provided by context manager helper."""
133
- //| ...
134
- //|
67
+ // TODO: def __enter__
135
68
136
- //| def __exit__(self) -> None:
137
- //| """Automatically deinitializes the hardware when exiting a context. See
138
- //| :ref:`lifetime-and-contextmanagers` for more info."""
139
- //| ...
140
- //|
141
- // Provided by context manager helper.
69
+ // TODO: def __exit__
142
70
143
71
static void check_lock (wiznet_pio_spi_obj_t * self ) {
144
72
asm ("" );
@@ -153,31 +81,7 @@ static void check_for_deinit(wiznet_pio_spi_obj_t *self) {
153
81
}
154
82
}
155
83
156
- //| def configure(
157
- //| self, *, baudrate: int = 100000, polarity: int = 0, phase: int = 0, bits: int = 8
158
- //| ) -> None:
159
- //| """Configures the SPI bus. The SPI object must be locked.
160
- //|
161
- //| :param int baudrate: the desired clock rate in Hertz. The actual clock rate may be higher or lower
162
- //| due to the granularity of available clock settings.
163
- //| Check the `frequency` attribute for the actual clock rate.
164
- //| :param int polarity: the base state of the clock line (0 or 1)
165
- //| :param int phase: the edge of the clock that data is captured. First (0)
166
- //| or second (1). Rising or falling depends on clock polarity.
167
- //| :param int bits: the number of bits per word
168
- //|
169
- //| .. note:: On the SAMD21, it is possible to set the baudrate to 24 MHz, but that
170
- //| speed is not guaranteed to work. 12 MHz is the next available lower speed, and is
171
- //| within spec for the SAMD21.
172
- //|
173
- //| .. note:: On the nRF52840, these baudrates are available: 125kHz, 250kHz, 1MHz, 2MHz, 4MHz,
174
- //| and 8MHz.
175
- //| If you pick a a baudrate other than one of these, the nearest lower
176
- //| baudrate will be chosen, with a minimum of 125kHz.
177
- //| Two SPI objects may be created, except on the Circuit Playground Bluefruit,
178
- //| which allows only one (to allow for an additional I2C object)."""
179
- //| ...
180
- //|
84
+ // TODO: def configure
181
85
182
86
static mp_obj_t wiznet_pio_spi_configure (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
183
87
enum { ARG_baudrate , ARG_polarity , ARG_phase , ARG_bits };
@@ -205,24 +109,15 @@ static mp_obj_t wiznet_pio_spi_configure(size_t n_args, const mp_obj_t *pos_args
205
109
}
206
110
MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_configure_obj , 1 , wiznet_pio_spi_configure );
207
111
208
- //| def try_lock(self) -> bool:
209
- //| """Attempts to grab the SPI lock. Returns True on success.
210
- //|
211
- //| :return: True when lock has been grabbed
212
- //| :rtype: bool"""
213
- //| ...
214
- //|
112
+ // TODO: def try_lock
215
113
216
114
static mp_obj_t wiznet_pio_spi_obj_try_lock (mp_obj_t self_in ) {
217
115
wiznet_pio_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
218
116
return mp_obj_new_bool (common_hal_wiznet_pio_spi_try_lock (self ));
219
117
}
220
118
MP_DEFINE_CONST_FUN_OBJ_1 (wiznet_pio_spi_try_lock_obj , wiznet_pio_spi_obj_try_lock );
221
119
222
- //| def unlock(self) -> None:
223
- //| """Releases the SPI lock."""
224
- //| ...
225
- //|
120
+ // TODO: def unlock
226
121
227
122
static mp_obj_t wiznet_pio_spi_obj_unlock (mp_obj_t self_in ) {
228
123
wiznet_pio_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -232,22 +127,7 @@ static mp_obj_t wiznet_pio_spi_obj_unlock(mp_obj_t self_in) {
232
127
}
233
128
MP_DEFINE_CONST_FUN_OBJ_1 (wiznet_pio_spi_unlock_obj , wiznet_pio_spi_obj_unlock );
234
129
235
- //| import sys
236
- //|
237
- //| def write(self, buffer: ReadableBuffer, *, start: int = 0, end: int = sys.maxsize) -> None:
238
- //| """Write the data contained in ``buffer``. The SPI object must be locked.
239
- //| If the buffer is empty, nothing happens.
240
- //|
241
- //| If ``start`` or ``end`` is provided, then the buffer will be sliced
242
- //| as if ``buffer[start:end]`` were passed, but without copying the data.
243
- //| The number of bytes written will be the length of ``buffer[start:end]``.
244
- //|
245
- //| :param ReadableBuffer buffer: write out bytes from this buffer
246
- //| :param int start: beginning of buffer slice
247
- //| :param int end: end of buffer slice; if not specified, use ``len(buffer)``
248
- //| """
249
- //| ...
250
- //|
130
+ // TODO: def write
251
131
252
132
static mp_obj_t wiznet_pio_spi_write (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
253
133
enum { ARG_buffer , ARG_start , ARG_end };
@@ -280,42 +160,15 @@ static mp_obj_t wiznet_pio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp
280
160
}
281
161
282
162
bool ok = common_hal_wiznet_pio_spi_write (self , ((uint8_t * )bufinfo .buf ) + start , length );
283
-
163
+
284
164
if (!ok ) {
285
165
mp_raise_OSError (MP_EIO );
286
166
}
287
167
return mp_const_none ;
288
168
}
289
169
MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_write_obj , 1 , wiznet_pio_spi_write );
290
170
291
-
292
- //| import sys
293
- //|
294
- //| def readinto(
295
- //| self,
296
- //| buffer: WriteableBuffer,
297
- //| *,
298
- //| start: int = 0,
299
- //| end: int = sys.maxsize,
300
- //| write_value: int = 0,
301
- //| ) -> None:
302
- //| """Read into ``buffer`` while writing ``write_value`` for each byte read.
303
- //| The SPI object must be locked.
304
- //| If the number of bytes to read is 0, nothing happens.
305
- //|
306
- //| If ``start`` or ``end`` is provided, then the buffer will be sliced
307
- //| as if ``buffer[start:end]`` were passed.
308
- //| The number of bytes read will be the length of ``buffer[start:end]``.
309
- //|
310
- //| :param WriteableBuffer buffer: read bytes into this buffer
311
- //| :param int start: beginning of buffer slice
312
- //| :param int end: end of buffer slice; if not specified, it will be the equivalent value
313
- //| of ``len(buffer)`` and for any value provided it will take the value of
314
- //| ``min(end, len(buffer))``
315
- //| :param int write_value: value to write while reading
316
- //| """
317
- //| ...
318
- //|
171
+ // TODO: def readinto
319
172
320
173
static mp_obj_t wiznet_pio_spi_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
321
174
enum { ARG_buffer , ARG_start , ARG_end , ARG_write_value };
@@ -355,42 +208,8 @@ static mp_obj_t wiznet_pio_spi_readinto(size_t n_args, const mp_obj_t *pos_args,
355
208
}
356
209
MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_readinto_obj , 1 , wiznet_pio_spi_readinto );
357
210
358
- //| import sys
359
- //|
360
- //| def write_readinto(
361
- //| self,
362
- //| out_buffer: ReadableBuffer,
363
- //| in_buffer: WriteableBuffer,
364
- //| *,
365
- //| out_start: int = 0,
366
- //| out_end: int = sys.maxsize,
367
- //| in_start: int = 0,
368
- //| in_end: int = sys.maxsize,
369
- //| ) -> None:
370
- //| """Write out the data in ``out_buffer`` while simultaneously reading data into ``in_buffer``.
371
- //| The SPI object must be locked.
372
- //|
373
- //| If ``out_start`` or ``out_end`` is provided, then the buffer will be sliced
374
- //| as if ``out_buffer[out_start:out_end]`` were passed, but without copying the data.
375
- //| The number of bytes written will be the length of ``out_buffer[out_start:out_end]``.
376
- //|
377
- //| If ``in_start`` or ``in_end`` is provided, then the input buffer will be sliced
378
- //| as if ``in_buffer[in_start:in_end]`` were passed,
379
- //| The number of bytes read will be the length of ``out_buffer[in_start:in_end]``.
380
- //|
381
- //| The lengths of the slices defined by ``out_buffer[out_start:out_end]``
382
- //| and ``in_buffer[in_start:in_end]`` must be equal.
383
- //| If buffer slice lengths are both 0, nothing happens.
384
- //|
385
- //| :param ReadableBuffer out_buffer: write out bytes from this buffer
386
- //| :param WriteableBuffer in_buffer: read bytes into this buffer
387
- //| :param int out_start: beginning of ``out_buffer`` slice
388
- //| :param int out_end: end of ``out_buffer`` slice; if not specified, use ``len(out_buffer)``
389
- //| :param int in_start: beginning of ``in_buffer`` slice
390
- //| :param int in_end: end of ``in_buffer slice``; if not specified, use ``len(in_buffer)``
391
- //| """
392
- //| ...
393
- //|
211
+
212
+ // TODO: def write_readinto
394
213
395
214
static mp_obj_t wiznet_pio_spi_write_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
396
215
enum { ARG_out_buffer , ARG_in_buffer , ARG_out_start , ARG_out_end , ARG_in_start , ARG_in_end };
@@ -447,12 +266,6 @@ static mp_obj_t wiznet_pio_spi_write_readinto(size_t n_args, const mp_obj_t *pos
447
266
}
448
267
MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_write_readinto_obj , 1 , wiznet_pio_spi_write_readinto );
449
268
450
- //| frequency: int
451
- //| """The actual SPI bus frequency. This may not match the frequency requested
452
- //| due to internal limitations."""
453
- //|
454
- //|
455
-
456
269
#endif // CIRCUITPY_WIZNET_PIO_SPI
457
270
458
271
static const mp_rom_map_elem_t wiznet_pio_spi_locals_dict_table [] = {
0 commit comments