00001 /* src_experimental/drivers/i2c/digital.c 00002 CubeOS Version 0.4.90 experimental 00003 Copyright (C) 1999,2000 Holger Kenn 00004 00005 CubeOS is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or any later version. 00009 00010 CubeOS is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 */ 00016 #include <stdlib.h> 00017 #include <stdio.h> 00018 #include <digital.h> 00019 #include <i2cd.h> 00020 00029 struct _I2C_bin_datas _I2C_bin_data[32]; 00030 int _I2C_binnum; 00031 00032 00033 unsigned char _I2C_digital_mbuf[MAXI2CMESSLENGTH]; 00034 struct i2cmess _I2C_digital_m; 00035 00041 unsigned char I2C_ReadBinIn (int chip) 00042 { 00043 00044 if (_I2C_bin_data[chip].active == 0) 00045 return (0); 00046 00047 /* read value */ 00048 _I2C_digital_m.address = _I2C_bin_data[chip].address | 0x1; /* Read adress */ 00049 _I2C_digital_m.nrBytes = 1; /* Read one byte */ 00050 _I2C_digital_mbuf[0] = 0; /* clear buffer */ 00051 _I2C_digital_m.buf = _I2C_digital_mbuf; 00052 00053 00054 I2C_process (_I2C_bin_data[chip].bus, I2C_MASTER, &_I2C_digital_m); 00055 00056 return _I2C_digital_mbuf[0]; 00057 00058 } 00059 00060 int I2C_WriteBinOut (int chip, unsigned char value) 00061 { 00062 00063 if (_I2C_bin_data[chip].active == 0) 00064 return (-1); 00065 00066 /* read value */ 00067 _I2C_digital_m.address = _I2C_bin_data[chip].address; /* Write adress */ 00068 _I2C_digital_m.nrBytes = 1; /* Write one byte */ 00069 _I2C_digital_mbuf[0] = value; 00070 _I2C_digital_m.buf = _I2C_digital_mbuf; 00071 00072 I2C_process (_I2C_bin_data[chip].bus, I2C_MASTER, &_I2C_digital_m); 00073 00074 return (_I2C_digital_m.status == I2C_OK); 00075 00076 } 00077 00087 int I2C_init_digital () 00088 { 00089 int i; 00090 00091 _I2C_binnum = 0; 00092 for (i = 0; i < 31; i++) 00093 _I2C_bin_data[i].active = 0; 00094 00095 // later this should come from the HWinfo struct 00096 for (i = 0; i < 31; i++) { 00097 00098 if (i < 16) { 00099 _I2C_bin_data[_I2C_binnum].bus = I2CA; 00100 } else { 00101 _I2C_bin_data[_I2C_binnum].bus = I2CB; 00102 } 00103 00104 if ((i & 0xf) < 8) { 00105 _I2C_bin_data[_I2C_binnum].address = ((i & 0x07) << 1) | 0x40; // PCF8574 00106 00107 } else { 00108 _I2C_bin_data[_I2C_binnum].address = ((i & 0x07) << 1) | 0x70; // PCF8574A 00109 00110 } 00111 _I2C_bin_data[_I2C_binnum].status = 0x00; // maybe later... 00112 00113 00114 00115 _I2C_digital_m.address = _I2C_bin_data[_I2C_binnum].address | 0x1; /* Read adress */ 00116 _I2C_digital_m.nrBytes = 1; /* Read one false byte, then read value */ 00117 _I2C_digital_mbuf[0] = 0; /* clear buffer */ 00118 _I2C_digital_m.buf = _I2C_digital_mbuf; 00119 I2C_process (_I2C_bin_data[_I2C_binnum].bus, I2C_MASTER, &_I2C_digital_m); 00120 00121 00122 switch (_I2C_digital_m.status) { 00123 case I2C_OK: 00124 printf ("digital device %d at adress 0x%x \n", _I2C_binnum, _I2C_bin_data[_I2C_binnum].address); 00125 _I2C_bin_data[_I2C_binnum].active = 1; 00126 _I2C_binnum++; 00127 break; 00128 case I2C_TIME_OUT: 00129 case I2C_NO_BUS: 00130 case I2C_NACK_ON_ADDRESS: 00131 break; 00132 00133 default: 00134 I2C_messagestatus (&_I2C_digital_m); 00135 } 00136 00137 00138 } 00139 00140 return (_I2C_binnum > 0 ? 0 : 1); 00141 00142 00143 }