/******************************************************************************
 * @file     retarget.c
 * @brief    'Retarget' layer for target-dependent low level functions
 * @version  V4.0.0
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include <stdio.h>
#include "spc1168.h"




/**
 *  @brief  These must be defined to avoid linking in stdio.o from the C Library
 */
struct __FILE
{
    int handle;   /* Add whatever you need here */
};
FILE __stdout;
FILE __stdin;
FILE __stderr;




/******************************************************************************
 * @brief      Get character from UART
 *
 * @param[in]  f:  Pointer to a FILE object that identifies the stream on which
 *                 the operation is to be performed
 *
 * @return     The character read from UART
 *
 ******************************************************************************/
#if defined(__ICCARM__)
#pragma optimize=low
#endif
int fgetc(FILE *f)
{
    /* Wait until data is available in RBR or the FIFO */
    while ((UART->UARTLSR.all & UARTLSR_ALL_DR_Msk) == UARTLSR_ALL_DR_NO_DATA_RECEVIED);

    return (int)UART_ReadByte(UART);
}




/******************************************************************************
 * @brief      Write character to stream
 *
 * @param[in]  ch:  Character to be written
 * @param[in]  f :  Pointer to a FILE object that identifies the stream on which
 *                  the operation is to be written
 *
 * @return     The same character that has been written
 *
 ******************************************************************************/
#if defined(__ICCARM__)
#pragma optimize=low
#endif
int fputc(int ch, FILE *f)
{
    if (ch == '\n')
    {
        UART_WriteByte(UART, (uint8_t)'\r');
        while ((UART->UARTLSR.all & UARTLSR_ALL_TXDONE_Msk) == UARTLSR_ALL_TXDONE_TRANSMIT_NOT_DONE) {};
    }

    UART_WriteByte(UART, (uint8_t) ch);
    while ((UART->UARTLSR.all & UARTLSR_ALL_TXDONE_Msk) == UARTLSR_ALL_TXDONE_TRANSMIT_NOT_DONE) {};

    return ch;
}




#if defined(__ICCARM__)
#pragma optimize=low
#endif
void _ttywrch(int ch)
{
    UART_WriteByte(UART, (uint8_t) ch);
    while ((UART->UARTLSR.all & UARTLSR_ALL_TXDONE_Msk) == UARTLSR_ALL_TXDONE_TRANSMIT_NOT_DONE) {};
}




#if defined(__ICCARM__)
#pragma optimize=low
#endif
int __backspace(FILE *f)
{
    return 0;
}




#if defined(__ICCARM__)
#pragma optimize=low
#endif
int ferror(FILE *f)
{
    /* Your implementation of ferror */
    return EOF;
}




#if defined(__ICCARM__)
#pragma optimize=low
#endif
void _sys_exit(int return_code)
{
    /* Endless loop */
    while (1);
}


/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
