/*
 *  for MB9BF618T on FRK-FM3
 * 
 *   Copyright (C) by Y.Ishioka.
 */
#include <basic.h>
#include <tk/tkernel.h>
#include <libstr.h>
#include <tk/syslib.h>
#include <drivers/sensor.h>

#include <tk/sysdef.h>


/***********************************************************/
/*
 * sensor port initialize
 */
void  sensport_init( void )
{
    /* set quad-counter : P50,P51,P52 */
    out_w( GPIO_PFR5, in_w(GPIO_PFR5)|0x07 ) ;
    out_w( GPIO_EPFR09, (in_w(GPIO_EPFR09)& ~(0x3<<0))|(0x3<<0) ) ; /* AIN0_2 */
    out_w( GPIO_EPFR09, (in_w(GPIO_EPFR09)& ~(0x3<<2))|(0x3<<2) ) ; /* BIN0_2 */
    out_w( GPIO_EPFR09, (in_w(GPIO_EPFR09)& ~(0x3<<4))|(0x3<<4) ) ; /* ZIN0_2 */
    out_w( GPIO_PCR5, in_w(GPIO_PCR5)|0x0001 ) ;          /* pull-up */

    out_h( QPRC0_QCR,  0x2419 ) ;  /* stop counter */
    out_h( QPRC0_QPCR, 0x0000 ) ;  /* clear counter */
    out_h( QPRC0_QRCR, 0x0000 ) ;

    out_h( QPRC0_QCR,  in_h(QPRC0_QCR) & ~0x0010 ) ;  /* start counter */
}


/*
 * bit0 : 0 : sensor OFF
 *        1 : sensor ON
 */
unsigned int  sensport_stat( void )
{
    unsigned int  stat = 0 ;

    stat = (int)( (in_w( GPIO_PDIR5 ) >> 3) & 0x0001 ) ;

    return( stat ) ;
}


unsigned int  sensport_counter( void )
{
    unsigned int  counter = 0 ;

    counter = (unsigned int)in_w( QPRC0_QPRCRR ) ;

    return( counter ) ;
}



