kstmApp/src/devadcpi.c
changeset 1 7029db7ac3db
equal deleted inserted replaced
0:bd6bb22c6533 1:7029db7ac3db
       
     1 #include <stdio.h>
       
     2 #include <stdint.h>
       
     3 #include <stdlib.h>
       
     4 #include <string.h>
       
     5 #include <errno.h>
       
     6 #include <unistd.h>
       
     7 #include <sys/types.h>
       
     8 #include <sys/stat.h>
       
     9 #include <sys/ioctl.h>
       
    10 #include <fcntl.h>
       
    11 #include <linux/i2c-dev.h>
       
    12 
       
    13 #include <epicsExport.h>
       
    14 #include <dbAccess.h>
       
    15 #include <devSup.h>
       
    16 #include <recGbl.h>
       
    17 
       
    18 #include <aiRecord.h>
       
    19 
       
    20 // ADCPi definitions
       
    21 #define ADC_1 		0x68
       
    22 #define ADC_2 		0x69
       
    23 #define ADC_CHANNEL1	0x98
       
    24 #define ADC_CHANNEL2	0xB8
       
    25 #define ADC_CHANNEL3 	0xD8
       
    26 #define ADC_CHANNEL4	0xF8
       
    27 
       
    28 
       
    29 static long init_record(aiRecord *pai);
       
    30 static long read_ai(aiRecord *pai);
       
    31 
       
    32 struct adcPiChannel {
       
    33   unsigned int channel;
       
    34 };
       
    35 
       
    36 
       
    37 int getadc (int chn){
       
    38   unsigned int fh,dummy, adc, adc_channel;
       
    39 //  double val;
       
    40   __u8  res[4];
       
    41 printf(" Read channel Nr: %d\n", chn);
       
    42   switch (chn){
       
    43     case 1: { adc=ADC_1; adc_channel=ADC_CHANNEL1; }; break;
       
    44     case 2: { adc=ADC_1; adc_channel=ADC_CHANNEL2; }; break;
       
    45     case 3: { adc=ADC_1; adc_channel=ADC_CHANNEL3; }; break;
       
    46     case 4: { adc=ADC_1; adc_channel=ADC_CHANNEL4; }; break;
       
    47     case 5: { adc=ADC_2; adc_channel=ADC_CHANNEL1; }; break;
       
    48     case 6: { adc=ADC_2; adc_channel=ADC_CHANNEL2; }; break;
       
    49     case 7: { adc=ADC_2; adc_channel=ADC_CHANNEL3; }; break;
       
    50     case 8: { adc=ADC_2; adc_channel=ADC_CHANNEL4; }; break;
       
    51     default: { adc=ADC_1; adc_channel=ADC_CHANNEL1; }; break;
       
    52   }
       
    53   fh = open("/dev/i2c-1", O_RDWR);
       
    54   ioctl(fh,I2C_SLAVE,adc);
       
    55   i2c_smbus_write_byte (fh, adc_channel);
       
    56   usleep (50000);
       
    57   i2c_smbus_read_i2c_block_data(fh,adc_channel,4,res);
       
    58   usleep(50000);
       
    59   close (fh);
       
    60   dummy = (res[0]<<8|res[1]);
       
    61   if (dummy>=32768) dummy=65536-dummy;
       
    62   //val = dummy * 0.000154;
       
    63 // printf(" dummy = %d, val = %lf\n", dummy, val);
       
    64   //return val;
       
    65   return dummy;
       
    66 }
       
    67 
       
    68 static long init_record(aiRecord *pai)
       
    69 {
       
    70   struct adcPiChannel* priv;
       
    71   unsigned long channel;
       
    72 
       
    73   priv=malloc(sizeof(struct adcPiChannel));
       
    74   if(!priv){
       
    75     recGblRecordError(S_db_noMemory, (void*)pai,
       
    76       "devAoTimebase failed to allocate private struct");
       
    77     return S_db_noMemory;
       
    78   }
       
    79 
       
    80   recGblInitConstantLink(&pai->inp,DBF_ULONG,&channel);
       
    81 
       
    82   priv->channel=channel;
       
    83   pai->dpvt=priv;
       
    84 
       
    85   return 0;
       
    86 }
       
    87 
       
    88 
       
    89 static long read_ai(aiRecord *pai)
       
    90 {
       
    91   struct adcPiChannel* priv=pai->dpvt;
       
    92 
       
    93   pai->rval = getadc(priv->channel);
       
    94 
       
    95   return 0;
       
    96 }
       
    97 
       
    98 struct {
       
    99   long num;
       
   100   DEVSUPFUN  report;
       
   101   DEVSUPFUN  init;
       
   102   DEVSUPFUN  init_record;
       
   103   DEVSUPFUN  get_ioint_info;
       
   104   DEVSUPFUN  read_ai;
       
   105   DEVSUPFUN  special_linconv;
       
   106 } devAiAdcPi = {
       
   107   6, /* space for 6 functions */
       
   108   NULL,
       
   109   NULL,
       
   110   init_record,
       
   111   NULL,
       
   112   read_ai,
       
   113   NULL
       
   114 };
       
   115 
       
   116 epicsExportAddress(dset,devAiAdcPi);
       
   117