/*******************************************************************************
* 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 2018-2022 Renesas Electronics Corporation All rights reserved.
*******************************************************************************/
/*******************************************************************************
 * DESCRIPTION   : SCIF driver
 ******************************************************************************/
/******************************************************************************
 * @file          scif.c
 * - Version      : 0.07
 * @brief         1. Initial setting of SCIF.
 *                2. Initial setting of HSCIF.
 *                3. Log output function.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 28.07.2021 0.01     First Release
 *         : 03.09.2021 0.02     Modify the timing of MODEMR judgement.
 *         : 15.10.2021 0.03     Modify register access to read modify write.
 *         : 03.12.2021 0.04     Fix incorrect configuration process.
 *         : 06.01.2022 0.05     Static analysis support
 *         : 16.06.2022 0.06     Change line feed code
 *         : 31.10.2022 0.07     License notation change.
 *****************************************************************************/

#include <stdint.h>
#include <types.h>
#include <scif.h>
#include <mem_io.h>
#include <rst_register.h>

/* Define  */
#define SCIF_SCFSR_TEND         (uint16_t)((uint16_t)1U << 6U)
#define SCIF_SCFSR_TDFE         (uint16_t)((uint16_t)1U << 5U)
#define TRANS_END_CHECK         (uint16_t)(SCIF_SCFSR_TEND | SCIF_SCFSR_TDFE)


static void (*rcar_putc)(uint8_t outchar);

static void scif_console_init(uint32_t modemr);
static void scif_console_putc(uint8_t outchar);
static void hscif_console_putc(uint8_t outchar);


static void scif_console_init(uint32_t modemr)
{
    switch(modemr)
    {
        case MODEMR_HSCIF_DLMODE_3000000:
        {
            /* Set the pointer to a function that outputs one character. */
            rcar_putc = hscif_console_putc;
            break;
        }
        case MODEMR_HSCIF_DLMODE_1843200:
        {
            /* Set the pointer to a function that outputs one character. */
            rcar_putc = hscif_console_putc;
            break;
        }
        case MODEMR_HSCIF_DLMODE_921600:
        {
            /* Set the pointer to a function that outputs one character. */
            rcar_putc = hscif_console_putc;
            break;
        }
        case MODEMR_SCIF_DLMODE:
        default:
        {
            /* Set the pointer to a function that outputs one character. */
            rcar_putc = scif_console_putc;
            break;
        }
    }
}
/* End of function scif_console_init(void) */

void scif_init(void)
{
    uint32_t modemr;

#ifdef FORCE_115200  /* force to serial speed to 115200 bps */
#define _MODE31     (0xEB22FFF0)      /* Gen4_ICUMX_loader at RT-VRAM */
#define _MODE_115200 0x00115200
	modemr = mem_read32(_MODE31);
	if (modemr == _MODE_115200)
		modemr = MODEMR_SCIF_DLMODE;
	else
#endif
	{
    modemr = ((mem_read32(RST_MODEMR0) & RST_MODEMR0_MD31) >> 31U);
    modemr |= ((mem_read32(RST_MODEMR1) & RST_MODEMR1_MD32) << 1U);
	}
    scif_console_init(modemr);
}
/* End of function scif_init(void) */

void console_putc(uint8_t outchar)
{
    if (outchar == 0x0A)   /* \n */
    {
        rcar_putc( 0x0D ); /* \r */
    }
    rcar_putc(outchar);
}
/* End of function console_putc(void) */


static void scif_console_putc(uint8_t outchar)
{
    uint16_t reg;

    /* Check that transfer of SCIF0 is completed */
    while (!((TRANS_END_CHECK & mem_read16(SCIF_SCFSR)) == TRANS_END_CHECK))
    {
        ;
    }

    mem_write8(SCIF_SCFTDR, outchar);   /* Transfer one character */
    reg = mem_read16(SCIF_SCFSR);
    reg &= (uint16_t)(~(TRANS_END_CHECK)); /* TEND,TDFE clear */
    mem_write16(SCIF_SCFSR, reg);

    /* Check that transfer of SCIF0 is completed */
    while (!((TRANS_END_CHECK & mem_read16(SCIF_SCFSR)) == TRANS_END_CHECK))
    {
        ;
    }
}
/* End of function scif_console_putc(uint8_t outchar) */

static void hscif_console_putc(uint8_t outchar)
{
    uint16_t reg;

    /* Check that transfer of SCIF0 is completed */
    while (!((TRANS_END_CHECK & mem_read16(HSCIF_HSFSR)) == TRANS_END_CHECK))
    {
        ;
    }

    mem_write8(HSCIF_HSFTDR, outchar);  /* Transfer one character */
    reg = mem_read16(HSCIF_HSFSR);
    reg &= (uint16_t)(~(TRANS_END_CHECK)); /* TEND,TDFE clear */
    mem_write16(HSCIF_HSFSR, reg);

    /* Check that transfer of SCIF0 is completed */
    while (!((TRANS_END_CHECK & mem_read16(HSCIF_HSFSR)) == TRANS_END_CHECK))
    {
        ;
    }
}
/* End of function hscif_console_putc(uint8_t outchar) */
