Skip to content

Commit 07ca619

Browse files
authored
Merge pull request #2 from kbsriram/fix-ci
Fix lint issues and add license so CI builds.
2 parents de44f0a + cabbef5 commit 07ca619

File tree

7 files changed

+92
-46
lines changed

7 files changed

+92
-46
lines changed

LICENSES/BSD-3-Clause.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
2+
3+
Redistribution and use in source and binary forms, with or without modification,
4+
are permitted provided that the following conditions are met:
5+
6+
1. Redistributions of source code must retain the above copyright notice,
7+
this list of conditions and the following disclaimer.
8+
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
13+
3. Neither the name of the copyright holder nor the names of its contributors
14+
may be used to endorse or promote products derived from this software without
15+
specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,23 @@ Dependencies
2929
This driver depends on:
3030

3131
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
32+
* `Adafruit CircuitPython PIOASM <https://github.com/adafruit/Adafruit_CircuitPython_PIOASM>`_
3233

3334
Please ensure all dependencies are available on the CircuitPython filesystem.
3435
This is easily achieved by downloading
3536
`the Adafruit library and driver bundle <https://circuitpython.org/libraries>`_
3637
or individual libraries can be installed using
3738
`circup <https://github.com/adafruit/circup>`_.
3839

40+
Works with the Raspberry Pi RP2040 Pico family.
3941

40-
41-
.. todo:: Describe the Adafruit product this library works with. For PCBs, you can also add the
42-
image from the assets folder in the PCB's GitHub repo.
43-
44-
`Purchase one from the Adafruit shop <http://www.adafruit.com/products/>`_
42+
`Purchase one from the Adafruit shop <https://www.adafruit.com/category/875>`_.
4543

4644
Installing from PyPI
4745
=====================
4846
.. note:: This library is not available on PyPI yet. Install documentation is included
4947
as a standard element. Stay tuned for PyPI availability!
5048

51-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
52-
5349
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
5450
PyPI <https://pypi.org/project/adafruit-circuitpython-pio-uart/>`_.
5551
To install for current user:
@@ -99,8 +95,16 @@ Or the following command to update an existing version:
9995
Usage Example
10096
=============
10197

102-
.. todo:: Add a quick, simple example. It and other examples should live in the
103-
examples folder and be included in docs/examples.rst.
98+
.. code-block:: python
99+
100+
import board
101+
import adafruit_pio_uart
102+
103+
uart = adafruit_pio_uart.UART(board.TX, board.RX)
104+
105+
uart.write(b"\x00")
106+
print(uart.read(1))
107+
104108
105109
Documentation
106110
=============

