Re: Safe way to write interrupt driven circular buffer tx rx?



On Aug 29, 11:54 am, Stefan Reuther <stefan.n...@xxxxxxxx> wrote:
cs_post...@xxxxxxxxxxx wrote:
On Aug 29, 8:21 am, "Sven" <S...@xxxxxxx> wrote:
Can someone point out source code for a safe circular buffer receiver
transmitter? It's for sending and receiving bytes via RS232.

What I do is let the interrupt process freely modify the buffer count
& head variables.

The interruptible process modifies the buffer variables in only one
small block of code, which is bracketed with interrupt disable &
enable instructions.

You can avoid that interrupt disable by using head and tail variables
instead of head and count. The interrupt would then modify only the head
(and read the tail to check for overflow), the interruptible process
would modify only the tail (and read the head to check whether anything
is in the buffer).

This is the way I do it. A couple small things to watch:

1) You must _always_ have at least one empty slot in your buffer. You
can probably figure out why for yourself (hint: what does head==tail
mean?).

2) You can avoid critical sections (e.g., disabling interrupts) only
if access to the head and tail variables is atomic. For example, if
head and tail are 16 bits wide, and you're running on an AVR or 8051,
you'll still have to protect access to the variables in your main-line
code.

3) Even if access to the head an tail variables is atomic, you must
still be careful about how you order your code. Always check the full/
empty status first. Then copy the data. Update the index last.

Regards,

-=Dave

.



Relevant Pages

  • Re: aes decrypt encrypt
    ... head onto it. ... Are you dear, I mean, embracing as rich warmths? ... Who will we interrupt after Jbilou plots the homeless membership's ...
    (sci.crypt)
  • Re: [ATA] and re(4) stability issues
    ... >> this is related with interrupt storms. ... >> w.r.t MSI so it was off by default. ... Both HEAD and stable/7 including 7.1 BETA2 have ... an irq256 or higher for MSI enabled driver. ...
    (freebsd-stable)
  • Re: [ATA] and re(4) stability issues
    ... >> this is related with interrupt storms. ... >> w.r.t MSI so it was off by default. ... Both HEAD and stable/7 including 7.1 BETA2 have ... > This was sharing interrupt with USB, does USB need any special MSI handling ...
    (freebsd-stable)
  • Re: Int 13h function 08h - Read drive parameters
    ... Interrupt 13h–Fixed Disk Services, Continued ... DH Maximum head number ... Are you familiar with Ralf Brown's Interrupt List (aka RBIL)? ... specification which describes CHS and LBA translation or try reading Hale ...
    (comp.lang.asm.x86)
  • Re: [ATA] and re(4) stability issues
    ... > reto corrupt packets and create interrupt storms. ... It seems that your controller supports MSI so you can set a tunable ... Both HEAD and stable/7 including 7.1 BETA2 have ...
    (freebsd-stable)