44//
55// SPDX-License-Identifier: MIT
66
7+ //TODO: busio.WIZNET_PIO_SPI class.
78// This file contains all of the Python API definitions for the
8- // busio.SPI class.
9+ // busio.WIZNET_PIO_SPI class.
910
1011#include <string.h>
1112
2021#include "py/objproperty.h"
2122#include "py/runtime.h"
2223
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+
9028static 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 ) {
9129 #if CIRCUITPY_WIZNET_PIO_SPI
9230 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
11654}
11755
11856#if CIRCUITPY_WIZNET_PIO_SPI
119- //| def deinit(self) -> None:
120- //| """Turn off the SPI bus."""
121- //| ...
122- //|
57+
58+ // TODO: def deinit
59+
12360static mp_obj_t wiznet_pio_spi_obj_deinit (mp_obj_t self_in ) {
12461 wiznet_pio_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
12562 common_hal_wiznet_pio_spi_deinit (self );
12663 return mp_const_none ;
12764}
12865MP_DEFINE_CONST_FUN_OBJ_1 (wiznet_pio_spi_deinit_obj , wiznet_pio_spi_obj_deinit );
12966
130- //| def __enter__(self) -> SPI:
131- //| """No-op used by Context Managers.
132- //| Provided by context manager helper."""
133- //| ...
134- //|
67+ // TODO: def __enter__
13568
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__
14270
14371static void check_lock (wiznet_pio_spi_obj_t * self ) {
14472 asm ("" );
@@ -153,31 +81,7 @@ static void check_for_deinit(wiznet_pio_spi_obj_t *self) {
15381 }
15482}
15583
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
18185
18286static mp_obj_t wiznet_pio_spi_configure (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
18387 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
205109}
206110MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_configure_obj , 1 , wiznet_pio_spi_configure );
207111
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
215113
216114static mp_obj_t wiznet_pio_spi_obj_try_lock (mp_obj_t self_in ) {
217115 wiznet_pio_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
218116 return mp_obj_new_bool (common_hal_wiznet_pio_spi_try_lock (self ));
219117}
220118MP_DEFINE_CONST_FUN_OBJ_1 (wiznet_pio_spi_try_lock_obj , wiznet_pio_spi_obj_try_lock );
221119
222- //| def unlock(self) -> None:
223- //| """Releases the SPI lock."""
224- //| ...
225- //|
120+ // TODO: def unlock
226121
227122static mp_obj_t wiznet_pio_spi_obj_unlock (mp_obj_t self_in ) {
228123 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) {
232127}
233128MP_DEFINE_CONST_FUN_OBJ_1 (wiznet_pio_spi_unlock_obj , wiznet_pio_spi_obj_unlock );
234129
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
251131
252132static mp_obj_t wiznet_pio_spi_write (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
253133 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
280160 }
281161
282162 bool ok = common_hal_wiznet_pio_spi_write (self , ((uint8_t * )bufinfo .buf ) + start , length );
283-
163+
284164 if (!ok ) {
285165 mp_raise_OSError (MP_EIO );
286166 }
287167 return mp_const_none ;
288168}
289169MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_write_obj , 1 , wiznet_pio_spi_write );
290170
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
319172
320173static mp_obj_t wiznet_pio_spi_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
321174 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,
355208}
356209MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_readinto_obj , 1 , wiznet_pio_spi_readinto );
357210
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
394213
395214static mp_obj_t wiznet_pio_spi_write_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
396215 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
447266}
448267MP_DEFINE_CONST_FUN_OBJ_KW (wiznet_pio_spi_write_readinto_obj , 1 , wiznet_pio_spi_write_readinto );
449268
450- //| frequency: int
451- //| """The actual SPI bus frequency. This may not match the frequency requested
452- //| due to internal limitations."""
453- //|
454- //|
455-
456269#endif // CIRCUITPY_WIZNET_PIO_SPI
457270
458271static const mp_rom_map_elem_t wiznet_pio_spi_locals_dict_table [] = {
0 commit comments