Probe Method of I2C driver framework, i2c framework probe

Source: Internet
Author: User

Probe Method of I2C driver framework, i2c framework probe

The Linux-based I2C driver adopts the probe method. The Linux driver for any device supporting I2C bus can be written according to the following framework.

The I2C device connects to the cpu's specific i2c interface, that is, it is mounted on the cpu's i2c Adapter. the i2c device must exchange information with the cpu through the cpu operation adapter. There are one or more adapters on the cpu, and each adapter can be mounted with 256 i2c devices with different device addresses, with the i2c driver, multiple different i2c devices on the cpu and adapter can communicate without conflict.

The driver contains two files: dev. c and drive. c, where dev. c is to build an I2C device, that is, to create the I2C_Client struct, while the driver. c gets dev in probe. i2c_client built in c, and then build fileoperation. The specific steps are as follows.

1. Build a I2C-CLient In the dev entry function

Static unsigned short addr_list [] = {0x60, 0x50, I2C_CLIENT_END // This array contains the device address, ending with an I2C_CLIENT_END array </span> }; static struct i2c_client * at24cxx_client; // create the I2C_Client structure static int forward (void) {/* construct I2C_Client */struct i2c_adapter * adapter; struct forward info; adapter = i2c_get_adapter (0 ); // obtain the adapter because some CPUs have multiple I2C adapters. The parameter 0 is the adapter number memset (& info, 0, sizeof (struct i2c_board_info); strlcpy (info. type, "at24cxx", I2C_NAME_SIZE); // This is I2C_NAME and is the keyword matching i2c_driver at24cxx_client = i2c_new_probed_device (adapter, & info, addr_list ); // if the device with the address in the array exists, the i2c_client structure is returned successfully.
// This function will eventually call device_create to create the device i2c_put_adapter (adapter); if (at24cxx_client) return 0; else return-ENODEV ;}

2. register the I2C_driver struct in the entry function of driver. c.

static const struct i2c_device_id = {

{ "at24cxx", 0 },
{ }
};
 
 
Static struct i2c_driver at24cxx_driver = {. driver = {. name = "100ask ",. owner = THIS_MODULE ,},. probe = at24cxx_probe ,. remove = _ devexit_p (at24cxx_remove ),. id_table = at24cxx_ids, // The name in id_table matches the name of the i2c_client created by dev. If yes, the probe device method is called.}; static int at24cxx_drv_init (void) {/* × ² ái2c _ driver */return i2c_add_driver (& at24cxx_driver );}
3. Create a file_operation registration character device in the probe device Method

Static struct file_operations at24cxx_fops = {. owner = THIS_MODULE ,. read = at24cxx_read ,. write = at24cxx_write,}; static int at24cxx_probe (struct i2c_client * client, const struct i2c_device_id * id) {printk ("% s % d. \ n ",__ FILE __,__ FUNCTION __,__ LINE _); at24cxx_client = client; // transmits the i2c_client struct constructed in dev because the struct is read, major = register_chrdev (0, "at24cxx", & at24cxx_fops) is required for transmitting information in write and other device methods; // register the character device class = class_create (THIS_MODULE, "at24cxx "); device_create (class, NULL, MKDEV (major, 0), NULL, "at24cxx"); return 0 ;}

4. Build device methods such as write and read to transmit I2C messages

Static ssize_t at24cxx_read (struct file * file, char _ user * buf, size_t count, loff_t * off) {unsigned char addr, data; copy_from_user (& addr, buf, 1 ); data = i2c_smbus_read_byte_data (at24cxx_client, addr ); // here, the i2c_smbus_read_byte_data function is used to interact with the actual I2C device. </span> // which function is used to transmit messages must be combined with the time sequence of the specific device, different time sequences have different transfer functions. Refer to the instructions in the Linux source code for instructions on transfer functions. Copy_to_user (buf, & data, 1); return 0;} static ssize_t at24cxx_write (struct file * file, const char _ user * buf, size_t count, loff_t * off) {unsigned char ker_buf [2]; unsigned char addr, data; copy_from_user (ker_buf, buf, 2); addr = ker_buf [0]; data = ker_buf [1]; if (! I2c_smbus_write_byte_data (at24cxx_client, addr, data) return 2; elsereturn-EIO ;}


If an i2c device is attached to the i2c bus and the device address is in the addr_list above. ko should load the driver first. ko will successfully execute the probe function, then create a character device, open the device in the application, and call read and write, then the read and write methods in the driver will be called accordingly.

Test the application

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>/* i2c_test r addr * i2c_test w addr val */void print_usage(char *file){printf("%s r addr\n", file);printf("%s w addr val\n", file);}int main(int argc, char **argv){int fd;unsigned char buf[2];if ((argc != 3) && (argc != 4)){print_usage(argv[0]);return -1;}fd = open("/dev/at24cxx", O_RDWR);if (fd < 0){printf("can't open /dev/at24cxx\n");return -1;}if (strcmp(argv[1], "r") == 0){buf[0] = strtoul(argv[2], NULL, 0);printf("before data: %c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);read(fd, buf, 1);printf("data: %c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);}else if ((strcmp(argv[1], "w") == 0) && (argc == 4)){buf[0] = strtoul(argv[2], NULL, 0);buf[1] = strtoul(argv[3], NULL, 0);if (write(fd, buf, 2) != 2)printf("write err, addr = 0x%02x, data = 0x%02x\n", buf[0], buf[1]);}else{print_usage(argv[0]);return -1;}return 0;}

2014--12--17

Start of journey

Subsequent supplements









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.