//---------------------------------------------------------------------------
#include <stdio.h>  
#include <time.h>

#pragma hdrstop

#ifndef linux
#include "..\..\core\api.h"
#else
#include "../../core/api.h"
#endif // linux

//---------------------------------------------------------------------------

#define DS3231 0xd0


             
tGetBoardName GetBoardName;

tSmBus_ReadBytes SmBus_ReadBytes;


int main(int argc, char* argv[])
{
unsigned char Buffer[0x20];
unsigned char Size;
unsigned int Temp;    
struct tm pstm;  
int i;
char * Name;

int i2;



	if (KT_API_Open(sfDISK))
	{
		printf("KTAPI Verion %d.%03d.%d.%d",KT_API->VersionMajor,KT_API->VersionMinor,KT_API->Build,KT_API->Type);

		if ( KT_API_GetFunction("GetBoardName",&GetBoardName))
		{	          
			Name="N/A";                 
			GetBoardName(&Name);
			
			printf(" - %s",Name);		
		}
		
		if (OS_String[0]!=0x00)
		{
			printf(" (%s)",OS_String);		
		}
		
		printf("\n");

		if ( KT_API_GetFunction("SmBus_ReadBytes",&SmBus_ReadBytes))
		{

			/*
			for(i2=0;i2<0x100;i2+=2)
			{
				Size=0x10;		
				if (SmBus_ReadBytes(i2,0x00,Size,Buffer))
				{
					
					printf("%02x : ",i2);
	
					for(i=0;i<Size;i++)
					{
						printf("%02x ",Buffer[i]);
					}                            
					printf("\n");
	
				}
			}
		    */
			
			Size=0x12;		
			if (SmBus_ReadBytes(DS3231,0x00,Size,Buffer))
			{
				
				for(i=0;i<Size;i++)
				{
					printf("%02x ",Buffer[i]);
				}                            
				printf("\n");
				
				Temp = 0;
				Temp = (Buffer[0] & 0x0f);
				Temp += (((Buffer[0]>>4)&0x07)*10);
				
				pstm.tm_sec = (int)Temp;
				
				Buffer[1] = Buffer[1]&0x7f;
				
				Temp = 0;
				Temp = (Buffer[1] & 0x0f);
				Temp += (((Buffer[1]>>4)&0x07)*10);
				
				pstm.tm_min = (int)Temp;
				
				
				Buffer[2] = Buffer[2]&0x7f;
				
				Temp = 0;
				Temp = (Buffer[2] & 0x0f);
				Temp += (((Buffer[2]>>4)&0x03)*10);
				
				pstm.tm_hour = (int)Temp;
				
				
				Temp = 0;
				Temp = (Buffer[3] & 0x0f);
				Temp += (((Buffer[3]>>4)&0x03)*10);
				
				pstm.tm_mday = (int)Temp;
				
				
				Temp = 0;
				Temp = (Buffer[4] & 0x0f);
				Temp += (((Buffer[4]>>4)&0x01)*10);
				
				pstm.tm_mon = (int)Temp;
				
				
				Temp = 0;
				Temp = (Buffer[6] & 0x0f);
				Temp += (((Buffer[6]>>4)&0x0f)*10);
				
				pstm.tm_year = ((int)Temp+2000);
				
				
				printf("\nTime: %02d:%02d:%02d\n",pstm.tm_hour,pstm.tm_min,pstm.tm_sec);
			}

			
		}
		



		KT_API_Close();
	}


	return 0;
}
//---------------------------------------------------------------------------
