comparison network/Lib/RingBuff.h @ 110:4eb5a746d7af avr-http

Import avrusbmodem code minus the USB bits. Not built yet.
author Matt Johnston <matt@ucc.asn.au>
date Sat, 15 Sep 2012 21:49:05 +0800
parents
children
comparison
equal deleted inserted replaced
109:315850d48eec 110:4eb5a746d7af
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /* Buffer Configuration: */
32 /* Buffer length - select static size of created ring buffers: */
33 #define BUFF_STATICSIZE 128 // Set to the static ring buffer size for all ring buffers (place size after define)
34
35 /* Volatile mode - uncomment to make buffers volatile, for use in ISRs, etc: */
36 // #define BUFF_VOLATILE // Uncomment to cause all ring buffers to become volatile (and atomic if multi-byte) in access
37
38 /* Drop mode - select behaviour when Buffer_StoreElement called on a full buffer: */
39 #define BUFF_DROPOLD // Uncomment to cause full ring buffers to drop the oldest character to make space when full
40 // #define BUFF_DROPNEW // Uncomment to cause full ring buffers to drop the new character when full
41 // #define BUFF_NODROPCHECK // Uncomment to ignore full ring buffer checks - checking left to user!
42
43 /* Underflow behaviour - select behaviour when Buffer_GetElement is called with an empty ring buffer: */
44 //#define BUFF_EMPTYRETURNSZERO // Uncomment to return 0 when an empty ring buffer is read
45 #define BUFF_NOEMPTYCHECK // Uncomment to disable checking of empty ring buffers - checking left to user!
46
47 /* Buffer storage type - set the datatype for the stored data */
48 #define BUFF_DATATYPE uint8_t // Change to the data type that is going to be stored into the buffer
49
50 /* Peek routine - uncomment to include the peek routine (fetches next byte without removing it from the buffer */
51 //#define BUFF_USEPEEK
52
53 #ifndef _RINGBUFF_H_
54 #define _RINGBUFF_H_
55
56 /* Includes: */
57 #include <avr/io.h>
58 #include <avr/interrupt.h>
59 #include <util/atomic.h>
60 #include <limits.h>
61
62 #include <LUFA/Common/Common.h>
63
64 /* Defines and checks: */
65 #if defined(BUFF_STATICSIZE)
66 #define BUFF_LENGTH BUFF_STATICSIZE
67 #else
68 #error No buffer length specified!
69 #endif
70
71 #if !(defined(BUFF_DROPOLD) || defined(BUFF_DROPNEW) || defined(BUFF_NODROPCHECK))
72 #error No buffer drop mode specified.
73 #endif
74
75 #if !defined(BUFF_DATATYPE)
76 #error Ringbuffer storage data type not specified.
77 #endif
78
79 #if defined(BUFF_VOLATILE)
80 #define BUFF_MODE volatile
81 #define BUFF_ATOMIC_BLOCK ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
82 #else
83 #define BUFF_MODE
84 #define BUFF_ATOMIC_BLOCK
85 #endif
86
87 #if (BUFF_STATICSIZE > LONG_MAX)
88 #define RingBuff_Elements_t uint64_t
89 #elif (BUFF_STATICSIZE > INT_MAX)
90 #define RingBuff_Elements_t uint32_t
91 #elif (BUFF_STATICSIZE > CHAR_MAX)
92 #define RingBuff_Elements_t uint16_t
93 #else
94 #define RingBuff_Elements_t uint8_t
95 #endif
96
97 /* Type Defines: */
98 typedef BUFF_DATATYPE RingBuff_Data_t;
99
100 typedef BUFF_MODE struct
101 {
102 RingBuff_Data_t Buffer[BUFF_LENGTH];
103 RingBuff_Data_t* InPtr;
104 RingBuff_Data_t* OutPtr;
105 RingBuff_Elements_t Elements;
106 } RingBuff_t;
107
108 /* Function Prototypes: */
109 void Buffer_Initialize(RingBuff_t* const Buff);
110 void Buffer_StoreElement(RingBuff_t* const Buffer,
111 RingBuff_Data_t Data);
112 RingBuff_Data_t Buffer_GetElement(RingBuff_t* const Buffer);
113 #if defined(BUFF_USEPEEK)
114 RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* const Buffer);
115 #endif
116
117 #endif