Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

/projects/cubeos/src_current/drivers/i2c/analog.c

Go to the documentation of this file.
00001 /*  src_experimental/drivers/i2c/analog.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 <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);     /* inactive a/d are 0 */
00045 
00046 /* set a/d channel */
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);    /* no autoincrement */
00051         I2C_process (_I2C_ad_data[chip].bus, I2C_MASTER, &_I2C_analog_m);
00052 
00053 /* read value */
00054         _I2C_analog_m.address = _I2C_ad_data[chip].address | 0x1;       /* Read adress */
00055         _I2C_analog_m.nrBytes = 2;      /* Read one false byte, then read value */
00056         _I2C_analog_mbuf[0] = 0;        /* clear buffer */
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 /* set status */
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 /* set status */
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 // later this should come from the HWinfo struct 
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;         // 4x single ended, analog output off 
00149 
00150                 // this is the power-on reset state
00151 
00152                 _I2C_analog_m.address = _I2C_ad_data[_I2C_adnum].address | 0x1;         /* Read adress */
00153                 _I2C_analog_m.nrBytes = 1;      /* Read one false byte, then read value */
00154                 _I2C_analog_mbuf[0] = 0;        /* clear buffer */
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 }

Generated on Thu Feb 20 15:38:43 2003 for cubeOS by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002