Embedded driver transplantation IIC bus driver transplantation

Publisher:MindfulCreatorLatest update time:2021-07-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Migration environment

Host development environment: Ubuntu 12.04

BootLoader:u-boot-1.1.6

kernel:linux-2.6.30.4

CPU:s3c2440

Development board: TQ2440


Transplantation steps

The IIC bus driver already exists in the 2.6.30.4 kernel. You only need to configure it and compile the kernel image to use the IIC bus. There is an IIC bus EEPROM (AT24C02) in TQ2440. You can test whether the IIC bus is driven by reading and writing it.


1. Configure the kernel

Device Drivers --->

    <* > I2C support --->

       --- I2C support

   <* > I2C device interface

   I2C Hardware Bus support --->

   *** I2C system bus drivers (mostly embedded / system-on-chip)

        <* > S3C2410 I2C Driver


2. Writing of IIC test program

You only need to complete the operation on the i2c_smbus_ioctl_bus structure, which is defined in the "include/linux/i2c-dev.h" file of the kernel source code.

Write 256 numbers into AT24C02 and then read them out. The written numbers start from 0xFF and end at 0x00.

The source code is as follows:


/*************************************

NAME:i2c_rw.c

*************************************/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define CHIP_ADDR 0x50 //Device address

#define I2C_DEV "/dev/i2c-0"

struct eeprom

{

char *dev; // device name, i2c-0

int addr; // device address

int fd; // device handle

};


static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, int size, union i2c_smbus_data *data)

{

struct i2c_smbus_ioctl_data args;

args.read_write = read_write;

args.command = command;

args.size = size;

args.data = data;

return ioctl(file, I2C_SMBUS, &args);

}


int eeprom_read_byte(struct eeprom* e, __u16 mem_addr)

{

int r;

union i2c_smbus_data data;

ioctl(e->fd, BLKFLSBUF);

__u8 buf = mem_addr & 0x0ff;

r = i2c_smbus_access(e->fd, I2C_SMBUS_WRITE, buf, I2C_SMBUS_BYTE, NULL);;

if (r < 0)

  return r;

if (i2c_smbus_access(e->fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))

  return -1;

else

  return 0x0FF & data.byte;

}


int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)

{

int r;

__u8 command = mem_addr & 0x00ff;

union i2c_smbus_data i2cdata;

i2cdata.byte = data;

r = i2c_smbus_access(e->fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &i2cdata);

if(r < 0)

  printf("iic write error");

usleep(10);

  return r;

}

int eeprom_open(char *dev_name, int addr, struct eeprom *e)

{

int funcs, fd, r;

e->fd = e->addr = 0;

e->dev = 0;

fd = open(dev_name, O_RDWR);

if(fd <= 0)

{

printf("iic open errorn");

return -1;

}

// set working device

ioctl(fd, I2C_TENBIT, 0);

ioctl(fd, I2C_SLAVE, addr);

e->fd = fd;

e->addr = addr;

e->dev = dev_name;

return 0;

}



int eeprom_close(struct eeprom *e)

{

close(e->fd);

e->fd = -1;

e->dev = 0;

return 0;

}


static int read_from_eeprom(struct eeprom *e, int addr, int size)

{

int temp, i, ret;

for(i = 0; i < size; ++i, ++addr)

{

temp = eeprom_read_byte(e, addr);

if(temp < 0)

{

printf("iic read error !");

}

if( (i % 16) == 0 )

printf("n %.4x| ", addr);

else if( (i % 8) == 0 )

printf(" ");

printf("%.2x ", temp);

fflush(stdout);

}

printf("nn");

return 0;

}



static int write_to_eeprom(struct eeprom *e, int addr)

{

int i, ret;

for(i=0, addr=0; i<256; i++, addr++)

{

if( (i % 16) == 0 )

  printf("n %.4x| ", addr);

else if( (i % 8) == 0 )

  printf(" ");

  printf("%.2x ", 255-i);

  fflush(stdout);

ret = eeprom_write_byte(e, addr, 255-i);

if(ret < 0)

{

     printf("iic write error !");

}

}

fprintf(stderr, "nn");

return 0;

}



int main (void)

{

struct eeprom e;

int ret;

ret = eeprom_open(I2C_DEV, CHIP_ADDR, &e);

if(ret < 0)

{

printf("i2c device (AT24C02) open failed !n");

return (-1);

}

printf(" Writing 0x00-0xff into AT24C02 n");

write_to_eeprom(&e, 0);

printf(" Reading 256 bytes from 0x0n");

read_from_eeprom(&e, 0, 256);

eeprom_close(&e);

return(0);

}


The Makefile is as follows:


CROSS=arm-linux-

all: i2c_rw

i2c_rw:i2c_rw.c

$(CROSS)gcc -o i2c_rw i2c_rw.c

$(CROSS)strip i2c_rw

clean:

@rm -vf i2c_rw *.o *~


3. IIC test

After compiling the program "i2c_rw", put it in the "/sbin/" directory of the NFS file system, and then run it. The screenshot is as follows:

Reference address:Embedded driver transplantation IIC bus driver transplantation

Previous article:Embedded driver learning GPIO driver
Next article:Embedded driver transplantation: watchdog driver transplantation

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号