Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array)};

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
if ((poll_flags & MP_STREAM_POLL_RD) && !ringbuf_is_empty(&stdin_ringbuf)) {
ret |= MP_STREAM_POLL_RD;
}
return ret;
Expand Down
2 changes: 1 addition & 1 deletion ports/esp8266/esp_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void mp_hal_delay_us(uint32_t us) {

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
if ((poll_flags & MP_STREAM_POLL_RD) && !ringbuf_is_empty(&stdin_ringbuf)) {
ret |= MP_STREAM_POLL_RD;
}
return ret;
Expand Down
3 changes: 2 additions & 1 deletion ports/esp8266/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ets_sys.h"
#include "uart.h"

#include "py/ringbuf.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "py/mperrno.h"
Expand Down Expand Up @@ -147,7 +148,7 @@ STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_o

// set rx ring buffer
if (args[ARG_rxbuf].u_int > 0) {
uint16_t len = args[ARG_rxbuf].u_int + 1; // account for usable items in ringbuf
uint16_t len = ringbuf_fix_len(args[ARG_rxbuf].u_int);
byte *buf;
if (len <= UART0_STATIC_RXBUF_LEN) {
buf = uart_ringbuf_array;
Expand Down
4 changes: 2 additions & 2 deletions ports/esp8266/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static void uart0_rx_intr_handler(void *para) {
bool uart_rx_wait(uint32_t timeout_us) {
uint32_t start = system_get_time();
for (;;) {
if (uart_ringbuf.iget != uart_ringbuf.iput) {
if (!ringbuf_is_empty(&uart_ringbuf)) {
return true; // have at least 1 char ready for reading
}
if (system_get_time() - start >= timeout_us) {
Expand All @@ -214,7 +214,7 @@ bool uart_rx_wait(uint32_t timeout_us) {
}

int uart_rx_any(uint8 uart) {
if (uart_ringbuf.iget != uart_ringbuf.iput) {
if (!ringbuf_is_empty(&uart_ringbuf)) {
return true; // have at least 1 char ready for reading
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/modules/machine/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
typedef struct _machine_hard_uart_buf_t {
uint8_t tx_buf[1];
uint8_t rx_buf[1];
uint8_t rx_ringbuf_array[64];
uint8_t rx_ringbuf_array[64]; // Must be divisible into 2^16, see py/ringbuf.h.
volatile ringbuf_t rx_ringbuf;
} machine_hard_uart_buf_t;

Expand Down Expand Up @@ -99,7 +99,7 @@ STATIC void uart_event_handler(nrfx_uart_event_t const *p_event, void *p_context
}

bool uart_rx_any(const machine_hard_uart_obj_t *self) {
return self->buf->rx_ringbuf.iput != self->buf->rx_ringbuf.iget;
return !ringbuf_is_empty((ringbuf_t*)&self->buf->rx_ringbuf);
}

int uart_rx_char(const machine_hard_uart_obj_t * self) {
Expand Down
5 changes: 3 additions & 2 deletions py/py.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ CSUPEROPT = -O3

# External modules written in C.
ifneq ($(USER_C_MODULES),)
# pre-define USERMOD variables as expanded so that variables are immediate
# pre-define USERMOD variables as expanded so that variables are immediate
# expanded as they're added to them
SRC_USERMOD :=
SRC_USERMOD :=
CFLAGS_USERMOD :=
LDFLAGS_USERMOD :=
$(foreach module, $(wildcard $(USER_C_MODULES)/*/micropython.mk), \
Expand Down Expand Up @@ -86,6 +86,7 @@ PY_CORE_O_BASENAME = $(addprefix py/,\
stackctrl.o \
argcheck.o \
warning.o \
ringbuf.o \
map.o \
obj.o \
objarray.o \
Expand Down
56 changes: 56 additions & 0 deletions py/ringbuf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Paul Sokolovsky
* Copyright (c) 2019 Jim Mussared
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "py/ringbuf.h"

uint16_t ringbuf_fix_len(uint16_t len) {
if (len == 0) {
return 1;
}
len--;
len |= len >> 1;
len |= len >> 2;
len |= len >> 4;
len |= len >> 8;
len++;
return len ? len : 32768;
}

int ringbuf_get(ringbuf_t *r) {
if (ringbuf_is_empty(r)) {
return -1;
}
return r->buf[r->iget++ % r->size];
}

int ringbuf_put(ringbuf_t *r, uint8_t v) {
if (r->iput - r->iget == r->size) {
return -1;
}
r->buf[r->iput++ % r->size] = v;
return 0;
}
52 changes: 27 additions & 25 deletions py/ringbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Paul Sokolovsky
* Copyright (c) 2019 Jim Mussared
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,6 +27,17 @@
#ifndef MICROPY_INCLUDED_PY_RINGBUF_H
#define MICROPY_INCLUDED_PY_RINGBUF_H

#include "py/mpconfig.h"
#include "py/misc.h"

// In order to avoid wasting a byte in the ringbuffer, and to reduce code
// size, the two indicies (iget & iput) do not wrap around at size. Instead
// they rely on unsigned integer overflow.
// This means that the empty state (iget == iput) can be distinguished from
// the full state (iput == iget + size).
// However, this means that the size must divide evenly into 2^16. Most uses
// of the ringbuffer use small power-of-two sizes anyway.

typedef struct _ringbuf_t {
uint8_t *buf;
uint16_t size;
Expand All @@ -34,39 +46,29 @@ typedef struct _ringbuf_t {
} ringbuf_t;

// Static initialization:
// byte buf_array[N];
// byte buf_array[N]; // N must be divisible into 2^16.
// ringbuf_t buf = {buf_array, sizeof(buf_array)};

// Dynamic initialization. This creates root pointer!
// Dynamic initialization.
#define ringbuf_alloc(r, sz) \
{ \
(r)->buf = m_new(uint8_t, sz); \
(r)->size = sz; \
(r)->size = ringbuf_fix_len(sz); \
(r)->buf = m_new(uint8_t, (r)->size); \
(r)->iget = (r)->iput = 0; \
}

static inline int ringbuf_get(ringbuf_t *r) {
if (r->iget == r->iput) {
return -1;
}
uint8_t v = r->buf[r->iget++];
if (r->iget >= r->size) {
r->iget = 0;
}
return v;
}
// Rounds len to the nearest power of two (to ensure divisibility into 2^16).
// Use this if dynamically allocating buf.
uint16_t ringbuf_fix_len(uint16_t len);

static inline int ringbuf_put(ringbuf_t *r, uint8_t v) {
uint32_t iput_new = r->iput + 1;
if (iput_new >= r->size) {
iput_new = 0;
}
if (iput_new == r->iget) {
return -1;
}
r->buf[r->iput] = v;
r->iput = iput_new;
return 0;
static inline bool ringbuf_is_empty(ringbuf_t *r) {
return r->iput == r->iget;
}

// Returns the next byte, or -1 if empty.
int ringbuf_get(ringbuf_t *r);

// Returns -1 if full, 0 otherwise.
int ringbuf_put(ringbuf_t *r, uint8_t v);

#endif // MICROPY_INCLUDED_PY_RINGBUF_H