/* set dip4 related gpio */
int dip4_open( void )
{
// set gpio's direction: set IOPMOD register mode bit to 0 = input
IOPMOD = IOPMOD & (~0x0000000F);
return 0;
}
在驱动程序 dip4_driver 中,作为一个标准的输入设备,我们仅仅实现了它的读操作。
/* get dip4 status */
int dip4_read( char * buf, int count )
{
int i = 0;
int dip4_value;
// the count has exceeds the count of our board
if( count > DIP4_NUM )
return -1;
// get low-4-bits of IOPDATA
dip4_value = IOPDATA & 0x0F;
for( i = 0; i < count; i++ )
buf = (dip4_value & ( 1 << i ))? 1 : 0;
return count;
}
在编程接口 dip4_api 中,我们模仿 led 的实现方法,给出了 dip4_get_value 接口实现:
/* get 4 dip4s status to low-4-bits of hex value */
int dip4_get_value( void )
{
char buf[DIP4_NUM];
int value = 0;
int i;
// read the dip status from dip4 driver
dip4_read( buf, DIP4_NUM );
for( i = 0; i < DIP4_NUM; i++ )
value = value | (buf << i);
// return the dip4 status in low-bits of value
return value;
}