#include "stdafx.h"
#include "tester1.h"
#include <commctrl.h>
//#include <aygshell.h>
#include <sipapi.h>
#include "setup.h"

// moved to config.h
//#define FUART 0x40100000


void UART_puts(char *s)
{
	UINT32 *base=(UINT32*)VirtualAlloc((void*)0x0,sizeof(void*)*0xffff, MEM_RESERVE,PAGE_READWRITE);
	int ret=VirtualCopy(base,(void *) ((UARTBASE)/256),sizeof(void*)*0xffff	, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
	int a=0;
	while(s[a])
	{
		while((base[UARTSTATUS/4]&1<<UARTTXRDY) == UARTTXBIT) {}
		base[UARTDATA/4]=(char)(s[a]);
		a++;
	}
}

void UART_setup()
{
	UINT32 *base=(UINT32*)VirtualAlloc((void*)0x0,sizeof(void*)*0xffff, MEM_RESERVE,PAGE_READWRITE);
	int ret=VirtualCopy(base,(void *) ((UARTBASE)/256),sizeof(void*)*0xffff	, PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);

#ifdef STRONGARM
// Apparently there is something we don't know. This is needed.
	HANDLE hSerial = CreateFile(L"COM1:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
// disable UART
	base[0x0C/4] = 0;
// clear status
	base[0x1C/4] = 0xFF;
// set 8 bit no parity 1 stopbit
	base[0x00/4] = 9;
// set 115200 bps
	base[0x04/4] = 0;
	base[0x08/4] = 1;
// enable TX/RX
	base[0x0C/4] = 3;
#else
// set DLAB
	base[0x0C/4]=128+2+1;
// set divisor
	base[0]=8; // 115200 bps
	base[0x04/4]=0;
// unset DLAB
	base[0x0C/4]=2+1;
// UART enable & no FIFO
	base[0x04/4]=64;
	base[0x08/4]=0;
#endif

	char test[]="LinExec: UART Initialized.\n\r";
	int a=0;
	while(test[a])
	{
		while((base[UARTSTATUS/4]&1<<UARTTXRDY) == UARTTXBIT) {}
		base[UARTDATA/4]=(char)(test[a]);
		a++;
	}
}

