Serial Port Status Program

Use this section to showcase your programming examples. Or to ask members for programming suggestions
Post Reply
amenjet
Posts: 179
Joined: Tue Jan 03, 2023 7:54 pm

Serial Port Status Program

Post by amenjet »

To test the USB adapters I have recently made, and verify that the design is correct with respect to the handshaking lines, I wrote this little utility. As the handshaking lines can't be manipulated in OPL, it's written in assembly language. I assembled it with my macro assembler and used the output that embeds it into OPL.

Assembly source code:

Code: Select all

	
	.INCLUDE MSWI.INC
	.INCLUDE MOSHEAD.INC
	.INCLUDE MOSVARS.INC
	.INCLUDE COMMS.INC

cts     .equ  1
ncts    .equ  254
dsr     .equ  2
rts     .equ  4

	;; --------------------------------------------------
	;;  Sign on
	;;
	;; --------------------------------------------------

	bsr 1$
	
	.ascic "Serial Status"
	
1$:	pulx
	ldab  0,x
	inx
	os    dp$prnt
	
	os    kb$getk	
	
	;; --------------------------------------------------
	;; Check for comms link. If present then proceed
	;; otherwise just exit
	;;
	;; --------------------------------------------------

	bsr check_present
	bcs not_present
	bra is_present

check_present:
	bsr 1$         ; PC relative
	.ascic "XFEOF" ; leading count byte string	

1$:  	pulx           ; PC = address of "XFEOF"
			   ;os dv$lkup     ; if XFEOF not there, no COMMS LINK
	rts            ; see tech. ref. manual

	;; --------------------------------------------------------------------------------
	;; 
	;; Not present so display a message
	;;
	
not_present:	

	bsr 1$
	
	.ascic "No Comms Link"
	
1$:	pulx
	ldab  0,x
	inx
	os    dp$prnt
	os    kb$getk	

        rts

	;; --------------------------------------------------------------------------------
	;; Comms Link present so sit in loop
	;; If X pressed then exit
	;; C toggles CTS
	;; Status of other lines displayed

is_present:	
	clrb           ; Open for reading and writing
	RS rs_open
	bcs error      ; Deal with error
           ; Now free to access the port

	bra  statloop
	
error:	

	rts

statloop:
	;; Stop kb$test from turning packs off, that would stop us getting
	;; access to the port hardware.
	
	ldaa #0
	staa kbb_pkof
	
	;; Check the keyboard
	os    kb$test
	cmpb  #0
	beq   nokey

	;; Key pressed, but kb$test has put it in the unget buffer
	;; where it will get stuck. Use kb$getk to actually get the key
	
	os    kb$getk
	
	;; Key pressed, handle it
	cmpb  #^A'X'
	beq   doexit

	;; CTS control
	cmpb  #^A'S'
	beq   cts0
	cmpb  #^A'T'
	beq   cts1

nokey:	
	;; Display status
	;; of serial port handshake lines
	
	ldaa  #0		;Cursor to top left
	ldab  #0
	os    dp$stat

	ldaa  #14
	os    dp$emit
	
	bsr   1$
	
	.ascic "RTS="
	
1$:	pulx
	ldab  0,x
	inx
	os    dp$prnt

	;; Get RTS state
	ldaa  POB_PORT2
	anda  #4
	asra
	asra
	adda  #^A'0'
	os    dp$emit

        bsr   2$
	
	.ascic	" CTS="

2$:	pulx
	ldab  0,x
	inx
	os    dp$prnt

	;; Get CTS state
	ldab  #0
	bsr   ctsvar
	adda  #^A'0'
	
	os    dp$emit
        ldaa  #^A' '
	os    dp$emit
        ldaa  #10
	os    dp$emit
        ldaa  #13
	os    dp$emit

	bsr   3$
	
	.ascic	"CTS:S,T Exit:X"

3$:	pulx
	ldab  0,x
	inx
	os    dp$prnt

	;; Loop back
	bra  statloop

	;; --------------------------------------------------------------------------------
	;; 
