Files
mr-library/include/device/serial.h
2023-11-25 00:28:56 +08:00

121 lines
4.0 KiB
C

/*
* @copyright (c) 2023, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-10-20 MacRsh First version
*/
#ifndef _MR_SERIAL_H_
#define _MR_SERIAL_H_
#include "mr_api.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef MR_USING_SERIAL
/**
* @brief SERIAL data bits.
*/
#define MR_SERIAL_DATA_BITS_5 (5) /**< 5 bits data */
#define MR_SERIAL_DATA_BITS_6 (6) /**< 6 bits data */
#define MR_SERIAL_DATA_BITS_7 (7) /**< 7 bits data */
#define MR_SERIAL_DATA_BITS_8 (8) /**< 8 bits data */
/**
* @brief SERIAL stop bits.
*/
#define MR_SERIAL_STOP_BITS_1 (1) /**< 1 bit stop */
#define MR_SERIAL_STOP_BITS_2 (2) /**< 2 bit stop */
#define MR_SERIAL_STOP_BITS_3 (3) /**< 3 bit stop */
#define MR_SERIAL_STOP_BITS_4 (4) /**< 4 bit stop */
/**
* @brief SERIAL parity.
*/
#define MR_SERIAL_PARITY_NONE (0) /**< No parity */
#define MR_SERIAL_PARITY_EVEN (1) /**< Even parity */
#define MR_SERIAL_PARITY_ODD (2) /**< Odd parity */
/**
* @brief SERIAL bit order.
*/
#define MR_SERIAL_BIT_ORDER_LSB (0) /**< LSB first */
#define MR_SERIAL_BIT_ORDER_MSB (1) /**< MSB first */
/**
* @brief SERIAL polarity.
*/
#define MR_SERIAL_NRZ_NORMAL (0) /**< Normal polarity */
#define MR_SERIAL_NRZ_INVERTED (1) /**< Inverted polarity */
/**
* @brief SERIAL default configuration.
*/
#define MR_SERIAL_CONFIG_DEFAULT \
{ \
115200, \
MR_SERIAL_DATA_BITS_8, \
MR_SERIAL_STOP_BITS_1, \
MR_SERIAL_PARITY_NONE, \
MR_SERIAL_BIT_ORDER_LSB, \
MR_SERIAL_NRZ_NORMAL, \
}
/**
* @brief SERIAL configuration structure.
*/
struct mr_serial_config
{
uint32_t baud_rate; /**< Baud rate */
uint32_t data_bits: 4; /**< Data bits */
uint32_t stop_bits: 3; /**< Stop bits */
uint32_t parity: 2; /**< Parity */
uint32_t bit_order: 1; /**< Bit order */
uint32_t invert: 1; /**< Invert */
uint32_t reserved: 21;
};
/**
* @brief SERIAL structure.
*/
struct mr_serial
{
struct mr_dev dev; /**< Device structure */
struct mr_serial_config config; /**< Configuration */
struct mr_ringbuf rd_fifo; /**< Read FIFO */
struct mr_ringbuf wr_fifo; /**< Write FIFO */
size_t rd_bufsz; /**< Read buffer size */
size_t wr_bufsz; /**< Write buffer size */
};
/**
* @brief SERIAL operations structure.
*/
struct mr_serial_ops
{
int (*configure)(struct mr_serial *serial, struct mr_serial_config *config);
ssize_t (*read)(struct mr_serial *serial, uint8_t *buf, size_t size);
ssize_t (*write)(struct mr_serial *serial, const uint8_t *buf, size_t size);
void (*start_tx)(struct mr_serial *serial);
void (*stop_tx)(struct mr_serial *serial);
};
/**
* @addtogroup SERIAL.
* @{
*/
int mr_serial_register(struct mr_serial *serial, const char *name, struct mr_drv *drv);
/** @} */
#endif /* MR_USING_SERIAL */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _MR_SERIAL_H_ */