FireBLE/I2C driver

来自Firefly wiki
跳转至: 导航搜索

Introduction

I2C is a multidirectional control bus,that is to say, multiple chips can be connected to the same bus structure,at the same time every chip can be used as a real-time data transmission control source.

  1. SCL – Clock Line
  2. SDI – Data Line

I2C bus is used in the various bus signal lines at least,with automatic addressing、Multi-master clock synchronization and arbitration, and other functions of the bus.Therefore,the computer system is very convenient and flexible use of I2C bus design,volume is small,and therefore is widely used in all kinds of practical application.

I2C serial bus and there are generally two signal lines,one of them is a two-way data SDA,the other is the clock line SCL.all to the I2C bus device serial data transmission through the SDA ,all the device of the clock line SCL received SCL on the bus.Any device can be as the Master ,also can be used as Slave,basically see is a transmission of a device.Due to the I2C addressing、start、response、end and so on mechanism,It is easy to distinguish between master-slave device.


Initialization

Typically driven development process: system initialization-->GPIO configuration-->Each driver module initialization-->The main loop function.The first step in system initialization is necessary to each routine,Initialization process is the same,so just call system initialization interface.

I2C as Master

Initialization function

After complete the GPIO configuration,the I2C master initialization,I2C baud rate configured to 100 KHZ,most can be configured to 400 KHZ,next is a global variable array as I2C data acquisition of the cache area,the third parameter is the cache's size.Along with the configuration initialization function,also opens the I2C interrupts.。

//Initialize I2C master
i2c_init(I2C_SCL_RATIO(100000), i2cbuffer, 104);

Initialize array,so that will be written to the buffer array behind from the slave,data read and write and read data form the slave to rxbuffer do validation.。

   for (j = 0; j < 100; j++) {
        buffer[j] = 0x55 + j;
        rxbuffer[j] = 0;
    }

I2C Master read-write

Write the device address 0x21 device by operation,to the device registers at address 0x00 write 0x80.delay waiting for after a period of time,read the device address 0x21 device by operation,to the registers address 0x06 reads one byte data,delay waiting.

// Write 1byte data(0x80) to register(address is 0x00 and 1byte) of device(address is 0x21)
I2C_BYTE_WRITE(0x21, 0x00, 0x80);
for (j = 0; j < 10000; j++);
// Read 1byte data from register(address is 0x06 and 1byte) of device(address is 0x21)
buffer[0] = I2C_BYTE_READ(0x21, 0x06);    
for (j = 0; j < 10000; j++);

Write the device address 0x50 device by operation,to the device registers at address 0x00 write 0x11.delay waiting for after a period of time,Read the device address 0x50 device by operation,from the device register address 0x00 register read a byte of data,delay waiting.I2C_BYTE_WRITE2 and I2C_BYTE_WRITE、I2C_BYTE_READ2 and I2C_BYTE_READ ,the difference between the reading device register address of 16 bit and 8 bit respectively.

// Write 1byte data(0x11) to register(address is 0x00 and 2byte) of device(address is 0x50)
I2C_BYTE_WRITE2(0x50, 0x00, 0x11);        
for (j = 0; j < 10000; j++);
// Read 1byte data from register(address is 0x00 and 2byte) of device(address is 0x50)
buffer[0] = I2C_BYTE_READ2(0x50, 0x00);    
for (j = 0; j < 10000; j++);

Write the device address 0x50 device by operation,to the device registers address 0x00 write to buffer in the array before 50 bytes.delay waiting for after a period of time,read the device address 0x50 device by operation,to the device registers address 0x00 reads 60 bytes of data and stored into the rxbuffer,delay waiting.then,this section also does not have too big distinction on parameters,joined the fourth read the parameters of the number of bytes,can be read once more bytes.

// Write nbyte data(50byte) to register(address is 0x00 and 2byte) of device(address is 0x50)
I2C_nBYTE_WRITE2(0x50, 0x00, buffer, 50);        
for (j = 0; j < 10000; j++);
// Read nbyte data(60byte) from register(address is 0x00 and 2byte) of device(address is 0x50)
I2C_nBYTE_READ2(0x50, 0x00, rxbuffer, 60); 
for (j = 0; j < 10000; j++);