\ SCI1 SERIAL I/O EXAMPLE \ For IsoPod versions 0.50 or earlier. HEX 0F10 CONSTANT SCI1BR \ baud rate register 0F11 CONSTANT SCI1CR \ control register 0F12 CONSTANT SCI1SR \ status register 0F13 CONSTANT SCI1DR \ data register 8000 CONSTANT SCISR_TDRE \ Transmit Data Reg Empty bit 2800 CONSTANT SCISR_RDRF \ Receive Data Reg Full bit OR Overrun bit 0800 CONSTANT SCISR_ROVR \ Receive Overrun bit 000C CONSTANT SCICR_TE&RE \ Transmit & Receive enable : SCI1-TX? ( -- f ) SCI1SR @ SCISR_TDRE AND ; : SCI1-TX ( c -- ) BEGIN SCI1-TX? UNTIL SCI1DR ! ; : SCI1-RX? ( -- f ) SCI1SR @ SCISR_RDRF AND ; : SCI1-RX ( -- c ) BEGIN SCI1-RX? UNTIL SCISR_ROVR SCI1SR CLEAR-BITS SCI1DR @ ; DECIMAL : U2/ ( n -- n ) 0 D2/ DROP ; : SCI1-BAUD ( n -- ) SCICR_TE&RE SCI1CR ! \ enable SCI1 port DUP U2/ 0 2500000. D+ ROT UM/MOD SWAP DROP \ compute baud divisor SCI1BR ! ; \ Example 1. Read exactly N characters from the SCI1 input, into the given \ address A. This will loop forever until six characters have been received. \ Note that the loop is from A to A+N, and the loop index I gives the \ address where data is to be stored. : RX-N-CHARS ( A N -- ) OVER + SWAP DO SCI1-RX I C! LOOP ; \ Test case. VARIABLE RX-ARRAY 6 ALLOT RX-ARRAY 6 RX-N-CHARS \ reads 6 characters into RX-ARRAY \ Example 2. Write exactly N characters to the SCI1 output. \ With unbuffered transmit, this will loop until all N characters have \ been transmitted. : TX-N-CHARS ( A N -- ) OVER + SWAP DO I C@ SCI1-TX LOOP ; \ Test case. RX-ARRAY 6 TX-N-CHARS \ transmits 6 characters from RX-ARRAY