<commit_before>\ serCM3_sapi_level0.fth - SAPI polled RX, polled TX, serial communication drivers
\ Written to run against the SockPuppet API.

\ Level 0 support, with WFI instead of pause.
\ The SAPI is a system call interace, so there is no blocking.
\ KEY requires blocking, so that must be implemented here.

only forth definitions

\ ==============
\ *T SAPI polled serial driver
\ ==============
\ *P This driver provides polled serial comms for Cortex-M3 CPUs
\ ** using the internal UARTs via the SAPI call layer.   This is 
\ ** The simplest implementation level.

\ ****************
\ *S Configuration
\ ****************
\ *P Configuration is managed by the SAPI

target

\ ********************
\ *S Serial primitives
\ ********************
internal

: +FaultConsole	( -- )  ;
\ *G Because this is a polled driver, *\fo{+FaultConsole} for
\ ** fault handling is a *\fo{NOOP}. See *\i{Cortex/FaultCortex.fth}
\ ** for more details.

target

\ CODE (seremitfc)	\ char base --
\ This is a system call.

: (seremit)	\ char base -- 
\ *G Wrapped call that checks for throttling, and if so,
\ calls PAUSE to let another task run, or just does a WFI if no multitasker.
\ The only interesting thing is failure, or -1.  That means retry.
	begin 
	2dup (seremitfc) dup 0 < \ char base ret t/f
[ tasking? ] [if]
	IF PAUSE THEN
[else]
	IF [asm wfi asm] THEN
[then] \ char base ret 
    0 >= until
    2drop
	;

: (sertype) \ caddr len base --
\ *G Transmit a string on the given UART.
\ * The system call wants     base len caddr
	>R  \ We'll use this a few times.
	begin 
	2dup R@ (sertypefc)  dup \ caddr len ret ret
	IF 
	  [ tasking? ] [if] PAUSE [else] [asm wfi asm] [then]
	  dup >R - swap R> + swap \ Advance the caddr len pair
	THEN 
	0= until
	2drop 
	R> drop 
;

: (sercr)	\ base --
\ *G Transmit a CR/LF pair on the given UART.
\ Wrapped version, just like (seremit)
	begin 
	dup (sercrfc) dup 0= \ base ret t/f
[ tasking? ] [if]
	IF PAUSE THEN
[else]
	IF [asm wfi asm] THEN
[then] \ base ret 
    until
    drop
	;

: (serkey) \ base -- c
\ *G Get a character from the port.  Retry until we get it.
\ See if its negative.  If so, discard it and leave false to re-loop.
\ If not negative, leave true on the stack to exit.
    begin \ base ret t/f 
	dup (serkeyfc) dup 0 <  \ base ret t/f   
	[ tasking? ] [if]
		IF PAUSE drop false ELSE true THEN
	[else]
		IF [asm wfi asm] drop false ELSE true THEN
	[then] \ base ret 
	until  \ base ret 
	swap drop 
;

\ THis is a system call.
\ CODE (serkey?)     \ base -- t/f
\ *G Return true if the given UART has a character avilable to read.

external 

: seremit0	\ char --
\ *G Transmit a character on UART0.
  0 (seremit)  ;
: sertype0	\ c-addr len --
\ *G Transmit a string on UART0.
  0 (sertype)  ;
: sercr0	\ --
\ *G Issue a CR/LF pair to UART0.
  0 (sercr)  ;
: serkey?0	\ -- flag
\ *G Return true if UART0 has a character available.
  0 (serkey?)  ;
: serkey0	\ -- char
\ *G Wait for a character on UART0.
  0 (serkey)  ;
create Console0	\ -- addr ; OUT managed by upper driver
\ *G Generic I/O device for UART0.
  ' serkey0 ,		\ -- char ; receive char<commit_msg>Make things a bit more concise.<commit_after>\ serCM3_sapi_level0.fth - SAPI polled RX, polled TX, serial communication drivers
