Hsersetup - Background Receive. I'm baffled

Goeytex

Senior Member
Per Manual 2 Page 76

HSERSETUP

=======================================================

In automatic mode the hardware serial input is fully automated. Serial data
received by the hardware pin is saved into the scratchpad memory area as
soon as it is received.

Upon the hsersetup command the serial pointer (hserptr) is reset to 0.
When a byte is received it is saved to this scratchpad address, the hserptr
variable is incremented and the hserinflag flag is set (must be cleared by user
software).

Therefore the value `hserptr -1' indicates the last byte written, and
`hserinflag = 1' indicates a byte has been received (see also the setintflags
command). The scratchpad is a circular buffer that overflows without
warning.


=========================================================

If I take this at face value there is a system variable called "hserptr"
and I should be able to read it like any other variable ? Correct ?

Then it says that "the hserinflag flag ..... must be reset by user software".
But no further explanation is given as to how ... or when this is necessary.

Shall I assume that "user sofware" means the running program and that the
Hser interrupt needs to be cleared at some point via the setintflags command?

Let me see if I understand this correctly.

So the Hserint Flag is set to 0 when HSERSETUP is invoked. The program
goes happily about its business until the first data byte is received. This
triggers an interrupt and set the hserint Flag to 1. Program execution then
stops while data is received. Program execution does not resume until the
program clears the interrupt via setintflags command. ?????

I have HSERIN and HSEROUT working fine at 9600 - 38400 baud. However
since the simulator cannot simulate background receive I cannot really test
anything without some kind of starting point for code. I am baffled.

The Manual 2 is vague and offers ABSOLUTELY NOTHING in the way of a
practical example on how to use HSERSETUP in background receive mode.

Can a practical example be provided ? Please, something a bit more than 2
lines with no explanation.

Thanks,

Goey
 

BeanieBots

Moderator
Seems clear enough to me, unless I've got it wrong.

Yes, there is a variable called hserptr which is set to zero when HSERSETUP is invoked.
When a char is received, it is put into scratchpad. (background task, it "just happens" without any intervention required on your part).
At the same time, the hserinflag flag is set so that YOU can test it and act accordingly. You can have the setting of the flag generate an interrupt so that your code can be interrupted so that YOU can act on it.

Nothing will get stuck unless you write an interrupt handler that gets stuck.

Just initiate with HSERSETUP and it all just happens!
Up to you if you want to write an interrupt driven handler or poll the flag in code. Data will just magically 'appear' in scratchpad.
 

Chavaquiah

Senior Member
If I take this at face value there is a system variable called "hserptr"
and I should be able to read it like any other variable ? Correct ?
Yes, I think.

Then it says that "the hserinflag flag ..... must be reset by user software".
But no further explanation is given as to how ... or when this is necessary.
You either do some bit manipulation on the flags variable or simply perform a hserinflag = 0.

I assume it is necessary so that when another byte comes along you know there is new data in the buffer, not the one you already processed. Also, if you're using interrupts, not clearing the hserinflag would probably retrigger an interrupt as soon as you reenable them (I think that's what would happen).

Program execution then
stops while data is received. Program execution does not resume until the
program clears the interrupt via setintflags command. ?????
Don't think so. Program execution should continue unless you set an interrupt for serinflag. If so, "normal" program execution would indeed halt while the interrupt routine does it's job. When you exit the interrupt routine, normal program should resume... unless you didn't clear the hserinflag...
 
Last edited:

hippy

Technical Support
Staff member
You can think of it as an outdoor mail box with a flag to indicate when mail is inside; the flag is the hserinFlag, set whenever any mail is placed in the box.

You see the flag high, rush out to get your mail. If you don't clear the flag, you get back inside, look out, see the flag's high again, rush out, even though there's no new mail.

If you are not using hserinFlag to determine if new data has arrived ( hserptr will increment on new data so that can be used instead ) you can simply ignore it. You can either poll the hserinFlag to see if it gets set or you can use SETINTFLAGS to generate an interrupt when it becomes set. This is where it becomes important to clear the hserinFlag or you'll keep re-entering the interrupt ( just like rushing outside to check your mail ).
 

Goeytex

Senior Member
Just initiate with HSERSETUP and it all just happens!
Well it wasn't just happening ain't far as I could tell. Especially since the simulator is not able to help.

So I wrote this so I could see what was happening. I use Hyperterminal with
the Emulator set to VT100 , Line wrap ON and local echo Off

Code:
INIT: 

  HSERSETUP B9600_4, %01       'background receive
  hserout 0,(27,"[?25l")              'Hide Cursor
  hserout 0,(27,"[2J")                ' Clear Screen
    
  hserout 0,(27,"[10;30HPress a Key to Start")
  pause 2000
    
    
  MAIN:

do
                    
     
  if hserinflag = 1 then
    let b3 = hserptr 
    
    hserout 0, (27,"[2;1HPress a Key")
    hserout 0, (27,"[4;1H")    'set cursor location
    hserout 0, ("Next received byte will be written at location ",#b3,"  ")
    hserout 0, (CR,LF,LF,"DATA") 
    hserout 0, (LF,LF)
    hserout 0, (27,"[10;1H")   ;set cursor position Row:Col    
    
    for b0 = 0 to 127
      get b0,b1
      hserout 0, (#b1,",")
      next b0
     endif
     
loop
This little test program showed me that it was working ok as it continously reads out all 128 scratchpad bytes. And displays the pointer location

But I have 1 Question. How do you reset the hserin flag ?

Is it as simple as ..... let hserinflag = 0 ?

Thanks,

Goey
 
Last edited:

Goeytex

Senior Member
OK I got it.

I see I had already received the answer from Chavaquiah but missed it.

It is all working very nicely now.

In 10 more years I'll be an expert on this stuff. :p


Thanks
 
Top