/* ------------------------------------------------------------------------------------------------ */
/* FILE :		SysCallSA.c																			*/
/*																									*/
/* CONTENTS :																						*/
/*				Standalone stdio library I/O														*/
/*																									*/
/*																									*/
/* Copyright (c) 2017-2019, Code-Time Technologies Inc. All rights reserved.						*/
/*																									*/
/* Redistribution and use in source and binary forms, with or without								*/
/* modification, are permitted provided that the following conditions are met:						*/
/*																									*/
/* 1. Redistributions of source code must retain the above copyright notice, this					*/
/*    list of conditions and the following disclaimer.												*/
/* 2. Redistributions in binary form must reproduce the above copyright notice,						*/
/*    this list of conditions and the following disclaimer in the documentation						*/
/*    and/or other materials provided with the distribution.										*/
/*																									*/
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND					*/
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED					*/
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE							*/
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR					*/
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES					*/
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;						*/
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND						*/
/* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT						*/
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS					*/
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.										*/
/*																									*/
/*																									*/
/*	$Revision: 1.6 $																				*/
/*	$Date: 2019/02/19 01:02:06 $																	*/
/*																									*/
/* ------------------------------------------------------------------------------------------------ */

#include <stdio.h>

#if  ((((OS_PLATFORM) & 0x00FFFFFF) == 0x0000AA10)													\
 ||   (((OS_PLATFORM) & 0x00FFFFFF) == 0x0000AAC5))
  #include "dw_uart.h"
#elif ((((OS_PLATFORM) & 0x00FFFFFF) == 0x00007020)													\
   ||  (((OS_PLATFORM) & 0x00FFFFFF) == 0x00007753))
  #include "cd_uart.h"
#else
  #error "This platform does not support the UART Driver"
#endif

/* ------------------------------------------------------------------------------------------------ */

extern int G_UartDevIn;
extern int G_UartDevOut;
extern int G_UartDevErr;

/* ------------------------------------------------------------------------------------------------ */

void SysCallInit(void)
{
	return;
}

/* ------------------------------------------------------------------------------------------------ */


int __putchar(int ccc)
{
char     cc;

	cc = (char )ccc;
	uart_send(G_UartDevOut, (const char *)&cc, 1);

	return(0);
}

/* ------------------------------------------------------------------------------------------------ */

int GetKey(void)
{
char     ccc;

	ccc = (char)0;
	uart_recv(G_UartDevIn, &ccc, -1);

	return(ccc);
}

/* ------------------------------------------------------------------------------------------------ */

#ifdef __CC_ARM										/* ARM/KEIL IO support							*/
  #include <rt_sys.h>

 #pragma import(__use_no_semihosting)
 struct __FILE { int handle;};
 FILE __stdout;
 FILE __stdin;
 FILE __stderr;


int fputc(int ccc, FILE *fp)
{
char cc;

	cc  = (char)ccc;
	ccc = EOF;
	if ((fp == stdout)
	||  (fp == stderr)) {
		ccc = uart_send(G_UartDevOut, &cc, 1);
	}

	return(ccc);
}

/* ------------------------------------------------------------------------------------------------ */

int fgetc(FILE *fp)
{
char ccc;
int  RetVal;

	RetVal = EOF;
	if (fp == stdin) {
		uart_recv(G_UartDevIn, &ccc, 1);
		RetVal = ccc;
	}
	return(RetVal);
}

/* ------------------------------------------------------------------------------------------------ */

int ferror(FILE *f)
{
int RetVal;

	RetVal = EOF;

	if ((f == stdin)
	||  (f == stdout)
	||  (f == stderr)) {
		RetVal = 0;
	}

	return(RetVal);
}

/* ------------------------------------------------------------------------------------------------ */

void _ttywrch(int ch) {

	__putchar(ch);

	return;
}

/* ------------------------------------------------------------------------------------------------ */

void _sys_exit(int return_code) {
	for(;;);
}

/* ------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------ */

#else												/* GCC I/O support								*/

/* ------------------------------------------------------------------------------------------------ */

#if !defined(_write_r)
_ssize_t _write_r (struct _reent *rreen, int fd, const void *vbuf, size_t len)
{
	return(uart_send(G_UartDevOut, (const char *)vbuf, (int)len));
}
#endif

/* ------------------------------------------------------------------------------------------------ */

#if !defined(_read_r)
_ssize_t _read_r(struct _reent *rreen, int fd, void *vbuf, size_t size)
{
	return(uart_recv(G_UartDevIn, (char *)vbuf, (int)size));
}
#endif

int _close_r(struct _reent *rreen, int fd)
{
	return(0);
}

int _fstat_r(struct _reent *reent, int fd, void *pstat)
{
	return(0);
}

int _isatty_r(struct _reent *Reent, int fd)
{
	return(0);
}

off_t _lseek_r(struct _reent *reent, int fd, off_t offset, int whence)
{
	return(0);
}

#endif
