/*******************************************************************************
* 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) 2012 - 2013 Renesas Electronics Corporation. All rights reserved.
*******************************************************************************/
/*******************************************************************************
* File Name   : siochar.c
* $Rev: 478 $
* $Date:: 2013-07-12 21:38:48 +0900#$
* Description : Serial I/O character R/W (SCIF 2-ch process)
*******************************************************************************/


/******************************************************************************
Includes   <System Includes> , "Project Includes"
******************************************************************************/
#include "r_typedefs.h"
#include "dev_drv.h"                /* Device Driver common header */
#include "devdrv_scif_uart.h"       /* UART Driver header */
#include "sio_char.h"
#include "iodefine.h"
#include "rza_io_regrw.h"

/******************************************************************************
Typedef definitions
******************************************************************************/


/******************************************************************************
Macro definitions
******************************************************************************/


/******************************************************************************
Imported global variables and functions (from other files)
******************************************************************************/


/******************************************************************************
Exported global variables and functions (to be accessed by other files)
******************************************************************************/


/******************************************************************************
Private global variables and functions
******************************************************************************/


/******************************************************************************
* Function Name: IoInitScif3
* Description  : This function initializes SCIF channel 3 as UART mode.
*              : The transmit and the receive of SCIF channel 3 are enabled.
* Arguments    : none
* Return Value : none
******************************************************************************/
void IoInitScif3(void)
{
    /* === Initialization of SCIF3 ==== */
    /* P1 clock=66.67MHz CKS=0 SCBRR=17 Bit rate error=0.46% => Baud rate=115200bps */
    R_SCIF_UART_Init(DEVDRV_CH_3, SCIF_UART_MODE_RW, SCIF_CKS_DIVISION_1, 17);

    /* === Initialization of PORT function ==== */
    /* ---- P7_10 : TxD3 ---- */
    /* Port initialize */
    /* Port mode : Multiplex mode                     */
    /* Port function setting : 6th multiplex function */
    /* I/O control mode : Peripheral function         */
    /* Bidirectional mode : Disable                   */
    RZA_IO_RegWrite_16(&GPIO.PIBC7,  0, GPIO_PIBC7_PIBC710_SHIFT,   GPIO_PIBC7_PIBC710);
    RZA_IO_RegWrite_16(&GPIO.PBDC7,  0, GPIO_PBDC7_PBDC710_SHIFT,   GPIO_PBDC7_PBDC710);
    RZA_IO_RegWrite_16(&GPIO.PIPC7,  0, GPIO_PIPC7_PIPC710_SHIFT,   GPIO_PIPC7_PIPC710);

    RZA_IO_RegWrite_16(&GPIO.PMC7,   1, GPIO_PMC7_PMC710_SHIFT,     GPIO_PMC7_PMC710);
    RZA_IO_RegWrite_16(&GPIO.PFCAE7, 1, GPIO_PFCAE7_PFCAE710_SHIFT, GPIO_PFCAE7_PFCAE710);
    RZA_IO_RegWrite_16(&GPIO.PFCE7,  0, GPIO_PFCE7_PFCE710_SHIFT,   GPIO_PFCE7_PFCE710);
    RZA_IO_RegWrite_16(&GPIO.PFC7,   0, GPIO_PFC7_PFC710_SHIFT,     GPIO_PFC7_PFC710);
    RZA_IO_RegWrite_16(&GPIO.PM7,    1, GPIO_PM7_PM710_SHIFT,       GPIO_PM7_PM710);

    /* ---- P7_11 : RxD3 ---- */
    /* Port initialize */
    RZA_IO_RegWrite_16(&GPIO.PIBC7,  0, GPIO_PIBC7_PIBC711_SHIFT,   GPIO_PIBC7_PIBC711);
    RZA_IO_RegWrite_16(&GPIO.PBDC7,  0, GPIO_PBDC7_PBDC711_SHIFT,   GPIO_PBDC7_PBDC711);
    RZA_IO_RegWrite_16(&GPIO.PIPC7,  0, GPIO_PIPC7_PIPC711_SHIFT,   GPIO_PIPC7_PIPC711);

    RZA_IO_RegWrite_16(&GPIO.PMC7,   1, GPIO_PMC7_PMC711_SHIFT,     GPIO_PMC7_PMC711);
    RZA_IO_RegWrite_16(&GPIO.PFCAE7, 1, GPIO_PFCAE7_PFCAE711_SHIFT, GPIO_PFCAE7_PFCAE711);
    RZA_IO_RegWrite_16(&GPIO.PFCE7,  0, GPIO_PFCE7_PFCE711_SHIFT,   GPIO_PFCE7_PFCE711);
    RZA_IO_RegWrite_16(&GPIO.PFC7,   0, GPIO_PFC7_PFC711_SHIFT,     GPIO_PFC7_PFC711);
    RZA_IO_RegWrite_16(&GPIO.PM7,    0, GPIO_PM7_PM711_SHIFT,       GPIO_PM7_PM711);
    /* Port mode : Multiplex mode                     */
    /* Port function setting : 4th multiplex function */
    /* I/O control mode : Peripheral function         */
    /* Bidirectional mode : Enable                    */

    /* === Enable SCIF3 transmission/reception ==== */
    /* ---- Serial control register (SCSCRi) setting ---- */
    SCIF3.SCSCR = 0x0030u;
                            /* SCIF transmitting and receiving operations are enabled */
}

