00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <analog.h>
00019 #include <i2cd.h>
00020 
00029 struct _I2C_ad_datas _I2C_ad_data[16];
00030 int _I2C_adnum;
00031 
00032 char _I2C_analog_mbuf[MAXI2CMESSLENGTH];
00033 struct i2cmess _I2C_analog_m;
00034 
00040 int I2C_ReadAnalogIn (int chip, int channel)
00041 {
00042 
00043         if (!_I2C_ad_data[chip].active)
00044                 return (0);     
00045 
00046 
00047         _I2C_analog_m.address = _I2C_ad_data[chip].address;
00048         _I2C_analog_m.nrBytes = 1;
00049         _I2C_analog_m.buf = _I2C_analog_mbuf;
00050         _I2C_analog_mbuf[0] = (_I2C_ad_data[chip].status & 0x70) | (channel & 0x03);    
00051         I2C_process (_I2C_ad_data[chip].bus, I2C_MASTER, &_I2C_analog_m);
00052 
00053 
00054         _I2C_analog_m.address = _I2C_ad_data[chip].address | 0x1;       
00055         _I2C_analog_m.nrBytes = 2;      
00056         _I2C_analog_mbuf[0] = 0;        
00057         _I2C_analog_mbuf[1] = 0;
00058         _I2C_analog_m.buf = _I2C_analog_mbuf;
00059 
00060 
00061         I2C_process (_I2C_ad_data[chip].bus, I2C_MASTER, &_I2C_analog_m);
00062 
00063         return ((int) (((unsigned int) _I2C_analog_mbuf[1]) & 0xff));
00064 
00065 }
00066 
00077 int I2C_ConfigureAnalog (int chip, char how)
00078 {
00079 
00080         if (!_I2C_ad_data[chip].active)
00081                 return (-1);
00082 
00083         _I2C_ad_data[chip].status = how & 0x70;
00084 
00085 
00086         _I2C_analog_m.address = _I2C_ad_data[chip].address;
00087         _I2C_analog_m.nrBytes = 1;
00088         _I2C_analog_m.buf = _I2C_analog_mbuf;
00089         _I2C_analog_mbuf[0] = (_I2C_ad_data[chip].status);
00090         I2C_process (_I2C_ad_data[chip].bus, I2C_MASTER, &_I2C_analog_m);
00091 
00092         return (_I2C_analog_m.status == I2C_OK);
00093 }
00094 
00101 int I2C_WriteAnalogOut (int chip, int value)
00102 {
00103 
00104         if (!_I2C_ad_data[chip].active)
00105                 return (-1);
00106 
00107 
00108 
00109         _I2C_analog_m.address = _I2C_ad_data[chip].address;
00110         _I2C_analog_m.nrBytes = 2;
00111         _I2C_analog_m.buf = _I2C_analog_mbuf;
00112         _I2C_analog_mbuf[0] = (_I2C_ad_data[chip].status);
00113         _I2C_analog_mbuf[1] = value & 0xff;
00114         I2C_process (_I2C_ad_data[chip].bus, I2C_MASTER, &_I2C_analog_m);
00115 
00116         return (_I2C_analog_m.status == I2C_OK);
00117 }
00118 
00119 
00129 int I2C_init_analog ()
00130 {
00131         int i;
00132         _I2C_adnum = 0;
00133 
00134         for (i = 0; i < 15; i++)
00135                 _I2C_ad_data[i].active = 0;
00136 
00137 
00138         for (i = 0; i < 15; i++) {
00139 
00140                 if (i < 8) {
00141                         _I2C_ad_data[_I2C_adnum].bus = I2CA;
00142                 } else {
00143                         _I2C_ad_data[_I2C_adnum].bus = I2CB;
00144                 }
00145 
00146                 _I2C_ad_data[_I2C_adnum].address = ((i & 0x07) << 1) | 0x90;
00147 
00148                 _I2C_ad_data[_I2C_adnum].status = 0x00;         
00149 
00150                 
00151 
00152                 _I2C_analog_m.address = _I2C_ad_data[_I2C_adnum].address | 0x1;         
00153                 _I2C_analog_m.nrBytes = 1;      
00154                 _I2C_analog_mbuf[0] = 0;        
00155                 _I2C_analog_m.buf = _I2C_analog_mbuf;
00156                 I2C_process (_I2C_ad_data[_I2C_adnum].bus, I2C_MASTER, &_I2C_analog_m);
00157 
00158                 switch (_I2C_analog_m.status) {
00159                 case I2C_OK:
00160                         printf ("analog device %d at adress 0x%x \n", _I2C_adnum, _I2C_ad_data[_I2C_adnum].address);
00161                         _I2C_ad_data[_I2C_adnum].active = 1;
00162                         _I2C_adnum++;
00163                         break;
00164                 case I2C_NO_BUS:
00165                 case I2C_TIME_OUT:
00166                 case I2C_NACK_ON_ADDRESS:
00167                         break;
00168                 default:
00169                         I2C_messagestatus (&_I2C_analog_m);
00170                 }
00171 
00172 
00173         }
00174 
00175         return (_I2C_adnum > 0 ? 0 : 1);
00176 
00177 }