<commit_before>#include "ucos_ii.h"
#include "sio_cpu.h"

#include "lwip/sys.h"
#include "lwip/sio.h"

#define __SIO_BUF_SIZE 64

struct __sio_buf {
	INT8U	buf[__SIO_BUF_SIZE];
	INT8U	rd;
	INT8U	wr;
	INT8U	len;
};

struct {
	struct {
		struct __sio_buf	buf;
		OS_EVENT		*sem;
	} rx, tx;
} __sio;

#define __sio_buf_empty(buf) ((buf)->len == 0)
#define __sio_buf_full(buf) ((buf)->len == __SIO_BUF_SIZE)

static void __sio_init_buf(struct __sio_buf *buf)
{
	buf->rd = 0;
	buf->wr = 0;
	buf->len = 0;
}

static INT8U __sio_read_buf(struct __sio_buf *buf)
{
	INT8U c = buf->buf[buf->rd++];

	if (buf->rd == __SIO_BUF_SIZE)
		buf->rd = 0;
	buf->len--;

	return c;
}

static void __sio_write_buf(struct __sio_buf *buf, INT8U c)
{
	buf->buf[buf->wr++] = c;
	if (buf->wr == __SIO_BUF_SIZE)
		buf->wr = 0;
	buf->len++;
}

/**
 * Opens a serial device for communication.
 * 
 * @param devnum device number
 * @return handle to serial device if successful, NULL otherwise
 */
sio_fd_t sio_open(u8_t devnum)
{
	__sio.rx.sem = OSSemCreate(__SIO_BUF_SIZE); /* number of bytes */
	LWIP_ASSERT("OSSemCreate", __sio.rx.sem);
	__sio.tx.sem = OSSemCreate(__SIO_BUF_SIZE + 1); /* number of spaces */
	LWIP_ASSERT("OSSemCreate", __sio.tx.sem);
	__sio_init_buf(&__sio.rx.buf);
	__sio_init_buf(&__sio.tx.buf);

	return &__sio;
}

/**
 * Sends a single character to the serial device.
 * 
 * @param c character to send
 * @param fd serial device handle
 * 
 * @note This function will block until the character can be sent.
 */
void sio_send(u8_t c, sio_fd_t fd)
{
	INT8U err;
	SYS_ARCH_DECL_PROTECT(sr);
<commit_msg>The number of bytes in the RX buffer should be 0<commit_after>#include "ucos_ii.h"
#include "sio_cpu.h"

#include "lwip/sys.h"
#include "lwip/sio.h"

#define __SIO_BUF_SIZE 64

struct __sio_buf {
	INT8U	buf[__SIO_BUF_SIZE];
	INT8U	rd;
	INT8U	wr;
	INT8U	len;
};

struct {
	struct {
		struct __sio_buf	buf;
		OS_EVENT		*sem;
	} rx, tx;
} __sio;

#define __sio_buf_empty(buf) ((buf)->len == 0)
#define __sio_buf_full(buf) ((buf)->len == __SIO_BUF_SIZE)

static void __sio_init_buf(struct __sio_buf *buf)
{
	buf->rd = 0;
	buf->wr = 0;
	buf->len = 0;
}

static INT8U __sio_read_buf(struct __sio_buf *buf)
{
	INT8U c = buf->buf[buf->rd++];

	if (buf->rd == __SIO_BUF_SIZE)
		buf->rd = 0;
	buf->len--;

	return c;
}

static void __sio_write_buf(struct __sio_buf *buf, INT8U c)
{
	buf->buf[buf->wr++] = c;
	if (buf->wr == __SIO_BUF_SIZE)
		buf->wr = 0;
	buf->len++;
}

/**
 * Opens a serial device for communication.
 * 
 * @param devnum device number
 * @return handle to serial device if successful, NULL otherwise
 */
sio_fd_t sio_open(u8_t devnum)
{
	__sio.rx.sem = OSSemCreate(0); /* number of bytes */
	LWIP_ASSERT("OSSemCreate", __sio.rx.sem);
	__sio.tx.sem = OSSemCreate(__SIO_BUF_SIZE + 1); /* number of spaces */
	LWIP_ASSERT("OSSemCreate", __sio.tx.sem);
	__sio_init_buf(&__sio.rx.buf);
	__sio_init_buf(&__sio.tx.buf);

	return &__sio;
}

/**
 * Sends a single character to the serial device.
 * 
 * @param c character to send
 * @param fd serial device handle
 * 
 * @note This function will block until the character can be sent.
 */
void sio_send(u8_t c, sio_fd_t fd)
{
	INT8U err;
	SYS_ARCH_DECL_PROTECT(sr);

{'fraction_non_alphanumeric': 0.14579439252336449, 'fraction_numerical': 0.012461059190031152, 'mean_word_length': 4.025039123630673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799111', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1409, 'n_words': 328}