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:
Previous article:Embedded driver learning GPIO driver
Next article:Embedded driver transplantation: watchdog driver transplantation
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Share the November 2018 STMicroelectronics-Shunde Motor Control Roadshow Conference Brochure
- My Journey of MCU Development (V)
- Why should we apply synchronous rectification technology?
- Can you return home during the 2022 Spring Festival? It is difficult for people in these provinces to return home, so people returning home should pay attention
- LTC3613DC/DC has a strange phenomenon, I hope the experts can answer it
- [Home Smart Lighting Control and Indoor Environment Monitoring System]--1. Unboxing Poster & Warm-up Poster
- Understand the performance test of 18650 lithium battery - cycle test
- Improving Power Supply Network Efficiency Using Fixed Ratio Converters
- Sinlinx A64 Linux writes LED driver through device tree (with reference code, not tested)
- [N32L43x Review] 10. Serial port download chip type identification error, USB download DFU driver failed