Difference between revisions of "ADC overo 2.6.39+"

From Gumstix User Wiki
Jump to: navigation, search
m (added brief example on reading adc's in c via fopen and fread)
m (Using sysfs in c and fopen and fread)
Line 81: Line 81:
 
>>>   
 
>>>   
  
==Using sysfs in c and fopen and fread==
+
==Using sysfs from c and fopen and fread==
 +
A much useful way to access the adc's is using fopen and fread.
 
<nowiki>
 
<nowiki>
 
#include <stdio.h>
 
#include <stdio.h>
Line 97: Line 98:
 
int main(void)
 
int main(void)
 
{
 
{
 
 
 
     char read_value[4];
 
     char read_value[4];
 
 
     char adcVal2[6];
 
     char adcVal2[6];
 
 
     if (( fp = fopen("/sys/class/hwmon/hwmon1/device/in2_input", "r")) == NULL)
 
     if (( fp = fopen("/sys/class/hwmon/hwmon1/device/in2_input", "r")) == NULL)
 
     {
 
     {
Line 108: Line 105:
 
         exit(1);
 
         exit(1);
 
     }
 
     }
 
 
     rewind(fp);
 
     rewind(fp);
  
Line 114: Line 110:
 
     strcpy(adcVal2 , read_value);
 
     strcpy(adcVal2 , read_value);
  
  fclose(fp);
+
    fclose(fp);
  printf("\nADC 2 value = %s \n",adcVal2);
+
    printf("\nADC 2 value = %s \n",adcVal2);
 
+
    return 0;
 
+
}
 
+
</nowiki>
  return 0;
+
}</nowiki>
+
  
 
==Building your own kernel ==
 
==Building your own kernel ==

Revision as of 23:45, 20 September 2011

Using the ADC's

From posts in the mailing list by Steve Sakoman and others

In linux-omap3-2.6.39 and 3.0 and above

The madc driver is now upstream and in the process it moved to the hwmon subsystem. It is now even easier to access using the /sys/class interface:

root@omap3-multi:~# ls /sys/class/hwmon/hwmon1/device/

curr10_input in11_input in3_input in7_input power

driver in12_input in4_input in8_input subsystem

hwmon in15_input in5_input in9_input temp1_input

in0_input in2_input in6_input modalias uevent

root@omap3-multi:~# cat /sys/class/hwmon/hwmon1/device/temp1_input

56

root@omap3-multi:~# cat /sys/class/hwmon/hwmon1/device/curr10_input

-750

ADC's via a system call

a quick and very nasty way to access them is by a system call

C

system("cat /sys/class/hwmon/hwmon1/device/in7_input");

Python

os.system("cat /sys/class/hwmon/hwmon1/device/in7_input")

Python

import os

import sys

import subprocess

cmd1 = "cat /sys/class/hwmon/hwmon1/device/in7_input"

process = subprocess.Popen(cmd1, stdout=subprocess.PIPE , shell=True)

os.waitpid(process.pid, 0)[1]

udata = process.stdout.read().strip()

print udata

In console (in interactive mode)

python

Python 2.6.6 (r266:84292, Sep 1 2011, 13:08:10)

[GCC 4.3.3] on linux2

>>> import os

>>> import sys

>>> import subprocess

>>> cmd1 = "cat /sys/class/hwmon/hwmon1/device/in7_input"

>>> process = subprocess.Popen(cmd1, stdout=subprocess.PIPE , shell=True)

>>> os.waitpid(process.pid, 0)[1]

0

>>> udata = process.stdout.read().strip()

>>> print udata

1388

>>>

Using sysfs from c and fopen and fread

A much useful way to access the adc's is using fopen and fread. #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/mman.h> #include <time.h> #include <string.h> FILE *fp; int main(void) { char read_value[4]; char adcVal2[6]; if (( fp = fopen("/sys/class/hwmon/hwmon1/device/in2_input", "r")) == NULL) { printf("Can not open hwmon1 \n"); exit(1); } rewind(fp); fread(&read_value, sizeof(char), 1 , fp); strcpy(adcVal2 , read_value); fclose(fp); printf("\nADC 2 value = %s \n",adcVal2); return 0; }

Building your own kernel

When building your own kernel Make sure your defconfig has CONFIG_TWL4030_MADC and CONFIG_SENSORS_TWL4030_MADC and make sure your rootfs has the module.

Link to kernel docs for Kernel driver twl4030-madc http://www.sakoman.com/cgi-bin/gitweb.cgi?p=linux-omap-2.6.git;a=blob;f=Documentation/hwmon/twl4030-madc-hwmon;h=ef7984317cecb29eca6ef23a30c21d920e66ef31;hb=refs/heads/omap-2.6.39-pm