Skip to content

Commit f08a16e

Browse files
committed
Added _rxpoll() function
1 parent a6994fa commit f08a16e

File tree

11 files changed

+2105
-1898
lines changed

11 files changed

+2105
-1898
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version 7.1.0
2+
- Added _rxpoll() to check for character without a timeout
23
- Updated -gbrk debug code (thanks Ada)
34

45
Version 7.0.5

doc/c.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,11 @@ These ioctls get and set flags controlling the operation of terminals (e.g. the
10661066

10671067
### _rxraw
10681068

1069-
Reads a single character from the default serial port, with no processing (no echoing and no CR/LF conversion). Takes a single parameter giving a timeout in milliseconds (with 0 meaning "no timeout, wait forever"). If a timeout occurs before the character is read, returns -1, otherwise returns the ASCII character read.
1069+
Reads a single character from the default serial port, with no processing (no echoing and no CR/LF conversion). Takes a single parameter giving a timeout in milliseconds (with 0 meaning "no timeout, wait forever"). If a timeout occurs before the character is read, returns -1, otherwise returns the ASCII character read. See also `_rxpoll()`.
1070+
1071+
### _rxpoll
1072+
1073+
Attempts to read a single character from the default serial port. If no character is available, returns -1 immediately, otherwise returns the character. If you would prefer to wait for a character, use `_rxraw` instead.
10701074

10711075
### _txraw
10721076

doc/general.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ All languages have a `_setbaud(N)` function to set the baud rate to `N`.
561561
### Sending and receiving characters
562562

563563
All languages support some functions for doing basic serial I/O.
564-
`_tx(ch)` sends a character to the serial port, with appropriate carriage return mapping (see below for how this is changed). `_txraw(ch)` sends the character with no interpretation. `_rx()` reads a character from the serial port, waiting until one is available. `_rxraw(tim)` attempts to read a character, waiting for up to `tim` milliseconds (actually 1/1024ths of a second) for one to be available; if no character is received before the timeout, returns -1.
564+
`_tx(ch)` sends a character to the serial port, with appropriate carriage return mapping (see below for how this is changed). `_txraw(ch)` sends the character with no interpretation. `_rx()` reads a character from the serial port, waiting until one is available. `_rxraw(tim)` attempts to read a character, waiting for up to `tim` milliseconds (actually 1/1024ths of a second) for one to be available; if no character is received before the timeout, returns -1. `_rxpoll()` is like `_rxraw()` but returns -1 immediately if no character is available.
565565

566566
### Changing echo and CR/LF interpretation
567567

@@ -925,7 +925,14 @@ sends character `c` out the default serial port. Always returns 1.
925925
```
926926
int _rxraw(int n=0)
927927
```
928-
Receives a character on the default serial port. `n` is a timeout in milliseconds. If the timeout elapses with no character received, `_rxraw` returns -1, otherwise it returns the received character. The default timeout (0) signifies "forever"; that is, if `n` is 0 the `_rxraw` function will wait as long as necessary until a character is received.
928+
Receives a character on the default serial port. `n` is a timeout in milliseconds. If the timeout elapses with no character received, `_rxraw` returns -1, otherwise it returns the received character. The default timeout (0) signifies "forever"; that is, if `n` is 0 the `_rxraw` function will wait as long as necessary until a character is received. If you would rather check for a character and have the function return immediately if none is available, use `_rxpoll()` instead of `_rxraw()`.
929+
930+
#### _rxpoll
931+
932+
```
933+
int _rxpoll()
934+
```
935+
Checks for a character on the default serial port. If no character is available, returns -1 immediately. Otherwise it returns the next character.
929936

930937
#### _setbaud
931938

sys/bytecode_rom.spin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ pri _rxraw(timeout = 0)
308308
_remotecall(3, timeout)
309309
return __helper_arg[0]
310310

311+
pri _rxpoll() | timeout
312+
timeout := 1 ' wait minimal number of cycles
313+
_remotecall(3, timeout)
314+
return __helper_arg[0]
315+
311316
''
312317
'' divide (n, nlo) by d, producing qlo and rlo (used in FRAC operation)
313318
''

sys/bytecode_rom.spin.h

Lines changed: 673 additions & 663 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/nucode_util.spin

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ pri _rxraw(timeout = 0) : rxbyte = long | z, endtime, temp2, rxpin
150150
if endtime - _getcnt() < 0
151151
quit
152152

153+
' returns -1 immediately if no pending character
154+
pri _rxpoll() : rxbyte = long | z, rxpin
155+
rxbyte := -1
156+
rxpin := _rxpin
157+
z := _pinr(rxpin)
158+
if z
159+
rxbyte := _rdpin(rxpin)>>24
160+
153161
''
154162
'' memset/memmove are here (in processor specific code)
155163
'' because on P2 we can optimize them (long operations do

sys/nucode_util.spin.h

Lines changed: 624 additions & 608 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/p1_code.spin

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ pri _rxraw(timeout = 0) | val, waitcycles, i, bitcycles
175175
_waitcnt(waitcycles + bitcycles)
176176
return val
177177

178+
' like _rxraw, but no timeout and returns -1 immediately if no data
179+
pri _rxpoll() | val, i, bitcycles, pin
180+
bitcycles := _bitcycles
181+
if (bitcycles == 0)
182+
bitcycles := _setbaud(__default_baud__)
183+
pin := _rxpin
184+
dira[pin] := 0
185+
186+
if ina[pin] <> 0
187+
return -1
188+
189+
waitcycles := cnt + (bitcycles>>1)
190+
val := 0
191+
repeat 8
192+
_waitcnt(waitcycles += bitcycles)
193+
val := (ina[pin] << 7) | (val>>1)
194+
_waitcnt(waitcycles + bitcycles)
195+
return val
196+
178197
pri _setbaud(rate) : r
179198
_bitcycles := r := __clkfreq_var / rate
180199

sys/p1_code.spin.h

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sys/p2_code.spin

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,34 @@ pri _rxraw(timeout = 0) : rxbyte = long | z, endtime, temp2, rxpin
217217
rxbyte := temp2 & $ff
218218
_rx_temp := temp2
219219

220+
' like _rxraw, but no timeout and returns -1 at once if no pending
221+
' character
222+
pri _rxpoll() : rxbyte = long | z, temp2, rxpin
223+
if _bitcycles == 0
224+
_setbaud(__default_baud__)
225+
rxbyte := -1
226+
rxpin := _rxpin
227+
z := 0
228+
temp2 := _rx_temp
229+
'' slightly tricky code for pulling out the bytes from the 28 bits
230+
'' of data presented by the smartpin
231+
'' Courtesy of evanh
232+
asm
233+
testb temp2, #8 wc ' check framing of prior character for valid character
234+
testbn temp2, #9 andc ' more framing check (1 then 0)
235+
shr temp2, #10 ' shift down next character, if any
236+
if_c mov z, #1
237+
if_c jmp #.breakone
238+
testp rxpin wz
239+
if_z mov z, #1
240+
if_z rdpin temp2, rxpin
241+
if_z shr temp2, #32 - 28
242+
.breakone
243+
endasm
244+
if z
245+
rxbyte := temp2 & $ff
246+
_rx_temp := temp2
247+
220248
pri _dirl(pin = long)
221249
asm
222250
dirl pin

0 commit comments

Comments
 (0)