I.MX6 SHT20 Linux 驱动移植

时间:2022-05-04 00:15:04
/***********************************************************************
 *                     I.MX6 SHT20 Linux 驱动移植 
 * 说明:
 *     本来打算使用SHT10来做湿度检测,发现其并不是I2C接口,当然也使用
 * GPIO能获取到相关数据了,不过由于不小心将其烧坏了,所以换成SHT20。
 *
 *                                     2016-10-10 深圳 南山平山村 曾剑锋
 **********************************************************************/


一、参考驱动:
    Android_Native_Service_SHT20Demo
        https://github.com/HowardLin/Android_Native_Service_SHT20Demo/blob/master/SHT20_Module_1.2/sht20_driver/sht20.c#L126

二、驱动修改:
    static s32 sht20_get_id(struct i2c_client *client, u8 serial[8])
    {
        s32 result;
        u8 serialA[8];
        u8 serialB[6];
    
        u8 command1[2] = {0xFA, 0x0F}; /* Read from memory location 1 */
        u8 command2[2] = {0xFC, 0xC9}; /* Read from memory location 2 */
    
        struct i2c_msg get_serial_number_msg1[2] = {
            {
                .addr = client->addr,
                .flags = ~I2C_M_RD,
                .len = 2,
                .buf = command1,
            },
            {
                .addr = client->addr,
                .flags = I2C_M_RD,
                .len = 8,
                .buf = serialA,
            },
        };
        result = i2c_transfer(client->adapter, get_serial_number_msg1, 2);

        struct i2c_msg get_serial_number_msg2[2] = {
            {
                .addr = client->addr,
                .flags = ~I2C_M_RD,
                .len = 2,
                .buf = command2,
            },
            {
                .addr = client->addr,
                .flags = I2C_M_RD,
                .len = 6,
                .buf = serialB,
            }
        };
        result = i2c_transfer(client->adapter, get_serial_number_msg2, 2);
    
        // printk(KERN_INFO "%x %x %x %x %x %x %x %x", serialB[3], serialB[4], serialA[0], serialA[2], serialA[4], serialA[6], serialB[0], serialB[1]);
        
        serial[0] = serialB[3];
        serial[1] = serialB[4];
        serial[2] = serialA[0];
        serial[3] = serialA[2];
        serial[4] = serialA[4];
        serial[5] = serialA[6];
        serial[6] = serialB[0];
        serial[7] = serialB[1];
        
        return result;
    }