drv_pct2075.c 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "drv_pct2075.h" 

#define PCT2075_I2C_NAME "i2c4" 
#define PCT2075_I2C_ADDR (0x4C) 

static struct rt_i2c_bus_device *i2c_bus = RT_NULL; 

float pct2075_read(void)
{
    struct rt_i2c_msg msg = {0};
    rt_uint8_t buf[2] = {0};
    float data = -0.666666f;
    
    if(i2c_bus == RT_NULL)
    {
        return -0.666666f; 
    }
    
    msg.addr  = PCT2075_I2C_ADDR; 
    msg.flags = RT_I2C_RD;
    msg.len   = 2;
    msg.buf   = buf;

    if (rt_i2c_transfer(i2c_bus, &msg, 1) != 1)
    {
        rt_kprintf("I2C write data failed.\n");
        return data;
    }
    
    if((buf[0]&0x80) == 0x00)
    {
        data = ((float)(((buf[0]<<8) + buf[1])>>5) * 0.125f); 
    }
    else
    {
        data = 0x800 - ((buf[0]<<8) + buf[1]>>5 );
        data = -(((float)(data)) * 0.125f); 
    }
    
    return data;
}
MSH_CMD_EXPORT(pct2075_read, read pct2075 oC); 

int rt_hw_pct2075_init(void)
{
    rt_err_t ret = RT_EOK; 
    
    i2c_bus = rt_i2c_bus_device_find(PCT2075_I2C_NAME); 
    if(i2c_bus == RT_NULL)
    {
        rt_kprintf("I2c bus not find.\n"); 
        ret = (-RT_EIO); 
        goto __fail; 
    }
    
    return RT_EOK;

__fail:     
    return ret; 
}
INIT_DEVICE_EXPORT(rt_hw_pct2075_init);