1.优化代码,提高效率。

2.移除对dma的支持(框架轻量化)。
This commit is contained in:
MacRsh
2023-08-27 15:11:34 +08:00
parent 08ed95c8c8
commit 3e66a862b7
2 changed files with 149 additions and 222 deletions

View File

@@ -10,52 +10,50 @@
#include "device/serial/serial.h"
#if (MR_CONF_SERIAL == MR_CONF_ENABLE)
#if (MR_CFG_SERIAL == MR_CFG_ENABLE)
static mr_err_t mr_serial_rx_fifo_init(mr_serial_t serial, mr_size_t rx_bufsz)
static mr_err_t err_io_serial_configure(mr_serial_t serial, struct mr_serial_config *config)
{
if (serial->rx_fifo.fifo.buffer)
{
mr_free(serial->rx_fifo.fifo.buffer);
mr_fifo_init(&serial->rx_fifo.fifo, MR_NULL, 0);
}
serial->rx_fifo.bufsz = rx_bufsz;
if (serial->rx_fifo.bufsz != 0)
{
mr_uint8_t *buffer = mr_malloc(serial->rx_fifo.bufsz);
if (buffer == MR_NULL)
{
serial->rx_fifo.bufsz = 0;
return -MR_ERR_NO_MEMORY;
}
mr_fifo_init(&serial->rx_fifo.fifo, buffer, serial->rx_fifo.bufsz);
}
return MR_ERR_OK;
return -MR_ERR_IO;
}
static mr_err_t mr_serial_tx_fifo_init(mr_serial_t serial, mr_size_t tx_bufsz)
static void err_io_serial_write(mr_serial_t serial, mr_uint8_t data)
{
if (serial->tx_fifo.fifo.buffer)
}
static mr_uint8_t err_io_serial_read(mr_serial_t serial)
{
return 0;
}
static void err_io_serial_start_tx(mr_serial_t serial)
{
}
static void err_io_serial_stop_tx(mr_serial_t serial)
{
}
static mr_err_t mr_serial_set_fifo(mr_rb_t rb, mr_size_t fifo_size)
{
mr_uint8_t *pool = MR_NULL;
/* Free old buffer */
if (rb->size != 0)
{
mr_free(serial->tx_fifo.fifo.buffer);
mr_fifo_init(&serial->tx_fifo.fifo, MR_NULL, 0);
mr_free(rb->buffer);
}
serial->tx_fifo.bufsz = tx_bufsz;
if (serial->tx_fifo.bufsz != 0)
/* Allocate new buffer */
pool = mr_malloc(fifo_size);
if (pool == MR_NULL && fifo_size != 0)
{
mr_uint8_t *buffer = mr_malloc(serial->tx_fifo.bufsz);
if (buffer == MR_NULL)
{
serial->tx_fifo.bufsz = 0;
return -MR_ERR_NO_MEMORY;
}
mr_fifo_init(&serial->tx_fifo.fifo, buffer, serial->tx_fifo.bufsz);
return -MR_ERR_NO_MEMORY;
}
mr_rb_init(rb, pool, fifo_size);
return MR_ERR_OK;
}
@@ -63,24 +61,11 @@ static mr_err_t mr_serial_tx_fifo_init(mr_serial_t serial, mr_size_t tx_bufsz)
static mr_err_t mr_serial_open(mr_device_t device)
{
mr_serial_t serial = (mr_serial_t)device;
mr_err_t ret = MR_ERR_OK;
struct mr_serial_config default_config = MR_SERIAL_CONFIG_DEFAULT;
/* Allocate the fifo buffer */
ret = mr_serial_rx_fifo_init(serial, serial->rx_fifo.bufsz);
if (ret != MR_ERR_OK)
{
return ret;
}
ret = mr_serial_tx_fifo_init(serial, serial->tx_fifo.bufsz);
if (ret != MR_ERR_OK)
{
return ret;
}
/* Setting serial to default config, if baud_rate not set */
/* Enable serial using the default config */
if (serial->config.baud_rate == 0)
{
struct mr_serial_config default_config = MR_SERIAL_CONFIG_DEFAULT;
serial->config = default_config;
}
@@ -90,24 +75,11 @@ static mr_err_t mr_serial_open(mr_device_t device)
static mr_err_t mr_serial_close(mr_device_t device)
{
mr_serial_t serial = (mr_serial_t)device;
mr_err_t ret = MR_ERR_OK;
/* Free the fifo buffer */
ret = mr_serial_rx_fifo_init(serial, 0);
if (ret != MR_ERR_OK)
{
return ret;
}
ret = mr_serial_tx_fifo_init(serial, 0);
if (ret != MR_ERR_OK)
{
return ret;
}
serial->rx_fifo.bufsz = MR_CONF_SERIAL_RX_BUFSZ;
serial->tx_fifo.bufsz = MR_CONF_SERIAL_TX_BUFSZ;
/* Setting serial to close config */
/* Disable serial */
serial->config.baud_rate = 0;
mr_serial_set_fifo(&serial->rx_fifo, 0);
mr_serial_set_fifo(&serial->tx_fifo, 0);
return serial->ops->configure(serial, &serial->config);
}
@@ -117,13 +89,13 @@ static mr_err_t mr_serial_ioctl(mr_device_t device, int cmd, void *args)
mr_serial_t serial = (mr_serial_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CTRL_FLAG_MASK)
switch (cmd & MR_CTRL_FLAG_MASK)
{
case MR_CTRL_SET_CONFIG:
{
if (args)
{
struct mr_serial_config *config = (struct mr_serial_config *)args;
mr_serial_config_t config = (mr_serial_config_t)args;
ret = serial->ops->configure(serial, config);
if (ret == MR_ERR_OK)
{
@@ -138,7 +110,7 @@ static mr_err_t mr_serial_ioctl(mr_device_t device, int cmd, void *args)
{
if (args)
{
struct mr_serial_config *config = (struct mr_serial_config *)args;
mr_serial_config_t config = (mr_serial_config_t)args;
*config = serial->config;
return MR_ERR_OK;
}
@@ -161,7 +133,8 @@ static mr_err_t mr_serial_ioctl(mr_device_t device, int cmd, void *args)
{
if (args)
{
return mr_serial_rx_fifo_init(serial, *((mr_size_t *)args));
mr_size_t bufsz = *((mr_size_t *)args);
return mr_serial_set_fifo(&serial->rx_fifo, bufsz);
}
return -MR_ERR_INVALID;
}
@@ -170,7 +143,8 @@ static mr_err_t mr_serial_ioctl(mr_device_t device, int cmd, void *args)
{
if (args)
{
return mr_serial_tx_fifo_init(serial, *((mr_size_t *)args));
mr_size_t bufsz = *((mr_size_t *)args);
return mr_serial_set_fifo(&serial->tx_fifo, bufsz);
}
return -MR_ERR_INVALID;
}
@@ -186,7 +160,7 @@ static mr_ssize_t mr_serial_read(mr_device_t device, mr_pos_t pos, void *buffer,
mr_uint8_t *read_buffer = (mr_uint8_t *)buffer;
mr_size_t read_size = 0;
if (serial->rx_fifo.bufsz == 0)
if (serial->rx_fifo.size == 0)
{
/* Blocking read */
for (read_size = 0; read_size < size; read_size += sizeof(*read_buffer))
@@ -199,7 +173,7 @@ static mr_ssize_t mr_serial_read(mr_device_t device, mr_pos_t pos, void *buffer,
/* Non-blocking read */
for (read_size = 0; read_size < size;)
{
read_size += mr_fifo_read(&serial->rx_fifo.fifo, read_buffer + read_size, size - read_size);
read_size += mr_rb_read(&serial->rx_fifo, read_buffer + read_size, size - read_size);
}
}
@@ -212,7 +186,7 @@ static mr_ssize_t mr_serial_write(mr_device_t device, mr_pos_t pos, const void *
mr_uint8_t *write_buffer = (mr_uint8_t *)buffer;
mr_size_t write_size = 0;
if (serial->tx_fifo.bufsz == 0 || (device->open_flag & MR_OPEN_NONBLOCKING) == 0)
if (serial->tx_fifo.size == 0 || ((device->open_flag & MR_OPEN_NONBLOCKING) == MR_FALSE))
{
/* Blocking write */
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
@@ -226,23 +200,15 @@ static mr_ssize_t mr_serial_write(mr_device_t device, mr_pos_t pos, const void *
for (write_size = 0; write_size < size;)
{
/* If this is the first write, start sending */
if (mr_fifo_get_data_size(&serial->tx_fifo.fifo) != 0)
if (mr_rb_get_data_size(&serial->tx_fifo) != 0)
{
write_size += mr_fifo_write(&serial->tx_fifo.fifo, write_buffer + write_size, size - write_size);
write_size += mr_rb_write(&serial->tx_fifo, write_buffer + write_size, size - write_size);
} else
{
write_size += mr_fifo_write(&serial->tx_fifo.fifo, write_buffer + write_size, size - write_size);
write_size += mr_rb_write(&serial->tx_fifo, write_buffer + write_size, size - write_size);
if ((device->open_flag & MR_OPEN_DMA) == 0)
{
/* Interrupt write */
serial->ops->start_tx(serial);
} else
{
/* DMA write */
mr_size_t dma_size = mr_fifo_read(&serial->tx_fifo.fifo, serial->tx_dma, sizeof(serial->tx_dma));
serial->ops->start_dma_tx(serial, serial->tx_dma, dma_size);
}
/* Start interrupt send */
serial->ops->start_tx(serial);
}
}
}
@@ -250,99 +216,77 @@ static mr_ssize_t mr_serial_write(mr_device_t device, mr_pos_t pos, const void *
return (mr_ssize_t)write_size;
}
static mr_err_t _err_io_serial_configure(mr_serial_t serial, struct mr_serial_config *config)
/**
* @brief This function adds the serial device.
*
* @param serial The serial device to be added.
* @param name The name of the device.
* @param ops The operations of the device.
* @param data The private data of the device.
*
* @return MR_ERR_OK on success, otherwise an error code.
*/
mr_err_t mr_serial_device_add(mr_serial_t serial, const char *name, struct mr_serial_ops *ops, void *data)
{
MR_ASSERT(0);
return -MR_ERR_IO;
}
static void _err_io_serial_write(mr_serial_t serial, mr_uint8_t data)
{
MR_ASSERT(0);
}
static mr_uint8_t _err_io_serial_read(mr_serial_t serial)
{
MR_ASSERT(0);
return 0;
}
static void _err_io_serial_start_tx(mr_serial_t serial)
{
MR_ASSERT(0);
}
static void _err_io_serial_stop_tx(mr_serial_t serial)
{
MR_ASSERT(0);
}
static void _err_io_serial_start_dma_tx(mr_serial_t serial, mr_uint8_t *buffer, mr_size_t size)
{
MR_ASSERT(0);
}
static void _err_io_serial_stop_dma_tx(mr_serial_t serial)
{
MR_ASSERT(0);
}
mr_err_t mr_serial_device_add(mr_serial_t serial, const char *name, void *data, struct mr_serial_ops *ops)
{
const static struct mr_device_ops device_ops =
{
mr_serial_open,
mr_serial_close,
mr_serial_ioctl,
mr_serial_read,
mr_serial_write,
};
static struct mr_device_ops device_ops =
{
mr_serial_open,
mr_serial_close,
mr_serial_ioctl,
mr_serial_read,
mr_serial_write,
};
mr_uint16_t support_flag = MR_OPEN_RDWR;
MR_ASSERT(serial != MR_NULL);
MR_ASSERT(name != MR_NULL);
MR_ASSERT(ops != MR_NULL);
/* Non-blocking mode */
if (ops->start_tx != MR_NULL && ops->stop_tx != MR_NULL)
{
support_flag |= MR_OPEN_NONBLOCKING;
}
/* Initialize the private fields */
serial->device.type = MR_DEVICE_TYPE_SERIAL;
serial->device.data = data;
serial->device.ops = &device_ops;
serial->config.baud_rate = 0;
serial->rx_fifo.bufsz = MR_CONF_SERIAL_RX_BUFSZ;
mr_fifo_init(&serial->rx_fifo.fifo, MR_NULL, 0);
serial->rx_fifo.bufsz = MR_CONF_SERIAL_TX_BUFSZ;
mr_fifo_init(&serial->tx_fifo.fifo, MR_NULL, 0);
mr_rb_init(&serial->rx_fifo, MR_NULL, 0);
mr_rb_init(&serial->tx_fifo, MR_NULL, 0);
/* Set operations as protection-ops if ops is null */
ops->configure = ops->configure ? ops->configure : _err_io_serial_configure;
ops->write = ops->write ? ops->write : _err_io_serial_write;
ops->read = ops->read ? ops->read : _err_io_serial_read;
ops->start_tx = ops->start_tx ? ops->start_tx : _err_io_serial_start_tx;
ops->stop_tx = ops->stop_tx ? ops->stop_tx : _err_io_serial_stop_tx;
ops->start_dma_tx = ops->start_dma_tx ? ops->start_dma_tx : _err_io_serial_start_dma_tx;
ops->stop_dma_tx = ops->stop_dma_tx ? ops->stop_dma_tx : _err_io_serial_stop_dma_tx;
/* Protect every operation of the serial device */
ops->configure = ops->configure ? ops->configure : err_io_serial_configure;
ops->write = ops->write ? ops->write : err_io_serial_write;
ops->read = ops->read ? ops->read : err_io_serial_read;
ops->start_tx = ops->start_tx ? ops->start_tx : err_io_serial_start_tx;
ops->stop_tx = ops->stop_tx ? ops->stop_tx : err_io_serial_stop_tx;
serial->ops = ops;
/* Add to the container */
return mr_device_add(&serial->device, name, MR_OPEN_RDWR | MR_OPEN_NONBLOCKING);
/* Add the device */
return mr_device_add(&serial->device, name, Mr_Device_Type_Serial, support_flag, &device_ops, data);
}
/**
* @brief This function service interrupt routine of the serial device.
*
* @param serial The serial device.
* @param event The interrupt event.
*/
void mr_serial_device_isr(mr_serial_t serial, mr_uint32_t event)
{
MR_ASSERT(serial != MR_NULL);
switch (event & _MR_SERIAL_EVENT_MASK)
switch (event & MR_SERIAL_EVENT_MASK)
{
case MR_SERIAL_EVENT_RX_INT:
{
/* Read data into the fifo */
mr_uint8_t data = serial->ops->read(serial);
mr_fifo_write_force(&serial->rx_fifo.fifo, &data, sizeof(data));
mr_rb_put_force(&serial->rx_fifo, data);
/* Invoke the rx-cb function */
/* Call the receiving completion function */
if (serial->device.rx_cb != MR_NULL)
{
mr_size_t length = mr_fifo_get_data_size(&serial->rx_fifo.fifo);
mr_size_t length = mr_rb_get_data_size(&serial->rx_fifo);
serial->device.rx_cb(&serial->device, &length);
}
break;
@@ -352,53 +296,15 @@ void mr_serial_device_isr(mr_serial_t serial, mr_uint32_t event)
{
/* Write data from the fifo */
mr_uint8_t data = 0;
if (mr_fifo_read(&serial->tx_fifo.fifo, &data, sizeof(data)) != 0)
if (mr_rb_get(&serial->tx_fifo, &data) == sizeof(data))
{
/* Transfer data */
serial->ops->write(serial, data);
} else
{
/* Stop transmission */
/* Stop interrupt send */
serial->ops->stop_tx(serial);
/* Invoke the tx-cb function */
if (serial->device.tx_cb != MR_NULL)
{
mr_size_t length = 0;
serial->device.tx_cb(&serial->device, &length);
}
}
break;
}
case MR_SERIAL_EVENT_RX_DMA:
{
mr_size_t dma_size = (event >> 16) & MR_UINT16_MAX;
mr_fifo_write_force(&serial->rx_fifo.fifo, serial->rx_dma, dma_size);
/* Invoke the rx-cb function */
if (serial->device.rx_cb != MR_NULL)
{
mr_size_t length = mr_fifo_get_data_size(&serial->rx_fifo.fifo);
serial->device.rx_cb(&serial->device, &length);
}
break;
}
case MR_SERIAL_EVENT_TX_DMA:
{
mr_size_t dma_size = mr_fifo_read(&serial->tx_fifo.fifo, serial->tx_dma, sizeof(serial->tx_dma));
/* Write data from the fifo to the dma */
if (dma_size != 0)
{
/* Transfer data */
serial->ops->start_dma_tx(serial, serial->tx_dma, dma_size);
} else
{
/* Stop transmission */
serial->ops->stop_dma_tx(serial);
/* Invoke the tx-cb function */
/* Call the sending completion function */
if (serial->device.tx_cb != MR_NULL)
{
mr_size_t length = 0;
@@ -413,4 +319,4 @@ void mr_serial_device_isr(mr_serial_t serial, mr_uint32_t event)
}
}
#endif /* MR_CONF_SERIAL */
#endif

View File

@@ -13,32 +13,50 @@
#include "mrlib.h"
#if (MR_CONF_SERIAL == MR_CONF_ENABLE)
#if (MR_CFG_SERIAL == MR_CFG_ENABLE)
/**
* @def Serial device data bits
*/
#define MR_SERIAL_DATA_BITS_8 0
#define MR_SERIAL_DATA_BITS_9 1
/**
* @def Serial device stop bits
*/
#define MR_SERIAL_STOP_BITS_1 0
#define MR_SERIAL_STOP_BITS_1_5 1
#define MR_SERIAL_STOP_BITS_2 2
/**
* @def Serial device parity
*/
#define MR_SERIAL_PARITY_NONE 0
#define MR_SERIAL_PARITY_EVEN 1
#define MR_SERIAL_PARITY_ODD 2
/**
* @def Serial device bit order
*/
#define MR_SERIAL_BIT_ORDER_LSB 0
#define MR_SERIAL_BIT_ORDER_MSB 1
/**
* @def Serial device invert
*/
#define MR_SERIAL_NRZ_NORMAL 0
#define MR_SERIAL_NRZ_INVERTED 1
/**
* @def Serial device interrupt event
*/
#define MR_SERIAL_EVENT_RX_INT 0x10000000
#define MR_SERIAL_EVENT_TX_INT 0x20000000
#define MR_SERIAL_EVENT_RX_DMA 0x40000000
#define MR_SERIAL_EVENT_TX_DMA 0x80000000
#define _MR_SERIAL_EVENT_MASK 0xf0000000
#define MR_SERIAL_EVENT_MASK 0xf0000000
/* Default config for mr_serial_config structure */
/**
* @def Serial device default config
*/
#define MR_SERIAL_CONFIG_DEFAULT \
{ \
115200, \
@@ -49,6 +67,9 @@
MR_SERIAL_NRZ_NORMAL, \
}
/**
* @struct Serial device config
*/
struct mr_serial_config
{
mr_uint32_t baud_rate;
@@ -58,46 +79,46 @@ struct mr_serial_config
mr_uint8_t bit_order: 1;
mr_uint8_t invert: 1;
};
struct mr_serial_fifo
{
mr_size_t bufsz;
struct mr_fifo fifo;
};
typedef struct mr_serial_config *mr_serial_config_t;
typedef struct mr_serial *mr_serial_t;
/**
* @struct Serial device operations
*/
struct mr_serial_ops
{
mr_err_t (*configure)(mr_serial_t serial, struct mr_serial_config *config);
mr_err_t (*configure)(mr_serial_t serial, mr_serial_config_t config);
void (*write)(mr_serial_t serial, mr_uint8_t data);
mr_uint8_t (*read)(mr_serial_t serial);
/* Interrupt */
/* Interrupt send operations */
void (*start_tx)(mr_serial_t serial);
void (*stop_tx)(mr_serial_t serial);
/* DMA */
void (*start_dma_tx)(mr_serial_t serial, mr_uint8_t *buffer, mr_size_t size);
void (*stop_dma_tx)(mr_serial_t serial);
};
/**
* @struct Serial device
*/
struct mr_serial
{
struct mr_device device;
struct mr_serial_config config;
struct mr_serial_fifo rx_fifo;
struct mr_serial_fifo tx_fifo;
mr_uint8_t rx_dma[MR_CONF_SERIAL_RX_DMA_BUFS];
mr_uint8_t tx_dma[MR_CONF_SERIAL_TX_DMA_BUFS];
struct mr_rb rx_fifo;
struct mr_rb tx_fifo;
const struct mr_serial_ops *ops;
};
mr_err_t mr_serial_device_add(mr_serial_t serial, const char *name, void *data, struct mr_serial_ops *ops);
/**
* @addtogroup Serial device
* @{
*/
mr_err_t mr_serial_device_add(mr_serial_t serial, const char *name, struct mr_serial_ops *ops, void *data);
void mr_serial_device_isr(mr_serial_t serial, mr_uint32_t event);
/** @} */
#endif /* MR_CONF_SERIAL */
#endif
#endif /* _SERIAL_H_ */
#endif /* _SERIAL_H_ */