doexit:
	;;  Now close the comms link
	ldab  #0
	RS    rs_close

	;; Allow packs to be turned off again by kb$test
	ldaa #255
	staa kbb_pkof

	rts

	;; --------------------------------------------------------------------------------
	;; Set CTS to zero
	;; 
cts0:
        aim #ncts, POB_PORT2
	ldaa #0
	ldab #1
	bsr  ctsvar
	
	bra  nokey

	;; Set CTS to 1
cts1:
        oim #cts,  POB_PORT2
	ldaa #1
	ldab #1
	bsr  ctsvar

	bra  nokey

	;; --------------------------------------------------------------------------------
	;;
	;;  We have to use a position independent varaible to hold the
	;; CTS state as we cn't read the state of the output

ctsvar:
	bsr   4$

	.byte 0

4$:	pulx

	;;  Set or read the var?
	cmpb  #0
	beq   readvar

	;; Write variable
	staa  0,x
	rts

readvar:
	ldaa  0,x
	rts

	;; --------------------------------------------------------------------------------
The OPL that is generated is here, it is stand-alone:

Code: Select all


statser:
LOCAL MC$(240)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$($3f)
MC$=MC$+CHR$($48)
MC$=MC$+CHR$($39)
MC$=MC$+CHR$($5f)
MC$=MC$+CHR$($bd)
MC$=MC$+CHR$($21)
MC$=MC$+CHR$($74)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($25)
MC$=MC$+CHR$($02)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($01)
MC$=MC$+CHR$($39)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($b7)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($c1)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($4b)
MC$=MC$+CHR$($c1)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($27)
MC$=MC$+CHR$($0e)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($48)
MC$=MC$+CHR$($c1)
MC$=MC$+CHR$($58)
MC$=MC$+CHR$($27)
MC$=MC$+CHR$($64)
MC$=MC$+CHR$($c1)
MC$=MC$+CHR$($53)
MC$=MC$+CHR$($27)
MC$=MC$+CHR$($6c)
MC$=MC$+CHR$($c1)
MC$=MC$+CHR$($54)
MC$=MC$+CHR$($27)
MC$=MC$+CHR$($73)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($c6)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($14)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($0e)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($10)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($05)
MC$=MC$+CHR$($04)
MC$=MC$+CHR$($52)
MC$=MC$+CHR$($54)
MC$=MC$+CHR$($53)
MC$=MC$+CHR$($3d)
MC$=MC$+CHR$($38)
MC$=MC$+CHR$($e6)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($08)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($11)
MC$=MC$+CHR$($96)
MC$=MC$+CHR$($03)
MC$=MC$+CHR$($84)
MC$=MC$+CHR$($04)
MC$=MC$+CHR$($47)
MC$=MC$+CHR$($47)
MC$=MC$+CHR$($8b)
MC$=MC$+CHR$($30)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($10)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($06)
MC$=MC$+CHR$($05)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($43)
MC$=MC$+CHR$($54)
MC$=MC$+CHR$($53)
MC$=MC$+CHR$($3d)
MC$=MC$+CHR$($38)
MC$=MC$+CHR$($e6)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($08)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($11)
MC$=MC$+CHR$($c6)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($4b)
MC$=MC$+CHR$($8b)
MC$=MC$+CHR$($30)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($10)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($10)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($0a)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($10)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($0d)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($10)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($0f)
MC$=MC$+CHR$($0e)
MC$=MC$+CHR$($43)
MC$=MC$+CHR$($54)
MC$=MC$+CHR$($53)
MC$=MC$+CHR$($3a)
MC$=MC$+CHR$($53)
MC$=MC$+CHR$($2c)
MC$=MC$+CHR$($54)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($45)
MC$=MC$+CHR$($78)
MC$=MC$+CHR$($69)
MC$=MC$+CHR$($74)
MC$=MC$+CHR$($3a)
MC$=MC$+CHR$($58)
MC$=MC$+CHR$($38)
MC$=MC$+CHR$($e6)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($08)
MC$=MC$+CHR$($3f)
MC$=MC$+CHR$($11)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($8b)
MC$=MC$+CHR$($c6)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($bd)
MC$=MC$+CHR$($21)
MC$=MC$+CHR$($74)
MC$=MC$+CHR$($01)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($ff)
MC$=MC$+CHR$($b7)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($c1)
MC$=MC$+CHR$($39)
MC$=MC$+CHR$($71)
MC$=MC$+CHR$($fe)
MC$=MC$+CHR$($03)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($c6)
MC$=MC$+CHR$($01)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($0d)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($72)
MC$=MC$+CHR$($01)
MC$=MC$+CHR$($03)
MC$=MC$+CHR$($86)
MC$=MC$+CHR$($01)
MC$=MC$+CHR$($c6)
MC$=MC$+CHR$($01)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($02)
MC$=MC$+CHR$($20)
MC$=MC$+CHR$($82)
MC$=MC$+CHR$($8d)
MC$=MC$+CHR$($01)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($38)
MC$=MC$+CHR$($c1)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($27)
MC$=MC$+CHR$($03)
MC$=MC$+CHR$($a7)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($39)
MC$=MC$+CHR$($a6)
MC$=MC$+CHR$($00)
MC$=MC$+CHR$($39)
RETURN USR(ADDR(MC$)+1,0)

