/*****************************************************************************/
/**
 * @file systembus_uart.h
 * @author FanHao (hao_fan_bit_ee@163.com)
 * @brief System bus uart driver header file.
 * @version 0.1
 * @date 2023-05-06
 *
 * @copyright Copyright(c) 2024 Beijing Institute of Technology.\n
 * Lab of Communication and Network
 *
 ******************************************************************************/

/**
 * @addtogroup SystemBus
 * @{
 */

/**
 * @defgroup SystemBus_UART
 * @{
 */
#ifndef SYSTEMBUS_UART_H
#define SYSTEMBUS_UART_H

/****************** Include Files ********************/
#include <stdint.h>
#include <stdbool.h>
#include "util.h"
/**************************** Type Definitions *****************************/
/**
 *
 * Write a value to a SYSTEMBUS_UART register. A 32 bit write is performed.
 * If the component is implemented in a smaller width, only the least
 * significant data is written.
 *
 * @param   baseAddr is the base address of the SYSTEMBUS_UARTdevice.
 * @param   offset is the register offset from the base to write to.
 * @param   data is the data written to the register.
 *
 * @return  None.
 *
 * @note
 * C-style signature:
 * 	void SYSTEMBUS_UART_mWriteReg(uint32_t BaseAddress, unsigned RegOffset, uint32_t Data)
 *
 */
#define SYSTEMBUS_UART_mWriteReg(baseAddr, offset, data) \
	IO_Out32((baseAddr) + (offset), (uint32_t)(data))

/**
 *
 * Read a value from a SYSTEMBUS_UART register. A 32 bit read is performed.
 * If the component is implemented in a smaller width, only the least
 * significant data is read from the register. The most significant data
 * will be read as 0.
 *
 * @param   baseAddr is the base address of the SYSTEMBUS_UART device.
 * @param   offset is the register offset from the base to write to.
 *
 * @return  Data is the data from the register.
 *
 * @note
 * C-style signature:
 * 	uint32_t SYSTEMBUS_UART_mReadReg(uint32_t BaseAddress, unsigned RegOffset)
 *
 */
#define SYSTEMBUS_UART_mReadReg(baseAddr, offset) \
	IO_In32((baseAddr) + (offset))

/************************** Data Type Definitions ****************************/
/**
 * @struct SYSBUS_UART_CONFIG
 * @brief SystemBus UART driver initialization parameters
 */
typedef struct
{
	uintptr_t baseAddr;
} SYSBUS_UART_CONFIG;

/**
 * @struct  SYSBUS_UART
 * @brief SystemBus UART driver instance
 */
typedef struct
{
	SYSBUS_UART_CONFIG config;
} SYSBUS_UART;

/**
 * @struct  SYSBUS_UART_FRAME
 * @brief SystemBus UART frame definition
 */
typedef struct
{
	uint16_t len;
	uint8_t checkSum;
	uint8_t isValid;
	uint8_t data[256];
} SYSBUS_UART_FRAME;

/************************** Function Prototypes ******************************/
// Instance initial
int SysbusUart_Init(SYSBUS_UART **instPtr, SYSBUS_UART_CONFIG *config);

// Send a system bus frame.
int SysbusUart_Send(SYSBUS_UART *instPtr, SYSBUS_UART_FRAME *frame);

// Get last system bus frame.
int SysbusUart_Recv(SYSBUS_UART *instPtr, SYSBUS_UART_FRAME *frame);

// Get system bus rx channel status.
bool SysbusUart_IsRxBusy(SYSBUS_UART *instPtr);

// Get system bus tx channel status.
bool SysbusUart_IsTxBusy(SYSBUS_UART *instPtr);

#endif // SYSTEMBUS_UART_H

/** @} SystemBus_UART*/

/** @} SystemBus*/