/******************************************************************************* 
* DISCLAIMER 
* This software is supplied by Renesas Electronics Corporation and is only  
* intended for use with Renesas products. No other uses are authorized. This  
* software is owned by Renesas Electronics Corporation and is protected under 
* all applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING 
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT 
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE  
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. 
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS  
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE  
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR 
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE 
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
* Renesas reserves the right, without notice, to make changes to this software 
* and to discontinue the availability of this software. By using this software, 
* you agree to the additional terms and conditions found by accessing the  
* following link: 
* http://www.renesas.com/disclaimer 
* 
* Copyright (C) 2013 Renesas Electronics Corporation. All rights reserved.     
*******************************************************************************/
/******************************************************************************* 
* File Name    : r_rl78_can_drv.h 
* Version      : 1.0 
* Description  : This is header file for CAN driver code.
******************************************************************************/ 
/***************************************************************************** 
* History      : DD.MM.YYYY Version Description 
*              : 29.03.2013 1.00     First Release 
******************************************************************************/
#ifndef R_RL78_CAN_DRV_H
#define R_RL78_CAN_DRV_H

typedef signed char         CHAR;
typedef unsigned char       UCHAR;
typedef signed short        INT;
typedef unsigned short      UINT;

typedef unsigned int  Can_RtnType;    

typedef UCHAR can_ch_t;
typedef UCHAR can_rxfifo_t;
typedef UCHAR can_trfifo_t;
typedef UCHAR can_txbuf_t;

/* ---- Channel ---- */
#define CAN_CH0             0U

/* ---- Rx FIFO ---- */
#define CAN_RXFIFO0         0U
#define CAN_RXFIFO1         1U
#define CAN_MAX_RXFIFO_NUM  2U

/* ---- Common (Tx/Rx) FIFO ---- */
#define CAN_TRFIFO0         0U
#define CAN_MAX_TRFIFO_NUM  1U

/* ---- Tx buffers ---- */
#define CAN_TXBUF0          0U
#define CAN_TXBUF1          1U
#define CAN_TXBUF2          2U
#define CAN_TXBUF3          3U
#define CAN_MAX_TXBUF_NUM   4U

/* ---- CAN frame ----- */
typedef struct
{
    UINT IDL:16;   /* ID Data (low)                      */
    UINT IDH:13;   /* ID Data (high)                     */
    UINT THDSE :1; /* Transmit History Data Store Enable */
    UINT RTR :1;   /* RTR  0:Data 1:Remote               */
    UINT IDE :1;   /* IDE  0:Standard 1:Extend           */
    UINT TS :16;   /* Timestamp Data                     */
    UINT LBL :12;  /* Label Data                         */
    UINT DLC :4;   /* DLC Data                           */
    UCHAR  DB[8];  /* Data Byte                          */
} can_frame_t;

#if 0
//only for test
typedef struct _IoInput_
{
    /*previous value,middle variable value , finally value of the switches,used for
       cleaning up the botton's dithering*/
    UINT   unused      : 1;   
    UINT   swIn        : 1;                                    /* Read from I/O pin.*/
    UINT   in          : 1;                                    /* Used by program,related to inMode.*/
    UINT   preSwIn     : 1;                                    /* Previous value read from I/O pin.*/

    UINT   preVal      : 1;
    UINT   curVal      : 1;
    UINT   inMode      : 2;
    UINT   shakeCnt    : 4;                                    /* Switch shaking counter */
    UINT   shakeNum    : 4;                                    /* Switvh shaking number  */
} IoInput;
#endif

extern const UCHAR       g_rxfifo0_use_mode;
extern const UCHAR       g_rxfifo1_use_mode;
extern const UCHAR       g_trfifo_use_mode;
extern const UCHAR       g_fifo_lost_isr;
extern const UINT        g_rxrule_table[][6];

/* ---- Configuration ---- */
#define CAN_NOUSE                           0x00U
#define CAN_USE                             0x80U
#define CAN_USE_RX_MODE                     0x81U
#define CAN_USE_TX_MODE                     0x82U
#define CAN_USE_CH_MASK                     0x07U

/* ---- function return value ---- */
#define CAN_RTN_OK                          0U
#define CAN_RTN_OK_WITH_LOST                1U
#define CAN_RTN_FIFO_FULL                   2U
#define CAN_RTN_BUFFER_EMPTY                3U
#define CAN_RTN_MODE_WAIT                   4U
#define CAN_RTN_RAM_INIT                    5U

#define CAN_RTN_TRANSMITTING                0U
#define CAN_RTN_TX_ABORT_OVER               1U
#define CAN_RTN_TX_END                      2U
#define CAN_RTN_TX_END_WITH_ABORT_REQ       3U

#define CAN_RTN_ERROR                       (0x8000U)
#define CAN_RTN_ARG_ERROR                   (CAN_RTN_ERROR + 0x7FFF)
#define CAN_RTN_STS_ERROR                   (CAN_RTN_ERROR + 0x7FFE)
#define CAN_RTN_OVERWRITE                   (CAN_RTN_ERROR + 0x7FFD)
#define CAN_RTN_MODE_ERROR                  (CAN_RTN_ERROR + 0x7FFC)

/******************************************************************************
Exported global functions (to be accessed by other files)
******************************************************************************/
extern Can_RtnType R_CAN_Init(void);
extern Can_RtnType R_CAN_GlobalStart(void);
extern Can_RtnType R_CAN_ChStart_CH0(void);
extern Can_RtnType R_CAN_TrmByTxBuf_CH0(can_txbuf_t, const can_frame_t *);
extern Can_RtnType R_CAN_AbortTrm_CH0(can_txbuf_t);
extern Can_RtnType R_CAN_CheckTxBufResult_CH0(can_txbuf_t);
extern Can_RtnType R_CAN_TrmByTRFIFO0_CH0(const can_frame_t *);
extern Can_RtnType R_CAN_ReadRxBuffer(UCHAR *, can_frame_t *);
extern Can_RtnType R_CAN_ReadRxFIFO(can_rxfifo_t, can_frame_t *);
extern Can_RtnType R_CAN_ReadTRFIFO0_CH0(can_frame_t *);
extern Can_RtnType R_CAN_ReadChStatus_CH0(void);
void enable_interrupt_request_mask(void);

#define R_CAN_ChStart(val1)                 (R_CAN_ChStart_CH0())
#define R_CAN_TrmByTxBuf(val1, val2, val3)  (R_CAN_TrmByTxBuf_CH0(val2, val3))
#define R_CAN_AbortTrm(val1, val2)          (R_CAN_AbortTrm_CH0(val2))
#define R_CAN_CheckTxBufResult(val1, val2)  (R_CAN_CheckTxBufResult_CH0(val2))
#define R_CAN_TrmByTRFIFO(val1, val2, val3) (R_CAN_TrmByTRFIFO_CH0(val3))
#define R_CAN_ReadTRFIFO(val1, val2, val3)  (R_CAN_ReadTRFIFO0_CH0(val3))
#define R_CAN_ReadChStatus(val1)            (R_CAN_ReadChStatus_CH0())

extern void CanUserInit(void);
extern can_frame_t frame,frame1;
#endif /* R_RL78_CAN_DRV_H */
