194 lines
6.3 KiB
C

/*********************************************************************************************************
* ------------------------------------------------------------------------------------------------------
* file description
* ------------------------------------------------------------------------------------------------------
* \file icom.h
* \unit icom
* \brief This is a simple internal communication module for C language
* \author Lamdonn
* \version v0.1.0
* \license GPL-2.0
* \copyright Copyright (C) 2026 Lamdonn.
********************************************************************************************************/
#ifndef __icom_H
#define __icom_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* Version infomation */
#define ICOM_V_MAJOR 0
#define ICOM_V_MINOR 1
#define ICOM_V_PATCH 0
/**
* \brief: ICOM error code
* \note: 0 means success, negative value means error
*/
#define ICOM_E_OK (0) /* OK, no error */
#define ICOM_E_IHANDLE (-1) /* Invalid icom handle */
#define ICOM_E_NINIT (-2) /* Not initialized */
#define ICOM_E_IOUT (-3) /* Iterator out of range */
#define ICOM_E_NBASE (-4) /* NULL base address */
#define ICOM_E_LOUT (-5) /* Length out of range */
#define ICOM_E_BUSY (-6) /* Current channel is busy */
#define ICOM_E_IBUFFER (-7) /* Invalid buffer */
#define ICOM_E_ILENGTH (-8) /* Invalid length */
#define ICOM_E_NRECEIVE (-9) /* No data received */
/**
* \brief: ICOM receive callback function
* \param channel: Channel number
* \param data: Data buffer
* \param length: Data length
* \return: ICOM error code
*/
typedef int (*icom_callback_t)(uint16_t channel, uint8_t *data, uint32_t length);
/**
* \brief: ICOM cache invalidate function
* \param address: Cache address
* \param size: Cache size
*/
typedef void (*icom_cache_inv_t)(uint8_t *address, uint32_t size);
/**
* \brief: ICOM cache write-back invalidate function
* \param address: Cache address
* \param size: Cache size
*/
typedef void (*icom_cache_wbinv_t)(uint8_t *address, uint32_t size);
/**
* \brief: ICOM channel configuration structure
* \note: This structure is used to configure the ICOM channel
*/
typedef struct
{
uint8_t *base; /**< Base address of the channel */
uint32_t size; /**< Size of the channel */
icom_callback_t callback; /**< Callback function */
uint32_t private[4]; /**< Private data */
} ICOM_CHNCFG;
/**
* \brief: ICOM structure
* \note: This structure is used to manage the ICOM module
*/
typedef struct
{
uint8_t init; /**< Initialize flag */
ICOM_CHNCFG *txconfig; /**< Transmit channel configuration */
ICOM_CHNCFG *rxconfig; /**< Receive channel configuration */
uint16_t txcount; /**< Transmit channel count */
uint16_t rxcount; /**< Receive channel count */
icom_cache_inv_t inv; /**< Cache invalidate function */
icom_cache_wbinv_t wbinv; /**< Cache write-back invalidate function */
} ICOM;
/**
* \brief: ICOM initialize function
* \param icom: ICOM handle
* \return: ICOM error code
*/
int icom_init(ICOM *icom);
/**
* \brief: ICOM task function
* \param icom: ICOM handle
* \return: ICOM error code
*/
int icom_task(ICOM *icom);
/**
* \brief: ICOM transmit getbuffer function
* \param icom: ICOM handle
* \param channel: Transmit channal index
* \param buffer: Pointer to store data buffer
* \param length: Pointer to store data length
* \return: ICOM error code
*/
int icom_transmit_getbuffer(ICOM *icom, uint16_t channel, uint8_t **buffer, uint32_t *length);
/**
* \brief: ICOM transmit trigger function
* \param icom: ICOM handle
* \param channel: Transmit channal index
* \param length: Data length
* \note: Data length must be less than or equal to the transmit buffer size minus ICOM_HEAD_SIZE
* \return: ICOM error code
*/
int icom_transmit_trigger(ICOM *icom, uint16_t channel, uint32_t length);
/**
* \brief: ICOM transmit function
* \param icom: ICOM handle
* \param channel: Transmit channal index
* \param data: Pointer to data buffer
* \param length: Data length
* \note: Data length must be less than or equal to the transmit buffer size minus ICOM_HEAD_SIZE
* \return: ICOM error code
*/
int icom_transmit(ICOM *icom, uint16_t channel, uint8_t *data, uint32_t length);
/**
* \brief: ICOM receive getbuffer function
* \param icom: ICOM handle
* \param channel: Receive channal index
* \param buffer: Pointer to store data buffer
* \param length: Pointer to store data length
* \return: ICOM error code
*/
int icom_receive_getbuffer(ICOM *icom, uint16_t channel, uint8_t **buffer, uint32_t *length);
/**
* \brief: ICOM receive indicate function
* \param icom: ICOM handle
* \param channel: Receive channal index
* \param length: Pointer to store data length
* \return: ICOM error code
*/
int icom_receive_indicate(ICOM *icom, uint16_t channel, uint32_t *length);
/**
* \brief: ICOM receive end function
* \param icom: ICOM handle
* \param channel: Receive channal index
* \return: ICOM error code
*/
int icom_receive_end(ICOM *icom, uint16_t channel);
/**
* \brief: ICOM receive function
* \param icom: ICOM handle
* \param channel: Receive channal index
* \param data: Pointer to data buffer
* \param length: Pointer to store data length
* \note: Data length must be less than or equal to the receive buffer size minus ICOM_HEAD_SIZE
* \return: ICOM error code
*/
int icom_receive(ICOM *icom, uint16_t channel, uint8_t *data, uint32_t *length);
/**
* \brief: ICOM transmit channal online check function
* \param icom: ICOM handle
* \param channel: Transmit channal index
* \param online: Pointer to store online status
* \return: ICOM error code
*/
int icom_txchannel_online(ICOM *icom, uint16_t channel, uint8_t *online);
/**
* \brief: ICOM receive channal online check function
* \param icom: ICOM handle
* \param channel: Receive channal index
* \param online: Pointer to store online status
* \return: ICOM error code
*/
int icom_rxchannel_online(ICOM *icom, uint16_t channel, uint8_t *online);
#endif