adafruit_pio_uart.py

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,29 @@
1616
* Author(s): Scott Shawcroft
1717
"""
1818

19-
import adafruit_pioasm
2019
import array
20+
import time
21+
22+
import adafruit_pioasm
2123
import busio
2224
import rp2pio
23-
import time
2425

2526
__version__ = "0.0.0+auto.0"
2627
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PIO_UART.git"
2728

2829

2930
class UART:
31+
"""PIO implementation of CircuitPython UART API."""
32+
3033
Parity = busio.UART.Parity
3134

32-
def __init__(self, tx=None, rx=None, baudrate=9600, bits=8, parity=None, stop=1, timeout=1):
35+
def __init__(
36+
self, tx=None, rx=None, baudrate=9600, bits=8, parity=None, stop=1, timeout=1
37+
): # pylint: disable=invalid-name, too-many-arguments
3338
self.bitcount = bits + (1 if parity else 0)
3439
self.bits = bits
3540
self.parity = parity
36-
self.mask = ((1 << bits) - 1)
41+
self.mask = (1 << bits) - 1
3742
self.shift = 8 - (self.bitcount % 8)
3843
self._timeout = timeout
3944
self.rx_pio = None
@@ -50,27 +55,26 @@ def __init__(self, tx=None, rx=None, baudrate=9600, bits=8, parity=None, stop=1,
5055
# * Sample data
5156
# * Each iteration is 8 cycles
5257
rx_code = adafruit_pioasm.assemble(
53-
".program uart_rx_mini\n" +
54-
"start:\n"
55-
" wait 0 pin 0\n" +
56-
f" set x, {self.bitcount - 1} [10]\n" +
57-
"bitloop:\n" +
58-
" in pins, 1\n" +
59-
" jmp x-- bitloop [6]\n" +
60-
" jmp pin good_stop\n" +
61-
# Skip IRQ
62-
" wait 1 pin 0\n" +
63-
" jmp start\n" +
64-
"good_stop:\n" +
65-
" push\n"
58+
".program uart_rx_mini\n"
59+
+ "start:\n"
60+
+ " wait 0 pin 0\n"
61+
+ f" set x, {self.bitcount - 1} [10]\n"
62+
+ "bitloop:\n"
63+
+ " in pins, 1\n"
64+
+ " jmp x-- bitloop [6]\n"
65+
+ " jmp pin good_stop\n"
66+
+ " wait 1 pin 0\n" # Skip IRQ
67+
+ " jmp start\n"
68+
+ "good_stop:\n"
69+
+ " push\n"
6670
)
6771
self.rx_pio = rp2pio.StateMachine(
6872
rx_code,
6973
first_in_pin=rx,
7074
jmp_pin=rx,
7175
frequency=8 * baudrate,
7276
auto_push=False,
73-
push_threshold=self.bitcount
77+
push_threshold=self.bitcount,
7478
)
7579

7680
self.tx_pio = None
@@ -86,13 +90,13 @@ def __init__(self, tx=None, rx=None, baudrate=9600, bits=8, parity=None, stop=1,
8690
# * Shift 1 bit from OSR to the first OUT pin
8791
# * Each loop iteration is 8 cycles.
8892
tx_code = adafruit_pioasm.Program(
89-
".program uart_tx\n" +
90-
".side_set 1 opt\n" +
91-
f" pull side 1 [{stop_delay}]\n" +
92-
f" set x, {self.bitcount - 1} side 0 [7]\n" +
93-
"bitloop:\n" +
94-
" out pins, 1\n" +
95-
" jmp x-- bitloop [6]\n"
93+
".program uart_tx\n"
94+
+ ".side_set 1 opt\n"
95+
+ f" pull side 1 [{stop_delay}]\n"
96+
+ f" set x, {self.bitcount - 1} side 0 [7]\n"
97+
+ "bitloop:\n"
98+
+ " out pins, 1\n"
99+
+ " jmp x-- bitloop [6]\n"
96100
)
97101
self.tx_pio = rp2pio.StateMachine(
98102
tx_code.assembled,
@@ -107,63 +111,74 @@ def __init__(self, tx=None, rx=None, baudrate=9600, bits=8, parity=None, stop=1,
107111
)
108112

109113
def deinit(self):
114+
"""De-initialize the UART object."""
110115
if self.rx_pio:
111116
self.rx_pio.deinit()
112117
if self.tx_pio:
113118
self.tx_pio.deinit()
114119

115120
@property
116121
def timeout(self):
122+
"""Return the UART timeout."""
117123
return self._timeout
118124

119125
@timeout.setter
120126
def timeout(self, value):
127+
"""Set the UART timeout."""
121128
self._timeout = value
122129

123130
@property
124131
def baudrate(self):
132+
"""Return the UART baudrate."""
125133
if self.tx_pio:
126134
return self.tx_pio.frequency // 8
127135
return self.rx_pio.frequency // 8
128136

129137
@baudrate.setter
130138
def baudrate(self, frequency):
139+
"""Set the UART baudrate."""
131140
if self.rx_pio:
132141
self.rx_pio.frequency = frequency * 8
133142
if self.tx_pio:
134143
self.tx_pio.frequency = frequency * 8
135144

136145
@property
137146
def in_waiting(self):
147+
"""Return whether the UART is waiting."""
138148
return self.rx_pio.in_waiting
139149

140150
def reset_input_buffer(self):
151+
"""Clear the UART input buffer."""
141152
self.rx_pio.clear_rxfifo()
142153

143154
def readinto(self, buf):
155+
"""Read UART data into buf and return the number of bytes read."""
144156
if self.bitcount > 8:
145157
raw_in = array.array("H")
146158
for _ in range(len(buf)):
147159
raw_in.append(0)
148160
else:
149161
raw_in = buf
150-
mv = memoryview(raw_in)
162+
mem_view = memoryview(raw_in)
151163
count = 0
152164
start_time = time.monotonic()
153-
while count < len(buf) and (self.timeout == 0 or (time.monotonic() - start_time) < self.timeout):
165+
while count < len(buf) and (
166+
self.timeout == 0 or (time.monotonic() - start_time) < self.timeout
167+
):
154168
waiting = min(len(buf) - count, self.rx_pio.in_waiting)
155-
self.rx_pio.readinto(mv[count:count+waiting])
169+
self.rx_pio.readinto(mem_view[count : count + waiting])
156170
if self.timeout == 0 and waiting == 0:
157171
return None if count == 0 else count
158172
count += waiting
159173

160-
if self.parity != None:
174+
if self.parity is not None:
161175
for i in range(count):
162176
# TODO: Check parity bits instead of just masking them.
163177
buf[i] = (raw_in[i] >> self.shift) & self.mask
164178
return count
165179

166180
def read(self, n):
181+
"""Read and return an array of up to n values from the UART."""
167182
if self.bits > 8:
168183
buf = array.array(n)
169184
else:
@@ -174,11 +189,12 @@ def read(self, n):
174189
return buf
175190

176191
def write(self, buf):
192+
"""Write the contents of buf to the UART."""
177193
# Compute parity if we need to
178194
if self.parity:
179195
if self.bitcount > 8:
180-
a = array.array("H")
181-
for i, v in enumerate(buf):
196+
a = array.array("H") # pylint: disable=invalid-name
197+
for i, v in enumerate(buf): # pylint: disable=invalid-name
182198
a.append(v)
183199
ones = 0
184200
for pos in range(self.bitcount - 1):
@@ -188,7 +204,8 @@ def write(self, buf):
188204
if self.parity == self.Parity.ODD:
189205
if (ones % 2) == 0:
190206
parity = 1
191-
elif (ones % 2) == 1: # even parity needs another one if the data is odd
207+
elif (ones % 2) == 1:
208+
# even parity needs another one if the data is odd
192209
parity = 1
193210
a[i] |= parity << (self.bitcount - 1)
194211
buf = a

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# digitalio, micropython and busio. List the modules you use. Without it, the
2929
# autodoc module docs will fail to generate with a warning.
3030
# autodoc_mock_imports = ["digitalio", "busio"]
31+
autodoc_mock_imports = ["rp2pio"]
3132

3233
autodoc_preserve_defaults = True
3334

docs/index.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ Table of Contents
2424
.. toctree::
2525
:caption: Tutorials
2626

27-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
28-
the toctree above for use later.
29-
3027
.. toctree::
3128
:caption: Related Products
3229

33-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
34-
the toctree above for use later.
30+
Adafruit Raspberry Pi RP2040 Pico products <https://www.adafruit.com/category/875>
3531

3632
.. toctree::
3733
:caption: Other Links

examples/pio_uart_simpletest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5+
import board
56
import adafruit_pio_uart
67

78
uart = adafruit_pio_uart.UART(board.TX, board.RX)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
# SPDX-License-Identifier: MIT
55

66
Adafruit-Blinka
7+
adafruit-circuitpython-pioasm

0 commit comments

Comments
 (0)