avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] generic queue library for AVR GCC?


From: gouy yann
Subject: Re: [avr-gcc-list] generic queue library for AVR GCC?
Date: Thu, 11 Nov 2004 20:32:50 +0100 (CET)

Hi David,

here is my implementation of a fifo.
I hope this will help you.

fifo.h:

//---------------------
//  Copyright (C) 2000-2004  <Yann GOUY>
//
//  This program is free software; you can
redistribute it and/or modify
//  it under the terms of the GNU General Public
License as published by
//  the Free Software Foundation; either version 2 of
the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it
will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied
warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General
Public License
//  along with this program; see the file COPYING.  If
not, write to
//  the Free Software Foundation, Inc., 59 Temple
Place - Suite 330,
//  Boston, MA 02111-1307, USA.
//
//  you can write to me at <address@hidden>
//


#ifndef __FIFO_H__
# define __FIFO_H__

# include "type_def.h"

typedef struct {
        u16 lng;      // data buffer length
        u16 nb;       // element number
        u8* donnees;  // pointer on the data buffer
        u8* in;       // insertion pointer
        u8* out;      // extraction pointer
} fifo_t;

extern void FIFO_init(fifo_t*, u16, u8*);    // init
the FIFO with the length and a pointer to the data
buffer
extern u8 FIFO_put(fifo_t*, u8);    // add a char
extern u8 FIFO_get(fifo_t*, u8*);   // get a char
// those 3 functions above return OK if every thing ok
else KO

extern u16 FIFO_free(fifo_t*);      // returns the
free place in the fifo
extern u16 FIFO_full(fifo_t*);      // returns the
place taken in the fifo

#endif

type_def.h:

//---------------------
//  Copyright (C) 2000-2004  <Yann GOUY>
//
//  This program is free software; you can
redistribute it and/or modify
//  it under the terms of the GNU General Public
License as published by
//  the Free Software Foundation; either version 2 of
the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it
will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied
warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General
Public License
//  along with this program; see the file COPYING.  If
not, write to
//  the Free Software Foundation, Inc., 59 Temple
Place - Suite 330,
//  Boston, MA 02111-1307, USA.
//
//  you can write to me at <address@hidden>
//

#ifndef __typ_def_h__
# define __typ_def_h__

#include <inttypes.h>

// redefinition of types
typedef uint8_t  u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef int8_t   s8;
typedef int16_t  s16;
typedef int32_t  s32;

// some pratical values
# define KO     ((u8)0)
# define OK     ((u8)1)

# define NULL   0

#endif


fifo.c:

//---------------------
//  Copyright (C) 2000-2004  <Yann GOUY>
//
//  This program is free software; you can
redistribute it and/or modify
//  it under the terms of the GNU General Public
License as published by
//  the Free Software Foundation; either version 2 of
the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it
will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied
warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General
Public License
//  along with this program; see the file COPYING.  If
not, write to
//  the Free Software Foundation, Inc., 59 Temple
Place - Suite 330,
//  Boston, MA 02111-1307, USA.
//
//  you can write to me at <address@hidden>
//

#include "fifo.h"


void FIFO_init(fifo_t *f, u16 lng, u8* adr)
{
        // set the internals thanks to the provided datas
        f->lng = lng;
        f->nb = 0;
        f->out = f->in = f->donnees = adr;
}

u8 FIFO_put(fifo_t *f, u8 c)
{
        // if there's at least a free place
        if (f->nb < f->lng) {
                // add the new element
                *f->in = c;
                f->in++;
                // loop back at the end
                if (f->in >= f->donnees + f->lng)
                        f->in = f->donnees;
                f->nb++;
                return OK;
        } else {
                return KO;
        }
}

u8 FIFO_get(fifo_t *f, u8 *c)
{
        // if there's no element, quit
        if (f->nb == 0)
                return KO;

        // get the element
        *c = *(f->out);
        f->nb--;

        // set the extraction pointer to the next position
        f->out++;
        if (f->out >= f->donnees+f->lng)
                f->out = f->donnees;

        return OK;
}

u16 FIFO_free(fifo_t* f)
{
        return (f->lng - f->nb);
}

u16 FIFO_full(fifo_t* f)
{
        return f->nb;
}



 --- David Morrison <address@hidden> a
écrit : 
> I know this is a long shot but I am developing an
> interrupt driven CAN controller and was wondering if
> anyone knows of a generic queue library for use with
> AVR GCC?  For some reason I am having major issues
> with the one that I wrote and am ready to chuck it
> in place of something better.
>  
> Thanks,
> dave
> 
> 
> I have always wished that my computer would be as
> easy to use as my telephone. My wish has come true.
> I no longer know how to use my telephone. 
> -Bjarne Stroustrup, computer science professor,
> designer of C++ 
> programming language (1950- )
> 
> 
> _______________________________________________
> avr-gcc-list mailing list
> address@hidden
> http://www.avr1.org/mailman/listinfo/avr-gcc-list
>  

=====



        

        
                
Vous manquez d’espace pour stocker vos mails ? 
Yahoo! Mail vous offre GRATUITEMENT 100 Mo !
Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/

Le nouveau Yahoo! Messenger est arrivé ! Découvrez toutes les nouveautés pour 
dialoguer instantanément avec vos amis. A télécharger gratuitement sur 
http://fr.messenger.yahoo.com


reply via email to

[Prev in Thread] Current Thread [Next in Thread]