\ Written to run against the SockPuppet API.

\ Level 0 support, with WFI instead of pause.
\ The SAPI is a system call interace, so there is no blocking.
\ KEY requires blocking, so that must be implemented here.

only forth definitions

\ ==============
\ *T SAPI polled serial driver
\ ==============
\ *P This driver provides polled serial comms for Cortex-M3 CPUs
\ ** using the internal UARTs via the SAPI call layer.   This is 
\ ** The simplest implementation level.

\ ****************
\ *S Configuration
\ ****************
\ *P Configuration is managed by the SAPI

target

\ ********************
\ *S Serial primitives
\ ********************
internal

: +FaultConsole	( -- )  ;
\ *G Because this is a polled driver, *\fo{+FaultConsole} for
\ ** fault handling is a *\fo{NOOP}. See *\i{Cortex/FaultCortex.fth}
\ ** for more details.

target

\ CODE (seremitfc)	\ char base --
\ This is a system call.

: (seremit)	\ char base -- 
\ *G Wrapped call that checks for throttling, and if so,
\ calls PAUSE to let another task run, or just does a WFI if no multitasker.
\ The only interesting thing is failure, or -1.  That means retry.
	begin 
	2dup (seremitfc) dup 0 < \ char base ret t/f
	IF [ tasking? ] [if] PAUSE [else] [asm wfi asm] [then] THEN \ char base ret 
    0 >= until
    2drop
	;

: (sertype) \ caddr len base --
\ *G Transmit a string on the given UART.
\ * The system call wants     base len caddr
	>R  \ We'll use this a few times.
	begin 
	2dup R@ (sertypefc)  dup \ caddr len ret ret
	IF 
	  [ tasking? ] [if] PAUSE [else] [asm wfi asm] [then]
	  dup >R - swap R> + swap \ Advance the caddr len pair
	THEN 
	0= until
	2drop 
	R> drop 
;

: (sercr)	\ base --
\ *G Transmit a CR/LF pair on the given UART.
\ Wrapped version, just like (seremit)
	begin 
	dup (sercrfc) dup 0= \ base ret t/f
    IF [ tasking? ] [if] PAUSE [else] [asm wfi asm] [then] THEN \ base ret 
    until
    drop
	;

: (serkey) \ base -- c
\ *G Get a character from the port.  Retry until we get it.
\ See if its negative.  If so, discard it and leave false to re-loop.
\ If not negative, leave true on the stack to exit.
    begin \ base ret t/f 
	dup (serkeyfc) dup 0 <  \ base ret t/f 
	 IF [ tasking? ] [if] PAUSE [else] [asm wfi asm] [then] drop false 
	 ELSE true 
	 THEN \ base ret t/f 
	until  \ base ret 
	swap drop 
;

\ THis is a system call.
\ CODE (serkey?)     \ base -- t/f
\ *G Return true if the given UART has a character avilable to read.

external 

: seremit0	\ char --
\ *G Transmit a character on UART0.
  0 (seremit)  ;
: sertype0	\ c-addr len --
\ *G Transmit a string on UART0.
  0 (sertype)  ;
: sercr0	\ --
\ *G Issue a CR/LF pair to UART0.
  0 (sercr)  ;
: serkey?0	\ -- flag
\ *G Return true if UART0 has a character available.
  0 (serkey?)  ;
: serkey0	\ -- char
\ *G Wait for a character on UART0.
  0 (serkey)  ;
create Console0	\ -- addr ; OUT managed by upper driver
\ *G Generic I/O device for UART0.
  ' serkey0 ,		\ -- char ; receive char
{'fraction_non_alphanumeric': 0.13301204819277107, 'fraction_numerical': 0.010281124497991968, 'mean_word_length': 2.634559252772913, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7292360', 'n_tokens_mistral': 2292, 'n_tokens_neox': 2212, 'n_words': 917}