/******************************************************************************
* Function Name: IoGetchar
* Description  : One character is received from SCIF2, and it's data is returned.
*              : This function keeps waiting until it can obtain the receiving data.
* Arguments    : none
* Return Value : Character to receive (Byte).
******************************************************************************/
char_t IoGetchar(void)
{
    char_t data;

    /* Confirming receive error(ER,BRK,FER,PER,ORER) */
    if ((SCIF3.SCFSR & 0x09Cu != 0) ||
        (1 == RZA_IO_RegRead_16(&SCIF3.SCLSR, SCIF3_SCLSR_ORER_SHIFT, SCIF3_SCLSR_ORER)))
    {
        /* ---- Detect receive error ---- */
        /* Disable reception             */
        /* Reset receiving FIFO          */
        /* Clearing FIFO reception reset */
        /* Error bit clear               */
        /* Enable reception              */
        RZA_IO_RegWrite_16(&SCIF3.SCSCR, 0, SCIF3_SCSCR_RE_SHIFT,    SCIF3_SCSCR_RE);
        RZA_IO_RegWrite_16(&SCIF3.SCFCR, 1, SCIF3_SCFCR_RFRST_SHIFT, SCIF3_SCFCR_RFRST);
        RZA_IO_RegWrite_16(&SCIF3.SCFCR, 0, SCIF3_SCFCR_RFRST_SHIFT, SCIF3_SCFCR_RFRST);
        SCIF3.SCFSR &= ~0x9Cu;
        RZA_IO_RegWrite_16(&SCIF3.SCLSR, 0, SCIF3_SCLSR_ORER_SHIFT,  SCIF3_SCLSR_ORER);
        RZA_IO_RegWrite_16(&SCIF3.SCSCR, 1, SCIF3_SCSCR_RE_SHIFT,    SCIF3_SCSCR_RE);

        return -1;
    }

    /* Is there receive FIFO data? */
    while (0 == RZA_IO_RegRead_16(&SCIF3.SCFSR, SCIF3_SCFSR_RDF_SHIFT, SCIF3_SCFSR_RDF))
    {
        /* WAIT */
    }

    /* Read receive data */
    data = (int_t)(SCIF3.SCFRDR & 0xFFu);
    /* Clear DR,RDF */
    RZA_IO_RegWrite_16(&SCIF3.SCFSR, 0, SCIF3_SCFSR_DR_SHIFT, SCIF3_SCFSR_DR);
    RZA_IO_RegWrite_16(&SCIF3.SCFSR, 0, SCIF3_SCFSR_RDF_SHIFT, SCIF3_SCFSR_RDF);

    return data;
}

/******************************************************************************
* Function Name: IoPutchar
* Description  : Character "buffer" is output to SCIF3.
*              : This function keeps waiting until it becomes the transmission
*              : enabled state.
* Arguments    : int_t buffer : character to output
* Return Value : None
******************************************************************************/
void IoPutchar(char_t buffer)
{
    /* Check if it is possible to transmit (TDFE flag) */
    while (0 == RZA_IO_RegRead_16(&SCIF3.SCFSR, SCIF3_SCFSR_TDFE_SHIFT, SCIF3_SCFSR_TDFE))
    {
        /* Wait */
    }

    /* Write the receiving data in TDR */
    SCIF3.SCFTDR = (uint8_t)buffer;

    /* Clear TDRE and TEND flag */
    SCIF3.SCFSR &= ~0x0060u;
}


/* End of File */

