1616* Author(s): Scott Shawcroft
1717"""
1818
19- import adafruit_pioasm
2019import array
20+ import time
21+
22+ import adafruit_pioasm
2123import busio
2224import 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
2930class 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
0 commit comments