Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MicroCART_17-18
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bbartels
MicroCART_17-18
Commits
d38a52f0
Commit
d38a52f0
authored
8 years ago
by
David Wehr
Browse files
Options
Downloads
Patches
Plain Diff
Added circular buffer for async UART
parent
0e9a206a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
quad/sw/comm_dev/src/circ_buffer.c
+76
-0
76 additions, 0 deletions
quad/sw/comm_dev/src/circ_buffer.c
quad/sw/comm_dev/src/circ_buffer.h
+42
-0
42 additions, 0 deletions
quad/sw/comm_dev/src/circ_buffer.h
with
118 additions
and
0 deletions
quad/sw/comm_dev/src/circ_buffer.c
0 → 100644
+
76
−
0
View file @
d38a52f0
//
// Created by dawehr on 10/24/2016.
//
#include
"circ_buffer.h"
#define circ_buf_min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
unsigned
char
buffer
[
CIRC_BUFFER_SIZE
];
size_t
buf_start
=
0
;
size_t
buf_end
=
0
;
size_t
size
=
0
;
struct
data_chunk
getChunk
()
{
size_t
chunk_end
=
buf_end
;
if
(
buf_end
<
buf_start
)
{
chunk_end
=
CIRC_BUFFER_SIZE
;
}
size_t
ret_size
=
chunk_end
-
buf_start
;
struct
data_chunk
c
=
{
ret_size
,
&
buffer
[
buf_start
]
};
return
c
;
}
void
markConsumed
(
size_t
n_consumed
)
{
size_t
chunk_end
=
buf_start
+
n_consumed
;
buf_start
=
chunk_end
;
size
-=
n_consumed
;
if
(
buf_start
==
CIRC_BUFFER_SIZE
)
{
buf_start
=
0
;
}
}
size_t
putChunk
(
struct
data_chunk
c
)
{
// The amount that will be placed into the buffer
size_t
will_place
=
circ_buf_min
(
CIRC_BUFFER_SIZE
-
size
,
c
.
length
);
// Actual amount placed so far
size_t
placed
=
0
;
while
(
placed
<
will_place
)
{
// Available room without wrapping
size_t
to_copy
;
if
(
buf_end
>=
buf_start
)
{
// If buffer is not wrapped, we can copy until the end of the buffer
to_copy
=
circ_buf_min
(
will_place
,
CIRC_BUFFER_SIZE
-
buf_end
);
}
else
{
// Otherwise, remaining space in buffer is contiguous
to_copy
=
will_place
-
placed
;
}
memcpy
(
buffer
+
buf_end
,
c
.
data
+
placed
,
to_copy
);
// Update buffer endpoint
buf_end
+=
to_copy
;
if
(
buf_end
>
CIRC_BUFFER_SIZE
)
{
printf
(
"Error: buf_end: %d
\n
"
,
buf_end
);
}
// If we copied to the end
if
(
buf_end
==
CIRC_BUFFER_SIZE
)
{
buf_end
=
0
;
}
placed
+=
to_copy
;
}
size
+=
placed
;
return
placed
;
}
size_t
get_buffer_size
()
{
return
size
;
}
This diff is collapsed.
Click to expand it.
quad/sw/comm_dev/src/circ_buffer.h
0 → 100644
+
42
−
0
View file @
d38a52f0
//
// Created by dawehr on 10/24/2016.
//
#ifndef CIRC_BUFFER_H
#define CIRC_BUFFER_H
#define CIRC_BUFFER_SIZE 2048
#include
<unistd.h>
#include
<memory.h>
#include
<stdio.h>
struct
data_chunk
{
size_t
length
;
unsigned
char
*
data
;
};
/*
* Returns the largest contiguous chunk from the buffer
* Does not move the buffer forward. You must call markConsumed to
* mark the area as free.
*/
struct
data_chunk
getChunk
();
/*
* Marks the n_consumed bytes as used, and that
* area of the buffer can be used for new data now
*/
void
markConsumed
(
size_t
n_consumed
);
/*
* Places data into the circular buffer. Returns the number of bytes stored.
* Will store the entire chunk, unless there is not enough remaining size in the buffer
*/
size_t
putChunk
(
struct
data_chunk
);
/*
* Returns the remaining size in the buffer
*/
size_t
get_buffer_size
();
#endif //CIRC_BUFFER_H
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment