Difference between revisions of "ADC overo 2.6.39+"

From Gumstix User Wiki
Jump to: navigation, search
m (Using the ADC's)
m (Python)
 
(6 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
It is now even easier to access using the /sys/class interface:
 
It is now even easier to access using the /sys/class interface:
  
root@omap3-multi:~# ls /sys/class/hwmon/hwmon1/device/
+
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
  
curr10_input  in11_input  in3_input  in7_input    power
 
  
driver          in12_input in4_input in8_input    subsystem
+
  root@omap3-multi:~# cat /sys/class/hwmon/hwmon1/device/temp1_input
 +
  56
  
hwmon          in15_input in5_input  in9_input    temp1_input
+
  root@omap3-multi:~# cat /sys/class/hwmon/hwmon1/device/curr10_input
 
+
-750
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==
 
==ADC's via a system call==
Line 35: Line 30:
  
 
=== Python ===
 
=== Python ===
import os
+
import os
 
+
import sys
import sys
+
import subprocess
 
+
cmd1 = "cat /sys/class/hwmon/hwmon1/device/in7_input"
import subprocess
+
process = subprocess.Popen(cmd1, stdout=subprocess.PIPE , shell=True)
 
+
os.waitpid(process.pid, 0)[1]
cmd1 = "cat /sys/class/hwmon/hwmon1/device/in7_input"
+
udata = process.stdout.read().strip()
 
+
print udata     
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)'''
 
'''In console (in interactive mode)'''
  
python
+
python
 
+
Python 2.6.6 (r266:84292, Sep  1 2011, 13:08:10)
Python 2.6.6 (r266:84292, Sep  1 2011, 13:08:10)
+
[GCC 4.3.3] on linux2
 
+
>>> import os
[GCC 4.3.3] on linux2
+
>>> import sys
 
+
>>> import subprocess
>>> import os
+
>>> cmd1 = "cat /sys/class/hwmon/hwmon1/device/in7_input"
 
+
>>> process = subprocess.Popen(cmd1, stdout=subprocess.PIPE , shell=True)
>>> import sys
+
>>> os.waitpid(process.pid, 0)[1]
 
+
0
>>> import subprocess
+
>>> udata = process.stdout.read().strip()
 
+
>>> print udata
>>> cmd1 = "cat /sys/class/hwmon/hwmon1/device/in7_input"
+
1388
 
+
>>>
>>> process = subprocess.Popen(cmd1, stdout=subprocess.PIPE , shell=True)
+
 
+
>>> os.waitpid(process.pid, 0)[1]
+
 
+
  
>>> 
+
==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 <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 ==
 
==Building your own kernel ==

Latest revision as of 23:11, 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 <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