1, IIC software (driver) frame
user层:open、read、write、ioctl、close
kernel: device driver layer: write drivers engineer, writing character device drivers provide access to the application layer interfaces;
The read package data packet, to the bus driver, complete access to the hardware. (I2c-dev.c generic device driver)
1, initialization assignment
struct i2c_driver mma8451q;
struct i2c_driver{
int (*probe)(struct i2c_client *, const struct i2c_device_id *);
// function execution after a successful match
int(*remove)(struct i2c_client *);
struct device_driver driver;//.name = "hello"; .owner = THIS_MODULE
const struct i2c_device_id * id_table; // idtable way match
};
2, registration, cancellation
#define i2c_add_driver(driver)/ i2c_register_driver(THIS_MODULE, driver)
void i2c_del_driver(struct i2c_driver *);
3, how to submit the name of the bus driver end
struct i2c_board_info {
char type [i2c_name_size]; // matches the user's name
unsigned short flags; // flag to read and write, generally do not
unsigned short addr; // slave address (SA0--0x1c \ SA1 --- 0x1d);
};
int i2c_register_board_info(int busnum,struct i2c_board_info const *info,
unsigned len);
Function: the list of i2c_board_info structure into the kernel, the bus driver parse this list
Parameters: bus number, i2c_board_info structure pointer, the number of transmission structure
Returns: Success: 0; failed: error code
4, the information storing device drivers: (mach-s5p6818)
kernel-3.4.39/arch/arm/mach-s5p6818/fs6818$ vi device.c
#include <linux/i2c.h>
static struct i2c_board_info mma8451q = {
.type = "mma8451q",
.addr = 0x1c,
};
5, recompile the kernel make uImage
6, transmission data encapsulation and data
#define I2C_M_RD 0x0001 //read data, from slave to master
Objects When the bus driver and device driver matching success when created, passed to the driver's probe function
struct i2c_client {
unsigned short addr; // slave address
char name[I2C_NAME_SIZE];//名字
struct i2c_adapter * adapter; // bus driver object
struct i2c_driver * driver; // device driver object
};
Message structure
struct i2c_msg {
__u16 addr; // slave address client-> addr
__u16 flags; // read signs written 01
__u16 len; // data length, in bytes
__u8 * buf; // first address data
};
Note: How many how many messages there are start bit, byte length of the message is expressed
a, a function of writing the package
int i2c_write_reg(char reg, char data)
{
char w_buf[] = {reg, data};
// encapsulated message
struct i2c_msg w_msg[] = {
[0] = {
.addr = client->addr,
.flags = 0,
.len = ARRAY_SIZE(w_buf),
.buf = w_buf,
},
};
// send message
ret = i2c_transfer(client->adapter, w_msg, ARRAY_SIZE(w_msg))
if(ret != 1) {
printk("write msg send error\n");
return -EAGAIN;
}
return 0;
}
a, a function of reading the package
int i2c_read_reg(char reg)
{
int ret;
chr val;
char r_buf[] = {reg};
// encapsulated message
struct i2c_msg r_msg[] = {
[0] = {
.addr = client->addr,
.flags = 0,
.len = ARRAY_SIZE(r_buf),
.buf = r_buf,
},
[1] = {
.addr = client->addr,
.flags = I2C_M_RD,
.len = 1,
.buf = &val,
},
};
// send message
ret = i2c_transfer(client->adapter, w_msg, ARRAY_SIZE(w_msg))
if(ret != 2) {
printk("read msg send error\n");
return -EAGAIN;
}
return val;
}
The message packet to the bus driver
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Parameters: adap: bus driver objects; msgs: first address message; num: message number;
Return Value: returns successfully transmitted message number, failure (successfully inverted)
Core layer: kernel implementation, provide the bus driver and device driver registration and deregistration methods, to complete the device driver
Bus driver matching process. (I2c-core.c)
Bus driver (driving control layer): vendor implementations, the data transceiver has been packaged good timing. (I2c-3c2410.c)
IIC works:
1, pull-up resistor: when the chip is not the control pin, the pin is in a floating state (high impedance state), i.e., the bus by default to a high level;
2, the working principle: before the device address sent from the host, transmission start signal from the answering machine to the host; master sends write bits,
Retransmission slave address from the answering machine to the host; write data from the transponder to the host machine; host sends a stop signal;
3, a start signal: high SCL, SDA high to low; stop signal: high SCL, SDA from low to high;
Response signal: a signal transmitted from the slave to the master, SDA high to low;
4, write timing: start signal (a), device address (7 bits), the write bit 0, acknowledge bit (1),
Register address (8 bits), the acknowledge bit data (8 bits), the response bits, stop bits (1)
5, read timing: start signal (a), device address (7 bits), the write bit 0, acknowledge bit (1),
Register address (8 bits), the acknowledge bit / start signal (a), device address (7 bits), the read bit 1
, Acknowledge bit (1-bit), Data (8), no response, stop bits (1)
6, SCL changed data when a high level, a low level when the data can not be modified
Note: kmalloc, kzalloc: allocating contiguous memory space, the maximum 128K, memory allocation must be a multiple of 2
vmalloc: no limit allocated memory size, allocated memory is not necessarily continuous;
get_free_page (gfp_mask) allocate a page size of 4096 GFP_KERNEL GFP_ATOMIC
get_free_pages (gfp_mask, 0) allocating a plurality of pages