// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// IASI interface
// This is the IASI kibrary for use with all IASI devices, these
// are normally BV41xx numbered devices.
// NOTE when using these words the IASI device needs
// by default a CR (13 emit1) sending to terminate
// a command, this tells the IASI to go off and process
// the command.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// HISTORY:
// Mar 2008 * replaced [char] : hold with 58 hold, limitation of BVT
// REQUIRED:
// #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb"
//
// CONSTANTS:
// the following constants are for setting the baud rate of UART1
// it will default to 9600, a higher baud rate will not necessarily
// make the device go faster
&E0010000 constant U1DLL
&E0010004 constant U1DLM
&E001000C constant U1LCR
// *******************************************************
// History:
// Feb 2007 Removed sign from iu. to stop & being
// sent to the UART1
// March 2007 Updated FOR, NEXT loops for ANSI ver 0.7 code
// removed until0
// Nov 2007 Use of #Library
// *******************************************************
// Communication set up for UART1
// set baud rate with 1 stop and 8 data
// no parity
: baud1 { rate --- }
rate 16 * 10 / // multiply rate by 1.6
PCLK swap / // divide by PCLK
5 + 10 / // round up and div by 10
256 u/mod
&83 U1LCR ! // 1 stop 8 bits
U1DLM ! // high divisor
U1DLL ! // low devisor
&3 U1LCR ! // DLAB=0, 1 stop 8 data
;
// returns baud rate for device 1
// ( --- rate )
: baud1@
&83 U1LCR ! // access to U1DLM,LL
U1DLM @ // high divisor
256 *
U1DLL @ // low devisor
+
&3 U1LCR ! // DLAB=0, 1 stop 8 data
// now calculate from these, baud rate
16 * pclk swap u/mod swap drop
;
// used for terminating a command, typical use is
// after sending command, example i.' BT"fred"' cr1
: cr1 13 emit1 ;
// gets a key form UART1 and will time out
// if no key is available. This saves complex
// code using key1? and prevents system
// crashing, set for time out of 50ms.
// It also has another purpose in that it will wait for a
// device to respond to a command
// meant for ASCII, return 0 if not valid
// ( --- r-val )
: i-in
0
50
for
key1?
if
drop
key1
leave
then
1 ms
next
;
// prints contents of uart1 buffer if any
// i-buff-dot
// ( --- )
: i-buff.
begin
i-in
dup
if
emit
-1 // dont quit loop
then
0= until
;
// clear buffer contents, does not use
// i-in as this simply needs clearing
// ( --- )
: i-cbuff
begin
key1? if key1 drop then
key1?
0= until
;
// Send string to IASI device using UART1
// addr is the address of a 0 terminated string
// max string length 255 chars
: stype1 { addr --- }
addr 255 + // end
addr // start
do
i c@ 0=
if
leave
else
i c@ emit1
then
next
;
// This is the same as u. but uses emit1 and
// stype1 to print to UART1
// prints n as ASCII
// ( n --- )
: iu. <# #s #> stype1 ;
// This is exactly the same as s" except that
// it uses ' to make it easier for sending
// string commands to IASI devices
// ( -- )
: ,'
int: addr1 len
39 word dup => addr1 // parse until '
length => len // length
addr1 here len move // ad1, ad2, len -- to dic
len allot // move pointer
0 c, // add string terminator
align
1 >in +! // only needed when using '
;
// specifically for sending text to IASI devices
// uses ' instead of " to make it easier for string commands
// ( -- )
// us in the form i.' BT"Fred"'
: I.'
['] lit , // compile LIT
here // address for type
16 + , // offset past STYPE + BRANCH + branch-address
['] stype1 , // compile type
['] branch , // branch round string
here // leave address to store fwd br to
0 , // to be resolved after string
,' // compile string to dic
here swap ! // resolve branch
; immediate
// simply sends 4 cr to device to establish baud rate
// ( --- )
: i-autobaud
4 for
cr1 100 ms
next
100 ms
i-cbuff // clear buffer
;
// ---------- command line words for debugging ------ //
//
// detect configuration
// x = 0 if no response
// 241 if inverted
// 76 if not inverted
// ( --- x)
: i-det
i-autobaud
cr1 // send cr and see what comes back
i-in // get first char
i-cbuff // clear buffer
;
// detects if device is in inverted mode or not
// as i-det, but this has print out
: i-det.
i-det
dup 214 = if ." Inverted mode" cr then
dup 76 = if ." Non-inverted mode" cr then
dup 0= if ." No response" cr then
drop
;
// initialises a device that has been configured
// for autobaud, reports success
: i-init
i-cbuff // clear buffer
5 for
i . // try 5 times
i-det // this does work
dup 214 =
if
." Device initialised, inverted mode"
leave
then
76 =
if
." Device initialised, non-inverted mode"
leave
then
next
;
// Software factory reset
: i-zf
." Wait"
i-cbuff // just in case
i-autobaud
i.' :00zf' cr1 // factory defaults, type 1
46 emit // wait..
500 ms
i-autobaud
i.' zf' cr1 // factory defaults, type 2
46 emit // wait..
500 ms
i-autobaud
;
// device configure
// n = factory configuration 0 to 5
// can use i-zc before this if required
// use: 3 i-config
// ( n --- )
: i-config
i.' ZC'
&30 + emit1 // convert stack value to ascii
i.' -r'
cr1
500 ms
i-autobaud
cr ." Device reports " i-det.
;
// -- multiple display addressing ---
// emits ":xx" to form the address
// numerical addresses only
// ( address ---)
: i-address
<# # # 58 hold #> stype1 // :addr
;
// This will set a device, using config slot 5
// to give null response at 9600 baud. It is a
// safe option. Supply address for device, must be
// in range 1-99. TEST with i-init, should return
// no response
// NOTE commands will still return values, there is simply
// no prompt.
// ( address --- )
: i-multi
." Wait."
i.' ZA' // set address
<# # # #>
stype1 // send as ASCII
cr1 100 ms // terminate address command
i.' ZB0' cr1 100 ms // baud rate 9600
i.' ZD0' cr1 100 ms // no error reporting
i.' ZC5 -w nnnnnnn' cr1 // set no response
46 emit
500 ms // device needs to reset
5 for // wait for response
i-det
0 > if leave then
next
100 ms
i.' ZC5 -r' cr1 // set it
;
// used for interfacing with device, see i-send
// and i-get, this word is not used on its own
// it gets and sends to UART1 the text in the TIB
// which is the text following the word
: (i-ipb)
i-cbuff // clear ipb
13 word // get i/p from stream
stype1 // send word to uart1
cr1 // get IASI to process it
;
// utility for sending and receiving from UART2
// sends text following word, must all be on 1 line
// use: I-SEND bt"hello"
// must be a space following the D of send
// ( --- )
: i-send
(i-ipb)
i-buff. // print result
cr
;
// for input devices will get the response
// from the command and return it on
// the stack. This expects 2 bytes
// use: i-get2 va*
// ( --- val)
: i-get2
(i-ipb) // get and send command following word
i-in 256 *
i-in +
;