Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read and write GPIO values with inb() and outb()
    primarykey
    data
    text
    <p>I have an atom board with a Fintek F75111 GPIO. I have info from the manufacturer that the SMbus address to access the chip is 06EH.</p> <p>I am trying to read and write values to the GPIO in Linux. I have a sample program from manufacturer written for Windows that looks like this.</p> <pre><code>#include “math.h” #include “stdio.h” #include “dos.h” void main(void){ int SMB_PORT_AD = 0x400; int SMB_DEVICE_ADD = 0x6E; /*75111R’s Add=6eh */ //programming DIO as output //0:input 1:Output /* Index 10, GPIO1x Output pin control */ SMB_Byte_WRITE(SMB_PORT_AD,SMB_DEVICE_ADD,0x10,0xff); delay(10); //programming DIO default LOW /* Index 11, GPIO1x Output Data value */ SMB_Byte_WRITE(SMB_PORT_AD,SMB_DEVICE_ADD,0x11,0x00); delay(10); } unsigned char SMB_Byte_READ (int SMPORT, int DeviceID, int REG_INDEX) { unsigned char SMB_R; outportb(SMPORT+02, 0x00); /* clear */ outportb(SMPORT+00, 0xff); /* clear */ delay(10); outportb(SMPORT+04, DeviceID+1); /* clear */ outportb(SMPORT+03, REG_INDEX); /* clear */ outportb(SMPORT+02, 0x48); /* read_byte */ delay(10); SMB_R= inportb(SMPORT+05); return SMB_R; } void SMB_Byte_WRITE(int SMPORT, int DeviceID, int REG_INDEX, int REG_DATA) { outportb(SMPORT+02, 0x00); /* clear */ outportb(SMPORT+00, 0xff); /* clear */ delay(10); outportb(SMPORT+04, DeviceID); /* clear */ outportb(SMPORT+03, REG_INDEX); /* clear */ outportb(SMPORT+05, REG_DATA); /* read_byte */ outportb(SMPORT+02, 0x48); /* read_byte */ delay(10); } </code></pre> <p>I have tried to translate this to Linux compatible functions inb() and outb() and this is what I got.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/io.h&gt; unsigned int gpio_read(int PORT, int DEVICE, int REG_INDEX){ unsigned int RESPONSE; outb(0x00, PORT+02); outb(0xff, PORT+00); usleep(100); outb(DEVICE+1, PORT+04); outb(REG_INDEX, PORT+03); outb(0x48, PORT+02); usleep(100); RESPONSE = inb(PORT+05); return RESPONSE; } unsigned int gpio_write(int PORT, int DEVICE, int REG_INDEX, int REG_DATA){ outb(0x00, PORT+02); outb(0xff, PORT+00); usleep(100); outb(DEVICE, PORT+04); outb(REG_INDEX, PORT+03); outb(DATA, PORT+05); outb(0x48, PORT+02); usleep(100); } void main() { int PORT = 0x400; int DEVICE = 0x6E; unsigned int RESPONSE; // Ask access to port from kernel ioperm(0x400, 100, 1); // GPIO1x set to input (0xff is output) gpio_write(PORT, DEVICE, 0x10, 0x00); RESPONSE = gpio_read(PORT, DEVICE, 1); printf("\n %u \n", RESPONSE); } </code></pre> <p>GPIO1X index 0x10 is used to set if the 8 GPIO ports that are connected to GPIO1x are output ports or input ports.</p> <p>Output values for GPIOs are set using the index 0x11, and if the ports work as input ports then index 0x12 is used for reading input values.</p> <p>The problem is that I do not know if this is correct or how to read the values (why the read function outputs something before reading?!?)</p> <p>When I run:</p> <pre><code>RESPONSE = gpio_read(PORT, DEVICE, X); </code></pre> <p>Changing X with values from 1..9 I get this as output: 0 8 8 0 0 0 0 0 0</p> <p>The number 8 confuses me...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload