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

/**
 * @defgroup SystemBus
 * @{
 */
#ifndef __SYSTEMBUS_H_
#define __SYSTEMBUS_H_

/***************************** Include Files *********************************/
#include <stdint.h>
#include <stdbool.h>
#include "systembus_uart.h" // SystemBus UART driver
#include "circ_queue.h"
#include "../system_monitor.h"

/************************** Constant Definitions *****************************/
/************************** Data Type Definitions ****************************/

/**
 * @struct SYSBUS_CONFIG
 * @brief SystemBus Config struct holds SystemBus initialization parameters
 */
typedef struct
{
	uintptr_t baseAddr;	 ///< base address in memory
	uint32_t txQueueMax; ///< maximum tx queue length
	uint32_t rxQueueMax; ///< maximum rx queue length
} SYSBUS_CONFIG;

/**
 * @struct SYSBUS
 * @brief SystemBus Instance struct
 */
typedef struct
{
	SYSBUS_CONFIG config;  ///< SystemBus initialization parameters
	SYSBUSLOG sysbusLog;   ///< SystemBus log
	LINKLOG linkLog;	   ///< Link layer log
	PHYLOG phyLog;		   ///< Physical layer log
	HWSTATE hwState;	   ///< Hardware status
	SYSBUS_UART *uartInst; ///< physical uart controller instance
	CIRC_QUEUE *txQueue;   ///< tx queue
	CIRC_QUEUE *rxQueue;   ///< rx queue
	bool isTxDone;		   ///< tx done indicator
	bool isRxDone;		   ///< rx done indicator
} SYSBUS;

/************************** Function Prototypes ******************************/

int32_t SystemBus_Init(SYSBUS **instPtr, SYSBUS_CONFIG *config);

/**
 * @defgroup SystemBus_Protocol_Routine
 * @{
 */
int32_t SystemBus_HandleCmd(SYSBUS *instPtr);
int32_t SystemBus_MakeResp(SYSBUS *instPtr);
/** @} */

int32_t SystemBus_TxIntrHandler(SYSBUS *instPtr);
int32_t SystemBus_RxIntrHandler(SYSBUS *instPtr);

bool SystemBus_IsReqPending(SYSBUS *instPtr);
bool SystemBus_IsRsqPending(SYSBUS *instPtr);

bool SystemBus_IsTxBusy(SYSBUS *instPtr);
bool SystemBus_IsRxBusy(SYSBUS *instPtr);

#endif //__SYSTEMBUS_H_

/** @} SystemBus*/