Writing interrupt driven circular buffer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sven

    #1

    Writing interrupt driven circular buffer

    Can someone point out source code for a safe circular buffer receiver
    transmitter? It's for sending and receiving bytes via RS232.

    Thanks in advance.

  • Sjouke Burry

    #2
    Re: Writing interrupt driven circular buffer

    Sven wrote:
    Can someone point out source code for a safe circular buffer receiver
    transmitter? It's for sending and receiving bytes via RS232.
    >
    Thanks in advance.
    >

    Does not need an interrupt, does need port access, and you can
    modify it to your harts desire.

    Comment

    • Peter Pichler

      #3
      Re: Writing interrupt driven circular buffer

      Sven wrote:
      Can someone point out source code for a safe circular buffer receiver
      transmitter? It's for sending and receiving bytes via RS232.
      >
      Thanks in advance.
      Sven,

      What you ask for is off-topic on comp.lang.c. Things like interrupts,
      receivers, transmitters and RS232 are platform dependent and hence not
      covered by the ISO/ANSI C Standard. Your best bet is finding a newsgroup
      that discusses your platform.

      It is all covered in the FAQ, which can be found at http://c-faq.com/

      Your question pertains to Qs 19.7 and 19.40b, but I recommend reading
      the whole section 19.

      <off-topic>
      When I was doing something like that back in '96, I found great help in
      a book "Serial Communication" by Peter W Gofton, SYBEX Inc., 1994. It
      covers DOS and Windows (but see the publishing date). I am sure there
      are better and more up-to-date books available now.
      </off-topic>

      Good luck,
      Peter

      Comment

      • CBFalconer

        #4
        Re: Writing interrupt driven circular buffer

        Sven wrote:
        >
        Can someone point out source code for a safe circular buffer
        receiver transmitter? It's for sending and receiving bytes via
        RS232.
        All you need to do is disable interrupts (or their equivalents)
        before altering the counters and flags.

        --
        Chuck F (cbfalconer at maineline dot net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net>


        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • Flash Gordon

          #5
          Re: Writing interrupt driven circular buffer

          Sjouke Burry wrote, On 28/10/07 00:53:
          Sven wrote:
          >Can someone point out source code for a safe circular buffer receiver
          >transmitter? It's for sending and receiving bytes via RS232.
          >>
          >Thanks in advance.
          >

          Does not need an interrupt, does need port access, and you can
          modify it to your harts desire.
          That code is highly DOS specific. Since it has function definitions
          (rather than declarations) in a header file I would also suggest it is
          badly written.

          Sven would be best asking somewhere like comp.arch.embed ded and
          specifying what his target is and rather more about his requirements. I
          know how I've implemented serial communications and it has been
          radically different depending on what the processor was capable of and
          what the requirements were.
          --
          Flash Gordon

          Comment

          • Ark Khasin

            #6
            Re: Writing interrupt driven circular buffer

            CBFalconer wrote:
            Sven wrote:
            >Can someone point out source code for a safe circular buffer
            >receiver transmitter? It's for sending and receiving bytes via
            >RS232.
            >
            All you need to do is disable interrupts (or their equivalents)
            before altering the counters and flags.
            >
            In fact, you don't, if volatile (and of course atomic) read and write
            indexes are modified exclusively by the read routine and the write
            routine respectively. Good encapsulation saves messing with disabling
            interrupts.

            --
            Ark

            Comment

            • CBFalconer

              #7
              Re: Writing interrupt driven circular buffer

              Ark Khasin wrote:
              CBFalconer wrote:
              >Sven wrote:
              >>
              >>Can someone point out source code for a safe circular buffer
              >>receiver transmitter? It's for sending and receiving bytes via
              >>RS232.
              >>
              >All you need to do is disable interrupts (or their equivalents)
              >before altering the counters and flags.
              >
              In fact, you don't, if volatile (and of course atomic) read and
              write indexes are modified exclusively by the read routine and
              the write routine respectively. Good encapsulation saves messing
              with disabling interrupts.
              No it doesn't. Consider, for example, something with multiple
              readers and one transmitter.

              --
              Chuck F (cbfalconer at maineline dot net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net>



              --
              Posted via a free Usenet account from http://www.teranews.com

              Comment

              • Ark Khasin

                #8
                Re: Writing interrupt driven circular buffer

                CBFalconer wrote:
                Ark Khasin wrote:
                >CBFalconer wrote:
                >>Sven wrote:
                >>>
                >>>Can someone point out source code for a safe circular buffer
                >>>receiver transmitter? It's for sending and receiving bytes via
                >>>RS232.
                >>All you need to do is disable interrupts (or their equivalents)
                >>before altering the counters and flags.
                >In fact, you don't, if volatile (and of course atomic) read and
                >write indexes are modified exclusively by the read routine and
                >the write routine respectively. Good encapsulation saves messing
                >with disabling interrupts.
                >
                No it doesn't. Consider, for example, something with multiple
                readers and one transmitter.
                >
                And... what those multiple readers would end up reading?

                Comment

                • CBFalconer

                  #9
                  Re: Writing interrupt driven circular buffer

                  Ark Khasin wrote:
                  CBFalconer wrote:
                  >Ark Khasin wrote:
                  >>CBFalconer wrote:
                  >>>Sven wrote:
                  >>>>
                  >>>>Can someone point out source code for a safe circular buffer
                  >>>>receiver transmitter? It's for sending and receiving bytes via
                  >>>>RS232.
                  >>>>
                  >>>All you need to do is disable interrupts (or their equivalents)
                  >>>before altering the counters and flags.
                  >>>
                  >>In fact, you don't, if volatile (and of course atomic) read and
                  >>write indexes are modified exclusively by the read routine and
                  >>the write routine respectively. Good encapsulation saves messing
                  >>with disabling interrupts.
                  >>
                  >No it doesn't. Consider, for example, something with multiple
                  >readers and one transmitter.
                  >
                  And... what those multiple readers would end up reading?
                  For example, the keyboard. How else can the single keyboard be
                  attached to various processes?

                  --
                  Chuck F (cbfalconer at maineline dot net)
                  Available for consulting/temporary embedded and systems.
                  <http://cbfalconer.home .att.net>



                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • Peter Pichler

                    #10
                    Re: Writing interrupt driven circular buffer

                    CBFalconer wrote:
                    Ark Khasin wrote:
                    >>CBFalconer wrote:
                    >>
                    >>>Consider, for example, something with multiple
                    >>>readers and one transmitter.
                    >>
                    >>And... what those multiple readers would end up reading?
                    >
                    For example, the keyboard. How else can the single keyboard be
                    attached to various processes?
                    This is deeply in the OT-land, but... I doubt the multiple processes
                    read a single keyboard simultaneously. Usually only one process at a
                    time has a keyboard focus. Even if they *do* all receive keystrokes,
                    they do so via some queueing mechanism. They do *not* access the
                    keyboard directly.

                    Comment

                    • Jack Klein

                      #11
                      Re: Writing interrupt driven circular buffer

                      On Sun, 28 Oct 2007 03:11:15 GMT, Ark Khasin
                      <akhasin@macroe xpressions.comw rote in comp.lang.c:
                      CBFalconer wrote:
                      Sven wrote:
                      Can someone point out source code for a safe circular buffer
                      receiver transmitter? It's for sending and receiving bytes via
                      RS232.
                      All you need to do is disable interrupts (or their equivalents)
                      before altering the counters and flags.
                      In fact, you don't, if volatile (and of course atomic) read and write
                      indexes are modified exclusively by the read routine and the write
                      routine respectively. Good encapsulation saves messing with disabling
                      interrupts.
                      Actually, no, that's not always true. I've seen this done wrong many
                      times. In fact, I've had to fix other people's code that got this
                      wrong several times.

                      The biggest vulnerability, and easiest place to code an error, is when
                      an index needs to wrap around.

                      --
                      Jack Klein
                      Home: http://JK-Technology.Com
                      FAQs for
                      comp.lang.c http://c-faq.com/
                      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                      alt.comp.lang.l earn.c-c++

                      Comment

                      • Ark Khasin

                        #12
                        Re: Writing interrupt driven circular buffer

                        Jack Klein wrote:
                        On Sun, 28 Oct 2007 03:11:15 GMT, Ark Khasin
                        <akhasin@macroe xpressions.comw rote in comp.lang.c:
                        >
                        >CBFalconer wrote:
                        >>Sven wrote:
                        >>>Can someone point out source code for a safe circular buffer
                        >>>receiver transmitter? It's for sending and receiving bytes via
                        >>>RS232.
                        >>All you need to do is disable interrupts (or their equivalents)
                        >>before altering the counters and flags.
                        >>>
                        >In fact, you don't, if volatile (and of course atomic) read and write
                        >indexes are modified exclusively by the read routine and the write
                        >routine respectively. Good encapsulation saves messing with disabling
                        >interrupts.
                        >
                        Actually, no, that's not always true. I've seen this done wrong many
                        times. In fact, I've had to fix other people's code that got this
                        wrong several times.
                        >
                        The biggest vulnerability, and easiest place to code an error, is when
                        an index needs to wrap around.
                        >
                        Sure. Below is a sketch of an implementation that doesn't require
                        disabling interrupts, assuming one read agent and one write agent and
                        that unsigned is atomic.
                        Is there a problem with it?

                        //conventions:
                        //empty buffer: writex==readx
                        //full buffer: (writex+1)mod(b uffer size) == readx
                        static volatile unsigned readx, writex; //indexes, init to 0
                        static unsigned char mybuf[MYSIZE];
                        static unsigned inx(unsigned x)
                        {
                        return (x+1)%MYSIZE;
                        }
                        int isempty(void) {unsigned r=readx, w=writex; return r==w;}
                        int isfull(void) {unsigned r=readx, w=writex; return r==inx(w);}
                        int readfrombuf(voi d)
                        {
                        if(isempty()) return BUFFER_EMPTY;
                        unsigned r = readx;
                        int ret =mybuf[r];
                        readx = inx(r);
                        return ret;
                        }
                        int writetobuf(unsi gned char val)
                        {
                        if(isfull()) return BUFFER_FULL;
                        unsigned w = writex;
                        mybuf[w] = val;
                        writex = inx(w);
                        return BUFFER_OK;
                        }

                        --
                        Ark

                        Comment

                        • karthikbalaguru

                          #13
                          Re: Writing interrupt driven circular buffer

                          On Oct 28, 12:07 am, Sven <nos...@nospam. invalidwrote:
                          Can someone point out source code for a safecircularbuf ferreceiver
                          transmitter? It's for sending and receiving bytes via RS232.
                          >
                          But why would you use a circular buffer here ?
                          Any advantage ? What is your requirement here ?

                          Karthik Balaguru

                          Comment

                          Working...