How to read and write GPIO values with inb() and outb()

3.6k Views Asked by At

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.

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.

#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);
}

I have tried to translate this to Linux compatible functions inb() and outb() and this is what I got.

#include <stdio.h>
#include <sys/io.h>

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);
}

GPIO1X index 0x10 is used to set if the 8 GPIO ports that are connected to GPIO1x are output ports or input ports.

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.

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?!?)

When I run:

RESPONSE = gpio_read(PORT, DEVICE, X);

Changing X with values from 1..9 I get this as output: 0 8 8 0 0 0 0 0 0

The number 8 confuses me...

1

There are 1 best solutions below

0
On

Instead of writing directly to the SMBus port, I'd rather use the i2c libraries. I2C (and SMBUS) use two Port Pins, one for the clock and one for the data. The data is trasmitted and received at the clock edges (synchronous), Reading the code, I cannot see clearly which of them (clock or data) is being accessed and when.

To get started I'd use i2ctools as a start point (check this site: http://elinux.org/Interfacing_with_I2C_Devices). This tool helps you to find devices connected to the I2C bus to your microprocessor and also perform basic commmunication.

Hope it helps...