/*
* co_drv.h - contains defines for driver
*
* Copyright (c) 2012-2024 emotas embedded communication GmbH
*
*-------------------------------------------------------------------
* $Id: co_drv.h 53003 2024-03-26 09:40:00Z boe $
*
*-------------------------------------------------------------------
*
*
*
*/

/**
* \brief defines for driver
*
* \file co_drv.h - contains defines for driver
*
*/

#ifndef CO_DRV_H
#define CO_DRV_H 1

#define CO_CAN_CC_MAX_DATA_LEN	8u	/**< maximum payload length for classical CAN frames */
#define CO_CAN_FD_MAX_DATA_LEN	64u	/**< maximum payload length for CAN-FD CAN frames */

# define CO_CAN_MAX_DATA_LEN	CO_CAN_CC_MAX_DATA_LEN


typedef	void *		LIBDRV_HANDLE_T;

#ifndef CO_CAN_TIMESTAMP_T
# define CO_CAN_TIMESTAMP_T UNSIGNED32
#endif /* CO_CAN_TIMESTAMP_T */


/** flags for CAN Messages from/to driver */
#define	CO_COBFLAG_NONE			((UNSIGNED8)(0u))		/**< none */
#define	CO_COBFLAG_ENABLED		((UNSIGNED8)(1u))		/**< cob enabled/disabled */
#define CO_COBFLAG_EXTENDED		((UNSIGNED8)(1u << 1))	/**< extended id  */
#define	CO_COBFLAG_FD				((UNSIGNED8)(1u << 2))	/**< fd frame */
#define	CO_COBFLAG_RTR			((UNSIGNED8)(1u << 3))	/**< rtr */
#define CO_COBFLAG_IND			((UNSIGNED8)(1u << 4))	/**< msg creates a indication, only valid for TX */


/* datatypes
-----------------------------------------------------------*/
/** CAN cob structure */
typedef struct {
	UNSIGNED32			canId;			/**< can identifier */
	UNSIGNED32			ignore;			/**< ignore mask for id */
	UNSIGNED16			canChan;		/**< reserved for driver */
	UNSIGNED8			flags;			/**< flags (rtr, extended, enabled, ... */
} CO_CAN_COB_T;


/** CAN receive message structure */
typedef struct {
	UNSIGNED32			canId;			/**< can identifier */
#ifdef CO_CAN_TIMESTAMP_SUPPORTED
	CO_CAN_TIMESTAMP_T	timestamp;		/**< timestamp */
#endif /* CO_CAN_TIMESTAMP_SUPPORTED */
	UNSIGNED8			flags;			/**< flags (rtr, extended, enabled, ... */
	UNSIGNED8			len;			/**< msg len */
	UNSIGNED8			data[CO_CAN_MAX_DATA_LEN];	/**< data */
} CO_CAN_REC_MSG_T;


/** CAN transmit message structure */
typedef struct {
	LIBDRV_HANDLE_T		handle;			/**< library internal handle */
	UNSIGNED32			canId;			/**< can identifier */
	UNSIGNED16			canChan;		/**< reserved for driver */
	UNSIGNED8			flags;			/**< flags (rtr, extended, enabled, ... */
	UNSIGNED8			len;			/**< msg len */
	UNSIGNED8			data[CO_CAN_MAX_DATA_LEN];	/**< data */
} CO_CAN_TR_MSG_T;


/** bittiming table entries */
typedef struct {
	UNSIGNED16 bitRate; 	/**< bitrate in kbit/s */
	UNSIGNED16 pre; 		/**< prescaler */
	UNSIGNED8 prop;			/**< propagation segment */
	UNSIGNED8 seg1; 		/**< segment 1 */
	UNSIGNED8 seg2; 		/**< segment 2 */
} CO_NV_STORAGE CODRV_BTR_T;


/* function prototypes */

EXTERN_DECL void codrvWaitForEvent(UNSIGNED32	msecTimeout);
EXTERN_DECL void codrvWaitForCanEvent(UNSIGNED32	waitTimeOut);
EXTERN_DECL void codrvHardwareInit(void);

/* CAN
-----------------------------------------------------------*/
EXTERN_DECL RET_T	codrvCanInit(CO_LINE_DECL, UNSIGNED16 bitRate);
EXTERN_DECL RET_T	codrvCanInitWithDev(CO_LINE_DECL, const char *devName,
						UNSIGNED16 bitRate);
EXTERN_DECL RET_T	codrvCanReInit(CO_LINE_DECL, UNSIGNED16	bitRate);
EXTERN_DECL RET_T	codrvCanSetBitRate(CO_LINE_DECL, UNSIGNED16	bitRate);
EXTERN_DECL RET_T	codrvCanStartTransmission(CO_LINE_DECL);
EXTERN_DECL void	codrvCanDriverHandler(CO_LINE_DECL);
EXTERN_DECL RET_T	codrvCanEnable(CO_LINE_DECL);
EXTERN_DECL RET_T	codrvCanDisable(CO_LINE_DECL);
EXTERN_DECL RET_T	codrvCanDeinit(CO_LINE_DECL);
EXTERN_DECL int		codrvGetCanFd(CO_LINE_DECL);


/* QUEUE
-----------------------------------------------------------*/
EXTERN_DECL UNSIGNED8 *coQueueGetReceiveBuffer(CO_LINE_REDCY,
			UNSIGNED32 canId, UNSIGNED8 dataLen, UNSIGNED8 flags
#ifdef CO_CAN_TIMESTAMP_SUPPORTED
			, CO_CAN_TIMESTAMP_T	timestamp
#endif /* CO_CAN_TIMESTAMP_SUPPORTED */
			);
EXTERN_DECL void	coQueueReceiveBufferIsFilled(CO_LINE_REDCY);
EXTERN_DECL BOOL_T	coQueueReceiveMessageAvailable(CO_LINE_DECL);
EXTERN_DECL CO_CAN_TR_MSG_T *coQueueGetNextTransmitMessage(CO_LINE_REDCY);
EXTERN_DECL void	coQueueMsgTransmitted(const CO_CAN_TR_MSG_T *pBuf);
EXTERN_DECL RET_T	CUSTOMER_TRANSMIT_MESSAGES_CALLBACK(CO_LINE_DECL,
						UNSIGNED32 canId, UNSIGNED8 len, UNSIGNED8 *buffer);
#ifdef OLD_FIXED_BUFFER
EXTERN_DECL BOOL_T	CUSTOMER_RECEIVE_MESSAGES_CALLBACK(CO_LINE_DECL, const CO_CAN_REC_MSG_T *pMsg);
#else /* OLD_FIXED_BUFFER */
EXTERN_DECL BOOL_T	CUSTOMER_RECEIVE_MESSAGES_CALLBACK(CO_LINE_DECL, const CO_CAN_REC_MSG_T *pMsg);
#endif /* OLD_FIXED_BUFFER */

/* GATEWAY
-----------------------------------------------------------*/
EXTERN_DECL void coGatewayTransmitMessage(const CO_CAN_TR_MSG_T *pMsg);
EXTERN_DECL void coQueueRecMsgFromGw(CO_CAN_REC_MSG_T *pMsg);

/* CAN IRQ
-----------------------------------------------------------*/
EXTERN_DECL void codrvCanEnableInterrupt(CO_LINE_DECL);
EXTERN_DECL void codrvCanDisableInterrupt(CO_LINE_DECL);

/* CAN - FullCAN functionality (Acceptance filter)
-----------------------------------------------------------*/
EXTERN_DECL RET_T codrvCanSetFilter(CO_LINE_DECL, CO_CAN_COB_T *pCanCob);

/* timer
-----------------------------------------------------------*/
EXTERN_DECL RET_T codrvTimerSetup(UNSIGNED32 timerInterval);
EXTERN_DECL RET_T codrvTimerStop(void);

#endif /* CO_DRV_H */

