Skip to content

Commit

Permalink
Fix circular buffer to overflow properly (#1163)
Browse files Browse the repository at this point in the history
* Bug fix, the instance comparision was wrong

* Update circular buffer to take into account the number of channels being sampled

* Update cb_init function, remove floating operations from the microblaze. Fix the computation of the log_capacity

* Fix variable name

* Add head overflow in the circular buffer structure to allow reseting it everytime it's initialized

* Remove overflow bit

* Enhance class to support a single channel

* Update changelog

* Modify MAX_SAMPLES to be multiple of 1, 2 and 3 channels

* Fix comments

* Include 'raw' APIs calling for backwards compatibility
  • Loading branch information
mariodruiz committed Oct 19, 2020
1 parent 8e10b3b commit 3fe99d4
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 262 deletions.
8 changes: 6 additions & 2 deletions boards/Pynq-Z1/base/notebooks/arduino/arduino_analog.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
{
"data": {
"text/plain": [
"[0.6403, 0.6848]"
"array([0.6403, 0.6848])"
]
},
"execution_count": 3,
Expand Down Expand Up @@ -106,7 +106,7 @@
}
],
"source": [
"analog1.read_raw()[0]"
"analog1.read('raw')[0]"
]
},
{
Expand Down Expand Up @@ -196,6 +196,8 @@
" 'bs', label=\"Y-axis of joystick\")\n",
"plt.title('Arduino Analog Voltage Log')\n",
"plt.axis([0, len(log1[0]), 0.0, 3.3])\n",
"plt.xlabel('Sample number')\n",
"plt.ylabel('Voltage')\n",
"plt.legend(loc=4,bbox_to_anchor=(1, -0.3),\n",
" ncol=2, borderaxespad=0.,\n",
" handler_map={line1: HandlerLine2D(numpoints=1),\n",
Expand Down Expand Up @@ -294,6 +296,8 @@
" 'g^', label=\"potentiometer\")\n",
"plt.title('Arduino Analog Voltage Log')\n",
"plt.axis([0, len(log2[0]), 0.0, 3.3])\n",
"plt.xlabel('Sample number')\n",
"plt.ylabel('Voltage')\n",
"plt.legend(loc=4,bbox_to_anchor=(1, -0.3),\n",
" ncol=2, borderaxespad=0.,\n",
" handler_map={line1: HandlerLine2D(numpoints=1),\n",
Expand Down
9 changes: 6 additions & 3 deletions boards/sw_repo/pynqmb/src/circular_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
* 1.00 yrq 01/09/18 release
* 1.01 mrn 09/28/20 Bug fix, the head of the circular
* buffer did not overflow
* 1.02 mrn 10/11/20 Update initialize function. Bug fix: move the head
* according to the number of channels.
*
* </pre>
*
Expand All @@ -54,13 +56,14 @@

/************************** Function Definitions ***************************/
int cb_init(circular_buffer *cb, volatile u32* log_start_addr,
size_t capacity, size_t sz){
size_t capacity, size_t sz, size_t channels){
cb->buffer = (volatile char*) log_start_addr;
if(cb->buffer == NULL)
return -1;
cb->buffer_end = (char *)cb->buffer + capacity * sz;
cb->capacity = capacity;
cb->sz = sz;
cb->channels = channels;
cb->head = cb->buffer;
cb->tail = cb->buffer;

Expand Down Expand Up @@ -106,11 +109,11 @@ void cb_push_incr_ptrs(circular_buffer *cb){
cb->tail = cb->buffer;

if (cb->tail == cb->head) {
cb->head = (char*)cb->head + cb->sz;
cb->head = (char*)cb->head + cb->sz * cb->channels;
// Move the head pointer to buffer start
if (cb->head >= cb->buffer_end)
cb->head = cb->buffer;
}

// update mailbox head and tail
MAILBOX_DATA(2) = (u32) cb->head;
MAILBOX_DATA(3) = (u32) cb->tail;
Expand Down
5 changes: 3 additions & 2 deletions boards/sw_repo/pynqmb/src/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* Ver Who Date Changes
* ----- --- ------- -----------------------------------------------
* 1.00 yrq 01/09/18 release
* 1.01 mrn 10/11/18 Include channels in the init function.
*
* </pre>
*
Expand Down Expand Up @@ -72,7 +73,7 @@ typedef struct circular_buffer
volatile void *buffer; // data buffer
void *buffer_end; // end of data buffer
size_t capacity; // maximum number of items in the buffer
size_t count; // number of items in the buffer
size_t channels; // number of channels in the buffer
size_t sz; // size of each item in the buffer
volatile void *head; // pointer to head
volatile void *tail; // pointer to tail
Expand All @@ -81,7 +82,7 @@ typedef struct circular_buffer
circular_buffer circular_log;

int cb_init(circular_buffer *cb, volatile u32* log_start_addr,
size_t capacity, size_t sz);
size_t capacity, size_t sz, size_t channels);
void cb_push_back(circular_buffer *cb, const void *item);
void cb_push_back_float(circular_buffer *cb, const float *item);
void cb_push_incr_ptrs(circular_buffer *cb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,8 @@ Remaining code:
case READ_AND_LOG:
// initialize logging variables, reset cmd
cb_init(&circular_log, LOG_BASE_ADDRESS, LOG_CAPACITY, LOG_ITEM_SIZE);
cb_init(&circular_log,
LOG_BASE_ADDRESS, LOG_CAPACITY, LOG_ITEM_SIZE, 1);
delay = MAILBOX_DATA(1);
MAILBOX_CMD_ADDR = 0x0;
Expand Down
Loading

0 comments on commit 3fe99d4

Please sign in to comment.