/*---------------------------------------------------------------------------*
  Project:  NitroSDK - RTC - include
  File:     gpio.h

  Copyright 2003,2004 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Log: gpio.h,v $
  Revision 1.1  2004/05/12 02:28:12  terui
  initial upload

  $NoKeywords: $
 *---------------------------------------------------------------------------*/

#ifndef	NITRO_RTC_ARM7_GPIO_H_
#define	NITRO_RTC_ARM7_GPIO_H_

#ifdef	__cplusplus
extern "C" {
#endif

/*===========================================================================*/

#include	<nitro/types.h>
#include	<nitro/hw/common/armArch.h>

/*---------------------------------------------------------------------------*
	Constant definitions
 *---------------------------------------------------------------------------*/

//#define	RTC_CODE_IN_MAIN_MEMORY
/* Defines the case when the function group included in gpio.c is located in main memory */
/* (As a result, the number of loops for wait time will be reduced) */

//#define	RTC_VDD_LESS_THAN_3V
/* Defines the case in which voltage provided to RTC hardware is less than 3.0V */
/* (As a result, each wait will increase (about 5 times) */

// Definition of GPIO port for RTC
#define		RTC_GPIO_DIRECTION_MASK		0x0070		// Ra , R9 , R8 (Direction Bit)
#define		RTC_GPIO_VALUE_MASK			0x0007		// Ra , R9 , R8 (Data Value)
#define		RTC_GPIO_MASK				( RTC_GPIO_DIRECTION_MASK | RTC_GPIO_VALUE_MASK )

#define		RTC_GPIO_DIRECTION_SEND		0x0070		// CS->send , CLOCK->send , DATA->send
#define		RTC_GPIO_DIRECTION_RECV		0x0060		// CS->send , CLOCK->send , DATA->recv

#define		RTC_GPIO_CS_MASK			0x0004		// Ra (Data Value)
#define		RTC_GPIO_CS_LO				0x0000
#define		RTC_GPIO_CS_HI				0x0004
#define		RTC_GPIO_CLOCK_MASK			0x0002		// R9 (Data Value)
#define		RTC_GPIO_CLOCK_LO			0x0000
#define		RTC_GPIO_CLOCK_HI			0x0002
#define		RTC_GPIO_DATA_MASK			0x0001		// R8 (Data Value)
#define		RTC_GPIO_DATA_LO			0x0000
#define		RTC_GPIO_DATA_HI			0x0001

// Macro that converts seconds into the number of ARM7 CPU clocks
#define		RTC_SEC_TO_CPUCLOCK( s )		( HW_CPU_CLOCK * s )
#define		RTC_MILLISEC_TO_CPUCLOCK( ms )	( HW_CPU_CLOCK * ms / 1000 )
#define		RTC_MICROSEC_TO_CPUCLOCK( us )	( HW_CPU_CLOCK * us / 1000000 )
#define		RTC_NANOSEC_TO_CPUCLOCK( ns )	( HW_CPU_CLOCK * ns / 1000000000 )

// Number of clocks for one CPU wait loop
#ifdef	RTC_CODE_IN_MAIN_MEMORY
#define		RTC_MIN_LOOP_CLOCK		17		// 5+5+2+5
#else
#define		RTC_MIN_LOOP_CLOCK		4		// 1+1+1+1
#endif

// RTC hardware AC characteristic specification values -> CPU wait loop count
#ifdef	RTC_VDD_LESS_THAN_3V
#define		RTC_WAIT_LOOP_COUNT_TDS		( ( RTC_MICROSEC_TO_CPUCLOCK( 1 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TCSH	( ( RTC_MICROSEC_TO_CPUCLOCK( 1 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TCSS	( ( RTC_MICROSEC_TO_CPUCLOCK( 1 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TDH		( ( RTC_MICROSEC_TO_CPUCLOCK( 1 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TSCK	( ( RTC_MICROSEC_TO_CPUCLOCK( 5 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#else
#define		RTC_WAIT_LOOP_COUNT_TDS		( ( RTC_NANOSEC_TO_CPUCLOCK( 200 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TCSH	( ( RTC_NANOSEC_TO_CPUCLOCK( 200 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TCSS	( ( RTC_NANOSEC_TO_CPUCLOCK( 200 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TDH		( ( RTC_NANOSEC_TO_CPUCLOCK( 200 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#define		RTC_WAIT_LOOP_COUNT_TSCK	( ( RTC_MICROSEC_TO_CPUCLOCK( 1 ) / RTC_MIN_LOOP_CLOCK ) + 1 )
#endif


/*---------------------------------------------------------------------------*
	Function definitions
 *---------------------------------------------------------------------------*/
void	RTCi_GpioStart( void );
void	RTCi_GpioEnd( void );
void	RTCi_GpioSendCommand( u16 command , u16 parameter );
void	RTCi_GpioSendData( const void* pData , u32 size );
void	RTCi_GpioReceiveData( void* pData , u32 size );


/*===========================================================================*/

#ifdef	__cplusplus
}		/* extern "C" */
#endif

#endif	/* NITRO_RTC_ARM7_GPIO_H_ */

/*---------------------------------------------------------------------------*
  End of file
 *---------------------------------------------------------------------------*/