When run, there's a sign on screen, press any key to get to the status display. You can set the CTS level and see the incoming RTS. DTR isn't displayed as it isn't passed through by the hardware, instead it turns the organiser on when asserted.

To assemble the source you will need the include files which are in the assembler repo, then use this line to assemble the code:

<path to assembler>/psion-org2-assembler/org2asm.tcl -f statser.asm

The OPL output I used was the file with an 'opl_3' extension.

Macro assembler:

https://github.com/blackjetrock/psion-org2-assembler

Just need to test the USB adapters now.

Andrew
Last edited by amenjet on Fri Mar 17, 2023 7:58 pm, edited 1 time in total.
MartinP
Posts: 45
Joined: Wed Jan 04, 2023 7:51 pm

Re: Serial Port Status Program

Post by MartinP »

Andrew,
Nice bit of programming. Maybe this should go in the hardware section, but it relates to your program:
In addition to powering up the Organiser, DTR on the original CommsLink could also be read on bit 1 (value 2) of the data port, as a one-way hand-shaking. The value of DTR at the port is after its inversion by the power on circuit. This was implemented in the CommsLink circuit with a diode, see partial circuit schematic below (sorry for the poor quality image, it's hand drawn). Did you add this diode to your CommsLink?
Martin P.
comms link v4.2 buffers-01.jpeg
You do not have the required permissions to view the files attached to this post.
amenjet
Posts: 179
Joined: Tue Jan 03, 2023 7:54 pm

Re: Serial Port Status Program

Post by amenjet »

Martin,

I was about to send you a one word reply:

'Nope!'.

Then I checked the circuit (stolen from Olivier's circuit, modified here and there, it uses the flash ROM chips from the 512K flash pack, as I have a few of them). The diode is indeed there, so I should be able to read DTR. I will now wander off to see if I can read DTR...

Andrew
amenjet
Posts: 179
Joined: Tue Jan 03, 2023 7:54 pm

Re: Serial Port Status Program

Post by amenjet »

OK, after add ing a DTR display to the code and squashing it to fit in the maximum string space in OPL and adding a trampoline for a relative jump, I have a new statser that displays DTR and RTS states. And yes, the USB adapter DTR state is available in bit 1.

New code:

Code: Select all

	.INCLUDE MSWI.INC
	.INCLUDE MOSHEAD.INC
	.INCLUDE MOSVARS.INC
	.INCLUDE COMMS.INC

cts     .equ  1
ncts    .equ  254
dsr     .equ  2
rts     .equ  4

	;; --------------------------------------------------
	;; Check for comms link. If present then proceed
	;; otherwise just exit
	;;
	;; --------------------------------------------------

	bsr check_present
	bcs not_present
	bra is_present

check_present:
	bsr 1$         ; PC relative
	.ascic "XFEOF" ; leading count byte string	

1$:  	pulx           ; PC = address of "XFEOF"
			   ;os dv$lkup     ; if XFEOF not there, no COMMS LINK
	rts            ; see tech. ref. manual

	;; --------------------------------------------------------------------------------
	;; 
	;; Not present so display a message
	;;
	
not_present:	

	bsr 1$
	
	.ascic "No Comms Link"
	
1$:	pulx
	ldab  0,x
	inx
	os    dp$prnt
	os    kb$getk	

        rts

	;; --------------------------------------------------------------------------------
	;; Comms Link present so sit in loop
	;; If X pressed then exit
	;; C toggles CTS
	;; Status of other lines displayed

is_present:	
	clrb           ; Open for reading and writing
	RS rs_open
	bcs error      ; Deal with error
           ; Now free to access the port

	bra  statloop
	
error:	

	rts

statloop:
	;; Stop kb$test from turning packs off, that would stop us getting
	;; access to the port hardware.
	
	ldaa #0
	staa kbb_pkof
	
	;; Check the keyboard
	os    kb$test
	cmpb  #0
	beq   nokey

	;; Key pressed, but kb$test has put it in the unget buffer
	;; where it will get stuck. Use kb$getk to actually get the key
	
	os    kb$getk
	
	;; Key pressed, handle it
	cmpb  #^A'X'
	beq   doexit

	;; CTS control
	cmpb  #^A'S'
	beq   cts0
	cmpb  #^A'T'
	beq   cts1

nokey:	
	;; Display status
	;; of serial port handshake lines
	
	ldaa  #0		;Cursor to top left
	ldab  #0
	os    dp$stat

	ldaa  #14
	os    dp$emit
	
	bsr   1$
	
	.ascic "RTS="
	
1$:	pulx
	ldab  0,x
	inx
	os    dp$prnt

	;; Get RTS state
	ldaa  POB_PORT2
	anda  #4
	asra
	asra
	adda  #^A'0'
	os    dp$emit

	bsr   tdtr
	
	.ascic " DTR="

statloop1: bra statloop

tdtr:	pulx
	ldab  0,x
	inx
	os    dp$prnt

	;; Get DTR state
	ldaa  POB_PORT2
	anda  #2
	asra
	adda  #^A'0'
	os    dp$emit

       bsr   2$
	
	.ascic	" CTS="

2$:	pulx
	ldab  0,x
	inx
	os    dp$prnt

	;; Get CTS state
	ldab  #0
	bsr   ctsvar
	adda  #^A'0'
	
	os    dp$emit
        ldaa  #^A' '
	os    dp$emit
        ldaa  #10
	os    dp$emit
        ldaa  #13
	os    dp$emit

	bsr   3$
	
	.ascic	"CTS:S,T Exit:X"

3$:	pulx
	ldab  0,x
	inx
	os    dp$prnt

	;; Loop back
	bra  statloop1

	;; --------------------------------------------------------------------------------
	;; 
doexit:
	;;  Now close the comms link
	ldab  #0
	RS    rs_close

	;; Allow packs to be turned off again by kb$test
	ldaa #255
	staa kbb_pkof

	rts

	;; --------------------------------------------------------------------------------
	;; Set CTS to zero
	;; 
cts0:
        aim #ncts, POB_PORT2
	ldaa #0
	ldab #1
	bsr  ctsvar
	
	bra  nokey

	;; Set CTS to 1
cts1:
        oim #cts,  POB_PORT2
	ldaa #1
	ldab #1
	bsr  ctsvar

	bra  nokey

	;; --------------------------------------------------------------------------------
	;;
	;;  We have to use a position independent varaible to hold the
	;; CTS state as we cn't read the state of the output

ctsvar:
	bsr   4$

	.byte 0

4$:	pulx

	;;  Set or read the var?
	cmpb  #0
	beq   readvar

	;; Write variable
	staa  0,x
	rts

readvar:
	ldaa  0,x
	rts

	;; --------------------------------------------------------------------------------

	

Andrew
Post Reply